Show
Ignore:
Timestamp:
09/17/09 11:10:46 (3 years ago)
Author:
leo
Message:

Applied a patch provided by Flavio Sugimoto

Location:
XerialJ/trunk/sqlite-jdbc/src
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • XerialJ/trunk/sqlite-jdbc/src/main/java/org/sqlite/JDBC.java

    r3265 r3597  
    2828    private static final String PREFIX = "jdbc:sqlite:"; 
    2929 
    30     static 
    31     { 
    32         try 
    33         { 
     30    static { 
     31        try { 
    3432            DriverManager.registerDriver(new JDBC()); 
    3533        } 
    36         catch (SQLException e) 
    37         { 
     34        catch (SQLException e) { 
    3835            e.printStackTrace(); 
    3936        } 
    4037    } 
    4138 
    42     public int getMajorVersion() 
    43     { 
    44         return 1; 
     39    public int getMajorVersion() { 
     40        return SQLiteJDBCLoader.getMajorVersion(); 
    4541    } 
    4642 
    47     public int getMinorVersion() 
    48     { 
    49         return 1; 
     43    public int getMinorVersion() { 
     44        return SQLiteJDBCLoader.getMinorVersion(); 
    5045    } 
    5146 
    52     public boolean jdbcCompliant() 
    53     { 
     47    public boolean jdbcCompliant() { 
    5448        return false; 
    5549    } 
    5650 
    57     public boolean acceptsURL(String url) 
    58     { 
     51    public boolean acceptsURL(String url) { 
    5952        return url != null && url.toLowerCase().startsWith(PREFIX); 
    6053    } 
    6154 
    62     public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException 
    63     { 
     55    public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException { 
    6456        DriverPropertyInfo sharedCache = new DriverPropertyInfo("shared_cache", "false"); 
    6557        sharedCache.choices = new String[] { "true", "false" }; 
     
    7567    } 
    7668 
    77     public Connection connect(String url, Properties info) throws SQLException 
    78     { 
     69    public Connection connect(String url, Properties info) throws SQLException { 
    7970        if (!acceptsURL(url)) 
    8071            return null; 
  • XerialJ/trunk/sqlite-jdbc/src/main/java/org/sqlite/MetaData.java

    r3532 r3597  
    389389            colType = colType == null ? "TEXT" : colType.toUpperCase(); 
    390390            int colJavaType = -1; 
    391             if (colType == "INT" || colType == "INTEGER") 
     391            if (colType.equals("INT") || colType.equals("INTEGER")) 
    392392                colJavaType = Types.INTEGER; 
    393             else if (colType == "TEXT") 
     393            else if (colType.equals("TEXT")) 
    394394                colJavaType = Types.VARCHAR; 
    395             else if (colType == "FLOAT") 
     395            else if (colType.equals("FLOAT")) 
    396396                colJavaType = Types.FLOAT; 
    397397            else 
  • XerialJ/trunk/sqlite-jdbc/src/main/java/org/sqlite/SQLiteJDBCLoader.java

    r3274 r3597  
    5656    private static boolean extracted = false; 
    5757 
    58     public static boolean initialize() 
    59     { 
     58    public static boolean initialize() { 
    6059        loadSQLiteNativeLibrary(); 
    6160        return extracted; 
    6261    } 
    6362 
    64     static boolean getPureJavaFlag() 
    65     { 
     63    static boolean getPureJavaFlag() { 
    6664        return Boolean.parseBoolean(System.getProperty("sqlite.purejava", "false")); 
    6765    } 
    6866 
    69     public static boolean isNativeMode() 
    70     { 
     67    public static boolean isNativeMode() { 
    7168        if (getPureJavaFlag()) 
    7269            return false; 
     
    8582     * @throws NoSuchAlgorithmException 
    8683     */ 
    87     static String md5sum(InputStream input) throws IOException 
    88     { 
     84    static String md5sum(InputStream input) throws IOException { 
    8985        BufferedInputStream in = new BufferedInputStream(input); 
    9086 
    91         try 
    92         { 
     87        try { 
    9388            MessageDigest digest = java.security.MessageDigest.getInstance("MD5"); 
    9489            DigestInputStream digestInputStream = new DigestInputStream(in, digest); 
    95             for (; digestInputStream.read() >= 0;) 
    96             { 
     90            for (; digestInputStream.read() >= 0;) { 
    9791 
    9892            } 
     
    10195            return md5out.toString(); 
    10296        } 
    103         catch (NoSuchAlgorithmException e) 
    104         { 
     97        catch (NoSuchAlgorithmException e) { 
    10598            throw new IllegalStateException("MD5 algorithm is not available: " + e); 
    10699        } 
    107         finally 
    108         { 
     100        finally { 
    109101            in.close(); 
    110102        } 
     
    120112     */ 
    121113    private static boolean extractAndLoadLibraryFile(String libFolderForCurrentOS, String libraryFileName, 
    122             String targetFolder) 
    123     { 
     114            String targetFolder) { 
    124115        String nativeLibraryFilePath = libFolderForCurrentOS + "/" + libraryFileName; 
    125116        final String prefix = "sqlite-" + getVersion() + "-"; 
     
    128119        File extractedLibFile = new File(targetFolder, extractedLibFileName); 
    129120 
    130         try 
    131         { 
    132             if (extractedLibFile.exists()) 
    133             { 
     121        try { 
     122            if (extractedLibFile.exists()) { 
    134123                // test md5sum value 
    135124                String md5sum1 = md5sum(SQLiteJDBCLoader.class.getResourceAsStream(nativeLibraryFilePath)); 
    136125                String md5sum2 = md5sum(new FileInputStream(extractedLibFile)); 
    137126 
    138                 if (md5sum1.equals(md5sum2)) 
    139                 { 
     127                if (md5sum1.equals(md5sum2)) { 
    140128                    return loadNativeLibrary(targetFolder, extractedLibFileName); 
    141129                } 
    142                 else 
    143                 { 
     130                else { 
    144131                    // remove old native library file 
    145132                    boolean deletionSucceeded = extractedLibFile.delete(); 
    146                     if (!deletionSucceeded) 
    147                     { 
     133                    if (!deletionSucceeded) { 
    148134                        throw new IOException("failed to remove existing native library file: " 
    149135                                + extractedLibFile.getAbsolutePath()); 
     
    157143            byte[] buffer = new byte[1024]; 
    158144            int bytesRead = 0; 
    159             while ((bytesRead = reader.read(buffer)) != -1) 
    160             { 
     145            while ((bytesRead = reader.read(buffer)) != -1) { 
    161146                writer.write(buffer, 0, bytesRead); 
    162147            } 
     
    165150            reader.close(); 
    166151 
    167             if (!System.getProperty("os.name").contains("Windows")) 
    168             { 
    169                 try 
    170                 { 
     152            if (!System.getProperty("os.name").contains("Windows")) { 
     153                try { 
    171154                    Runtime.getRuntime().exec(new String[] { "chmod", "755", extractedLibFile.getAbsolutePath() }) 
    172155                            .waitFor(); 
    173156                } 
    174                 catch (Throwable e) 
    175                 {} 
     157                catch (Throwable e) {} 
    176158            } 
    177159 
    178160            return loadNativeLibrary(targetFolder, extractedLibFileName); 
    179161        } 
    180         catch (IOException e) 
    181         { 
     162        catch (IOException e) { 
    182163            System.err.println(e.getMessage()); 
    183164            return false; 
     
    186167    } 
    187168 
    188     private static synchronized boolean loadNativeLibrary(String path, String name) 
    189     { 
     169    private static synchronized boolean loadNativeLibrary(String path, String name) { 
    190170        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 { 
    199174                System.load(new File(path, name).getAbsolutePath()); 
    200175                return true; 
    201176            } 
    202             catch (UnsatisfiedLinkError e) 
    203             { 
     177            catch (UnsatisfiedLinkError e) { 
    204178                System.err.println(e); 
    205179                return false; 
     
    211185    } 
    212186 
    213     private static void loadSQLiteNativeLibrary() 
    214     { 
     187    private static void loadSQLiteNativeLibrary() { 
    215188        if (extracted) 
    216189            return; 
    217190 
    218191        boolean runInPureJavaMode = getPureJavaFlag(); 
    219         if (runInPureJavaMode) 
    220         { 
     192        if (runInPureJavaMode) { 
    221193            extracted = false; 
    222194            return; 
     
    229201            sqliteNativeLibraryName = System.mapLibraryName("sqlitejdbc"); 
    230202 
    231         if (sqliteNativeLibraryPath != null) 
    232         { 
    233             if (loadNativeLibrary(sqliteNativeLibraryPath, sqliteNativeLibraryName)) 
    234             { 
     203        if (sqliteNativeLibraryPath != null) { 
     204            if (loadNativeLibrary(sqliteNativeLibraryPath, sqliteNativeLibraryName)) { 
    235205                extracted = true; 
    236206                return; 
     
    241211        sqliteNativeLibraryPath = "/native/" + OSInfo.getNativeLibFolderPathForCurrentOS(); 
    242212 
    243         if (SQLiteJDBCLoader.class.getResource(sqliteNativeLibraryPath + "/" + sqliteNativeLibraryName) == null) 
    244         { 
     213        if (SQLiteJDBCLoader.class.getResource(sqliteNativeLibraryPath + "/" + sqliteNativeLibraryName) == null) { 
    245214            // use nested VM version 
    246215            return; 
     
    250219        String tempFolder = new File(System.getProperty("java.io.tmpdir")).getAbsolutePath(); 
    251220        /* Try extracting the library from jar */ 
    252         if (extractAndLoadLibraryFile(sqliteNativeLibraryPath, sqliteNativeLibraryName, tempFolder)) 
    253         { 
     221        if (extractAndLoadLibraryFile(sqliteNativeLibraryPath, sqliteNativeLibraryName, tempFolder)) { 
    254222            extracted = true; 
    255223            return; 
     
    260228    } 
    261229 
    262     private static void getNativeLibraryFolderForTheCurrentOS() 
    263     { 
     230    private static void getNativeLibraryFolderForTheCurrentOS() { 
    264231        String osName = OSInfo.getOSName(); 
    265232        String archName = OSInfo.getArchName(); 
     
    267234    } 
    268235 
    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"); 
    272251 
    273252        String version = "unknown"; 
    274         try 
    275         { 
    276             if (versionFile != null) 
    277             { 
     253        try { 
     254            if (versionFile != null) { 
    278255                Properties versionData = new Properties(); 
    279256                versionData.load(versionFile.openStream()); 
    280                 version = versionData.getProperty("sqlite_version", version); 
     257                version = versionData.getProperty("version", version); 
    281258                version = version.trim().replaceAll("[^0-9\\.]", ""); 
    282259            } 
    283260        } 
    284         catch (IOException e) 
    285         { 
     261        catch (IOException e) { 
    286262            System.err.println(e); 
    287263        } 
  • XerialJ/trunk/sqlite-jdbc/src/test/java/org/sqlite/DBMetaDataTest.java

    r2254 r3597  
    1313import java.sql.SQLException; 
    1414import java.sql.Statement; 
     15import java.sql.Types; 
    1516 
    1617import org.junit.After; 
     
    3738        conn = DriverManager.getConnection("jdbc:sqlite:"); 
    3839        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);"); 
    4041        stat.executeUpdate("create view testView as select * from test;"); 
    4142        meta = conn.getMetaData(); 
     
    125126        assertEquals(rs.getString("TABLE_NAME"), "test"); 
    126127        assertEquals(rs.getString("COLUMN_NAME"), "id"); 
     128        assertEquals(rs.getInt("DATA_TYPE"), Types.INTEGER); 
    127129        assertFalse(rs.next()); 
    128130 
     
    130132        assertTrue(rs.next()); 
    131133        assertEquals(rs.getString("COLUMN_NAME"), "fn"); 
     134        assertEquals(rs.getInt("DATA_TYPE"), Types.FLOAT); 
    132135        assertFalse(rs.next()); 
    133136 
  • XerialJ/trunk/sqlite-jdbc/src/test/java/org/sqlite/JDBCTest.java

    r3200 r3597  
    2121{ 
    2222    @BeforeClass 
    23     public static void forName() throws Exception 
    24     { 
     23    public static void forName() throws Exception { 
    2524        Class.forName("org.sqlite.JDBC"); 
    2625    } 
    2726 
    2827    @Test 
    29     public void enableLoadExtensionTest() throws Exception 
    30     { 
     28    public void enableLoadExtensionTest() throws Exception { 
    3129        Properties prop = new Properties(); 
    3230        prop.setProperty("enable_load_extension", "true"); 
    3331 
    3432        Connection conn = null; 
    35         try 
    36         { 
     33        try { 
    3734            conn = DriverManager.getConnection("jdbc:sqlite:", prop); 
    3835            Statement stat = conn.createStatement(); 
     
    4845 
    4946        } 
    50         finally 
    51         { 
     47        finally { 
    5248            if (conn != null) 
    5349                conn.close(); 
     
    5551    } 
    5652 
     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 
    5759}