提交 ba5492da authored 作者: Thomas Mueller's avatar Thomas Mueller

Android API

上级 a01642bb
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
*/ */
package org.h2.android; package org.h2.android;
import org.h2.result.ResultInterface;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.database.AbstractWindowedCursor; import android.database.AbstractWindowedCursor;
import android.database.CharArrayBuffer; import android.database.CharArrayBuffer;
...@@ -21,13 +22,18 @@ import android.os.Bundle; ...@@ -21,13 +22,18 @@ import android.os.Bundle;
public class H2Cursor extends AbstractWindowedCursor { public class H2Cursor extends AbstractWindowedCursor {
private H2Database database; private H2Database database;
private ResultInterface result;
H2Cursor(H2Database db, H2CursorDriver driver, String editTable, H2Query query) { H2Cursor(H2Database db, H2CursorDriver driver, String editTable, H2Query query) {
// TODO // TODO
} }
H2Cursor(ResultInterface result) {
this.result = result;
}
public void close() { public void close() {
// TODO result.close();
} }
public void deactivate() { public void deactivate() {
...@@ -43,7 +49,7 @@ public class H2Cursor extends AbstractWindowedCursor { ...@@ -43,7 +49,7 @@ public class H2Cursor extends AbstractWindowedCursor {
} }
public int getCount() { public int getCount() {
return 0; return result.getRowCount();
} }
/** /**
...@@ -93,7 +99,10 @@ public class H2Cursor extends AbstractWindowedCursor { ...@@ -93,7 +99,10 @@ public class H2Cursor extends AbstractWindowedCursor {
} }
public boolean move(int offset) { public boolean move(int offset) {
return false; if (offset == 1) {
return result.next();
}
throw H2Database.unsupported();
} }
public void copyStringToBuffer(int columnIndex, CharArrayBuffer buffer) { public void copyStringToBuffer(int columnIndex, CharArrayBuffer buffer) {
...@@ -137,13 +146,11 @@ public class H2Cursor extends AbstractWindowedCursor { ...@@ -137,13 +146,11 @@ public class H2Cursor extends AbstractWindowedCursor {
} }
public int getInt(int columnIndex) { public int getInt(int columnIndex) {
// TODO return result.currentRow()[columnIndex].getInt();
return 0;
} }
public long getLong(int columnIndex) { public long getLong(int columnIndex) {
// TODO return result.currentRow()[columnIndex].getLong();
return 0;
} }
public int getPosition() { public int getPosition() {
...@@ -157,8 +164,7 @@ public class H2Cursor extends AbstractWindowedCursor { ...@@ -157,8 +164,7 @@ public class H2Cursor extends AbstractWindowedCursor {
} }
public String getString(int columnIndex) { public String getString(int columnIndex) {
// TODO return result.currentRow()[columnIndex].getString();
return null;
} }
public boolean getWantsAllOnMoveCalls() { public boolean getWantsAllOnMoveCalls() {
......
...@@ -7,11 +7,21 @@ ...@@ -7,11 +7,21 @@
package org.h2.android; package org.h2.android;
import java.io.File; import java.io.File;
import java.util.ArrayList;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import org.h2.command.Prepared;
import org.h2.engine.ConnectionInfo; import org.h2.engine.ConnectionInfo;
import org.h2.engine.Engine; import org.h2.engine.Database;
import org.h2.engine.Session; import org.h2.engine.Session;
import org.h2.expression.Parameter;
import org.h2.result.ResultInterface;
import org.h2.store.PageStore;
import org.h2.value.Value;
import org.h2.value.ValueInt;
import org.h2.value.ValueLong;
import org.h2.value.ValueNull;
import org.h2.value.ValueString;
import android.content.ContentValues; import android.content.ContentValues;
import android.database.Cursor; import android.database.Cursor;
...@@ -73,8 +83,10 @@ public class H2Database { ...@@ -73,8 +83,10 @@ public class H2Database {
public static final int OPEN_READWRITE = 0; public static final int OPEN_READWRITE = 0;
private final Session session; private final Session session;
private final CursorFactory factory;
private H2Database(Session session) { H2Database(Session session, CursorFactory factory) {
this.factory = factory;
this.session = session; this.session = session;
} }
...@@ -86,8 +98,9 @@ public class H2Database { ...@@ -86,8 +98,9 @@ public class H2Database {
*/ */
public static H2Database create(H2Database.CursorFactory factory) { public static H2Database create(H2Database.CursorFactory factory) {
ConnectionInfo ci = new ConnectionInfo("mem:"); ConnectionInfo ci = new ConnectionInfo("mem:");
Session s = Engine.getInstance().createSession(ci); Database db = new Database(ci, null);
return new H2Database(s); Session s = db.getSystemSession();
return new H2Database(s, factory);
} }
/** /**
...@@ -106,8 +119,10 @@ public class H2Database { ...@@ -106,8 +119,10 @@ public class H2Database {
if ((flags & CREATE_IF_NECESSARY) == 0) { if ((flags & CREATE_IF_NECESSARY) == 0) {
ci.setProperty("IFEXISTS", "TRUE"); ci.setProperty("IFEXISTS", "TRUE");
} }
Session s = Engine.getInstance().createSession(ci); ci.setProperty("FILE_LOCK", "FS");
return new H2Database(s); Database db = new Database(ci, null);
Session s = db.getSystemSession();
return new H2Database(s, factory);
} }
/** /**
...@@ -194,7 +209,7 @@ public class H2Database { ...@@ -194,7 +209,7 @@ public class H2Database {
* @param bindArgs the parameter values * @param bindArgs the parameter values
*/ */
public void execSQL(String sql, Object[] bindArgs) { public void execSQL(String sql, Object[] bindArgs) {
// TODO prepare(sql, bindArgs).update();
} }
/** /**
...@@ -203,7 +218,7 @@ public class H2Database { ...@@ -203,7 +218,7 @@ public class H2Database {
* @param sql the statement * @param sql the statement
*/ */
public void execSQL(String sql) { public void execSQL(String sql) {
// TODO session.prepare(sql).update();
} }
/** /**
...@@ -232,7 +247,8 @@ public class H2Database { ...@@ -232,7 +247,8 @@ public class H2Database {
* @return the page size * @return the page size
*/ */
public long getPageSize() { public long getPageSize() {
return 0; PageStore store = session.getDatabase().getPageStore();
return store == null ? 0 : store.getPageSize();
} }
/** /**
...@@ -457,7 +473,9 @@ public class H2Database { ...@@ -457,7 +473,9 @@ public class H2Database {
* @return the cursor * @return the cursor
*/ */
public Cursor rawQuery(String sql, String[] selectionArgs) { public Cursor rawQuery(String sql, String[] selectionArgs) {
return null; Prepared prep = prepare(sql, selectionArgs);
ResultInterface result = prep.query(0);
return new H2Cursor(result);
} }
/** /**
...@@ -635,4 +653,36 @@ public class H2Database { ...@@ -635,4 +653,36 @@ public class H2Database {
Cursor newCursor(H2Database db, H2CursorDriver masterQuery, String editTable, H2Query query); Cursor newCursor(H2Database db, H2CursorDriver masterQuery, String editTable, H2Query query);
} }
private Prepared prepare(String sql, Object[] args) {
Prepared prep = session.prepare(sql);
int len = args.length;
if (len > 0) {
ArrayList<Parameter> params = prep.getParameters();
for (int i = 0; i < len; i++) {
Parameter p = params.get(i);
p.setValue(getValue(args[i]));
}
}
return prep;
}
private Value getValue(Object o) {
if (o == null) {
return ValueNull.INSTANCE;
} else if (o instanceof String) {
return ValueString.get((String) o);
} else if (o instanceof Integer) {
return ValueInt.get((Integer) o);
} else if (o instanceof Long) {
return ValueLong.get((Integer) o);
}
return ValueString.get(o.toString());
// TODO
}
public static RuntimeException unsupported() {
// TODO
return new RuntimeException("Feature not supported");
}
} }
...@@ -20,7 +20,7 @@ public class H2Utils { ...@@ -20,7 +20,7 @@ public class H2Utils {
* @return the database connection * @return the database connection
*/ */
public static H2Database openOrCreateDatabase(String name, int mode, H2Database.CursorFactory factory) { public static H2Database openOrCreateDatabase(String name, int mode, H2Database.CursorFactory factory) {
return null; return H2Database.openOrCreateDatabase(name, factory);
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论