Changeset 3597 for XerialJ/trunk/sqlite-jdbc/src
- Timestamp:
- 09/17/09 11:10:46 (3 years ago)
- Location:
- XerialJ/trunk/sqlite-jdbc/src
- Files:
-
- 5 modified
-
main/java/org/sqlite/JDBC.java (modified) (2 diffs)
-
main/java/org/sqlite/MetaData.java (modified) (1 diff)
-
main/java/org/sqlite/SQLiteJDBCLoader.java (modified) (14 diffs)
-
test/java/org/sqlite/DBMetaDataTest.java (modified) (4 diffs)
-
test/java/org/sqlite/JDBCTest.java (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
XerialJ/trunk/sqlite-jdbc/src/main/java/org/sqlite/JDBC.java
r3265 r3597 28 28 private static final String PREFIX = "jdbc:sqlite:"; 29 29 30 static 31 { 32 try 33 { 30 static { 31 try { 34 32 DriverManager.registerDriver(new JDBC()); 35 33 } 36 catch (SQLException e) 37 { 34 catch (SQLException e) { 38 35 e.printStackTrace(); 39 36 } 40 37 } 41 38 42 public int getMajorVersion() 43 { 44 return 1; 39 public int getMajorVersion() { 40 return SQLiteJDBCLoader.getMajorVersion(); 45 41 } 46 42 47 public int getMinorVersion() 48 { 49 return 1; 43 public int getMinorVersion() { 44 return SQLiteJDBCLoader.getMinorVersion(); 50 45 } 51 46 52 public boolean jdbcCompliant() 53 { 47 public boolean jdbcCompliant() { 54 48 return false; 55 49 } 56 50 57 public boolean acceptsURL(String url) 58 { 51 public boolean acceptsURL(String url) { 59 52 return url != null && url.toLowerCase().startsWith(PREFIX); 60 53 } 61 54 62 public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException 63 { 55 public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException { 64 56 DriverPropertyInfo sharedCache = new DriverPropertyInfo("shared_cache", "false"); 65 57 sharedCache.choices = new String[] { "true", "false" }; … … 75 67 } 76 68 77 public Connection connect(String url, Properties info) throws SQLException 78 { 69 public Connection connect(String url, Properties info) throws SQLException { 79 70 if (!acceptsURL(url)) 80 71 return null; -
XerialJ/trunk/sqlite-jdbc/src/main/java/org/sqlite/MetaData.java
r3532 r3597 389 389 colType = colType == null ? "TEXT" : colType.toUpperCase(); 390 390 int colJavaType = -1; 391 if (colType == "INT" || colType == "INTEGER")391 if (colType.equals("INT") || colType.equals("INTEGER")) 392 392 colJavaType = Types.INTEGER; 393 else if (colType == "TEXT")393 else if (colType.equals("TEXT")) 394 394 colJavaType = Types.VARCHAR; 395 else if (colType == "FLOAT")395 else if (colType.equals("FLOAT")) 396 396 colJavaType = Types.FLOAT; 397 397 else -
XerialJ/trunk/sqlite-jdbc/src/main/java/org/sqlite/SQLiteJDBCLoader.java
r3274 r3597 56 56 private static boolean extracted = false; 57 57 58 public static boolean initialize() 59 { 58 public static boolean initialize() { 60 59 loadSQLiteNativeLibrary(); 61 60 return extracted; 62 61 } 63 62 64 static boolean getPureJavaFlag() 65 { 63 static boolean getPureJavaFlag() { 66 64 return Boolean.parseBoolean(System.getProperty("sqlite.purejava", "false")); 67 65 } 68 66 69 public static boolean isNativeMode() 70 { 67 public static boolean isNativeMode() { 71 68 if (getPureJavaFlag()) 72 69 return false; … … 85 82 * @throws NoSuchAlgorithmException 86 83 */ 87 static String md5sum(InputStream input) throws IOException 88 { 84 static String md5sum(InputStream input) throws IOException { 89 85 BufferedInputStream in = new BufferedInputStream(input); 90 86 91 try 92 { 87 try { 93 88 MessageDigest digest = java.security.MessageDigest.getInstance("MD5"); 94 89 DigestInputStream digestInputStream = new DigestInputStream(in, digest); 95 for (; digestInputStream.read() >= 0;) 96 { 90 for (; digestInputStream.read() >= 0;) { 97 91 98 92 } … … 101 95 return md5out.toString(); 102 96 } 103 catch (NoSuchAlgorithmException e) 104 { 97 catch (NoSuchAlgorithmException e) { 105 98 throw new IllegalStateException("MD5 algorithm is not available: " + e); 106 99 } 107 finally 108 { 100 finally { 109 101 in.close(); 110 102 } … … 120 112 */ 121 113 private static boolean extractAndLoadLibraryFile(String libFolderForCurrentOS, String libraryFileName, 122 String targetFolder) 123 { 114 String targetFolder) { 124 115 String nativeLibraryFilePath = libFolderForCurrentOS + "/" + libraryFileName; 125 116 final String prefix = "sqlite-" + getVersion() + "-"; … … 128 119 File extractedLibFile = new File(targetFolder, extractedLibFileName); 129 120 130 try 131 { 132 if (extractedLibFile.exists()) 133 { 121 try { 122 if (extractedLibFile.exists()) { 134 123 // test md5sum value 135 124 String md5sum1 = md5sum(SQLiteJDBCLoader.class.getResourceAsStream(nativeLibraryFilePath)); 136 125 String md5sum2 = md5sum(new FileInputStream(extractedLibFile)); 137 126 138 if (md5sum1.equals(md5sum2)) 139 { 127 if (md5sum1.equals(md5sum2)) { 140 128 return loadNativeLibrary(targetFolder, extractedLibFileName); 141 129 } 142 else 143 { 130 else { 144 131 // remove old native library file 145 132 boolean deletionSucceeded = extractedLibFile.delete(); 146 if (!deletionSucceeded) 147 { 133 if (!deletionSucceeded) { 148 134 throw new IOException("failed to remove existing native library file: " 149 135 + extractedLibFile.getAbsolutePath()); … … 157 143 byte[] buffer = new byte[1024]; 158 144 int bytesRead = 0; 159 while ((bytesRead = reader.read(buffer)) != -1) 160 { 145 while ((bytesRead = reader.read(buffer)) != -1) { 161 146 writer.write(buffer, 0, bytesRead); 162 147 } … … 165 150 reader.close(); 166 151 167 if (!System.getProperty("os.name").contains("Windows")) 168 { 169 try 170 { 152 if (!System.getProperty("os.name").contains("Windows")) { 153 try { 171 154 Runtime.getRuntime().exec(new String[] { "chmod", "755", extractedLibFile.getAbsolutePath() }) 172 155 .waitFor(); 173 156 } 174 catch (Throwable e) 175 {} 157 catch (Throwable e) {} 176 158 } 177 159 178 160 return loadNativeLibrary(targetFolder, extractedLibFileName); 179 161 } 180 catch (IOException e) 181 { 162 catch (IOException e) { 182 163 System.err.println(e.getMessage()); 183 164 return false; … … 186 167 } 187 168 188 private static synchronized boolean loadNativeLibrary(String path, String name) 189 { 169 private static synchronized boolean loadNativeLibrary(String path, String name) { 190 170 File libPath = new File(path, name); 191 if (libPath.exists()) 192 { 193 // System.setProperty("org.sqlite.lib.path", path == null ? "./" : 194 // path); 195 // System.setProperty("org.sqlite.lib.name", name); 196 197 try 198 { 171 if (libPath.exists()) { 172 173 try { 199 174 System.load(new File(path, name).getAbsolutePath()); 200 175 return true; 201 176 } 202 catch (UnsatisfiedLinkError e) 203 { 177 catch (UnsatisfiedLinkError e) { 204 178 System.err.println(e); 205 179 return false; … … 211 185 } 212 186 213 private static void loadSQLiteNativeLibrary() 214 { 187 private static void loadSQLiteNativeLibrary() { 215 188 if (extracted) 216 189 return; 217 190 218 191 boolean runInPureJavaMode = getPureJavaFlag(); 219 if (runInPureJavaMode) 220 { 192 if (runInPureJavaMode) { 221 193 extracted = false; 222 194 return; … … 229 201 sqliteNativeLibraryName = System.mapLibraryName("sqlitejdbc"); 230 202 231 if (sqliteNativeLibraryPath != null) 232 { 233 if (loadNativeLibrary(sqliteNativeLibraryPath, sqliteNativeLibraryName)) 234 { 203 if (sqliteNativeLibraryPath != null) { 204 if (loadNativeLibrary(sqliteNativeLibraryPath, sqliteNativeLibraryName)) { 235 205 extracted = true; 236 206 return; … … 241 211 sqliteNativeLibraryPath = "/native/" + OSInfo.getNativeLibFolderPathForCurrentOS(); 242 212 243 if (SQLiteJDBCLoader.class.getResource(sqliteNativeLibraryPath + "/" + sqliteNativeLibraryName) == null) 244 { 213 if (SQLiteJDBCLoader.class.getResource(sqliteNativeLibraryPath + "/" + sqliteNativeLibraryName) == null) { 245 214 // use nested VM version 246 215 return; … … 250 219 String tempFolder = new File(System.getProperty("java.io.tmpdir")).getAbsolutePath(); 251 220 /* Try extracting the library from jar */ 252 if (extractAndLoadLibraryFile(sqliteNativeLibraryPath, sqliteNativeLibraryName, tempFolder)) 253 { 221 if (extractAndLoadLibraryFile(sqliteNativeLibraryPath, sqliteNativeLibraryName, tempFolder)) { 254 222 extracted = true; 255 223 return; … … 260 228 } 261 229 262 private static void getNativeLibraryFolderForTheCurrentOS() 263 { 230 private static void getNativeLibraryFolderForTheCurrentOS() { 264 231 String osName = OSInfo.getOSName(); 265 232 String archName = OSInfo.getArchName(); … … 267 234 } 268 235 269 public static String getVersion() 270 { 271 URL versionFile = SQLiteJDBCLoader.class.getResource("/VERSION"); 236 public static int getMajorVersion() { 237 String[] c = getVersion().split("\\."); 238 return (c.length > 0) ? Integer.parseInt(c[0]) : 1; 239 } 240 241 public static int getMinorVersion() { 242 String[] c = getVersion().split("\\."); 243 return (c.length > 1) ? Integer.parseInt(c[1]) : 0; 244 } 245 246 public static String getVersion() { 247 248 URL versionFile = SQLiteJDBCLoader.class.getResource("/META-INF/maven/org.xerial/sqlite-jdbc/pom.properties"); 249 if (versionFile == null) 250 versionFile = SQLiteJDBCLoader.class.getResource("/META-INF/maven/org.xerial/sqlite-jdbc/VERSION"); 272 251 273 252 String version = "unknown"; 274 try 275 { 276 if (versionFile != null) 277 { 253 try { 254 if (versionFile != null) { 278 255 Properties versionData = new Properties(); 279 256 versionData.load(versionFile.openStream()); 280 version = versionData.getProperty(" sqlite_version", version);257 version = versionData.getProperty("version", version); 281 258 version = version.trim().replaceAll("[^0-9\\.]", ""); 282 259 } 283 260 } 284 catch (IOException e) 285 { 261 catch (IOException e) { 286 262 System.err.println(e); 287 263 } -
XerialJ/trunk/sqlite-jdbc/src/test/java/org/sqlite/DBMetaDataTest.java
r2254 r3597 13 13 import java.sql.SQLException; 14 14 import java.sql.Statement; 15 import java.sql.Types; 15 16 16 17 import org.junit.After; … … 37 38 conn = DriverManager.getConnection("jdbc:sqlite:"); 38 39 stat = conn.createStatement(); 39 stat.executeUpdate("create table test (id integer primary key, fn , sn);");40 stat.executeUpdate("create table test (id integer primary key, fn float, sn);"); 40 41 stat.executeUpdate("create view testView as select * from test;"); 41 42 meta = conn.getMetaData(); … … 125 126 assertEquals(rs.getString("TABLE_NAME"), "test"); 126 127 assertEquals(rs.getString("COLUMN_NAME"), "id"); 128 assertEquals(rs.getInt("DATA_TYPE"), Types.INTEGER); 127 129 assertFalse(rs.next()); 128 130 … … 130 132 assertTrue(rs.next()); 131 133 assertEquals(rs.getString("COLUMN_NAME"), "fn"); 134 assertEquals(rs.getInt("DATA_TYPE"), Types.FLOAT); 132 135 assertFalse(rs.next()); 133 136 -
XerialJ/trunk/sqlite-jdbc/src/test/java/org/sqlite/JDBCTest.java
r3200 r3597 21 21 { 22 22 @BeforeClass 23 public static void forName() throws Exception 24 { 23 public static void forName() throws Exception { 25 24 Class.forName("org.sqlite.JDBC"); 26 25 } 27 26 28 27 @Test 29 public void enableLoadExtensionTest() throws Exception 30 { 28 public void enableLoadExtensionTest() throws Exception { 31 29 Properties prop = new Properties(); 32 30 prop.setProperty("enable_load_extension", "true"); 33 31 34 32 Connection conn = null; 35 try 36 { 33 try { 37 34 conn = DriverManager.getConnection("jdbc:sqlite:", prop); 38 35 Statement stat = conn.createStatement(); … … 48 45 49 46 } 50 finally 51 { 47 finally { 52 48 if (conn != null) 53 49 conn.close(); … … 55 51 } 56 52 53 @Test 54 public void majorVersion() throws Exception { 55 int major = DriverManager.getDriver("jdbc:sqlite:").getMajorVersion(); 56 int minor = DriverManager.getDriver("jdbc:sqlite:").getMinorVersion(); 57 } 58 57 59 }


