| @@ -0,0 +1,319 @@ | ||
|
|
||
| package com.jfixby.scarabei.db.realm; | ||
|
|
||
| import java.io.IOException; | ||
| import java.sql.Connection; | ||
| import java.sql.PreparedStatement; | ||
| import java.sql.ResultSet; | ||
| import java.sql.SQLException; | ||
|
|
||
| import com.jfixby.scarabei.api.collections.Collection; | ||
| import com.jfixby.scarabei.api.collections.Collections; | ||
| import com.jfixby.scarabei.api.collections.List; | ||
| import com.jfixby.scarabei.api.collections.Sequence; | ||
| import com.jfixby.scarabei.api.db.Entry; | ||
| import com.jfixby.scarabei.api.db.Table; | ||
| import com.jfixby.scarabei.api.db.TableSchema; | ||
| import com.jfixby.scarabei.api.debug.Debug; | ||
| import com.jfixby.scarabei.api.log.L; | ||
| import com.jfixby.scarabei.api.strings.Strings; | ||
|
|
||
| import io.realm.Realm; | ||
| import io.realm.RealmResults; | ||
|
|
||
| class RealmTable implements Table { | ||
|
|
||
| final RealmDataBase db; | ||
| final String sql_table_name; | ||
| private final RealmTableSchema schema; | ||
|
|
||
| public RealmTable (final RealmDataBase mySQL, final String name) throws IOException { | ||
| this.db = mySQL; | ||
| this.sql_table_name = name; | ||
| this.schema = new RealmTableSchema(this); | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public List<Entry> listAll () throws IOException { | ||
| final Realm connection = this.db.obtainConnection(); | ||
|
|
||
| CallsFo | ||
|
|
||
| final RealmResults<RealmDBEntry> result = connection.where(RealmDBEntry.class).findAll(); | ||
| final List<Entry> resultList = this.collectResult(result); | ||
| return resultList; | ||
|
|
||
| } | ||
|
|
||
| private List<Entry> collectResult (final RealmResults<RealmDBEntry> list) { | ||
| final List<Entry> entries = Collections.newList(); | ||
| final TableSchema schema = this.getSchema(); | ||
| final Collection<String> columns = schema.getColumns(); | ||
| for (final RealmDBEntry e : list) { | ||
| final Entry entry = this.readEntry(e, columns); | ||
| entries.add(entry); | ||
| } | ||
|
|
||
| return entries; | ||
|
|
||
| } | ||
|
|
||
| private Entry readEntry (final RealmDBEntry dbEntry, final Collection<String> columns) { | ||
| final RealmEntry entry = this.newEntry(); | ||
| final int N = columns.size(); | ||
| for (int i = 0; i < N; i++) { | ||
| final String key = columns.getElementAt(i); | ||
| final String value = dbEntry.getString(i); | ||
| entry.set(this.schema, this.schema.indexOf(key), value); | ||
| } | ||
| return entry; | ||
| } | ||
|
|
||
| @Override | ||
| public RealmEntry newEntry () { | ||
| return new RealmEntry(); | ||
| } | ||
|
|
||
| @Override | ||
| public TableSchema getSchema () { | ||
| return this.schema; | ||
| } | ||
|
|
||
| private String paramString (final Entry entry, final List<String> keys, final String bracketLeft, final String bracketRight) | ||
| throws IOException { | ||
| final TableSchema schema = this.getSchema(); | ||
| final Collection<String> colums = schema.getColumns(); | ||
|
|
||
| for (int i = 0; i < colums.size(); i++) { | ||
| final String key = colums.getElementAt(i); | ||
| final String value = entry.getValue(key); | ||
| if (value != null) { | ||
| keys.add(key); | ||
| } | ||
| } | ||
| final String schemaString = Strings.wrapSequence(keys, keys.size(), bracketLeft, bracketRight, ", "); | ||
|
|
||
| return schemaString + " VALUES " + Strings.wrapSequence(new Sequence<String>() { | ||
| @Override | ||
| public String getElementAt (final long i) { | ||
| return "?"; | ||
| } | ||
| }, keys.size(), "(", ")", ", "); | ||
| } | ||
|
|
||
| @Override | ||
| public void clear () throws IOException { | ||
| L.d("clear sql table", this.sql_table_name); | ||
| final String request = "TRUNCATE " + this.sql_table_name; | ||
| final Realm connection = this.db.obtainConnection(); | ||
| connection.delete(clazz); | ||
| connection.checkIsOpen(); | ||
| try { | ||
| final Connection mysql_connection = connection.getConnection(); | ||
| final PreparedStatement statement = mysql_connection.prepareStatement(request); | ||
| statement.execute(); | ||
| L.d(" ", "done"); | ||
| } catch (final SQLException e) { | ||
| e.printStackTrace(); | ||
| throw new IOException(e); | ||
| } finally { | ||
| this.db.releaseConnection(connection); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void replaceEntries (final List<Entry> batch) throws IOException { | ||
| if (batch.size() == 0) { | ||
| return; | ||
| } | ||
| final Entry entry0 = batch.getElementAt(0); | ||
| final String table_name = this.sql_table_name; | ||
| final List<String> keys = Collections.newList(); | ||
| final String stm = "REPLACE " + table_name + " " + this.paramString(entry0, keys, "(", ")"); | ||
| final RealmConnection connection = this.db.obtainConnection(); | ||
| connection.checkIsOpen(); | ||
| try { | ||
| final Connection mysql_connection = connection.getConnection(); | ||
| final PreparedStatement statement = mysql_connection.prepareStatement(stm); | ||
| for (int b = 0; b < batch.size(); b++) { | ||
| final Entry entry = batch.getElementAt(b); | ||
| for (int i = 0; i < keys.size(); i++) { | ||
| final String key = keys.getElementAt(i); | ||
| final String value = entry.getValue(key); | ||
| statement.setString(i + 1, value); | ||
| } | ||
| statement.addBatch(); | ||
| } | ||
| statement.executeBatch(); | ||
| } catch (final SQLException e) { | ||
| e.printStackTrace(); | ||
| throw new IOException(e); | ||
| } finally { | ||
| this.db.releaseConnection(connection); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void addEntries (final Collection<Entry> batch) throws IOException { | ||
| if (batch.size() == 0) { | ||
| return; | ||
| } | ||
| final Entry entry0 = batch.getElementAt(0); | ||
| final String table_name = this.sql_table_name; | ||
| final List<String> keys = Collections.newList(); | ||
| final String stm = "INSERT INTO " + table_name + " " + this.paramString(entry0, keys, "(", ")"); | ||
| final RealmConnection connection = this.db.obtainConnection(); | ||
| connection.checkIsOpen(); | ||
| try { | ||
| final Connection mysql_connection = connection.getConnection(); | ||
|
|
||
| final PreparedStatement statement = mysql_connection.prepareStatement(stm); | ||
| for (int b = 0; b < batch.size(); b++) { | ||
| final Entry entry = batch.getElementAt(b); | ||
| for (int i = 0; i < keys.size(); i++) { | ||
| final String key = keys.getElementAt(i); | ||
| // final String value = entry.getValue(key); | ||
| final String value = entry.getValue(key); | ||
| statement.setString(i + 1, value); | ||
| } | ||
| statement.addBatch(); | ||
| } | ||
| statement.executeBatch(); | ||
| } catch (final SQLException e) { | ||
| e.printStackTrace(); | ||
| throw new IOException(e); | ||
| } finally { | ||
| this.db.releaseConnection(connection); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void addEntry (final Entry entry) throws IOException { | ||
|
|
||
| final String table_name = this.sql_table_name; | ||
| final List<String> keys = Collections.newList(); | ||
| final String stm = "INSERT INTO " + table_name + " " + this.paramString(entry, keys, "(", ")"); | ||
| final RealmConnection connection = this.db.obtainConnection(); | ||
| connection.checkIsOpen(); | ||
| try { | ||
| final Connection mysql_connection = connection.getConnection(); | ||
|
|
||
| final PreparedStatement statement = mysql_connection.prepareStatement(stm); | ||
|
|
||
| for (int i = 0; i < keys.size(); i++) { | ||
| final String key = keys.getElementAt(i); | ||
| final String value = entry.getValue(key); | ||
| statement.setString(i + 1, value); | ||
| } | ||
|
|
||
| statement.execute(); | ||
| } catch (final SQLException e) { | ||
| e.printStackTrace(); | ||
| throw new IOException(e); | ||
| } finally { | ||
| this.db.releaseConnection(connection); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean deleteEntry (final TableSchema schema, final int keyIndex, final Object value) throws IOException { | ||
| final String key = this.schema.getColumns().getElementAt(keyIndex); | ||
| Debug.checkNull("value", value); | ||
|
|
||
| final RealmConnection connection = this.db.obtainConnection(); | ||
| connection.checkIsOpen(); | ||
| try { | ||
| final Connection mysql_connection = connection.getConnection(); | ||
|
|
||
| final String table_name = this.sql_table_name; | ||
| final String stm = "DELETE * FROM " + table_name + " WHERE " + key + " = ?"; | ||
| final PreparedStatement statement = mysql_connection// | ||
| .prepareStatement(stm); | ||
| statement.setString(1, value + ""); | ||
| return statement.execute(); | ||
| } catch (final SQLException e) { | ||
| e.printStackTrace(); | ||
| throw new IOException(e); | ||
| } finally { | ||
| this.db.releaseConnection(connection); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public Collection<Entry> findEntries (final TableSchema schema, final int keyIndex, final Object value) throws IOException { | ||
| final String key = this.schema.getColumns().getElementAt(keyIndex); | ||
| Debug.checkNull("value", value); | ||
|
|
||
| final RealmConnection connection = this.db.obtainConnection(); | ||
| connection.checkIsOpen(); | ||
| try { | ||
| final Connection mysql_connection = connection.getConnection(); | ||
|
|
||
| final String table_name = this.sql_table_name; | ||
| final String stm = "SELECT * FROM " + table_name + " WHERE " + key + " = ?"; | ||
| final PreparedStatement statement = mysql_connection// | ||
| .prepareStatement(stm); | ||
| statement.setString(1, value + ""); | ||
| final ResultSet result = statement.executeQuery(); | ||
| final List<Entry> res = this.collectResult(result); | ||
| return res; | ||
| } catch (final SQLException e) { | ||
| e.printStackTrace(); | ||
| throw new IOException(e); | ||
| } finally { | ||
| this.db.releaseConnection(connection); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean deleteEntry (final Entry entry) throws IOException { | ||
| final Collection<String> shema = this.schema.getColumns(); | ||
|
|
||
| final List<String> keys = Collections.newList(); | ||
| this.paramString(entry, keys, "(", ")"); | ||
| final String table_name = this.sql_table_name; | ||
| final String stm; | ||
| if (keys.size() == 0) { | ||
| stm = "DELETE FROM " + table_name; | ||
| } else { | ||
| stm = "DELETE FROM " + table_name + " WHERE " + Strings.wrapSequence(new Sequence<String>() { | ||
| @Override | ||
| public String getElementAt (final long i) { | ||
| return keys.getElementAt(i) + "=" + "?"; | ||
| } | ||
| }, keys.size(), "", "", " AND "); | ||
| } | ||
|
|
||
| final RealmConnection connection = this.db.obtainConnection(); | ||
| connection.checkIsOpen(); | ||
| try { | ||
| final Connection mysql_connection = connection.getConnection(); | ||
| final PreparedStatement statement = mysql_connection// | ||
| .prepareStatement(stm); | ||
| int k = 1; | ||
| for (int i = 0; i < keys.size(); i++) { | ||
| final String valuei = entry.getValue(keys.getElementAt(i)); | ||
| statement.setString(k, valuei); | ||
| k++; | ||
| } | ||
|
|
||
| return statement.execute(); | ||
| } catch (final SQLException e) { | ||
| e.printStackTrace(); | ||
| throw new IOException(e); | ||
| } finally { | ||
| this.db.releaseConnection(connection); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void deleteEntries (final Collection<Entry> paramEntries) throws IOException { | ||
| for (final Entry e : paramEntries) {// fuck you | ||
| this.deleteEntry(e); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } |
| @@ -0,0 +1,46 @@ | ||
|
|
||
| package com.jfixby.scarabei.db.realm; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| import com.jfixby.scarabei.api.collections.Collection; | ||
| import com.jfixby.scarabei.api.collections.Collections; | ||
| import com.jfixby.scarabei.api.collections.Set; | ||
| import com.jfixby.scarabei.api.db.TableSchema; | ||
| import com.jfixby.scarabei.api.log.L; | ||
|
|
||
| import io.realm.Realm; | ||
| import io.realm.RealmObjectSchema; | ||
| import io.realm.RealmSchema; | ||
|
|
||
| class RealmTableSchema implements TableSchema { | ||
|
|
||
| private final RealmTable mySQLTable; | ||
| private final Set<String> columns = Collections.newSet(); | ||
| private final RealmObjectSchema realmSchema; | ||
|
|
||
| public RealmTableSchema (final RealmTable mySQLTable) throws IOException { | ||
| this.mySQLTable = mySQLTable; | ||
| final Realm connection = this.mySQLTable.db.obtainConnection(); | ||
| final RealmSchema dbSchema = connection.getSchema(); | ||
| this.realmSchema = dbSchema.get(mySQLTable.sql_table_name); | ||
| final java.util.Set<String> names = this.realmSchema.getFieldNames(); | ||
| this.columns.addAll(names); | ||
| L.d(mySQLTable.sql_table_name + " : columns", this.columns); | ||
| } | ||
|
|
||
| @Override | ||
| public Collection<String> getColumns () { | ||
| return this.columns; | ||
| } | ||
|
|
||
| @Override | ||
| public int indexOf (final String key) { | ||
| final int result = this.columns.indexOf(key); | ||
| if (result == -1) { | ||
| L.e("schema", this.columns); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| } |
| @@ -1,25 +1,70 @@ | ||
|
|
||
| package com.jfixby.scarabei.red.android.db; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| import com.jfixby.scarabei.api.collections.Collections; | ||
| import com.jfixby.scarabei.api.collections.Map; | ||
| import com.jfixby.scarabei.api.db.DataBase; | ||
| import com.jfixby.scarabei.api.db.Table; | ||
| import com.jfixby.scarabei.api.debug.Debug; | ||
| import com.jfixby.scarabei.api.err.Err; | ||
| import com.jfixby.scarabei.api.log.L; | ||
|
|
||
| import android.database.Cursor; | ||
| import android.database.sqlite.SQLiteDatabase; | ||
|
|
||
| public class AndroidDB implements DataBase { | ||
|
|
||
| private final AndroidDBHelper helper; | ||
| private final String dbName; | ||
| final Map<String, AndroidDBTable> tables = Collections.newMap(); | ||
|
|
||
| public AndroidDB (final AndroidDBConfig config) { | ||
| Err.throwNotImplementedYet(); | ||
| Debug.checkNull("dbName", config.dbName); | ||
| this.dbName = Debug.checkEmpty("dbName", config.dbName); | ||
| this.helper = new AndroidDBHelper(this.dbName); | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public Table getTable (final String tableName) throws IOException { | ||
| Debug.checkNull("tableName", tableName); | ||
| Debug.checkEmpty("tableName", tableName); | ||
|
|
||
| AndroidDBTable table = this.tables.get(tableName); | ||
| if (table == null) { | ||
| final SQLiteDatabase db = this.helper.getReadableDatabase(); | ||
| final Cursor c = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null); | ||
| final String[] tableNames = c.getColumnNames(); | ||
| for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) { | ||
| final int tablesCount = c.getColumnCount(); | ||
| for (int i = 0; i < tablesCount; i++) { | ||
| final String tableName_i = c.getString(i); | ||
| if (tableName_i.equals(tableName)) { | ||
| table = new AndroidDBTable(this, tableName); | ||
| this.tables.put(tableName, table); | ||
| return table; | ||
| } | ||
| } | ||
| } | ||
| L.e("tableNames", tableNames); | ||
| throw new IOException("Table <" + tableName + "> not found"); | ||
| } | ||
| return table; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDBName () { | ||
| return this.dbName; | ||
| } | ||
|
|
||
| public AndroidDBHelper obtainConnection () { | ||
| return this.helper; | ||
| } | ||
|
|
||
| public void releaseConnection (final AndroidDBHelper helper) { | ||
| } | ||
|
|
||
| } |
| @@ -0,0 +1,8 @@ | ||
|
|
||
| package com.jfixby.scarabei.red.android.db; | ||
|
|
||
| public class AndroidDBConfig { | ||
|
|
||
| public String dbName; | ||
|
|
||
| } |
| @@ -0,0 +1,36 @@ | ||
|
|
||
| package com.jfixby.scarabei.red.android.db; | ||
|
|
||
| import com.jfixby.scarabei.api.collections.Collections; | ||
| import com.jfixby.scarabei.api.collections.Map; | ||
| import com.jfixby.scarabei.api.db.Entry; | ||
| import com.jfixby.scarabei.api.db.TableSchema; | ||
|
|
||
| class AndroidDBEntry implements Entry { | ||
|
|
||
| public AndroidDBEntry () { | ||
| } | ||
|
|
||
| final Map<String, String> values = Collections.newMap(); | ||
|
|
||
| void set (final String key, final Object value) { | ||
| this.values.put(key, value + ""); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString () { | ||
| return "" + this.values + ""; | ||
| } | ||
|
|
||
| @Override | ||
| public void set (final TableSchema schema, final int keyIndex, final Object value) { | ||
| final String key = schema.getColumns().getElementAt(keyIndex); | ||
| this.set(key, value); | ||
| } | ||
|
|
||
| @Override | ||
| public String getValue (final String key) { | ||
| return this.values.get(key); | ||
| } | ||
|
|
||
| } |
| @@ -0,0 +1,25 @@ | ||
|
|
||
| package com.jfixby.scarabei.red.android.db; | ||
|
|
||
| import com.jfixby.scarabei.android.api.Android; | ||
|
|
||
| import android.database.sqlite.SQLiteDatabase; | ||
| import android.database.sqlite.SQLiteOpenHelper; | ||
|
|
||
| public class AndroidDBHelper extends SQLiteOpenHelper { | ||
| static final int DATABASE_VERSION = 5; | ||
|
|
||
| public AndroidDBHelper (final String dbName) { | ||
| super(Android.getApplicationContext(), dbName, null, DATABASE_VERSION); | ||
| } | ||
|
|
||
| @Override | ||
| public void onCreate (final SQLiteDatabase db) { | ||
| } | ||
|
|
||
| @Override | ||
| public void onUpgrade (final SQLiteDatabase db, final int oldVersion, final int newVersion) { | ||
|
|
||
| } | ||
|
|
||
| } |
| @@ -0,0 +1,313 @@ | ||
|
|
||
| package com.jfixby.scarabei.red.android.db; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| import com.jfixby.scarabei.api.collections.Collection; | ||
| import com.jfixby.scarabei.api.collections.Collections; | ||
| import com.jfixby.scarabei.api.collections.List; | ||
| import com.jfixby.scarabei.api.collections.Sequence; | ||
| import com.jfixby.scarabei.api.db.Entry; | ||
| import com.jfixby.scarabei.api.db.Table; | ||
| import com.jfixby.scarabei.api.db.TableSchema; | ||
| import com.jfixby.scarabei.api.debug.Debug; | ||
| import com.jfixby.scarabei.api.log.L; | ||
| import com.jfixby.scarabei.api.strings.Strings; | ||
|
|
||
| import android.database.Cursor; | ||
| import android.database.sqlite.SQLiteDatabase; | ||
|
|
||
| public class AndroidDBTable implements Table { | ||
|
|
||
| final String sql_table_name; | ||
| final AndroidDB db; | ||
| private final AndroidDBTableSchema schema; | ||
|
|
||
| public AndroidDBTable (final AndroidDB androidDB, final String tableName) throws IOException { | ||
| this.db = androidDB; | ||
| this.sql_table_name = tableName; | ||
| this.schema = new AndroidDBTableSchema(this); | ||
| } | ||
|
|
||
| @Override | ||
| public List<Entry> listAll () throws IOException { | ||
| final AndroidDBHelper connection = this.db.obtainConnection(); | ||
| final SQLiteDatabase db = connection.getReadableDatabase(); | ||
| final String request = "SELECT * FROM " + this.sql_table_name; | ||
| final Cursor cur = db.rawQuery(request, null); | ||
| final List<Entry> resultList = this.collectResult(cur); | ||
| this.db.releaseConnection(connection); | ||
| return resultList; | ||
|
|
||
| } | ||
|
|
||
| private List<Entry> collectResult (final Cursor cursor) { | ||
| final List<Entry> entries = Collections.newList(); | ||
| final TableSchema schema = this.getSchema(); | ||
| final Collection<String> columns = schema.getColumns(); | ||
|
|
||
| if (cursor.getCount() != 0) { | ||
| cursor.moveToFirst(); | ||
| do { | ||
| final Entry entry = this.readEntry(cursor, columns); | ||
| entries.add(entry); | ||
| } while (cursor.moveToNext()); | ||
| } | ||
|
|
||
| return entries; | ||
|
|
||
| } | ||
|
|
||
| private Entry readEntry (final Cursor cursor, final Collection<String> columns) { | ||
| final Entry entry = this.newEntry(); | ||
| final int N = columns.size(); | ||
| for (int i = 0; i < N; i++) { | ||
| final String key = columns.getElementAt(i); | ||
| final String value = cursor.getString(i); | ||
| entry.set(this.schema, this.schema.indexOf(key), value); | ||
| } | ||
| return entry; | ||
| } | ||
|
|
||
| @Override | ||
| public Entry newEntry () { | ||
| return new AndroidDBEntry(); | ||
| } | ||
|
|
||
| @Override | ||
| public AndroidDBTableSchema getSchema () { | ||
| return this.schema; | ||
| } | ||
|
|
||
| private String paramString (final Entry entry, final List<String> keys, final String bracketLeft, final String bracketRight) | ||
| throws IOException { | ||
| final AndroidDBTableSchema schema = this.getSchema(); | ||
| final Collection<String> colums = schema.getColumns(); | ||
|
|
||
| for (int i = 0; i < colums.size(); i++) { | ||
| final String key = colums.getElementAt(i); | ||
| final String value = entry.getValue(key); | ||
| if (value != null) { | ||
| keys.add(key); | ||
| } | ||
| } | ||
| final String schemaString = Strings.wrapSequence(keys, keys.size(), bracketLeft, bracketRight, ", "); | ||
|
|
||
| return schemaString + " VALUES " + Strings.wrapSequence(new Sequence<String>() { | ||
| @Override | ||
| public String getElementAt (final long i) { | ||
| return "?"; | ||
| } | ||
| }, keys.size(), "(", ")", ", "); | ||
| } | ||
|
|
||
| @Override | ||
| public void clear () throws IOException { | ||
| L.d("clear sql table", this.sql_table_name); | ||
|
|
||
| final AndroidDBHelper connection = this.db.obtainConnection(); | ||
| final SQLiteDatabase db = connection.getReadableDatabase(); | ||
| final String request = "TRUNCATE " + this.sql_table_name; | ||
| final Cursor cur = db.rawQuery(request, null); | ||
| this.db.releaseConnection(connection); | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void replaceEntries (final List<Entry> batch) throws IOException { | ||
| if (batch.size() == 0) { | ||
| return; | ||
| } | ||
| // final Entry entry0 = batch.getElementAt(0); | ||
| // final String table_name = this.sql_table_name; | ||
| // final List<String> keys = Collections.newList(); | ||
| // final String stm = "REPLACE " + table_name + " " + this.paramString(entry0, keys, "(", ")"); | ||
| // final AndroidDBConnection connection = this.db.obtainConnection(); | ||
| // connection.checkIsOpen(); | ||
| // try { | ||
| // final SQLExecutor mysql_connection = connection.getConnection(); | ||
| // final PreparedStatement statement = mysql_connection.prepareStatement(stm); | ||
| // for (int b = 0; b < batch.size(); b++) { | ||
| // final Entry entry = batch.getElementAt(b); | ||
| // for (int i = 0; i < keys.size(); i++) { | ||
| // final String key = keys.getElementAt(i); | ||
| // final String value = entry.getValue(key); | ||
| // statement.setString(i + 1, value); | ||
| // } | ||
| // statement.addBatch(); | ||
| // } | ||
| // statement.executeBatch(); | ||
| // } catch (final SQLException e) { | ||
| // e.printStackTrace(); | ||
| // throw new IOException(e); | ||
| // } finally { | ||
| // this.db.releaseConnection(connection); | ||
| // } | ||
| } | ||
|
|
||
| @Override | ||
| public void addEntries (final Collection<Entry> batch) throws IOException { | ||
| if (batch.size() == 0) { | ||
| return; | ||
| } | ||
| // final Entry entry0 = batch.getElementAt(0); | ||
| // final String table_name = this.sql_table_name; | ||
| // final List<String> keys = Collections.newList(); | ||
| // final String stm = "INSERT INTO " + table_name + " " + this.paramString(entry0, keys, "(", ")"); | ||
| // final AndroidDBConnection connection = this.db.obtainConnection(); | ||
| // connection.checkIsOpen(); | ||
| // try { | ||
| // final SQLExecutor mysql_connection = connection.getConnection(); | ||
| // | ||
| // final PreparedStatement statement = mysql_connection.prepareStatement(stm); | ||
| // for (int b = 0; b < batch.size(); b++) { | ||
| // final Entry entry = batch.getElementAt(b); | ||
| // for (int i = 0; i < keys.size(); i++) { | ||
| // final String key = keys.getElementAt(i); | ||
| //// final String value = entry.getValue(key); | ||
| // final String value = entry.getValue(key); | ||
| // statement.setString(i + 1, value); | ||
| // } | ||
| // statement.addBatch(); | ||
| // } | ||
| // statement.executeBatch(); | ||
| // } catch (final SQLException e) { | ||
| // e.printStackTrace(); | ||
| // throw new IOException(e); | ||
| // } finally { | ||
| // this.db.releaseConnection(connection); | ||
| // } | ||
| } | ||
|
|
||
| @Override | ||
| public void addEntry (final Entry entry) throws IOException { | ||
|
|
||
| // final String table_name = this.sql_table_name; | ||
| // final List<String> keys = Collections.newList(); | ||
| // final String stm = "INSERT INTO " + table_name + " " + this.paramString(entry, keys, "(", ")"); | ||
| // final AndroidDBConnection connection = this.db.obtainConnection(); | ||
| // connection.checkIsOpen(); | ||
| // try { | ||
| // final SQLExecutor mysql_connection = connection.getConnection(); | ||
| // | ||
| // final PreparedStatement statement = mysql_connection.prepareStatement(stm); | ||
| // | ||
| // for (int i = 0; i < keys.size(); i++) { | ||
| // final String key = keys.getElementAt(i); | ||
| // final String value = entry.getValue(key); | ||
| // statement.setString(i + 1, value); | ||
| // } | ||
| // | ||
| // statement.execute(); | ||
| // } catch (final SQLException e) { | ||
| // e.printStackTrace(); | ||
| // throw new IOException(e); | ||
| // } finally { | ||
| // this.db.releaseConnection(connection); | ||
| // } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean deleteEntry (final TableSchema schema, final int keyIndex, final Object value) throws IOException { | ||
| final String key = this.schema.getColumns().getElementAt(keyIndex); | ||
| Debug.checkNull("value", value); | ||
| return false; | ||
|
|
||
| // final AndroidDBConnection connection = this.db.obtainConnection(); | ||
| // connection.checkIsOpen(); | ||
| // try { | ||
| // final SQLExecutor mysql_connection = connection.getConnection(); | ||
| // | ||
| // final String table_name = this.sql_table_name; | ||
| // final String stm = "DELETE * FROM " + table_name + " WHERE " + key + " = ?"; | ||
| // final PreparedStatement statement = mysql_connection// | ||
| // .prepareStatement(stm); | ||
| // statement.setString(1, value + ""); | ||
| // return statement.execute(); | ||
| // } catch (final SQLException e) { | ||
| // e.printStackTrace(); | ||
| // throw new IOException(e); | ||
| // } finally { | ||
| // this.db.releaseConnection(connection); | ||
| // } | ||
| } | ||
|
|
||
| @Override | ||
| public Collection<Entry> findEntries (final TableSchema schema, final int keyIndex, final Object value) throws IOException { | ||
| return null; | ||
| // final String key = this.schema.getColumns().getElementAt(keyIndex); | ||
| // Debug.checkNull("value", value); | ||
| // | ||
| // final AndroidDBConnection connection = this.db.obtainConnection(); | ||
| // connection.checkIsOpen(); | ||
| // try { | ||
| // final SQLExecutor mysql_connection = connection.getConnection(); | ||
| // | ||
| // final String table_name = this.sql_table_name; | ||
| // final String stm = "SELECT * FROM " + table_name + " WHERE " + key + " = ?"; | ||
| // final PreparedStatement statement = mysql_connection// | ||
| // .prepareStatement(stm); | ||
| // statement.setString(1, value + ""); | ||
| // final ResultSet result = statement.executeQuery(); | ||
| // final List<Entry> res = this.collectResult(result); | ||
| // return res; | ||
| // } catch (final SQLException e) { | ||
| // e.printStackTrace(); | ||
| // throw new IOException(e); | ||
| // } finally { | ||
| // this.db.releaseConnection(connection); | ||
| // } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean deleteEntry (final Entry entry) throws IOException { | ||
| final Collection<String> shema = this.schema.getColumns(); | ||
| // | ||
| // final List<String> keys = Collections.newList(); | ||
| // this.paramString(entry, keys, "(", ")"); | ||
| // final String table_name = this.sql_table_name; | ||
| // final String stm; | ||
| // if (keys.size() == 0) { | ||
| // stm = "DELETE FROM " + table_name; | ||
| // } else { | ||
| // stm = "DELETE FROM " + table_name + " WHERE " + Strings.wrapSequence(new Sequence<String>() { | ||
| // @Override | ||
| // public String getElementAt (final long i) { | ||
| // return keys.getElementAt(i) + "=" + "?"; | ||
| // } | ||
| // }, keys.size(), "", "", " AND "); | ||
| // } | ||
| // | ||
| // final AndroidDBConnection connection = this.db.obtainConnection(); | ||
| // connection.checkIsOpen(); | ||
| // try { | ||
| // final SQLExecutor mysql_connection = connection.getConnection(); | ||
| // final PreparedStatement statement = mysql_connection// | ||
| // .prepareStatement(stm); | ||
| // int k = 1; | ||
| // for (int i = 0; i < keys.size(); i++) { | ||
| // final String valuei = entry.getValue(keys.getElementAt(i)); | ||
| // statement.setString(k, valuei); | ||
| // k++; | ||
| // } | ||
| // | ||
| // return statement.execute(); | ||
| // } catch (final SQLException e) { | ||
| // e.printStackTrace(); | ||
| // throw new IOException(e); | ||
| // } finally { | ||
| // this.db.releaseConnection(connection); | ||
| // } | ||
| return false; | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void deleteEntries (final Collection<Entry> paramEntries) throws IOException { | ||
| for (final Entry e : paramEntries) {// fuck you | ||
| this.deleteEntry(e); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } |
| @@ -0,0 +1,54 @@ | ||
|
|
||
| package com.jfixby.scarabei.red.android.db; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| import com.jfixby.scarabei.api.collections.Collection; | ||
| import com.jfixby.scarabei.api.collections.Collections; | ||
| import com.jfixby.scarabei.api.collections.Set; | ||
| import com.jfixby.scarabei.api.db.TableSchema; | ||
| import com.jfixby.scarabei.api.log.L; | ||
|
|
||
| import android.database.Cursor; | ||
| import android.database.sqlite.SQLiteDatabase; | ||
|
|
||
| class AndroidDBTableSchema implements TableSchema { | ||
|
|
||
| private final AndroidDBTable mySQLTable; | ||
| private final Set<String> columns = Collections.newSet(); | ||
|
|
||
| public AndroidDBTableSchema (final AndroidDBTable mySQLTable) throws IOException { | ||
| this.mySQLTable = mySQLTable; | ||
| final AndroidDBHelper helper = this.mySQLTable.db.obtainConnection(); | ||
| try { | ||
|
|
||
| final SQLiteDatabase db = helper.getReadableDatabase(); | ||
| final String tableName = mySQLTable.sql_table_name; | ||
| final Cursor c1 = db.rawQuery("SELECT * FROM " + tableName, null); | ||
| c1.moveToFirst(); | ||
| final String[] columnNames = c1.getColumnNames(); | ||
| for (int j = 0; j < columnNames.length; j++) { | ||
| c1.move(j); | ||
| this.columns.add(columnNames[j]); | ||
| } | ||
|
|
||
| } finally { | ||
| this.mySQLTable.db.releaseConnection(helper); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public Collection<String> getColumns () { | ||
| return this.columns; | ||
| } | ||
|
|
||
| @Override | ||
| public int indexOf (final String key) { | ||
| final int result = this.columns.indexOf(key); | ||
| if (result == -1) { | ||
| L.e("schema", this.columns); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| } |
| @@ -20,4 +20,6 @@ include "scarabei-gson" | ||
|
|
||
| include "scarabei-db-mysql" | ||
|
|
||
| include "scarabei-red-flutter" | ||
|
|
||
| include "scarabei-db-realm" | ||