package org.sqlite;

import java.sql.SQLException;

/** This class provides a thin JNI layer over the SQLite3 C API.  */
class DB implements Codes
{

    /** database pointer */
    long pointer = 0;

    static {
        System.loadLibrary("sqlitejdbc");
        init();
    }

    private static native void init();


    // WRAPPER FUNCTIONS ////////////////////////////////////////////

    native void open(String filename) throws SQLException;
    native void close() throws SQLException;
    native void interrupt();
    native void busy_timeout(int ms);
    native void exec(String sql) throws SQLException;
    native long prepare(String sql) throws SQLException;
    native String errmsg();

    native int changes(long stmt);
    native int finalize(long stmt);
    native int step(long stmt);
    native int reset(long stmt);
    native int clear_bindings(long stmt);

    native int bind_null  (long stmt, int pos);
    native int bind_text  (long stmt, int pos, String value);
    native int bind_blob  (long stmt, int pos, byte[] value);
    native int bind_double(long stmt, int pos, double value);
    native int bind_long  (long stmt, int pos, long   value);
    native int bind_int   (long stmt, int pos, int    value);

    native int    column_count      (long stmt);
    native int    column_type       (long stmt, int col);
    native String column_decltype   (long stmt, int col);
    native String column_table_name (long stmt, int col);
    native String column_name       (long stmt, int col);
    native String column_text       (long stmt, int col);
    native byte[] column_blob       (long stmt, int col);
    native double column_double     (long stmt, int col);
    native long   column_long       (long stmt, int col);
    native int    column_int        (long stmt, int col);


    // COMPOUND FUNCTIONS (for optimisation) /////////////////////////

    native int executeUpdate(long stmt) throws SQLException;

    native String[]    column_names    (long stmt);

    /** Provides metadata for the columns of a statement. Returns:
     *   res[col][0] = true if column constrained NOT NULL
     *   res[col][1] = true if column is part of the primary key
     *   res[col][2] = true if column is auto-increment
     */
    native boolean[][] column_metadata(long stmt, String[] colNames);
    boolean[][] column_metadata(long stmt) {
        return column_metadata(stmt, column_names(stmt));
    }


    // HELPER FUNCTIONS /////////////////////////////////////////////

    void throwex() throws SQLException { throw ex(); }
    void throwex(String msg) throws SQLException { throw new SQLException(msg);}
    SQLException ex() { return new SQLException("SQLite error: " + errmsg()); }
}

@@ -142,17 +142,16 @@
 Codes%0A%7B%0A
-%0A
     /** 
@@ -390,24 +390,37 @@
 %0A    native 
+synchronized 
 void open(St
@@ -462,24 +462,37 @@
 %0A    native 
+synchronized 
 void close()
@@ -520,24 +520,37 @@
 %0A    native 
+synchronized 
 void interru
@@ -562,24 +562,37 @@
 %0A    native 
+synchronized 
 void busy_ti
@@ -613,24 +613,37 @@
 %0A    native 
+synchronized 
 void exec(St
@@ -680,24 +680,37 @@
 %0A    native 
+synchronized 
 long prepare
@@ -750,24 +750,37 @@
 %0A    native 
+synchronized 
 String errms
@@ -792,24 +792,37 @@
 %0A    native 
+synchronized 
 int changes(
@@ -840,24 +840,37 @@
 %0A    native 
+synchronized 
 int finalize
@@ -889,24 +889,37 @@
 %0A    native 
+synchronized 
 int step(lon
@@ -934,24 +934,37 @@
 %0A    native 
+synchronized 
 int reset(lo
@@ -980,24 +980,37 @@
 %0A    native 
+synchronized 
 int clear_bi
@@ -1032,32 +1032,45 @@
 t);%0A%0A    native 
+synchronized 
 int bind_null  (
@@ -1093,32 +1093,45 @@
 os);%0A    native 
+synchronized 
 int bind_text  (
@@ -1168,32 +1168,45 @@
 ue);%0A    native 
+synchronized 
 int bind_blob  (
@@ -1243,32 +1243,45 @@
 ue);%0A    native 
+synchronized 
 int bind_double(
@@ -1318,32 +1318,45 @@
 ue);%0A    native 
+synchronized 
 int bind_long  (
@@ -1397,24 +1397,37 @@
 %0A    native 
+synchronized 
 int bind_int
@@ -1469,32 +1469,45 @@
 e);%0A%0A    native 
+synchronized 
 int    column_co
@@ -1531,32 +1531,45 @@
 mt);%0A    native 
+synchronized 
 int    column_ty
@@ -1602,32 +1602,45 @@
 ol);%0A    native 
+synchronized 
 String column_de
@@ -1673,32 +1673,45 @@
 ol);%0A    native 
+synchronized 
 String column_ta
@@ -1744,32 +1744,45 @@
 ol);%0A    native 
+synchronized 
 String column_na
@@ -1819,24 +1819,37 @@
 %0A    native 
+synchronized 
 String colum
@@ -1894,16 +1894,29 @@
  native 
+synchronized 
 byte%5B%5D c
@@ -1965,16 +1965,29 @@
  native 
+synchronized 
 double c
@@ -2036,16 +2036,29 @@
  native 
+synchronized 
 long   c
@@ -2103,24 +2103,37 @@
 %0A    native 
+synchronized 
 int    colum
@@ -2252,16 +2252,29 @@
  native 
+synchronized 
 int exec
@@ -2327,24 +2327,37 @@
  native 
+synchronized 
 String%5B%5D
     colu
@@ -2348,19 +2348,16 @@
 String%5B%5D
-   
  column_
@@ -2361,20 +2361,16 @@
 mn_names
-    
 (long st
@@ -2641,16 +2641,29 @@
  native 
+synchronized 
 boolean%5B
@@ -2706,12 +2706,9 @@
 g%5B%5D 
-colN
+n
 ames
@@ -2714,16 +2714,29 @@
 s);%0A    
+synchronized 
 boolean%5B

synchronize db access