diff options
Diffstat (limited to 'sqlite-jdbc/src/main/java/SQLite/JDBC2y')
7 files changed, 4250 insertions, 0 deletions
diff --git a/sqlite-jdbc/src/main/java/SQLite/JDBC2y/JDBCConnection.java b/sqlite-jdbc/src/main/java/SQLite/JDBC2y/JDBCConnection.java new file mode 100644 index 0000000..20c98e3 --- /dev/null +++ b/sqlite-jdbc/src/main/java/SQLite/JDBC2y/JDBCConnection.java @@ -0,0 +1,452 @@ +package SQLite.JDBC2y; + +import java.sql.*; +import java.util.*; + +public class JDBCConnection + implements java.sql.Connection, SQLite.BusyHandler { + + /** + * Open database. + */ + protected DatabaseX db; + + /** + * Database URL. + */ + protected String url; + + /** + * Character encoding. + */ + protected String enc; + + /** + * Autocommit flag, true means autocommit. + */ + protected boolean autocommit = true; + + /** + * In-transaction flag. + * Can be true only when autocommit false. + */ + protected boolean intrans = false; + + /** + * Timeout for Database.exec() + */ + protected int timeout = 1000000; + + /** + * File name of database. + */ + private String dbfile = null; + + /** + * Reference to meta data or null. + */ + private JDBCDatabaseMetaData meta = null; + + /** + * Base time value for timeout handling. + */ + private long t0; + + /** + * Database in readonly mode. + */ + private boolean readonly = false; + + + private boolean busy0(DatabaseX db, int count) { + if (count <= 1) { + t0 = System.currentTimeMillis(); + } + if (db != null) { + long t1 = System.currentTimeMillis(); + if (t1 - t0 > timeout) { + return false; + } + db.wait(100); + return true; + } + return false; + } + + public boolean busy(String table, int count) { + return busy0(db, count); + } + + protected boolean busy3(DatabaseX db, int count) { + if (count <= 1) { + t0 = System.currentTimeMillis(); + } + if (db != null) { + long t1 = System.currentTimeMillis(); + if (t1 - t0 > timeout) { + return false; + } + return true; + } + return false; + } + + private DatabaseX open(boolean readonly) throws SQLException { + DatabaseX db = null; + try { + db = new DatabaseX(); + db.open(dbfile, readonly ? 0444 : 0644); + db.set_encoding(enc); + } catch (SQLite.Exception e) { + throw new SQLException(e.toString()); + } + int loop = 0; + while (true) { + try { + db.exec("PRAGMA short_column_names = off;", null); + db.exec("PRAGMA full_column_names = on;", null); + db.exec("PRAGMA empty_result_callbacks = on;", null); + if (SQLite.Database.version().compareTo("2.6.0") >= 0) { + db.exec("PRAGMA show_datatypes = on;", null); + } + } catch (SQLite.Exception e) { + if (db.last_error() != SQLite.Constants.SQLITE_BUSY || + !busy0(db, ++loop)) { + try { + db.close(); + } catch (SQLite.Exception ee) { + } + throw new SQLException(e.toString()); + } + continue; + } + break; + } + return db; + } + + public JDBCConnection(String url, String enc) throws SQLException { + if (url.startsWith("sqlite:/")) { + dbfile = url.substring(8); + } else if (url.startsWith("jdbc:sqlite:/")) { + dbfile = url.substring(13); + } else { + throw new SQLException("unsupported url"); + } + this.url = url; + this.enc = enc; + try { + db = open(readonly); + db.busy_handler(this); + } catch (SQLException e) { + if (db != null) { + try { + db.close(); + } catch (SQLite.Exception ee) { + } + } + throw e; + } + } + + /* non-standard */ + public SQLite.Database getSQLiteDatabase() { + return (SQLite.Database) db; + } + + public Statement createStatement() { + JDBCStatement s = new JDBCStatement(this); + return s; + } + + public Statement createStatement(int resultSetType, + int resultSetConcurrency) + throws SQLException { + JDBCStatement s = new JDBCStatement(this); + return s; + } + + public DatabaseMetaData getMetaData() throws SQLException { + if (meta == null) { + meta = new JDBCDatabaseMetaData(this); + } + return meta; + } + + public void close() throws SQLException { + try { + rollback(); + } catch (SQLException e) { + /* ignored */ + } + intrans = false; + if (db != null) { + try { + db.close(); + db = null; + } catch (SQLite.Exception e) { + throw new SQLException(e.toString()); + } + } + } + + public boolean isClosed() throws SQLException { + return db == null; + } + + public boolean isReadOnly() throws SQLException { + return readonly; + } + + public void clearWarnings() throws SQLException { + } + + public void commit() throws SQLException { + if (db == null) { + throw new SQLException("stale connection"); + } + if (!intrans) { + return; + } + try { + db.exec("COMMIT", null); + intrans = false; + } catch (SQLite.Exception e) { + throw new SQLException(e.toString()); + } + } + + public boolean getAutoCommit() throws SQLException { + return autocommit; + } + + public String getCatalog() throws SQLException { + return null; + } + + public int getTransactionIsolation() throws SQLException { + return TRANSACTION_SERIALIZABLE; + } + + public SQLWarning getWarnings() throws SQLException { + return null; + } + + public String nativeSQL(String sql) throws SQLException { + throw new SQLException("not supported"); + } + + public CallableStatement prepareCall(String sql) throws SQLException { + throw new SQLException("not supported"); + } + + public CallableStatement prepareCall(String sql, int x, int y) + throws SQLException { + throw new SQLException("not supported"); + } + + public PreparedStatement prepareStatement(String sql) throws SQLException { + JDBCPreparedStatement s = new JDBCPreparedStatement(this, sql); + return s; + } + + public PreparedStatement prepareStatement(String sql, int resultSetType, + int resultSetConcurrency) + throws SQLException { + JDBCPreparedStatement s = new JDBCPreparedStatement(this, sql); + return s; + } + + public void rollback() throws SQLException { + if (db == null) { + throw new SQLException("stale connection"); + } + if (!intrans) { + return; + } + try { + db.exec("ROLLBACK", null); + intrans = false; + } catch (SQLite.Exception e) { + throw new SQLException(e.toString()); + } + } + + public void setAutoCommit(boolean ac) throws SQLException { + if (ac && intrans && db != null) { + try { + db.exec("ROLLBACK", null); + } catch (SQLite.Exception e) { + throw new SQLException(e.toString()); + } + } + intrans = false; + autocommit = ac; + } + + public void setCatalog(String catalog) throws SQLException { + } + + public void setReadOnly(boolean ro) throws SQLException { + if (intrans) { + throw new SQLException("incomplete transaction"); + } + if (ro != readonly) { + DatabaseX db = null; + try { + db = open(ro); + this.db.close(); + this.db = db; + db = null; + readonly = ro; + } catch (SQLException e) { + throw e; + } catch (SQLite.Exception ee) { + if (db != null) { + try { + db.close(); + } catch (SQLite.Exception eee) { + } + } + throw new SQLException(ee.toString()); + } + } + } + + public void setTransactionIsolation(int level) throws SQLException { + if (level != TRANSACTION_SERIALIZABLE) { + throw new SQLException("not supported"); + } + } + + public java.util.Map<String, Class<?>> getTypeMap() throws SQLException { + throw new SQLException("not supported"); + } + + public void setTypeMap(java.util.Map map) throws SQLException { + throw new SQLException("not supported"); + } + + public int getHoldability() throws SQLException { + return ResultSet.HOLD_CURSORS_OVER_COMMIT; + } + + public void setHoldability(int holdability) throws SQLException { + if (holdability == ResultSet.HOLD_CURSORS_OVER_COMMIT) { + return; + } + throw new SQLException("not supported"); + } + + public Savepoint setSavepoint() throws SQLException { + throw new SQLException("not supported"); + } + + public Savepoint setSavepoint(String name) throws SQLException { + throw new SQLException("not supported"); + } + + public void rollback(Savepoint x) throws SQLException { + throw new SQLException("not supported"); + } + + public void releaseSavepoint(Savepoint x) throws SQLException { + throw new SQLException("not supported"); + } + + public Statement createStatement(int resultSetType, + int resultSetConcurrency, + int resultSetHoldability) + throws SQLException { + if (resultSetHoldability != ResultSet.HOLD_CURSORS_OVER_COMMIT) { + throw new SQLException("not supported"); + } + return createStatement(resultSetType, resultSetConcurrency); + } + + public PreparedStatement prepareStatement(String sql, int resultSetType, + int resultSetConcurrency, + int resultSetHoldability) + throws SQLException { + if (resultSetHoldability != ResultSet.HOLD_CURSORS_OVER_COMMIT) { + throw new SQLException("not supported"); + } + return prepareStatement(sql, resultSetType, resultSetConcurrency); + } + + public CallableStatement prepareCall(String sql, int x, int y, int z) + throws SQLException { + throw new SQLException("not supported"); + } + + public PreparedStatement prepareStatement(String sql, int autokeys) + throws SQLException { + if (autokeys != Statement.NO_GENERATED_KEYS) { + throw new SQLException("not supported"); + } + return prepareStatement(sql); + } + + public PreparedStatement prepareStatement(String sql, int colIndexes[]) + throws SQLException { + throw new SQLException("not supported"); + } + + public PreparedStatement prepareStatement(String sql, String columns[]) + throws SQLException { + throw new SQLException("not supported"); + } + +} + +class DatabaseX extends SQLite.Database { + + static Object lock = new Object(); + + public DatabaseX() { + super(); + } + + void wait(int ms) { + try { + synchronized (lock) { + lock.wait(ms); + } + } catch (java.lang.Exception e) { + } + } + + public void exec(String sql, SQLite.Callback cb) + throws SQLite.Exception { + super.exec(sql, cb); + synchronized (lock) { + lock.notifyAll(); + } + } + + public void exec(String sql, SQLite.Callback cb, String args[]) + throws SQLite.Exception { + super.exec(sql, cb, args); + synchronized (lock) { + lock.notifyAll(); + } + } + + public SQLite.TableResult get_table(String sql, String args[]) + throws SQLite.Exception { + SQLite.TableResult ret = super.get_table(sql, args); + synchronized (lock) { + lock.notifyAll(); + } + return ret; + } + + public void get_table(String sql, String args[], SQLite.TableResult tbl) + throws SQLite.Exception { + super.get_table(sql, args, tbl); + synchronized (lock) { + lock.notifyAll(); + } + } + +} diff --git a/sqlite-jdbc/src/main/java/SQLite/JDBC2y/JDBCDatabaseMetaData.java b/sqlite-jdbc/src/main/java/SQLite/JDBC2y/JDBCDatabaseMetaData.java new file mode 100644 index 0000000..8c14d1d --- /dev/null +++ b/sqlite-jdbc/src/main/java/SQLite/JDBC2y/JDBCDatabaseMetaData.java @@ -0,0 +1,1578 @@ +package SQLite.JDBC2y; + +import java.sql.*; +import java.util.Hashtable; + +public class JDBCDatabaseMetaData implements DatabaseMetaData { + + private JDBCConnection conn; + + public JDBCDatabaseMetaData(JDBCConnection conn) { + this.conn = conn; + } + + public boolean allProceduresAreCallable() throws SQLException { + return false; + } + + public boolean allTablesAreSelectable() throws SQLException { + return true; + } + + public String getURL() throws SQLException { + return conn.url; + } + + public String getUserName() throws SQLException { + return ""; + } + + public boolean isReadOnly() throws SQLException { + return false; + } + + public boolean nullsAreSortedHigh() throws SQLException { + return false; + } + + public boolean nullsAreSortedLow() throws SQLException { + return false; + } + + public boolean nullsAreSortedAtStart() throws SQLException { + return false; + } + + public boolean nullsAreSortedAtEnd() throws SQLException { + return false; + } + + public String getDatabaseProductName() throws SQLException { + return "SQLite"; + } + + public String getDatabaseProductVersion() throws SQLException { + return SQLite.Database.version(); + } + + public String getDriverName() throws SQLException { + return "SQLite/JDBC"; + } + + public String getDriverVersion() throws SQLException { + return "" + SQLite.JDBCDriver.MAJORVERSION + "." + + SQLite.JDBCDriver.MINORVERSION; + } + + public int getDriverMajorVersion() { + return SQLite.JDBCDriver.MAJORVERSION; + } + + public int getDriverMinorVersion() { + return SQLite.JDBCDriver.MINORVERSION; + } + + public boolean usesLocalFiles() throws SQLException { + return true; + } + + public boolean usesLocalFilePerTable() throws SQLException { + return false; + } + + public boolean supportsMixedCaseIdentifiers() throws SQLException { + return false; + } + + public boolean storesUpperCaseIdentifiers() throws SQLException { + return false; + } + + public boolean storesLowerCaseIdentifiers() throws SQLException { + return false; + } + + public boolean storesMixedCaseIdentifiers() throws SQLException { + return true; + } + + public boolean supportsMixedCaseQuotedIdentifiers() throws SQLException { + return false; + } + + public boolean storesUpperCaseQuotedIdentifiers() throws SQLException { + return false; + } + + public boolean storesLowerCaseQuotedIdentifiers() throws SQLException { + return false; + } + + public boolean storesMixedCaseQuotedIdentifiers() throws SQLException { + return true; + } + + public String getIdentifierQuoteString() throws SQLException { + return "\""; + } + + public String getSQLKeywords() throws SQLException { + return "SELECT,UPDATE,CREATE,TABLE,VIEW,DELETE,FROM,WHERE" + + ",COMMIT,ROLLBACK,TRIGGER"; + } + + public String getNumericFunctions() throws SQLException { + return ""; + } + + public String getStringFunctions() throws SQLException { + return ""; + } + + public String getSystemFunctions() throws SQLException { + return ""; + } + + public String getTimeDateFunctions() throws SQLException { + return ""; + } + + public String getSearchStringEscape() throws SQLException { + return "\\"; + } + + public String getExtraNameCharacters() throws SQLException { + return ""; + } + + public boolean supportsAlterTableWithAddColumn() throws SQLException { + return false; + } + + public boolean supportsAlterTableWithDropColumn() throws SQLException { + return false; + } + + public boolean supportsColumnAliasing() throws SQLException { + return true; + } + + public boolean nullPlusNonNullIsNull() throws SQLException { + return false; + } + + public boolean supportsConvert() throws SQLException { + return false; + } + + public boolean supportsConvert(int fromType, int toType) + throws SQLException { + return false; + } + + public boolean supportsTableCorrelationNames() throws SQLException { + return true; + } + + public boolean supportsDifferentTableCorrelationNames() + throws SQLException { + return false; + } + + public boolean supportsExpressionsInOrderBy() throws SQLException { + return true; + } + + public boolean supportsOrderByUnrelated() throws SQLException { + return true; + } + + public boolean supportsGroupBy() throws SQLException { + return true; + } + + public boolean supportsGroupByUnrelated() throws SQLException { + return true; + } + + public boolean supportsGroupByBeyondSelect() throws SQLException { + return false; + } + + public boolean supportsLikeEscapeClause() throws SQLException { + return false; + } + + public boolean supportsMultipleResultSets() throws SQLException { + return false; + } + + public boolean supportsMultipleTransactions() throws SQLException { + return false; + } + + public boolean supportsNonNullableColumns() throws SQLException { + return true; + } + + public boolean supportsMinimumSQLGrammar() throws SQLException { + return true; + } + + public boolean supportsCoreSQLGrammar() throws SQLException { + return false; + } + + public boolean supportsExtendedSQLGrammar() throws SQLException { + return false; + } + + public boolean supportsANSI92EntryLevelSQL() throws SQLException { + return true; + } + + public boolean supportsANSI92IntermediateSQL() throws SQLException { + return false; + } + + public boolean supportsANSI92FullSQL() throws SQLException { + return false; + } + + public boolean supportsIntegrityEnhancementFacility() + throws SQLException { + return false; + } + + public boolean supportsOuterJoins() throws SQLException { + return false; + } + + public boolean supportsFullOuterJoins() throws SQLException { + return false; + } + + public boolean supportsLimitedOuterJoins() throws SQLException { + return false; + } + + public String getSchemaTerm() throws SQLException { + return ""; + } + + public String getProcedureTerm() throws SQLException { + return ""; + } + + public String getCatalogTerm() throws SQLException { + return ""; + } + + public boolean isCatalogAtStart() throws SQLException { + return false; + } + + public String getCatalogSeparator() throws SQLException { + return ""; + } + + public boolean supportsSchemasInDataManipulation() throws SQLException { + return false; + } + + public boolean supportsSchemasInProcedureCalls() throws SQLException { + return false; + } + + public boolean supportsSchemasInTableDefinitions() throws SQLException { + return false; + } + + public boolean supportsSchemasInIndexDefinitions() throws SQLException { + return false; + } + + public boolean supportsSchemasInPrivilegeDefinitions() + throws SQLException { + return false; + } + + public boolean supportsCatalogsInDataManipulation() throws SQLException { + return false; + } + + public boolean supportsCatalogsInProcedureCalls() throws SQLException { + return false; + } + + public boolean supportsCatalogsInTableDefinitions() throws SQLException { + return false; + } + + public boolean supportsCatalogsInIndexDefinitions() throws SQLException { + return false; + } + + public boolean supportsCatalogsInPrivilegeDefinitions() + throws SQLException { + return false; + } + + public boolean supportsPositionedDelete() throws SQLException { + return false; + } + + public boolean supportsPositionedUpdate() throws SQLException { + return false; + } + + public boolean supportsSelectForUpdate() throws SQLException { + return true; + } + + public boolean supportsStoredProcedures() throws SQLException { + return false; + } + + public boolean supportsSubqueriesInComparisons() throws SQLException { + return true; + } + + public boolean supportsSubqueriesInExists() throws SQLException { + return true; + } + + public boolean supportsSubqueriesInIns() throws SQLException { + return true; + } + + public boolean supportsSubqueriesInQuantifieds() throws SQLException { + return false; + } + + public boolean supportsCorrelatedSubqueries() throws SQLException { + return false; + } + + public boolean supportsUnion() throws SQLException { + return false; + } + + public boolean supportsUnionAll() throws SQLException { + return false; + } + + public boolean supportsOpenCursorsAcrossCommit() throws SQLException { + return false; + } + + public boolean supportsOpenCursorsAcrossRollback() throws SQLException { + return false; + } + + public boolean supportsOpenStatementsAcrossCommit() throws SQLException { + return false; + } + + public boolean supportsOpenStatementsAcrossRollback() throws SQLException { + return false; + } + + public int getMaxBinaryLiteralLength() throws SQLException { + return 0; + } + + public int getMaxCharLiteralLength() throws SQLException { + return 0; + } + + public int getMaxColumnNameLength() throws SQLException { + return 0; + } + + public int getMaxColumnsInGroupBy() throws SQLException { + return 0; + } + + public int getMaxColumnsInIndex() throws SQLException { + return 0; + } + + public int getMaxColumnsInOrderBy() throws SQLException { + return 0; + } + + public int getMaxColumnsInSelect() throws SQLException { + return 0; + } + + public int getMaxColumnsInTable() throws SQLException { + return 0; + } + + public int getMaxConnections() throws SQLException { + return 0; + } + + public int getMaxCursorNameLength() throws SQLException { + return 8; + } + + public int getMaxIndexLength() throws SQLException { + return 0; + } + + public int getMaxSchemaNameLength() throws SQLException { + return 0; + } + + public int getMaxProcedureNameLength() throws SQLException { + return 0; + } + + public int getMaxCatalogNameLength() throws SQLException { + return 0; + } + + public int getMaxRowSize() throws SQLException { + return 0; + } + + public boolean doesMaxRowSizeIncludeBlobs() throws SQLException { + return true; + } + + public int getMaxStatementLength() throws SQLException { + return 0; + } + + public int getMaxStatements() throws SQLException { + return 0; + } + + public int getMaxTableNameLength() throws SQLException { + return 0; + } + + public int getMaxTablesInSelect() throws SQLException { + return 0; + } + + public int getMaxUserNameLength() throws SQLException { + return 0; + } + + public int getDefaultTransactionIsolation() throws SQLException { + return Connection.TRANSACTION_SERIALIZABLE; + } + + public boolean supportsTransactions() throws SQLException { + return true; + } + + public boolean supportsTransactionIsolationLevel(int level) + throws SQLException { + return level == Connection.TRANSACTION_SERIALIZABLE; + } + + public boolean supportsDataDefinitionAndDataManipulationTransactions() + throws SQLException { + return true; + } + + public boolean supportsDataManipulationTransactionsOnly() + throws SQLException { + return false; + } + + public boolean dataDefinitionCausesTransactionCommit() + throws SQLException { + return false; + } + + public boolean dataDefinitionIgnoredInTransactions() throws SQLException { + return false; + } + + public ResultSet getProcedures(String catalog, String schemaPattern, + String procedureNamePattern) + throws SQLException { + return null; + } + + public ResultSet getProcedureColumns(String catalog, + String schemaPattern, + String procedureNamePattern, + String columnNamePattern) + throws SQLException { + return null; + } + + public ResultSet getTables(String catalog, String schemaPattern, + String tableNamePattern, String types[]) + throws SQLException { + JDBCStatement s = new JDBCStatement(conn); + StringBuffer sb = new StringBuffer(); + sb.append("SELECT '' AS 'TABLE_CAT', " + + "'' AS 'TABLE_SCHEM', " + + "tbl_name AS 'TABLE_NAME', " + + "upper(type) AS 'TABLE_TYPE', " + + "'' AS REMARKS FROM sqlite_master " + + "WHERE tbl_name like "); + if (tableNamePattern != null) { + sb.append(SQLite.Shell.sql_quote(tableNamePattern)); + } else { + sb.append("'%'"); + } + sb.append(" AND "); + if (types == null || types.length == 0) { + sb.append("(type = 'table' or type = 'view')"); + } else { + sb.append("("); + String sep = ""; + for (int i = 0; i < types.length; i++) { + sb.append(sep); + sb.append("type = "); + sb.append(SQLite.Shell.sql_quote(types[i].toLowerCase())); + sep = " or "; + } + sb.append(")"); + } + ResultSet rs = null; + try { + rs = s.executeQuery(sb.toString()); + s.close(); + } catch (SQLException e) { + throw e; + } finally { + s.close(); + } + return rs; + } + + public ResultSet getSchemas() throws SQLException { + String cols[] = { "TABLE_SCHEM" }; + SQLite.TableResult tr = new SQLite.TableResult(); + tr.columns(cols); + String row[] = { "" }; + tr.newrow(row); + JDBCResultSet rs = new JDBCResultSet(tr, null); + return (ResultSet) rs; + } + + public ResultSet getCatalogs() throws SQLException { + String cols[] = { "TABLE_CAT" }; + SQLite.TableResult tr = new SQLite.TableResult(); + tr.columns(cols); + String row[] = { "" }; + tr.newrow(row); + JDBCResultSet rs = new JDBCResultSet(tr, null); + return (ResultSet) rs; + } + + public ResultSet getTableTypes() throws SQLException { + String cols[] = { "TABLE_TYPE" }; + SQLite.TableResult tr = new SQLite.TableResult(); + tr.columns(cols); + String row[] = new String[1]; + row[0] = "TABLE"; + tr.newrow(row); + row = new String[1]; + row[0] = "VIEW"; + tr.newrow(row); + JDBCResultSet rs = new JDBCResultSet(tr, null); + return (ResultSet) rs; + } + + public ResultSet getColumns(String catalog, String schemaPattern, + String tableNamePattern, + String columnNamePattern) + throws SQLException { + JDBCStatement s = new JDBCStatement(conn); + JDBCResultSet rs0 = null; + try { + rs0 = (JDBCResultSet) + (s.executeQuery("PRAGMA table_info(" + + SQLite.Shell.sql_quote(tableNamePattern) + + ")")); + s.close(); + } catch (SQLException e) { + throw e; + } finally { + s.close(); + } + String cols[] = { + "TABLE_CAT", "TABLE_SCHEM", "TABLE_NAME", + "COLUMN_NAME", "DATA_TYPE", "TYPE_NAME", + "COLUMN_SIZE", "BUFFER_LENGTH", "DECIMAL_POINTS", + "NUM_PREC_RADIX", "NULLABLE", "REMARKS", + "COLUMN_DEF", "SQL_DATA_TYPE", "SQL_DATETIME_SUB", + "CHAR_OCTET_LENGTH", "ORDINAL_POSITION", "IS_NULLABLE" + }; + int types[] = { + Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, + Types.VARCHAR, Types.SMALLINT, Types.VARCHAR, + Types.INTEGER, Types.INTEGER, Types.INTEGER, + Types.INTEGER, Types.INTEGER, Types.VARCHAR, + Types.VARCHAR, Types.INTEGER, Types.INTEGER, + Types.INTEGER, Types.INTEGER, Types.VARCHAR + }; + TableResultX tr = new TableResultX(); + tr.columns(cols); + tr.sql_types(types); + JDBCResultSet rs = new JDBCResultSet((SQLite.TableResult) tr, null); + if (rs0 != null && rs0.tr != null && rs0.tr.nrows > 0) { + Hashtable<String, Integer> h = new Hashtable<String, Integer>(); + for (int i = 0; i < rs0.tr.ncolumns; i++) { + h.put(rs0.tr.column[i], new Integer(i)); + } + if (columnNamePattern != null && + columnNamePattern.charAt(0) == '%') { + columnNamePattern = null; + } + for (int i = 0; i < rs0.tr.nrows; i++) { + String r0[] = (String [])(rs0.tr.rows.elementAt(i)); + int col = ((Integer) h.get("name")).intValue(); + if (columnNamePattern != null) { + if (r0[col].compareTo(columnNamePattern) != 0) { + continue; + } + } + String row[] = new String[cols.length]; + row[0] = ""; + row[1] = ""; + row[2] = tableNamePattern; + row[3] = r0[col]; + col = ((Integer) h.get("type")).intValue(); + String typeStr = r0[col]; + int type = mapSqlType(typeStr); + row[4] = "" + type; + row[5] = mapTypeName(type); + row[6] = "" + getD(typeStr, type); + row[7] = "" + getM(typeStr, type); + row[8] = "10"; + row[9] = "0"; + row[11] = null; + col = ((Integer) h.get("dflt_value")).intValue(); + row[12] = r0[col]; + row[13] = "0"; + row[14] = "0"; + row[15] = "65536"; + col = ((Integer) h.get("cid")).intValue(); + Integer cid = new Integer(r0[col]); + row[16] = "" + (cid.intValue() + 1); + col = ((Integer) h.get("notnull")).intValue(); + row[17] = (r0[col].charAt(0) == '0') ? "YES" : "NO"; + row[10] = (r0[col].charAt(0) == '0') ? "" + columnNullable : + "" + columnNoNulls; + tr.newrow(row); + } + } + return rs; + } + + public ResultSet getColumnPrivileges(String catalog, String schema, + String table, + String columnNamePattern) + throws SQLException { + String cols[] = { + "TABLE_CAT", "TABLE_SCHEM", "TABLE_NAME", + "COLUMN_NAME", "GRANTOR", "GRANTEE", + "PRIVILEGE", "IS_GRANTABLE" + }; + int types[] = { + Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, + Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, + Types.VARCHAR, Types.VARCHAR + }; + TableResultX tr = new TableResultX(); + tr.columns(cols); + tr.sql_types(types); + JDBCResultSet rs = new JDBCResultSet((SQLite.TableResult) tr, null); + return rs; + } + + public ResultSet getTablePrivileges(String catalog, String schemaPattern, + String tableNamePattern) + throws SQLException { + String cols[] = { + "TABLE_CAT", "TABLE_SCHEM", "TABLE_NAME", + "COLUMN_NAME", "GRANTOR", "GRANTEE", + "PRIVILEGE", "IS_GRANTABLE" + }; + int types[] = { + Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, + Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, + Types.VARCHAR, Types.VARCHAR + }; + TableResultX tr = new TableResultX(); + tr.columns(cols); + tr.sql_types(types); + JDBCResultSet rs = new JDBCResultSet((SQLite.TableResult) tr, null); + return rs; + } + + public ResultSet getBestRowIdentifier(String catalog, String schema, + String table, int scope, + boolean nullable) + throws SQLException { + JDBCStatement s0 = new JDBCStatement(conn); + JDBCResultSet rs0 = null; + JDBCStatement s1 = new JDBCStatement(conn); + JDBCResultSet rs1 = null; + try { + rs0 = (JDBCResultSet) + (s0.executeQuery("PRAGMA index_list(" + + SQLite.Shell.sql_quote(table) + ")")); + rs1 = (JDBCResultSet) + (s1.executeQuery("PRAGMA table_info(" + + SQLite.Shell.sql_quote(table) + ")")); + } catch (SQLException e) { + throw e; + } finally { + s0.close(); + s1.close(); + } + String cols[] = { + "SCOPE", "COLUMN_NAME", "DATA_TYPE", + "TYPE_NAME", "COLUMN_SIZE", "BUFFER_LENGTH", + "DECIMAL_DIGITS", "PSEUDO_COLUMN" + }; + int types[] = { + Types.SMALLINT, Types.VARCHAR, Types.SMALLINT, + Types.VARCHAR, Types.INTEGER, Types.INTEGER, + Types.SMALLINT, Types.SMALLINT + }; + TableResultX tr = new TableResultX(); + tr.columns(cols); + tr.sql_types(types); + JDBCResultSet rs = new JDBCResultSet((SQLite.TableResult) tr, null); + if (rs0 != null && rs0.tr != null && rs0.tr.nrows > 0 && + rs1 != null && rs1.tr != null && rs1.tr.nrows > 0) { + Hashtable<String, Integer> h0 = new Hashtable<String, Integer>(); + for (int i = 0; i < rs0.tr.ncolumns; i++) { + h0.put(rs0.tr.column[i], new Integer(i)); + } + Hashtable<String, Integer> h1 = new Hashtable<String, Integer>(); + for (int i = 0; i < rs1.tr.ncolumns; i++) { + h1.put(rs1.tr.column[i], new Integer(i)); + } + for (int i = 0; i < rs0.tr.nrows; i++) { + String r0[] = (String [])(rs0.tr.rows.elementAt(i)); + int col = ((Integer) h0.get("unique")).intValue(); + String uniq = r0[col]; + col = ((Integer) h0.get("name")).intValue(); + String iname = r0[col]; + if (uniq.charAt(0) == '0') { + continue; + } + JDBCStatement s2 = new JDBCStatement(conn); + JDBCResultSet rs2 = null; + try { + rs2 = (JDBCResultSet) + (s2.executeQuery("PRAGMA index_info(" + + SQLite.Shell.sql_quote(iname) + ")")); + } catch (SQLException e) { + } finally { + s2.close(); + } + if (rs2 == null || rs2.tr == null || rs2.tr.nrows <= 0) { + continue; + } + Hashtable<String, Integer> h2 = + new Hashtable<String, Integer>(); + for (int k = 0; k < rs2.tr.ncolumns; k++) { + h2.put(rs2.tr.column[k], new Integer(k)); + } + for (int k = 0; k < rs2.tr.nrows; k++) { + String r2[] = (String [])(rs2.tr.rows.elementAt(k)); + col = ((Integer) h2.get("name")).intValue(); + String cname = r2[col]; + for (int m = 0; m < rs1.tr.nrows; m++) { + String r1[] = (String [])(rs1.tr.rows.elementAt(m)); + col = ((Integer) h1.get("name")).intValue(); + if (cname.compareTo(r1[col]) == 0) { + String row[] = new String[cols.length]; + row[0] = "" + scope; + row[1] = cname; + row[2] = "" + Types.VARCHAR; + row[3] = "VARCHAR"; + row[4] = "65536"; + row[5] = "0"; + row[6] = "0"; + row[7] = "" + bestRowNotPseudo; + tr.newrow(row); + } + } + } + } + } + if (tr.nrows <= 0) { + String row[] = new String[cols.length]; + row[0] = "" + scope; + row[1] = "_ROWID_"; + row[2] = "" + Types.INTEGER; + row[3] = "INTEGER"; + row[4] = "10"; + row[5] = "0"; + row[6] = "0"; + row[7] = "" + bestRowPseudo; + tr.newrow(row); + } + return rs; + } + + public ResultSet getVersionColumns(String catalog, String schema, + String table) throws SQLException { + String cols[] = { + "SCOPE", "COLUMN_NAME", "DATA_TYPE", + "TYPE_NAME", "COLUMN_SIZE", "BUFFER_LENGTH", + "DECIMAL_DIGITS", "PSEUDO_COLUMN" + }; + int types[] = { + Types.SMALLINT, Types.VARCHAR, Types.SMALLINT, + Types.VARCHAR, Types.INTEGER, Types.INTEGER, + Types.SMALLINT, Types.SMALLINT + }; + TableResultX tr = new TableResultX(); + tr.columns(cols); + tr.sql_types(types); + JDBCResultSet rs = new JDBCResultSet((SQLite.TableResult) tr, null); + return rs; + } + + public ResultSet getPrimaryKeys(String catalog, String schema, + String table) throws SQLException { + JDBCStatement s0 = new JDBCStatement(conn); + JDBCResultSet rs0 = null; + try { + rs0 = (JDBCResultSet) + (s0.executeQuery("PRAGMA index_list(" + + SQLite.Shell.sql_quote(table) + ")")); + } catch (SQLException e) { + throw e; + } finally { + s0.close(); + } + String cols[] = { + "TABLE_CAT", "TABLE_SCHEM", "TABLE_NAME", + "COLUMN_NAME", "KEY_SEQ", "PK_NAME" + }; + int types[] = { + Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, + Types.VARCHAR, Types.SMALLINT, Types.VARCHAR + }; + TableResultX tr = new TableResultX(); + tr.columns(cols); + tr.sql_types(types); + JDBCResultSet rs = new JDBCResultSet((SQLite.TableResult) tr, null); + if (rs0 != null && rs0.tr != null && rs0.tr.nrows > 0) { + Hashtable<String, Integer> h0 = new Hashtable<String, Integer>(); + for (int i = 0; i < rs0.tr.ncolumns; i++) { + h0.put(rs0.tr.column[i], new Integer(i)); + } + for (int i = 0; i < rs0.tr.nrows; i++) { + String r0[] = (String [])(rs0.tr.rows.elementAt(i)); + int col = ((Integer) h0.get("unique")).intValue(); + String uniq = r0[col]; + col = ((Integer) h0.get("name")).intValue(); + String iname = r0[col]; + if (uniq.charAt(0) == '0') { + continue; + } + JDBCStatement s1 = new JDBCStatement(conn); + JDBCResultSet rs1 = null; + try { + rs1 = (JDBCResultSet) + (s1.executeQuery("PRAGMA index_info(" + + SQLite.Shell.sql_quote(iname) + ")")); + } catch (SQLException e) { + } finally { + s1.close(); + } + if (rs1 == null || rs1.tr == null || rs1.tr.nrows <= 0) { + continue; + } + Hashtable<String, Integer> h1 = + new Hashtable<String, Integer>(); + for (int k = 0; k < rs1.tr.ncolumns; k++) { + h1.put(rs1.tr.column[k], new Integer(k)); + } + for (int k = 0; k < rs1.tr.nrows; k++) { + String r1[] = (String [])(rs1.tr.rows.elementAt(k)); + String row[] = new String[cols.length]; + row[0] = ""; + row[1] = ""; + row[2] = table; + col = ((Integer) h1.get("name")).intValue(); + row[3] = r1[col]; + col = ((Integer) h1.get("seqno")).intValue(); +// BEGIN android-changed + row[4] = "" + (Integer.parseInt(r1[col]) + 1); +// END android-changed + row[5] = iname; + tr.newrow(row); + } + } + } + JDBCStatement s1 = new JDBCStatement(conn); + try { + rs0 = (JDBCResultSet) + (s1.executeQuery("PRAGMA table_info(" + + SQLite.Shell.sql_quote(table) + ")")); + } catch (SQLException e) { + throw e; + } finally { + s1.close(); + } + if (rs0 != null && rs0.tr != null && rs0.tr.nrows > 0) { + Hashtable<String, Integer> h0 = new Hashtable<String, Integer>(); + for (int i = 0; i < rs0.tr.ncolumns; i++) { + h0.put(rs0.tr.column[i], new Integer(i)); + } + for (int i = 0; i < rs0.tr.nrows; i++) { + String r0[] = (String [])(rs0.tr.rows.elementAt(i)); + int col = ((Integer) h0.get("type")).intValue(); + String type = r0[col]; + if (!type.equalsIgnoreCase("integer")) { + continue; + } + col = ((Integer) h0.get("pk")).intValue(); + String pk = r0[col]; + if (pk.charAt(0) == '0') { + continue; + } + String row[] = new String[cols.length]; + row[0] = ""; + row[1] = ""; + row[2] = table; + col = ((Integer) h0.get("name")).intValue(); + row[3] = r0[col]; + col = ((Integer) h0.get("cid")).intValue(); +// BEGIN android-changed + row[4] = "" + (Integer.parseInt(r0[col]) + 1); +// END android-changed + row[5] = ""; + tr.newrow(row); + } + } + return rs; + } + + private void internalImportedKeys(String table, String pktable, + JDBCResultSet in, TableResultX out) { + Hashtable<String, Integer> h0 = new Hashtable<String, Integer>(); + for (int i = 0; i < in.tr.ncolumns; i++) { + h0.put(in.tr.column[i], new Integer(i)); + } + for (int i = 0; i < in.tr.nrows; i++) { + String r0[] = (String [])(in.tr.rows.elementAt(i)); + int col = ((Integer) h0.get("table")).intValue(); + String pktab = r0[col]; + if (pktable != null && !pktable.equalsIgnoreCase(pktab)) { + continue; + } + col = ((Integer) h0.get("from")).intValue(); + String pkcol = r0[col]; + col = ((Integer) h0.get("to")).intValue(); + String fkcol = r0[col]; + col = ((Integer) h0.get("seq")).intValue(); + String seq = r0[col]; + String row[] = new String[out.ncolumns]; + row[0] = ""; + row[1] = ""; + row[2] = pktab; + row[3] = pkcol; + row[4] = ""; + row[5] = ""; + row[6] = table; + row[7] = fkcol == null ? pkcol : fkcol; +// BEGIN android-changed + row[8] = "" + ((Integer.parseInt(seq)) + 1); +// END android-changed + row[9] = + "" + java.sql.DatabaseMetaData.importedKeyNoAction; + row[10] = + "" + java.sql.DatabaseMetaData.importedKeyNoAction; + row[11] = null; + row[12] = null; + row[13] = + "" + java.sql.DatabaseMetaData.importedKeyNotDeferrable; + out.newrow(row); + } + } + + public ResultSet getImportedKeys(String catalog, String schema, + String table) throws SQLException { + JDBCStatement s0 = new JDBCStatement(conn); + JDBCResultSet rs0 = null; + try { + rs0 = (JDBCResultSet) + (s0.executeQuery("PRAGMA foreign_key_list(" + + SQLite.Shell.sql_quote(table) + ")")); + } catch (SQLException e) { + throw e; + } finally { + s0.close(); + } + String cols[] = { + "PKTABLE_CAT", "PKTABLE_SCHEM", "PKTABLE_NAME", + "PKCOLUMN_NAME", "FKTABLE_CAT", "FKTABLE_SCHEM", + "FKTABLE_NAME", "FKCOLUMN_NAME", "KEY_SEQ", + "UPDATE_RULE", "DELETE_RULE", "FK_NAME", + "PK_NAME", "DEFERRABILITY" + }; + int types[] = { + Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, + Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, + Types.VARCHAR, Types.VARCHAR, Types.SMALLINT, + Types.SMALLINT, Types.SMALLINT, Types.VARCHAR, + Types.VARCHAR, Types.SMALLINT + }; + TableResultX tr = new TableResultX(); + tr.columns(cols); + tr.sql_types(types); + JDBCResultSet rs = new JDBCResultSet((SQLite.TableResult) tr, null); + if (rs0 != null && rs0.tr != null && rs0.tr.nrows > 0) { + internalImportedKeys(table, null, rs0, tr); + } + return rs; + } + + public ResultSet getExportedKeys(String catalog, String schema, + String table) throws SQLException { + String cols[] = { + "PKTABLE_CAT", "PKTABLE_SCHEM", "PKTABLE_NAME", + "PKCOLUMN_NAME", "FKTABLE_CAT", "FKTABLE_SCHEM", + "FKTABLE_NAME", "FKCOLUMN_NAME", "KEY_SEQ", + "UPDATE_RULE", "DELETE_RULE", "FK_NAME", + "PK_NAME", "DEFERRABILITY" + }; + int types[] = { + Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, + Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, + Types.VARCHAR, Types.VARCHAR, Types.SMALLINT, + Types.SMALLINT, Types.SMALLINT, Types.VARCHAR, + Types.VARCHAR, Types.SMALLINT + }; + TableResultX tr = new TableResultX(); + tr.columns(cols); + tr.sql_types(types); + JDBCResultSet rs = new JDBCResultSet(tr, null); + return rs; + } + + public ResultSet getCrossReference(String primaryCatalog, + String primarySchema, + String primaryTable, + String foreignCatalog, + String foreignSchema, + String foreignTable) + throws SQLException { + JDBCResultSet rs0 = null; + if (foreignTable != null && foreignTable.charAt(0) != '%') { + JDBCStatement s0 = new JDBCStatement(conn); + try { + rs0 = (JDBCResultSet) + (s0.executeQuery("PRAGMA foreign_key_list(" + + SQLite.Shell.sql_quote(foreignTable) + ")")); + } catch (SQLException e) { + throw e; + } finally { + s0.close(); + } + } + String cols[] = { + "PKTABLE_CAT", "PKTABLE_SCHEM", "PKTABLE_NAME", + "PKCOLUMN_NAME", "FKTABLE_CAT", "FKTABLE_SCHEM", + "FKTABLE_NAME", "FKCOLUMN_NAME", "KEY_SEQ", + "UPDATE_RULE", "DELETE_RULE", "FK_NAME", + "PK_NAME", "DEFERRABILITY" + }; + int types[] = { + Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, + Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, + Types.VARCHAR, Types.VARCHAR, Types.SMALLINT, + Types.SMALLINT, Types.SMALLINT, Types.VARCHAR, + Types.VARCHAR, Types.SMALLINT + }; + TableResultX tr = new TableResultX(); + tr.columns(cols); + tr.sql_types(types); + JDBCResultSet rs = new JDBCResultSet(tr, null); + if (rs0 != null && rs0.tr != null && rs0.tr.nrows > 0) { + String pktable = null; + if (primaryTable != null && primaryTable.charAt(0) != '%') { + pktable = primaryTable; + } + internalImportedKeys(foreignTable, pktable, rs0, tr); + } + return rs; + } + + public ResultSet getTypeInfo() throws SQLException { + String cols[] = { + "TYPE_NAME", "DATA_TYPE", "PRECISION", + "LITERAL_PREFIX", "LITERAL_SUFFIX", "CREATE_PARAMS", + "NULLABLE", "CASE_SENSITIVE", "SEARCHABLE", + "UNSIGNED_ATTRIBUTE", "FIXED_PREC_SCALE", "AUTO_INCREMENT", + "LOCAL_TYPE_NAME", "MINIMUM_SCALE", "MAXIMUM_SCALE", + "SQL_DATA_TYPE", "SQL_DATETIME_SUB", "NUM_PREC_RADIX" + }; + int types[] = { + Types.VARCHAR, Types.SMALLINT, Types.INTEGER, + Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, + Types.SMALLINT, Types.BIT, Types.SMALLINT, + Types.BIT, Types.BIT, Types.BIT, + Types.VARCHAR, Types.SMALLINT, Types.SMALLINT, + Types.INTEGER, Types.INTEGER, Types.INTEGER + }; + TableResultX tr = new TableResultX(); + tr.columns(cols); + tr.sql_types(types); + JDBCResultSet rs = new JDBCResultSet(tr, null); + String row1[] = { + "VARCHAR", "" + Types.VARCHAR, "65536", + "'", "'", null, + "" + typeNullable, "1", "" + typeSearchable, + "0", "0", "0", + null, "0", "0", + "0", "0", "0" + }; + tr.newrow(row1); + String row2[] = { + "INTEGER", "" + Types.INTEGER, "32", + null, null, null, + "" + typeNullable, "0", "" + typeSearchable, + "0", "0", "1", + null, "0", "0", + "0", "0", "2" + }; + tr.newrow(row2); + String row3[] = { + "DOUBLE", "" + Types.DOUBLE, "16", + null, null, null, + "" + typeNullable, "0", "" + typeSearchable, + "0", "0", "1", + null, "0", "0", + "0", "0", "10" + }; + tr.newrow(row3); + String row4[] = { + "FLOAT", "" + Types.FLOAT, "7", + null, null, null, + "" + typeNullable, "0", "" + typeSearchable, + "0", "0", "1", + null, "0", "0", + "0", "0", "10" + }; + tr.newrow(row4); + String row5[] = { + "SMALLINT", "" + Types.SMALLINT, "16", + null, null, null, + "" + typeNullable, "0", "" + typeSearchable, + "0", "0", "1", + null, "0", "0", + "0", "0", "2" + }; + tr.newrow(row5); + String row6[] = { + "BIT", "" + Types.BIT, "1", + null, null, null, + "" + typeNullable, "0", "" + typeSearchable, + "0", "0", "1", + null, "0", "0", + "0", "0", "2" + }; + tr.newrow(row6); + String row7[] = { + "TIMESTAMP", "" + Types.TIMESTAMP, "30", + null, null, null, + "" + typeNullable, "0", "" + typeSearchable, + "0", "0", "1", + null, "0", "0", + "0", "0", "0" + }; + tr.newrow(row7); + String row8[] = { + "DATE", "" + Types.DATE, "10", + null, null, null, + "" + typeNullable, "0", "" + typeSearchable, + "0", "0", "1", + null, "0", "0", + "0", "0", "0" + }; + tr.newrow(row8); + String row9[] = { + "TIME", "" + Types.TIME, "8", + null, null, null, + "" + typeNullable, "0", "" + typeSearchable, + "0", "0", "1", + null, "0", "0", + "0", "0", "0" + }; + tr.newrow(row9); + String row10[] = { + "BINARY", "" + Types.BINARY, "65536", + null, null, null, + "" + typeNullable, "0", "" + typeSearchable, + "0", "0", "1", + null, "0", "0", + "0", "0", "0" + }; + tr.newrow(row10); + String row11[] = { + "VARBINARY", "" + Types.VARBINARY, "65536", + null, null, null, + "" + typeNullable, "0", "" + typeSearchable, + "0", "0", "1", + null, "0", "0", + "0", "0", "0" + }; + tr.newrow(row11); + return rs; + } + + public ResultSet getIndexInfo(String catalog, String schema, String table, + boolean unique, boolean approximate) + throws SQLException { + JDBCStatement s0 = new JDBCStatement(conn); + JDBCResultSet rs0 = null; + try { + rs0 = (JDBCResultSet) + (s0.executeQuery("PRAGMA index_list(" + + SQLite.Shell.sql_quote(table) + ")")); + } catch (SQLException e) { + throw e; + } finally { + s0.close(); + } + String cols[] = { + "TABLE_CAT", "TABLE_SCHEM", "TABLE_NAME", + "NON_UNIQUE", "INDEX_QUALIFIER", "INDEX_NAME", + "TYPE", "ORDINAL_POSITION", "COLUMN_NAME", + "ASC_OR_DESC", "CARDINALITY", "PAGES", + "FILTER_CONDITION" + }; + int types[] = { + Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, + Types.BIT, Types.VARCHAR, Types.VARCHAR, + Types.SMALLINT, Types.SMALLINT, Types.VARCHAR, + Types.VARCHAR, Types.INTEGER, Types.INTEGER, + Types.VARCHAR + }; + TableResultX tr = new TableResultX(); + tr.columns(cols); + tr.sql_types(types); + JDBCResultSet rs = new JDBCResultSet(tr, null); + if (rs0 != null && rs0.tr != null && rs0.tr.nrows > 0) { + Hashtable<String, Integer> h0 = new Hashtable<String, Integer>(); + for (int i = 0; i < rs0.tr.ncolumns; i++) { + h0.put(rs0.tr.column[i], new Integer(i)); + } + for (int i = 0; i < rs0.tr.nrows; i++) { + String r0[] = (String [])(rs0.tr.rows.elementAt(i)); + int col = ((Integer) h0.get("unique")).intValue(); + String uniq = r0[col]; + col = ((Integer) h0.get("name")).intValue(); + String iname = r0[col]; + if (unique && uniq.charAt(0) == '0') { + continue; + } + JDBCStatement s1 = new JDBCStatement(conn); + JDBCResultSet rs1 = null; + try { + rs1 = (JDBCResultSet) + (s1.executeQuery("PRAGMA index_info(" + + SQLite.Shell.sql_quote(iname) + ")")); + } catch (SQLException e) { + } finally { + s1.close(); + } + if (rs1 == null || rs1.tr == null || rs1.tr.nrows <= 0) { + continue; + } + Hashtable<String, Integer> h1 = + new Hashtable<String, Integer>(); + for (int k = 0; k < rs1.tr.ncolumns; k++) { + h1.put(rs1.tr.column[k], new Integer(k)); + } + for (int k = 0; k < rs1.tr.nrows; k++) { + String r1[] = (String [])(rs1.tr.rows.elementAt(k)); + String row[] = new String[cols.length]; + row[0] = ""; + row[1] = ""; + row[2] = table; + row[3] = (uniq.charAt(0) != '0' || + (iname.charAt(0) == '(' && + iname.indexOf(" autoindex ") > 0)) ? "0" : "1"; + row[4] = ""; + row[5] = iname; + row[6] = "" + tableIndexOther; + col = ((Integer) h1.get("seqno")).intValue(); +// BEGIN android-changed + row[7] = "" + (Integer.parseInt(r1[col]) + 1); +// END android-changed + col = ((Integer) h1.get("name")).intValue(); + row[8] = r1[col]; + row[9] = "A"; + row[10] = "0"; + row[11] = "0"; + row[12] = null; + tr.newrow(row); + } + } + } + return rs; + } + + public boolean supportsResultSetType(int type) throws SQLException { + return type == ResultSet.CONCUR_READ_ONLY; + } + + public boolean supportsResultSetConcurrency(int type, int concurrency) + throws SQLException { + return false; + } + + public boolean ownUpdatesAreVisible(int type) throws SQLException { + return false; + } + + public boolean ownDeletesAreVisible(int type) throws SQLException { + return false; + } + + public boolean ownInsertsAreVisible(int type) throws SQLException { + return false; + } + + public boolean othersUpdatesAreVisible(int type) throws SQLException { + return false; + } + + public boolean othersDeletesAreVisible(int type) throws SQLException { + return false; + } + + public boolean othersInsertsAreVisible(int type) throws SQLException { + return false; + } + + public boolean updatesAreDetected(int type) throws SQLException { + return false; + } + + public boolean deletesAreDetected(int type) throws SQLException { + return false; + } + + public boolean insertsAreDetected(int type) throws SQLException { + return false; + } + + public boolean supportsBatchUpdates() throws SQLException { + return false; + } + + public ResultSet getUDTs(String catalog, String schemaPattern, + String typeNamePattern, int[] types) + throws SQLException { + return null; + } + + public Connection getConnection() throws SQLException { + return conn; + } + + static String mapTypeName(int type) { + switch (type) { + case Types.INTEGER: return "integer"; + case Types.SMALLINT: return "smallint"; + case Types.FLOAT: return "float"; + case Types.DOUBLE: return "double"; + case Types.TIMESTAMP: return "timestamp"; + case Types.DATE: return "date"; + case Types.TIME: return "time"; + case Types.BINARY: return "binary"; + case Types.VARBINARY: return "varbinary"; + } + return "varchar"; + } + + static int mapSqlType(String type) { + if (type == null) { + return Types.VARCHAR; + } + type = type.toLowerCase(); + if (type.startsWith("inter")) { + return Types.VARCHAR; + } + if (type.startsWith("numeric") || + type.startsWith("int")) { + return Types.INTEGER; + } + if (type.startsWith("tinyint") || + type.startsWith("smallint")) { + return Types.SMALLINT; + } + if (type.startsWith("float")) { + return Types.FLOAT; + } + if (type.startsWith("double")) { + return Types.DOUBLE; + } + if (type.startsWith("datetime") || + type.startsWith("timestamp")) { + return Types.TIMESTAMP; + } + if (type.startsWith("date")) { + return Types.DATE; + } + if (type.startsWith("time")) { + return Types.TIME; + } + if (type.startsWith("blob")) { + return Types.BINARY; + } + if (type.startsWith("binary")) { + return Types.BINARY; + } + if (type.startsWith("varbinary")) { + return Types.VARBINARY; + } + return Types.VARCHAR; + } + + static int getM(String typeStr, int type) { + int m = 65536; + switch (type) { + case Types.INTEGER: m = 11; break; + case Types.SMALLINT: m = 6; break; + case Types.FLOAT: m = 25; break; + case Types.DOUBLE: m = 54; break; + case Types.TIMESTAMP: return 30; + case Types.DATE: return 10; + case Types.TIME: return 8; + } + typeStr = typeStr.toLowerCase(); + int i1 = typeStr.indexOf('('); + if (i1 > 0) { + ++i1; + int i2 = typeStr.indexOf(',', i1); + if (i2 < 0) { + i2 = typeStr.indexOf(')', i1); + } + if (i2 - i1 > 0) { + String num = typeStr.substring(i1, i2); + try { + m = java.lang.Integer.parseInt(num, 10); + } catch (NumberFormatException e) { + } + } + } + return m; + } + + static int getD(String typeStr, int type) { + int d = 0; + switch (type) { + case Types.INTEGER: d = 10; break; + case Types.SMALLINT: d = 5; break; + case Types.FLOAT: d = 24; break; + case Types.DOUBLE: d = 53; break; + default: return getM(typeStr, type); + } + typeStr = typeStr.toLowerCase(); + int i1 = typeStr.indexOf('('); + if (i1 > 0) { + ++i1; + int i2 = typeStr.indexOf(',', i1); + if (i2 < 0) { + return getM(typeStr, type); + } + i1 = i2; + i2 = typeStr.indexOf(')', i1); + if (i2 - i1 > 0) { + String num = typeStr.substring(i1, i2); + try { + d = java.lang.Integer.parseInt(num, 10); + } catch (NumberFormatException e) { + } + } + } + return d; + } + + public boolean supportsSavepoints() { + return false; + } + + public boolean supportsNamedParameters() { + return false; + } + + public boolean supportsMultipleOpenResults() { + return false; + } + + public boolean supportsGetGeneratedKeys() { + return false; + } + + public boolean supportsResultSetHoldability(int x) { + return false; + } + + public boolean supportsStatementPooling() { + return false; + } + + public boolean locatorsUpdateCopy() throws SQLException { + throw new SQLException("not supported"); + } + + public ResultSet getSuperTypes(String catalog, String schemaPattern, + String typeNamePattern) + throws SQLException { + throw new SQLException("not supported"); + } + + public ResultSet getSuperTables(String catalog, String schemaPattern, + String tableNamePattern) + throws SQLException { + throw new SQLException("not supported"); + } + + public ResultSet getAttributes(String catalog, String schemaPattern, + String typeNamePattern, + String attributeNamePattern) + throws SQLException { + throw new SQLException("not supported"); + } + + public int getResultSetHoldability() throws SQLException { + return ResultSet.HOLD_CURSORS_OVER_COMMIT; + } + + public int getDatabaseMajorVersion() { + return SQLite.JDBCDriver.MAJORVERSION; + } + + public int getDatabaseMinorVersion() { + return SQLite.JDBCDriver.MINORVERSION; + } + + public int getJDBCMajorVersion() { + return 1; + } + + public int getJDBCMinorVersion() { + return 0; + } + + public int getSQLStateType() throws SQLException { + return sqlStateXOpen; + } + +} diff --git a/sqlite-jdbc/src/main/java/SQLite/JDBC2y/JDBCPreparedStatement.java b/sqlite-jdbc/src/main/java/SQLite/JDBC2y/JDBCPreparedStatement.java new file mode 100644 index 0000000..ab81867 --- /dev/null +++ b/sqlite-jdbc/src/main/java/SQLite/JDBC2y/JDBCPreparedStatement.java @@ -0,0 +1,752 @@ +package SQLite.JDBC2y; + +import java.sql.*; +import java.math.BigDecimal; +import java.util.*; + +class BatchArg { + String arg; + boolean blob; + + BatchArg(String arg, boolean blob) { + if (arg == null) { + this.arg = null; + } else { + this.arg = new String(arg); + } + this.blob = blob; + } +} + +public class JDBCPreparedStatement extends JDBCStatement + implements java.sql.PreparedStatement { + + private String sql; + private String args[]; + private boolean blobs[]; + private ArrayList<BatchArg> batch; + private static final boolean nullrepl = + SQLite.Database.version().compareTo("2.5.0") < 0; + + public JDBCPreparedStatement(JDBCConnection conn, String sql) { + super(conn); + this.args = null; + this.blobs = null; + this.batch = null; + this.sql = fixup(sql); + } + + private String fixup(String sql) { + StringBuffer sb = new StringBuffer(); + boolean inq = false; + int nparm = 0; + for (int i = 0; i < sql.length(); i++) { + char c = sql.charAt(i); + if (c == '\'') { + if (inq) { + char nextChar = 0; + if(i + 1 < sql.length()) { + nextChar = sql.charAt(i + 1); + } + if (nextChar == '\'') { + sb.append(c); + sb.append(nextChar); + i++; + } else { + inq = false; + sb.append(c); + } + } else { + inq = true; + sb.append(c); + } + } else if (c == '?') { + if (inq) { + sb.append(c); + } else { + ++nparm; + sb.append(nullrepl ? "'%q'" : "%Q"); + } + } else if (c == ';') { + if (!inq) { + break; + } + sb.append(c); + } else if (c == '%') { + sb.append("%%"); + } else { + sb.append(c); + } + } + args = new String[nparm]; + blobs = new boolean[nparm]; + try { + clearParameters(); + } catch (SQLException e) { + } + return sb.toString(); + } + + private String fixup2(String sql) { + if (!conn.db.is3()) { + return sql; + } + StringBuffer sb = new StringBuffer(); + int parm = -1; + for (int i = 0; i < sql.length(); i++) { + char c = sql.charAt(i); + if (c == '%') { + sb.append(c); + ++i; + c = sql.charAt(i); + if (c == 'Q') { + parm++; + if (blobs[parm]) { + c = 's'; + } + } + } + sb.append(c); + } + return sb.toString(); + } + + public ResultSet executeQuery() throws SQLException { + return executeQuery(fixup2(sql), args, false); + } + + public int executeUpdate() throws SQLException { + executeQuery(fixup2(sql), args, true); + return updcnt; + } + + public void setNull(int parameterIndex, int sqlType) throws SQLException { + if (parameterIndex < 1 || parameterIndex > args.length) { + throw new SQLException("bad parameter index"); + } + args[parameterIndex - 1] = nullrepl ? "" : null; + blobs[parameterIndex - 1] = false; + } + + public void setBoolean(int parameterIndex, boolean x) + throws SQLException { + if (parameterIndex < 1 || parameterIndex > args.length) { + throw new SQLException("bad parameter index"); + } + args[parameterIndex - 1] = x ? "1" : "0"; + blobs[parameterIndex - 1] = false; + } + + public void setByte(int parameterIndex, byte x) throws SQLException { + if (parameterIndex < 1 || parameterIndex > args.length) { + throw new SQLException("bad parameter index"); + } + args[parameterIndex - 1] = "" + x; + blobs[parameterIndex - 1] = false; + } + + public void setShort(int parameterIndex, short x) throws SQLException { + if (parameterIndex < 1 || parameterIndex > args.length) { + throw new SQLException("bad parameter index"); + } + args[parameterIndex - 1] = "" + x; + blobs[parameterIndex - 1] = false; + } + + public void setInt(int parameterIndex, int x) throws SQLException { + if (parameterIndex < 1 || parameterIndex > args.length) { + throw new SQLException("bad parameter index"); + } + args[parameterIndex - 1] = "" + x; + blobs[parameterIndex - 1] = false; + } + + public void setLong(int parameterIndex, long x) throws SQLException { + if (parameterIndex < 1 || parameterIndex > args.length) { + throw new SQLException("bad parameter index"); + } + args[parameterIndex - 1] = "" + x; + blobs[parameterIndex - 1] = false; + } + + public void setFloat(int parameterIndex, float x) throws SQLException { + if (parameterIndex < 1 || parameterIndex > args.length) { + throw new SQLException("bad parameter index"); + } + args[parameterIndex - 1] = "" + x; + blobs[parameterIndex - 1] = false; + } + + public void setDouble(int parameterIndex, double x) throws SQLException { + if (parameterIndex < 1 || parameterIndex > args.length) { + throw new SQLException("bad parameter index"); + } + args[parameterIndex - 1] = "" + x; + blobs[parameterIndex - 1] = false; + } + + public void setBigDecimal(int parameterIndex, BigDecimal x) + throws SQLException { + if (parameterIndex < 1 || parameterIndex > args.length) { + throw new SQLException("bad parameter index"); + } + if (x == null) { + args[parameterIndex - 1] = nullrepl ? "" : null; + } else { + args[parameterIndex - 1] = "" + x; + } + blobs[parameterIndex - 1] = false; + } + + public void setString(int parameterIndex, String x) throws SQLException { + if (parameterIndex < 1 || parameterIndex > args.length) { + throw new SQLException("bad parameter index"); + } + if (x == null) { + args[parameterIndex - 1] = nullrepl ? "" : null; + } else { + args[parameterIndex - 1] = x; + } + blobs[parameterIndex - 1] = false; + } + + public void setBytes(int parameterIndex, byte x[]) throws SQLException { + if (parameterIndex < 1 || parameterIndex > args.length) { + throw new SQLException("bad parameter index"); + } + blobs[parameterIndex - 1] = false; + if (x == null) { + args[parameterIndex - 1] = nullrepl ? "" : null; + } else { + if (conn.db.is3()) { + args[parameterIndex - 1] = SQLite.StringEncoder.encodeX(x); + blobs[parameterIndex - 1] = true; + } else { + args[parameterIndex - 1] = SQLite.StringEncoder.encode(x); + } + } + } + + public void setDate(int parameterIndex, java.sql.Date x) + throws SQLException { + if (parameterIndex < 1 || parameterIndex > args.length) { + throw new SQLException("bad parameter index"); + } + if (x == null) { + args[parameterIndex - 1] = nullrepl ? "" : null; + } else { + args[parameterIndex - 1] = x.toString(); + } + blobs[parameterIndex - 1] = false; + } + + public void setTime(int parameterIndex, java.sql.Time x) + throws SQLException { + if (parameterIndex < 1 || parameterIndex > args.length) { + throw new SQLException("bad parameter index"); + } + if (x == null) { + args[parameterIndex - 1] = nullrepl ? "" : null; + } else { + args[parameterIndex - 1] = x.toString(); + } + blobs[parameterIndex - 1] = false; + } + + public void setTimestamp(int parameterIndex, java.sql.Timestamp x) + throws SQLException { + if (parameterIndex < 1 || parameterIndex > args.length) { + throw new SQLException("bad parameter index"); + } + if (x == null) { + args[parameterIndex - 1] = nullrepl ? "" : null; + } else { + args[parameterIndex - 1] = x.toString(); + } + blobs[parameterIndex - 1] = false; + } + + public void setAsciiStream(int parameterIndex, java.io.InputStream x, + int length) throws SQLException { + throw new SQLException("not supported"); + } + + @Deprecated + public void setUnicodeStream(int parameterIndex, java.io.InputStream x, + int length) throws SQLException { + throw new SQLException("not supported"); + } + + public void setBinaryStream(int parameterIndex, java.io.InputStream x, + int length) throws SQLException { + throw new SQLException("not supported"); + } + + public void clearParameters() throws SQLException { + for (int i = 0; i < args.length; i++) { + args[i] = nullrepl ? "" : null; + blobs[i] = false; + } + } + + public void setObject(int parameterIndex, Object x, int targetSqlType, + int scale) throws SQLException { + if (parameterIndex < 1 || parameterIndex > args.length) { + throw new SQLException("bad parameter index"); + } + if (x == null) { + args[parameterIndex - 1] = nullrepl ? "" : null; + } else { + if (x instanceof byte[]) { + byte[] bx = (byte[]) x; + if (conn.db.is3()) { + args[parameterIndex - 1] = + SQLite.StringEncoder.encodeX(bx); + blobs[parameterIndex - 1] = true; + return; + } + args[parameterIndex - 1] = SQLite.StringEncoder.encode(bx); + } else { + args[parameterIndex - 1] = x.toString(); + } + } + blobs[parameterIndex - 1] = false; + } + + public void setObject(int parameterIndex, Object x, int targetSqlType) + throws SQLException { + if (parameterIndex < 1 || parameterIndex > args.length) { + throw new SQLException("bad parameter index"); + } + if (x == null) { + args[parameterIndex - 1] = nullrepl ? "" : null; + } else { + if (x instanceof byte[]) { + byte[] bx = (byte[]) x; + if (conn.db.is3()) { + args[parameterIndex - 1] = + SQLite.StringEncoder.encodeX(bx); + blobs[parameterIndex - 1] = true; + return; + } + args[parameterIndex - 1] = SQLite.StringEncoder.encode(bx); + } else { + args[parameterIndex - 1] = x.toString(); + } + } + blobs[parameterIndex - 1] = false; + } + + public void setObject(int parameterIndex, Object x) throws SQLException { + if (parameterIndex < 1 || parameterIndex > args.length) { + throw new SQLException("bad parameter index"); + } + if (x == null) { + args[parameterIndex - 1] = nullrepl ? "" : null; + } else { + if (x instanceof byte[]) { + byte[] bx = (byte[]) x; + if (conn.db.is3()) { + args[parameterIndex - 1] = + SQLite.StringEncoder.encodeX(bx); + blobs[parameterIndex - 1] = true; + return; + } + args[parameterIndex - 1] = SQLite.StringEncoder.encode(bx); + } else { + args[parameterIndex - 1] = x.toString(); + } + } + blobs[parameterIndex - 1] = false; + } + + public boolean execute() throws SQLException { + return executeQuery(fixup2(sql), args, false) != null; + } + + public void addBatch() throws SQLException { + if (batch == null) { + batch = new ArrayList<BatchArg>(args.length); + } + for (int i = 0; i < args.length; i++) { + batch.add(new BatchArg(args[i], blobs[i])); + } + } + + public int[] executeBatch() throws SQLException { + if (batch == null) { + return new int[0]; + } + int[] ret = new int[batch.size() / args.length]; + for (int i = 0; i < ret.length; i++) { + ret[i] = EXECUTE_FAILED; + } + int errs = 0; + int index = 0; + for (int i = 0; i < ret.length; i++) { + for (int k = 0; k < args.length; k++) { + BatchArg b = (BatchArg) batch.get(index++); + + args[i] = b.arg; + blobs[i] = b.blob; + } + try { + ret[i] = executeUpdate(); + } catch (SQLException e) { + ++errs; + } + } + if (errs > 0) { + throw new BatchUpdateException("batch failed", ret); + } + return ret; + } + + public void clearBatch() throws SQLException { + if (batch != null) { + batch.clear(); + batch = null; + } + } + + public void setCharacterStream(int parameterIndex, + java.io.Reader reader, + int length) throws SQLException { + throw new SQLException("not supported"); + } + + public void setRef(int i, Ref x) throws SQLException { + throw new SQLException("not supported"); + } + + public void setBlob(int i, Blob x) throws SQLException { + throw new SQLException("not supported"); + } + + public void setClob(int i, Clob x) throws SQLException { + throw new SQLException("not supported"); + } + + public void setArray(int i, Array x) throws SQLException { + throw new SQLException("not supported"); + } + + public ResultSetMetaData getMetaData() throws SQLException { + return rs.getMetaData(); + } + + public void setDate(int parameterIndex, java.sql.Date x, Calendar cal) + throws SQLException { + setDate(parameterIndex, x); + } + + public void setTime(int parameterIndex, java.sql.Time x, Calendar cal) + throws SQLException { + setTime(parameterIndex, x); + } + + public void setTimestamp(int parameterIndex, java.sql.Timestamp x, + Calendar cal) throws SQLException { + setTimestamp(parameterIndex, x); + } + + public void setNull(int parameterIndex, int sqlType, String typeName) + throws SQLException { + setNull(parameterIndex, sqlType); + } + + public ParameterMetaData getParameterMetaData() throws SQLException { + throw new SQLException("not supported"); + } + + public void registerOutputParameter(String parameterName, int sqlType) + throws SQLException { + throw new SQLException("not supported"); + } + + public void registerOutputParameter(String parameterName, int sqlType, + int scale) + throws SQLException { + throw new SQLException("not supported"); + } + + public void registerOutputParameter(String parameterName, int sqlType, + String typeName) + throws SQLException { + throw new SQLException("not supported"); + } + + public java.net.URL getURL(int parameterIndex) throws SQLException { + throw new SQLException("not supported"); + } + + public void setURL(int parameterIndex, java.net.URL url) + throws SQLException { + throw new SQLException("not supported"); + } + + public void setNull(String parameterName, int sqlType) + throws SQLException { + throw new SQLException("not supported"); + } + + public void setBoolean(String parameterName, boolean val) + throws SQLException { + throw new SQLException("not supported"); + } + + public void setByte(String parameterName, byte val) + throws SQLException { + throw new SQLException("not supported"); + } + + public void setShort(String parameterName, short val) + throws SQLException { + throw new SQLException("not supported"); + } + + public void setInt(String parameterName, int val) + throws SQLException { + throw new SQLException("not supported"); + } + + public void setLong(String parameterName, long val) + throws SQLException { + throw new SQLException("not supported"); + } + + public void setFloat(String parameterName, float val) + throws SQLException { + throw new SQLException("not supported"); + } + + public void setDouble(String parameterName, double val) + throws SQLException { + throw new SQLException("not supported"); + } + + public void setBigDecimal(String parameterName, BigDecimal val) + throws SQLException { + throw new SQLException("not supported"); + } + + public void setString(String parameterName, String val) + throws SQLException { + throw new SQLException("not supported"); + } + + public void setBytes(String parameterName, byte val[]) + throws SQLException { + throw new SQLException("not supported"); + } + + public void setDate(String parameterName, java.sql.Date val) + throws SQLException { + throw new SQLException("not supported"); + } + + public void setTime(String parameterName, java.sql.Time val) + throws SQLException { + throw new SQLException("not supported"); + } + + public void setTimestamp(String parameterName, java.sql.Timestamp val) + throws SQLException { + throw new SQLException("not supported"); + } + + public void setAsciiStream(String parameterName, + java.io.InputStream s, int length) + throws SQLException { + throw new SQLException("not supported"); + } + + public void setBinaryStream(String parameterName, + java.io.InputStream s, int length) + throws SQLException { + throw new SQLException("not supported"); + } + + public void setObject(String parameterName, Object val, int targetSqlType, + int scale) + throws SQLException { + throw new SQLException("not supported"); + } + + public void setObject(String parameterName, Object val, int targetSqlType) + throws SQLException { + throw new SQLException("not supported"); + } + + public void setObject(String parameterName, Object val) + throws SQLException { + throw new SQLException("not supported"); + } + + public void setCharacterStream(String parameterName, + java.io.Reader r, int length) + throws SQLException { + throw new SQLException("not supported"); + } + + public void setDate(String parameterName, java.sql.Date val, + Calendar cal) + throws SQLException { + throw new SQLException("not supported"); + } + + public void setTime(String parameterName, java.sql.Time val, + Calendar cal) + throws SQLException { + throw new SQLException("not supported"); + } + + public void setTimestamp(String parameterName, java.sql.Timestamp val, + Calendar cal) + throws SQLException { + throw new SQLException("not supported"); + } + + public void setNull(String parameterName, int sqlType, String typeName) + throws SQLException { + throw new SQLException("not supported"); + } + + public String getString(String parameterName) throws SQLException { + throw new SQLException("not supported"); + } + + public boolean getBoolean(String parameterName) throws SQLException { + throw new SQLException("not supported"); + } + + public byte getByte(String parameterName) throws SQLException { + throw new SQLException("not supported"); + } + + public short getShort(String parameterName) throws SQLException { + throw new SQLException("not supported"); + } + + public int getInt(String parameterName) throws SQLException { + throw new SQLException("not supported"); + } + + public long getLong(String parameterName) throws SQLException { + throw new SQLException("not supported"); + } + + public float getFloat(String parameterName) throws SQLException { + throw new SQLException("not supported"); + } + + public double getDouble(String parameterName) throws SQLException { + throw new SQLException("not supported"); + } + + public byte[] getBytes(String parameterName) throws SQLException { + throw new SQLException("not supported"); + } + + public java.sql.Date getDate(String parameterName) throws SQLException { + throw new SQLException("not supported"); + } + + public java.sql.Time getTime(String parameterName) throws SQLException { + throw new SQLException("not supported"); + } + + public java.sql.Timestamp getTimestamp(String parameterName) + throws SQLException { + throw new SQLException("not supported"); + } + + public Object getObject(String parameterName) throws SQLException { + throw new SQLException("not supported"); + } + + public Object getObject(int parameterIndex) throws SQLException { + throw new SQLException("not supported"); + } + + public BigDecimal getBigDecimal(String parameterName) throws SQLException { + throw new SQLException("not supported"); + } + + public Object getObject(String parameterName, Map map) + throws SQLException { + throw new SQLException("not supported"); + } + + public Object getObject(int parameterIndex, Map map) + throws SQLException { + throw new SQLException("not supported"); + } + + public Ref getRef(int parameterIndex) throws SQLException { + throw new SQLException("not supported"); + } + + public Ref getRef(String parameterName) throws SQLException { + throw new SQLException("not supported"); + } + + public Blob getBlob(String parameterName) throws SQLException { + throw new SQLException("not supported"); + } + + public Blob getBlob(int parameterIndex) throws SQLException { + throw new SQLException("not supported"); + } + + public Clob getClob(String parameterName) throws SQLException { + throw new SQLException("not supported"); + } + + public Clob getClob(int parameterIndex) throws SQLException { + throw new SQLException("not supported"); + } + + public Array getArray(String parameterName) throws SQLException { + throw new SQLException("not supported"); + } + + public Array getArray(int parameterIndex) throws SQLException { + throw new SQLException("not supported"); + } + + public java.sql.Date getDate(String parameterName, Calendar cal) + throws SQLException { + throw new SQLException("not supported"); + } + + public java.sql.Date getDate(int parameterIndex, Calendar cal) + throws SQLException { + throw new SQLException("not supported"); + } + + public java.sql.Time getTime(String parameterName, Calendar cal) + throws SQLException { + throw new SQLException("not supported"); + } + + public java.sql.Time getTime(int parameterIndex, Calendar cal) + throws SQLException { + throw new SQLException("not supported"); + } + + public java.sql.Timestamp getTimestamp(String parameterName, Calendar cal) + throws SQLException { + throw new SQLException("not supported"); + } + + public java.sql.Timestamp getTimestamp(int parameterIndex, Calendar cal) + throws SQLException { + throw new SQLException("not supported"); + } + + public java.net.URL getURL(String parameterName) throws SQLException { + throw new SQLException("not supported"); + } + +} diff --git a/sqlite-jdbc/src/main/java/SQLite/JDBC2y/JDBCResultSet.java b/sqlite-jdbc/src/main/java/SQLite/JDBC2y/JDBCResultSet.java new file mode 100644 index 0000000..06384eb --- /dev/null +++ b/sqlite-jdbc/src/main/java/SQLite/JDBC2y/JDBCResultSet.java @@ -0,0 +1,932 @@ +package SQLite.JDBC2y; + +import java.sql.*; +import java.math.BigDecimal; + +public class JDBCResultSet implements java.sql.ResultSet { + + /** + * Current row to be retrieved. + */ + private int row; + + /** + * Table returned by Database.get_table() + */ + protected SQLite.TableResult tr; + + /** + * Statement from which result set was produced. + */ + private JDBCStatement s; + + /** + * Meta data for result set or null. + */ + private JDBCResultSetMetaData m; + + /** + * Last result cell retrieved or null. + */ + private String lastg; + + + public JDBCResultSet(SQLite.TableResult tr, JDBCStatement s) { + this.tr = tr; + this.s = s; + this.m = null; + this.lastg = null; + this.row = -1; + } + + public boolean next() throws SQLException { + if (tr == null) { + return false; + } + row++; + return row < tr.nrows; + } + + public int findColumn(String columnName) throws SQLException { + JDBCResultSetMetaData m = (JDBCResultSetMetaData) getMetaData(); + return m.findColByName(columnName); + } + + public int getRow() throws SQLException { + if (tr == null) { + throw new SQLException("no rows"); + } + return row + 1; + } + + public boolean previous() throws SQLException { + if (tr == null) { + return false; + } + if (row >= 0) { + row--; + } + return row >= 0; + } + + public boolean absolute(int row) throws SQLException { + if (tr == null) { + return false; + } + if (row < 0) { + row = tr.nrows + 1 + row; + } + row--; + if (row < 0 || row > tr.nrows) { + return false; + } + this.row = row; + return true; + } + + public boolean relative(int row) throws SQLException { + if (tr == null) { + return false; + } + if (this.row + row < 0 || this.row + row >= tr.nrows) { + return false; + } + this.row += row; + return true; + } + + public void setFetchDirection(int dir) throws SQLException { + throw new SQLException("not supported"); + } + + public int getFetchDirection() throws SQLException { + throw new SQLException("not supported"); + } + + public void setFetchSize(int fsize) throws SQLException { + throw new SQLException("not supported"); + } + + public int getFetchSize() throws SQLException { + throw new SQLException("not supported"); + } + + public String getString(int columnIndex) throws SQLException { + if (tr == null || columnIndex < 1 || columnIndex > tr.ncolumns) { + throw new SQLException("column " + columnIndex + " not found"); + } + String rd[] = (String []) tr.rows.elementAt(row); + lastg = rd[columnIndex - 1]; + return lastg; + } + + public String getString(String columnName) throws SQLException { + int col = findColumn(columnName); + return getString(col); + } + + public int getInt(int columnIndex) throws SQLException { + Integer i = internalGetInt(columnIndex); + if (i != null) { + return i.intValue(); + } + return 0; + } + + private Integer internalGetInt(int columnIndex) throws SQLException { + if (tr == null || columnIndex < 1 || columnIndex > tr.ncolumns) { + throw new SQLException("column " + columnIndex + " not found"); + } + String rd[] = (String []) tr.rows.elementAt(row); + lastg = rd[columnIndex - 1]; + try { + return Integer.valueOf(lastg); + } catch (java.lang.Exception e) { + lastg = null; + } + return null; + } + + public int getInt(String columnName) throws SQLException { + int col = findColumn(columnName); + return getInt(col); + } + + public boolean getBoolean(int columnIndex) throws SQLException { + throw new SQLException("not supported"); + } + + public boolean getBoolean(String columnName) throws SQLException { + throw new SQLException("not supported"); + } + + public ResultSetMetaData getMetaData() throws SQLException { + if (m == null) { + m = new JDBCResultSetMetaData(this); + } + return m; + } + + public short getShort(int columnIndex) throws SQLException { + Short s = internalGetShort(columnIndex); + if (s != null) { + return s.shortValue(); + } + return 0; + } + + private Short internalGetShort(int columnIndex) throws SQLException { + if (tr == null || columnIndex < 1 || columnIndex > tr.ncolumns) { + throw new SQLException("column " + columnIndex + " not found"); + } + String rd[] = (String []) tr.rows.elementAt(row); + lastg = rd[columnIndex - 1]; + try { + return Short.valueOf(lastg); + } catch (java.lang.Exception e) { + lastg = null; + } + return null; + } + + public short getShort(String columnName) throws SQLException { + int col = findColumn(columnName); + return getShort(col); + } + + public java.sql.Time getTime(int columnIndex) throws SQLException { + return internalGetTime(columnIndex, null); + } + + private java.sql.Time internalGetTime(int columnIndex, + java.util.Calendar cal) + throws SQLException { + if (tr == null || columnIndex < 1 || columnIndex > tr.ncolumns) { + throw new SQLException("column " + columnIndex + " not found"); + } + String rd[] = (String []) tr.rows.elementAt(row); + lastg = rd[columnIndex - 1]; + try { + return java.sql.Time.valueOf(lastg); + } catch (java.lang.Exception e) { + lastg = null; + } + return null; + } + + public java.sql.Time getTime(String columnName) throws SQLException { + int col = findColumn(columnName); + return getTime(col); + } + + public java.sql.Time getTime(int columnIndex, java.util.Calendar cal) + throws SQLException { + return internalGetTime(columnIndex, cal); + } + + public java.sql.Time getTime(String columnName, java.util.Calendar cal) + throws SQLException{ + int col = findColumn(columnName); + return getTime(col, cal); + } + + public java.sql.Timestamp getTimestamp(int columnIndex) + throws SQLException{ + return internalGetTimestamp(columnIndex, null); + } + + private java.sql.Timestamp internalGetTimestamp(int columnIndex, + java.util.Calendar cal) + throws SQLException { + if (tr == null || columnIndex < 1 || columnIndex > tr.ncolumns) { + throw new SQLException("column " + columnIndex + " not found"); + } + String rd[] = (String []) tr.rows.elementAt(row); + lastg = rd[columnIndex - 1]; + try { + return java.sql.Timestamp.valueOf(lastg); + } catch (java.lang.Exception e) { + lastg = null; + } + return null; + } + + public java.sql.Timestamp getTimestamp(String columnName) + throws SQLException{ + int col = findColumn(columnName); + return getTimestamp(col); + } + + public java.sql.Timestamp getTimestamp(int columnIndex, + java.util.Calendar cal) + throws SQLException { + return internalGetTimestamp(columnIndex, cal); + } + + public java.sql.Timestamp getTimestamp(String columnName, + java.util.Calendar cal) + throws SQLException { + int col = findColumn(columnName); + return getTimestamp(col, cal); + } + + public java.sql.Date getDate(int columnIndex) throws SQLException { + return internalGetDate(columnIndex, null); + } + + private java.sql.Date internalGetDate(int columnIndex, + java.util.Calendar cal) + throws SQLException { + if (tr == null || columnIndex < 1 || columnIndex > tr.ncolumns) { + throw new SQLException("column " + columnIndex + " not found"); + } + String rd[] = (String []) tr.rows.elementAt(row); + lastg = rd[columnIndex - 1]; + try { + return java.sql.Date.valueOf(lastg); + } catch (java.lang.Exception e) { + lastg = null; + } + return null; + } + + public java.sql.Date getDate(String columnName) throws SQLException { + int col = findColumn(columnName); + return getDate(col); + } + + public java.sql.Date getDate(int columnIndex, java.util.Calendar cal) + throws SQLException{ + return internalGetDate(columnIndex, cal); + } + + public java.sql.Date getDate(String columnName, java.util.Calendar cal) + throws SQLException{ + int col = findColumn(columnName); + return getDate(col, cal); + } + + public double getDouble(int columnIndex) throws SQLException { + Double d = internalGetDouble(columnIndex); + if (d != null) { + return d.doubleValue(); + } + return 0; + } + + private Double internalGetDouble(int columnIndex) throws SQLException { + if (tr == null || columnIndex < 1 || columnIndex > tr.ncolumns) { + throw new SQLException("column " + columnIndex + " not found"); + } + String rd[] = (String []) tr.rows.elementAt(row); + lastg = rd[columnIndex - 1]; + try { + return Double.valueOf(lastg); + } catch (java.lang.Exception e) { + lastg = null; + } + return null; + } + + public double getDouble(String columnName) throws SQLException { + int col = findColumn(columnName); + return getDouble(col); + } + + public float getFloat(int columnIndex) throws SQLException { + Float f = internalGetFloat(columnIndex); + if (f != null) { + return f.floatValue(); + } + return 0; + } + + private Float internalGetFloat(int columnIndex) throws SQLException { + if (tr == null || columnIndex < 1 || columnIndex > tr.ncolumns) { + throw new SQLException("column " + columnIndex + " not found"); + } + String rd[] = (String []) tr.rows.elementAt(row); + lastg = rd[columnIndex - 1]; + try { + return Float.valueOf(lastg); + } catch (java.lang.Exception e) { + lastg = null; + } + return null; + } + + public float getFloat(String columnName) throws SQLException { + int col = findColumn(columnName); + return getFloat(col); + } + + public long getLong(int columnIndex) throws SQLException { + Long l = internalGetLong(columnIndex); + if (l != null) { + return l.longValue(); + } + return 0; + } + + private Long internalGetLong(int columnIndex) throws SQLException { + if (tr == null || columnIndex < 1 || columnIndex > tr.ncolumns) { + throw new SQLException("column " + columnIndex + " not found"); + } + String rd[] = (String []) tr.rows.elementAt(row); + lastg = rd[columnIndex - 1]; + try { + return Long.valueOf(lastg); + } catch (java.lang.Exception e) { + lastg = null; + } + return null; + } + + public long getLong(String columnName) throws SQLException { + int col = findColumn(columnName); + return getLong(col); + } + + @Deprecated + public java.io.InputStream getUnicodeStream(int columnIndex) + throws SQLException { + throw new SQLException("not supported"); + } + + @Deprecated + public java.io.InputStream getUnicodeStream(String columnName) + throws SQLException { + throw new SQLException("not supported"); + } + + public java.io.InputStream getAsciiStream(String columnName) + throws SQLException { + throw new SQLException("not supported"); + } + + public java.io.InputStream getAsciiStream(int columnIndex) + throws SQLException { + throw new SQLException("not supported"); + } + + public BigDecimal getBigDecimal(String columnName) + throws SQLException { + throw new SQLException("not supported"); + } + + @Deprecated + public BigDecimal getBigDecimal(String columnName, int scale) + throws SQLException { + throw new SQLException("not supported"); + } + + public BigDecimal getBigDecimal(int columnIndex) throws SQLException { + throw new SQLException("not supported"); + } + + @Deprecated + public BigDecimal getBigDecimal(int columnIndex, int scale) + throws SQLException { + throw new SQLException("not supported"); + } + + public java.io.InputStream getBinaryStream(int columnIndex) + throws SQLException { + throw new SQLException("not supported"); + } + + public java.io.InputStream getBinaryStream(String columnName) + throws SQLException { + throw new SQLException("not supported"); + } + + public byte getByte(int columnIndex) throws SQLException { + throw new SQLException("not supported"); + } + + public byte getByte(String columnName) throws SQLException { + throw new SQLException("not supported"); + } + + public byte[] getBytes(int columnIndex) throws SQLException { + if (tr == null || columnIndex < 1 || columnIndex > tr.ncolumns) { + throw new SQLException("column " + columnIndex + " not found"); + } + byte ret[] = null; + String rd[] = (String []) tr.rows.elementAt(row); + lastg = rd[columnIndex - 1]; + if (lastg != null) { + ret = SQLite.StringEncoder.decode(lastg); + } + return ret; + } + + public byte[] getBytes(String columnName) throws SQLException { + int col = findColumn(columnName); + return getBytes(col); + } + + public String getCursorName() throws SQLException { + return null; + } + + public Object getObject(int columnIndex) throws SQLException { + if (tr == null || columnIndex < 1 || columnIndex > tr.ncolumns) { + throw new SQLException("column " + columnIndex + " not found"); + } + String rd[] = (String []) tr.rows.elementAt(row); + lastg = rd[columnIndex - 1]; + Object ret = lastg; + if (tr instanceof TableResultX) { + switch (((TableResultX) tr).sql_type[columnIndex - 1]) { + case Types.SMALLINT: + ret = internalGetShort(columnIndex); + break; + case Types.INTEGER: + ret = internalGetInt(columnIndex); + break; + case Types.DOUBLE: + ret = internalGetDouble(columnIndex); + break; + case Types.FLOAT: + ret = internalGetFloat(columnIndex); + break; + case Types.BIGINT: + ret = internalGetLong(columnIndex); + break; + case Types.BINARY: + case Types.VARBINARY: + case Types.LONGVARBINARY: + ret = getBytes(columnIndex); + break; + case Types.NULL: + ret = null; + break; + /* defaults to String below */ + } + } + return ret; + } + + public Object getObject(String columnName) throws SQLException { + int col = findColumn(columnName); + return getObject(col); + } + + public Object getObject(int columnIndex, java.util.Map map) + throws SQLException { + throw new SQLException("not supported"); + } + + public Object getObject(String columnIndex, java.util.Map map) + throws SQLException { + throw new SQLException("not supported"); + } + + public java.sql.Ref getRef(int columnIndex) throws SQLException { + throw new SQLException("not supported"); + } + + public java.sql.Ref getRef(String columnIndex) throws SQLException { + throw new SQLException("not supported"); + } + + public java.sql.Blob getBlob(int columnIndex) throws SQLException { + throw new SQLException("not supported"); + } + + public java.sql.Blob getBlob(String columnIndex) throws SQLException { + throw new SQLException("not supported"); + } + + public java.sql.Clob getClob(int columnIndex) throws SQLException { + throw new SQLException("not supported"); + } + + public java.sql.Clob getClob(String columnIndex) throws SQLException { + throw new SQLException("not supported"); + } + + public java.sql.Array getArray(int columnIndex) throws SQLException { + throw new SQLException("not supported"); + } + + public java.sql.Array getArray(String columnIndex) throws SQLException { + throw new SQLException("not supported"); + } + + public java.io.Reader getCharacterStream(int columnIndex) + throws SQLException { + throw new SQLException("not supported"); + } + + public java.io.Reader getCharacterStream(String columnName) + throws SQLException { + throw new SQLException("not supported"); + } + + public SQLWarning getWarnings() throws SQLException { + throw new SQLException("not supported"); + } + + public boolean wasNull() throws SQLException { + return lastg == null; + } + + public void clearWarnings() throws SQLException { + throw new SQLException("not supported"); + } + + public boolean isFirst() throws SQLException { + if (tr == null) { + return true; + } + return row == 0; + } + + public boolean isBeforeFirst() throws SQLException { + if (tr == null || tr.nrows <= 0) { + return false; + } + return row < 0; + } + + public void beforeFirst() throws SQLException { + if (tr == null) { + return; + } + row = -1; + } + + public boolean first() throws SQLException { + if (tr == null || tr.nrows <= 0) { + return false; + } + row = 0; + return true; + } + + public boolean isAfterLast() throws SQLException { + if (tr == null || tr.nrows <= 0) { + return false; + } + return row >= tr.nrows; + } + + public void afterLast() throws SQLException { + if (tr == null) { + return; + } + row = tr.nrows; + } + + public boolean isLast() throws SQLException { + if (tr == null) { + return true; + } + return row == tr.nrows - 1; + } + + public boolean last() throws SQLException { + if (tr == null || tr.nrows <= 0) { + return false; + } + row = tr.nrows -1; + return true; + } + + public int getType() throws SQLException { + return TYPE_SCROLL_INSENSITIVE; + } + + public int getConcurrency() throws SQLException { + return CONCUR_READ_ONLY; + } + + public boolean rowUpdated() throws SQLException { + throw new SQLException("not supported"); + } + + public boolean rowInserted() throws SQLException { + throw new SQLException("not supported"); + } + + public boolean rowDeleted() throws SQLException { + throw new SQLException("not supported"); + } + + public void insertRow() throws SQLException { + throw new SQLException("not supported"); + } + + public void updateRow() throws SQLException { + throw new SQLException("not supported"); + } + + public void deleteRow() throws SQLException { + throw new SQLException("not supported"); + } + + public void refreshRow() throws SQLException { + throw new SQLException("not supported"); + } + + public void cancelRowUpdates() throws SQLException { + throw new SQLException("not supported"); + } + + public void moveToInsertRow() throws SQLException { + throw new SQLException("not supported"); + } + + public void moveToCurrentRow() throws SQLException { + throw new SQLException("not supported"); + } + + public void updateNull(int colIndex) throws SQLException { + throw new SQLException("not supported"); + } + + public void updateBoolean(int colIndex, boolean b) throws SQLException { + throw new SQLException("not supported"); + } + + public void updateByte(int colIndex, byte b) throws SQLException { + throw new SQLException("not supported"); + } + + public void updateShort(int colIndex, short b) throws SQLException { + throw new SQLException("not supported"); + } + + public void updateInt(int colIndex, int b) throws SQLException { + throw new SQLException("not supported"); + } + + public void updateLong(int colIndex, long b) throws SQLException { + throw new SQLException("not supported"); + } + + public void updateFloat(int colIndex, float f) throws SQLException { + throw new SQLException("not supported"); + } + + public void updateDouble(int colIndex, double f) throws SQLException { + throw new SQLException("not supported"); + } + + public void updateBigDecimal(int colIndex, BigDecimal f) + throws SQLException { + throw new SQLException("not supported"); + } + + public void updateString(int colIndex, String s) throws SQLException { + throw new SQLException("not supported"); + } + + public void updateBytes(int colIndex, byte[] s) throws SQLException { + throw new SQLException("not supported"); + } + + public void updateDate(int colIndex, java.sql.Date d) throws SQLException { + throw new SQLException("not supported"); + } + + public void updateTime(int colIndex, java.sql.Time t) throws SQLException { + throw new SQLException("not supported"); + } + + public void updateTimestamp(int colIndex, java.sql.Timestamp t) + throws SQLException { + throw new SQLException("not supported"); + } + + public void updateAsciiStream(int colIndex, java.io.InputStream in, int s) + throws SQLException { + throw new SQLException("not supported"); + } + + public void updateBinaryStream(int colIndex, java.io.InputStream in, int s) + throws SQLException { + throw new SQLException("not supported"); + } + + public void updateCharacterStream(int colIndex, java.io.Reader in, int s) + throws SQLException { + throw new SQLException("not supported"); + } + + public void updateObject(int colIndex, Object obj) throws SQLException { + throw new SQLException("not supported"); + } + + public void updateObject(int colIndex, Object obj, int s) + throws SQLException { + throw new SQLException("not supported"); + } + + public void updateNull(String colIndex) throws SQLException { + throw new SQLException("not supported"); + } + + public void updateBoolean(String colIndex, boolean b) throws SQLException { + throw new SQLException("not supported"); + } + + public void updateByte(String colIndex, byte b) throws SQLException { + throw new SQLException("not supported"); + } + + public void updateShort(String colIndex, short b) throws SQLException { + throw new SQLException("not supported"); + } + + public void updateInt(String colIndex, int b) throws SQLException { + throw new SQLException("not supported"); + } + + public void updateLong(String colIndex, long b) throws SQLException { + throw new SQLException("not supported"); + } + + public void updateFloat(String colIndex, float f) throws SQLException { + throw new SQLException("not supported"); + } + + public void updateDouble(String colIndex, double f) throws SQLException { + throw new SQLException("not supported"); + } + + public void updateBigDecimal(String colIndex, BigDecimal f) + throws SQLException { + throw new SQLException("not supported"); + } + + public void updateString(String colIndex, String s) throws SQLException { + throw new SQLException("not supported"); + } + + public void updateBytes(String colIndex, byte[] s) throws SQLException { + throw new SQLException("not supported"); + } + + public void updateDate(String colIndex, java.sql.Date d) + throws SQLException { + throw new SQLException("not supported"); + } + + public void updateTime(String colIndex, java.sql.Time t) + throws SQLException { + throw new SQLException("not supported"); + } + + public void updateTimestamp(String colIndex, java.sql.Timestamp t) + throws SQLException { + throw new SQLException("not supported"); + } + + public void updateAsciiStream(String colIndex, java.io.InputStream in, + int s) + throws SQLException { + throw new SQLException("not supported"); + } + + public void updateBinaryStream(String colIndex, java.io.InputStream in, + int s) + throws SQLException { + throw new SQLException("not supported"); + } + + public void updateCharacterStream(String colIndex, java.io.Reader in, + int s) + throws SQLException { + throw new SQLException("not supported"); + } + + public void updateObject(String colIndex, Object obj) + throws SQLException { + throw new SQLException("not supported"); + } + + public void updateObject(String colIndex, Object obj, int s) + throws SQLException { + throw new SQLException("not supported"); + } + + public Statement getStatement() throws SQLException { + if (s == null) { + throw new SQLException("stale result set"); + } + return s; + } + + public void close() throws SQLException { + s = null; + tr = null; + lastg = null; + row = -1; + } + + public java.net.URL getURL(int colIndex) throws SQLException { + if (tr == null || colIndex < 1 || colIndex > tr.ncolumns) { + throw new SQLException("column " + colIndex + " not found"); + } + String rd[] = (String []) tr.rows.elementAt(row); + lastg = rd[colIndex - 1]; + java.net.URL url = null; + if (lastg == null) { + return url; + } + try { + url = new java.net.URL(lastg); + } catch (java.lang.Exception e) { + url = null; + } + return url; + } + + public java.net.URL getURL(String colIndex) throws SQLException { + int col = findColumn(colIndex); + return getURL(col); + } + + public void updateRef(int colIndex, java.sql.Ref x) throws SQLException { + throw new SQLException("not supported"); + } + + public void updateRef(String colIndex, java.sql.Ref x) + throws SQLException { + throw new SQLException("not supported"); + } + + public void updateBlob(int colIndex, java.sql.Blob x) + throws SQLException { + throw new SQLException("not supported"); + } + + public void updateBlob(String colIndex, java.sql.Blob x) + throws SQLException { + throw new SQLException("not supported"); + } + + public void updateClob(int colIndex, java.sql.Clob x) + throws SQLException { + throw new SQLException("not supported"); + } + + public void updateClob(String colIndex, java.sql.Clob x) + throws SQLException { + throw new SQLException("not supported"); + } + + public void updateArray(int colIndex, java.sql.Array x) + throws SQLException { + throw new SQLException("not supported"); + } + + public void updateArray(String colIndex, java.sql.Array x) + throws SQLException { + throw new SQLException("not supported"); + } + +} diff --git a/sqlite-jdbc/src/main/java/SQLite/JDBC2y/JDBCResultSetMetaData.java b/sqlite-jdbc/src/main/java/SQLite/JDBC2y/JDBCResultSetMetaData.java new file mode 100644 index 0000000..934ca78 --- /dev/null +++ b/sqlite-jdbc/src/main/java/SQLite/JDBC2y/JDBCResultSetMetaData.java @@ -0,0 +1,212 @@ +package SQLite.JDBC2y; + +import java.sql.*; + +public class JDBCResultSetMetaData implements java.sql.ResultSetMetaData { + + private JDBCResultSet r; + + public JDBCResultSetMetaData(JDBCResultSet r) { + this.r = r; + } + + public String getCatalogName(int column) throws java.sql.SQLException { + return null; + } + + public String getColumnClassName(int column) throws java.sql.SQLException { + column--; + if (r != null && r.tr != null) { + if (column < 0 || column >= r.tr.ncolumns) { + return null; + } + if (r.tr instanceof TableResultX) { + switch (((TableResultX) r.tr).sql_type[column]) { + case Types.SMALLINT: return "java.lang.Short"; + case Types.INTEGER: return "java.lang.Integer"; + case Types.DOUBLE: return "java.lang.Double"; + case Types.FLOAT: return "java.lang.Float"; + case Types.BIGINT: return "java.lang.Long"; + case Types.DATE: return "java.sql.Date"; + case Types.TIME: return "java.sql.Time"; + case Types.TIMESTAMP: return "java.sql.Timestamp"; + case Types.BINARY: + case Types.VARBINARY: return "[B"; + /* defaults to varchar below */ + } + } + return "java.lang.String"; + } + return null; + } + + public int getColumnCount() throws java.sql.SQLException { + if (r != null && r.tr != null) { + return r.tr.ncolumns; + } + return 0; + } + + public int getColumnDisplaySize(int column) throws java.sql.SQLException { + return 0; + } + + public String getColumnLabel(int column) throws java.sql.SQLException { + column--; + String c = null; + if (r != null && r.tr != null) { + if (column < 0 || column >= r.tr.ncolumns) { + return c; + } + c = r.tr.column[column]; + } + return c; + } + + public String getColumnName(int column) throws java.sql.SQLException { + column--; + String c = null; + if (r != null && r.tr != null) { + if (column < 0 || column >= r.tr.ncolumns) { + return c; + } + c = r.tr.column[column]; + if (c != null) { + int i = c.indexOf('.'); + if (i > 0) { + return c.substring(i + 1); + } + } + } + return c; + } + + public int getColumnType(int column) throws java.sql.SQLException { + column--; + if (r != null && r.tr != null) { + if (column >= 0 && column < r.tr.ncolumns) { + if (r.tr instanceof TableResultX) { + return ((TableResultX) r.tr).sql_type[column]; + } + return Types.VARCHAR; + } + } + throw new SQLException("bad column index"); + } + + public String getColumnTypeName(int column) throws java.sql.SQLException { + column--; + if (r != null && r.tr != null) { + if (column >= 0 && column < r.tr.ncolumns) { + if (r.tr instanceof TableResultX) { + switch (((TableResultX) r.tr).sql_type[column]) { + case Types.SMALLINT: return "smallint"; + case Types.INTEGER: return "integer"; + case Types.DOUBLE: return "double"; + case Types.FLOAT: return "float"; + case Types.BIGINT: return "bigint"; + case Types.DATE: return "date"; + case Types.TIME: return "time"; + case Types.TIMESTAMP: return "timestamp"; + case Types.BINARY: return "binary"; + case Types.VARBINARY: return "varbinary"; + /* defaults to varchar below */ + } + } + return "varchar"; + } + } + throw new SQLException("bad column index"); + } + + public int getPrecision(int column) throws java.sql.SQLException { + return 0; + } + + public int getScale(int column) throws java.sql.SQLException { + return 0; + } + + public String getSchemaName(int column) throws java.sql.SQLException { + return null; + } + + public String getTableName(int column) throws java.sql.SQLException { + column--; + String c = null; + if (r != null && r.tr != null) { + if (column < 0 || column >= r.tr.ncolumns) { + return c; + } + c = r.tr.column[column]; + if (c != null) { + int i = c.indexOf('.'); + if (i > 0) { + return c.substring(0, i); + } + c = null; + } + } + return c; + } + + public boolean isAutoIncrement(int column) throws java.sql.SQLException { + return false; + } + + public boolean isCaseSensitive(int column) throws java.sql.SQLException { + return false; + } + + public boolean isCurrency(int column) throws java.sql.SQLException { + return false; + } + + public boolean isDefinitelyWritable(int column) + throws java.sql.SQLException { + return true; + } + + public int isNullable(int column) throws java.sql.SQLException { + return columnNullableUnknown; + } + + public boolean isReadOnly(int column) throws java.sql.SQLException { + return false; + } + + public boolean isSearchable(int column) throws java.sql.SQLException { + return true; + } + + public boolean isSigned(int column) throws java.sql.SQLException { + return false; + } + + public boolean isWritable(int column) throws java.sql.SQLException { + return true; + } + + int findColByName(String columnName) throws java.sql.SQLException { + String c = null; + if (r != null && r.tr != null) { + for (int i = 0; i < r.tr.ncolumns; i++) { + c = r.tr.column[i]; + if (c != null) { + if (c.compareToIgnoreCase(columnName) == 0) { + return i + 1; + } + int k = c.indexOf('.'); + if (k > 0) { + c = c.substring(k + 1); + if (c.compareToIgnoreCase(columnName) == 0) { + return i + 1; + } + } + } + c = null; + } + } + throw new SQLException("column " + columnName + " not found"); + } +} diff --git a/sqlite-jdbc/src/main/java/SQLite/JDBC2y/JDBCStatement.java b/sqlite-jdbc/src/main/java/SQLite/JDBC2y/JDBCStatement.java new file mode 100644 index 0000000..99d12d3 --- /dev/null +++ b/sqlite-jdbc/src/main/java/SQLite/JDBC2y/JDBCStatement.java @@ -0,0 +1,287 @@ +package SQLite.JDBC2y; + +import java.sql.*; +import java.util.*; + +public class JDBCStatement implements java.sql.Statement { + + protected JDBCConnection conn; + protected JDBCResultSet rs; + protected int updcnt; + private ArrayList<String> batch; + + public JDBCStatement(JDBCConnection conn) { + this.conn = conn; + this.updcnt = 0; + this.rs = null; + this.batch = null; + } + + public void setFetchSize(int fetchSize) throws SQLException { + throw new SQLException("not supported"); + } + + public int getFetchSize() throws SQLException { + return 1; + } + + public int getMaxRows() throws SQLException { + return 0; + } + + public void setMaxRows(int max) throws SQLException { + throw new SQLException("not supported"); + } + + public void setFetchDirection(int fetchDirection) throws SQLException { + throw new SQLException("not supported"); + } + + public int getFetchDirection() throws SQLException { + return ResultSet.FETCH_UNKNOWN; + } + + public int getResultSetConcurrency() throws SQLException { + return ResultSet.CONCUR_READ_ONLY; + } + + public int getResultSetType() throws SQLException { + return ResultSet.TYPE_SCROLL_INSENSITIVE; + } + + public void setQueryTimeout(int seconds) throws SQLException { + conn.timeout = seconds * 1000; + if (conn.timeout < 0) { + conn.timeout = 120000; + } else if (conn.timeout < 1000) { + conn.timeout = 5000; + } + } + + public int getQueryTimeout() throws SQLException { + return conn.timeout; + } + + public ResultSet getResultSet() throws SQLException { + return rs; + } + + ResultSet executeQuery(String sql, String args[], boolean updonly) + throws SQLException { + SQLite.TableResult tr = null; + if (rs != null) { + rs.close(); + rs = null; + } + updcnt = -1; + if (conn == null || conn.db == null) { + throw new SQLException("stale connection"); + } + int busy = 0; + boolean starttrans = !conn.autocommit && !conn.intrans; + while (true) { + try { + if (starttrans) { + conn.db.exec("BEGIN TRANSACTION", null); + conn.intrans = true; + } + if (args == null) { + if (updonly) { + conn.db.exec(sql, null); + } else { + tr = conn.db.get_table(sql); + } + } else { + if (updonly) { + conn.db.exec(sql, null, args); + } else { + tr = conn.db.get_table(sql, args); + } + } + updcnt = (int) conn.db.changes(); + } catch (SQLite.Exception e) { + if (conn.db.is3() && + conn.db.last_error() == SQLite.Constants.SQLITE_BUSY && + conn.busy3(conn.db, ++busy)) { + try { + if (starttrans && conn.intrans) { + conn.db.exec("ROLLBACK", null); + conn.intrans = false; + } + } catch (SQLite.Exception ee) { + } + try { + int ms = 20 + busy * 10; + if (ms > 1000) { + ms = 1000; + } + synchronized (this) { + this.wait(ms); + } + } catch (java.lang.Exception eee) { + } + continue; + } + throw new SQLException(e.toString()); + } + break; + } + if (!updonly && tr == null) { + throw new SQLException("no result set produced"); + } + if (!updonly && tr != null) { + rs = new JDBCResultSet(new TableResultX(tr), this); + } + return rs; + } + + public ResultSet executeQuery(String sql) throws SQLException { + return executeQuery(sql, null, false); + } + + public boolean execute(String sql) throws SQLException { + return executeQuery(sql) != null; + } + + public void cancel() throws SQLException { + if (conn == null || conn.db == null) { + throw new SQLException("stale connection"); + } + conn.db.interrupt(); + } + + public void clearWarnings() throws SQLException { + } + + public Connection getConnection() throws SQLException { + return conn; + } + + public void addBatch(String sql) throws SQLException { + if (batch == null) { + batch = new ArrayList<String>(1); + } + batch.add(sql); + } + + public int[] executeBatch() throws SQLException { + if (batch == null) { + return new int[0]; + } + int[] ret = new int[batch.size()]; + for (int i = 0; i < ret.length; i++) { + ret[i] = EXECUTE_FAILED; + } + int errs = 0; + for (int i = 0; i < ret.length; i++) { + try { + execute((String) batch.get(i)); + ret[i] = updcnt; + } catch (SQLException e) { + ++errs; + } + } + if (errs > 0) { + throw new BatchUpdateException("batch failed", ret); + } + return ret; + } + + public void clearBatch() throws SQLException { + if (batch != null) { + batch.clear(); + batch = null; + } + } + + public void close() throws SQLException { + clearBatch(); + conn = null; + } + + public int executeUpdate(String sql) throws SQLException { + executeQuery(sql, null, true); + return updcnt; + } + + public int getMaxFieldSize() throws SQLException { + return 0; + } + + public boolean getMoreResults() throws SQLException { + if (rs != null) { + rs.close(); + rs = null; + } + return false; + } + + public int getUpdateCount() throws SQLException { + return updcnt; + } + + public SQLWarning getWarnings() throws SQLException { + return null; + } + + public void setCursorName(String name) throws SQLException { + throw new SQLException("not supported"); + } + + public void setEscapeProcessing(boolean enable) throws SQLException { + throw new SQLException("not supported"); + } + + public void setMaxFieldSize(int max) throws SQLException { + throw new SQLException("not supported"); + } + + public boolean getMoreResults(int x) throws SQLException { + throw new SQLException("not supported"); + } + + public ResultSet getGeneratedKeys() throws SQLException { + throw new SQLException("not supported"); + } + + public int executeUpdate(String sql, int autokeys) + throws SQLException { + if (autokeys != Statement.NO_GENERATED_KEYS) { + throw new SQLException("not supported"); + } + return executeUpdate(sql); + } + + public int executeUpdate(String sql, int colIndexes[]) + throws SQLException { + throw new SQLException("not supported"); + } + + public int executeUpdate(String sql, String colIndexes[]) + throws SQLException { + throw new SQLException("not supported"); + } + + public boolean execute(String sql, int autokeys) + throws SQLException { + if (autokeys != Statement.NO_GENERATED_KEYS) { + throw new SQLException("not supported"); + } + return execute(sql); + } + + public boolean execute(String sql, int colIndexes[]) + throws SQLException { + throw new SQLException("not supported"); + } + + public boolean execute(String sql, String colIndexes[]) + throws SQLException { + throw new SQLException("not supported"); + } + + public int getResultSetHoldability() throws SQLException { + return ResultSet.HOLD_CURSORS_OVER_COMMIT; + } + +} diff --git a/sqlite-jdbc/src/main/java/SQLite/JDBC2y/TableResultX.java b/sqlite-jdbc/src/main/java/SQLite/JDBC2y/TableResultX.java new file mode 100644 index 0000000..205372f --- /dev/null +++ b/sqlite-jdbc/src/main/java/SQLite/JDBC2y/TableResultX.java @@ -0,0 +1,37 @@ +package SQLite.JDBC2y; + +import java.sql.Types; +import java.util.Vector; + +public class TableResultX extends SQLite.TableResult { + public int sql_type[]; + + public TableResultX() { + super(); + sql_type = new int[this.ncolumns]; + for (int i = 0; i < this.ncolumns; i++) { + sql_type[i] = Types.VARCHAR; + } + } + + public TableResultX(SQLite.TableResult tr) { + this.column = tr.column; + this.rows = tr.rows; + this.ncolumns = tr.ncolumns; + this.nrows = tr.nrows; + this.types = tr.types; + sql_type = new int[tr.ncolumns]; + for (int i = 0; i < this.ncolumns; i++) { + sql_type[i] = Types.VARCHAR; + } + if (tr.types != null) { + for (int i = 0; i < tr.types.length; i++) { + sql_type[i] = JDBCDatabaseMetaData.mapSqlType(tr.types[i]); + } + } + } + + void sql_types(int types[]) { + sql_type = types; + } +} |