Permalink
Browse files

not working android sqlite implementation added. fix later

  • Loading branch information...
1 parent a3559f1 commit 3b1cc97903871b444dc4cbf83a8113c52512e1e2 @ahmetb committed May 13, 2011
View
@@ -5,5 +5,6 @@
<classpathentry kind="lib" path="lib/sqlite4java.jar"/>
<classpathentry kind="lib" path="lib/mysql-connector-java-5.0.8-bin.jar"/>
<classpathentry kind="lib" path="lib/log4j-1.2.16.jar"/>
+ <classpathentry kind="lib" path="lib/android.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
@@ -45,7 +45,7 @@ private void throwError(SQLiteException e){
@Override
public ResultList executeForResultList(Query q) {
- Log.trace("Executing: " + q); // TODO log.
+ Log.trace("Executing: " + q);
try {
SQLiteStatement s = db.prepare(q.getExecutableSql());
@@ -85,7 +85,7 @@ public ResultList executeForResultList(Query q) {
@Override
public Object executeForSingleValue(Query q) {
- Log.trace("Executing: " + q); // TODO log.
+ Log.trace("Executing: " + q);
try {
SQLiteStatement s = db.prepare(q.getExecutableSql());
@@ -4,6 +4,7 @@
import org.orman.datasource.Database;
import org.orman.datasource.QueryExecutionContainer;
import org.orman.sql.SQLGrammarProvider;
+import org.orman.util.Log;
/**
* SQLite implementation with sqlite4java library
@@ -53,7 +54,7 @@ public DataTypeMapper getTypeMapper() {
@Override
public void closeConnection() {
executer.close();
- System.out.println("Connection terminated successfully."); //TODO log.
+ Log.info("Connection terminated successfully.");
}
@Override
@@ -45,10 +45,6 @@
}};
- protected SQLiteGrammar() {
-
- }
-
@Override
public String getTemplate(QueryType type) {
String tpl = grammar.get(type);
@@ -0,0 +1,158 @@
+
+package org.orman.sqlite.android;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.logging.Logger;
+
+import org.orman.datasource.QueryExecutionContainer;
+import org.orman.datasource.ResultList;
+import org.orman.datasource.exception.QueryExecutionException;
+import org.orman.sql.Query;
+import org.orman.util.Log;
+
+import android.database.Cursor;
+import android.database.sqlite.SQLiteDatabase;
+import android.database.sqlite.SQLiteException;
+
+// TODO dispose() time.
+public class QueryExecutionContainerImpl implements QueryExecutionContainer {
+
+ private SQLiteDatabase db;
+
+ /**
+ * Initialize database, create db file if not exists.
+ * @param settings
+ */
+ public QueryExecutionContainerImpl(SQLiteDatabase db){
+ this.db = db;
+ }
+
+ private void throwError(SQLiteException e){
+ throw new QueryExecutionException("SQLiteAndroid error:" + e.toString());
+ }
+
+ /**
+ * Only executes the query without obtaining any results.
+ * throws {@link QueryExecutionException} if error occurs.
+ */
+ @Override
+ public void executeOnly(Query q) {
+
+ android.util.Log.d("DEMO", "db = " + db);
+ android.util.Log.d("DEMO", "q = " + q.toString());
+
+ Log.trace("Executing: " + q);
+ try {
+ db.execSQL(q.getExecutableSql());
+ } catch (SQLiteException e) {
+ throwError(e);
+ }
+ }
+
+ @Override
+ public ResultList executeForResultList(Query q) {
+ Log.trace("Executing: " + q);
+
+
+ try {
+ Cursor cur = db.rawQuery(q.getExecutableSql(), null);
+
+ int columnCount = cur.getColumnCount();
+ String[] colNames = cur.getColumnNames();
+
+ List<Object[]> result = new ArrayList<Object[]>();
+
+ int rowIndex = 0;
+
+ boolean hasRecord = cur.moveToFirst();
+
+ while(hasRecord){
+ Object[] row = new Object[columnCount];
+
+ for(int j = 0; j < columnCount; j++){ // for each column
+ row[j] = cur.getString(j);
+ }
+ result.add(row);
+ ++rowIndex;
+
+ hasRecord = cur.moveToNext();
+ }
+
+ if(result.size() > 0){
+ Object[][] resultArr = new Object[result.size()][columnCount];
+ int i = 0;
+ for(Object[] row : result)
+ resultArr[i++] = row;
+
+ return new ResultList(colNames, resultArr);
+ }
+ } catch (SQLiteException ex) {
+ throwError(ex);
+ }
+ return null;
+ }
+
+
+ @Override
+ public Object executeForSingleValue(Query q) {
+ Log.trace("Executing: " + q); // TODO log.
+
+ try {
+ Cursor cur = db.rawQuery(q.getExecutableSql(), null);
+ if (!cur.moveToNext()){
+ return null;
+ } else {
+ return cur.getString(0);
+ }
+ } catch (SQLiteException e) {
+ throwError(e);
+ }
+ return null;
+ }
+
+ @Override
+ public Object getLastInsertId() {
+ try {
+ Cursor cur = db.rawQuery("SELECT last_insert_rowid();", null);
+ cur.moveToFirst();
+ if (!cur.moveToNext()){
+ return null;
+ } else {
+ return cur.getLong(0); // TODO returns long. test for int and String behavior.
+ }
+ } catch (SQLiteException e) {
+ throwError(e);
+ return null;
+ }
+ }
+
+ @Override
+ public <T> Object getLastInsertId(Class<T> ofType) {
+ Object val = getLastInsertId();
+
+ if(ofType.equals(String.class)){
+ return new String(val.toString());
+ } else if(ofType.equals(Integer.class) || ofType.equals(Integer.TYPE)){
+ return new Integer(val.toString()); // TODO inefficient?
+ } else if(ofType.equals(Long.class) || ofType.equals(Long.TYPE)){
+ return new Long(val.toString()); // TODO inefficient?
+ }
+ return val;
+ }
+
+ @Override
+ public void close() {
+ db.close();
+ }
+
+ /**
+ * To bind {@link SQLiteDatabase} onCreate.
+ *
+ * @param db
+ */
+ public void setDatabase(SQLiteDatabase db) {
+ this.db = db;
+ }
+
+}
@@ -0,0 +1,83 @@
+package org.orman.sqlite.android;
+
+import org.orman.datasource.DataTypeMapper;
+import org.orman.datasource.Database;
+import org.orman.datasource.QueryExecutionContainer;
+import org.orman.sql.SQLGrammarProvider;
+import org.orman.sqlite.DataTypeMapperImpl;
+import org.orman.sqlite.SQLiteGrammar;
+import org.orman.util.Log;
+
+import android.content.Context;
+import android.database.sqlite.SQLiteDatabase;
+import android.database.sqlite.SQLiteOpenHelper;
+
+/**
+ * SQLite wrapper for Android SDK 3 <code>database.sqlite
+ * </code> package.
+ *
+ * Shares same grammar and data type mapper with <code>org.orman.sqlite</code>
+ * package.
+ *
+ * @author alp
+ */
+public class SQLiteAndroid extends SQLiteOpenHelper implements Database {
+ private static final int SQLITE_VERSION = 33; //TODO read from somewhere else ASAP
+ private DataTypeMapper typeMapper;
+ private QueryExecutionContainerImpl executer;
+ private SQLiteGrammar grammar;
+
+ private String databaseName;
+
+ public SQLiteAndroid(Context context, String databaseName) {
+ super(context, databaseName, null, SQLITE_VERSION);
+
+ this.databaseName = databaseName;
+ typeMapper = new DataTypeMapperImpl();
+ executer = new QueryExecutionContainerImpl(null); //bind database onCreate.
+ grammar = new SQLiteGrammar();
+ }
+
+ @Override
+ public QueryExecutionContainer getExecuter() {
+ return executer;
+ }
+
+ @Override
+ public DataTypeMapper getTypeMapper() {
+ return typeMapper;
+ }
+
+ @Override
+ public void closeConnection() {
+ executer.close();
+ Log.info("Connection terminated successfully.");
+ }
+
+ @Override
+ public SQLGrammarProvider getSQLGrammar() {
+ return grammar;
+ }
+
+ @Override
+ public void onCreate(SQLiteDatabase db) {
+ executer.setDatabase(this.getWritableDatabase()); // no need for readable db.
+
+ // drop scheme first
+ // construct scheme
+ }
+
+ @Override
+ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
+ // TODO not implemented. have a destroySchemeQueries function in MappingSession.
+ executer.setDatabase(this.getWritableDatabase());
+
+ // drop scheme
+
+ // construct scheme
+ }
+
+ public String getDatabaseName() {
+ return databaseName;
+ }
+}

0 comments on commit 3b1cc97

Please sign in to comment.