From c3f8764bdc3e5b3e65ce41bbad14f53d2f38e204 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Sat, 4 Apr 2015 00:09:25 +0300 Subject: [PATCH] More IndexedDB wrappers --- .../org/teavm/dom/indexeddb/IDBCursor.java | 32 +++++++++++++ .../teavm/dom/indexeddb/IDBCursorSource.java | 10 ++++ .../org/teavm/dom/indexeddb/IDBIndex.java | 39 ++++++++++++++- .../org/teavm/dom/indexeddb/IDBKeyRange.java | 48 +++++++++++++++++++ .../teavm/dom/indexeddb/IDBObjectStore.java | 4 +- .../teavm/dom/indexeddb/IDBOpenDBRequest.java | 3 ++ .../teavm/dom/indexeddb/IDBTransaction.java | 24 +++++++++- 7 files changed, 157 insertions(+), 3 deletions(-) create mode 100644 teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBCursorSource.java create mode 100644 teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBKeyRange.java diff --git a/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBCursor.java b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBCursor.java index 6eb09dfac..572613ad3 100644 --- a/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBCursor.java +++ b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBCursor.java @@ -15,12 +15,44 @@ */ package org.teavm.dom.indexeddb; +import org.teavm.jso.JSMethod; import org.teavm.jso.JSObject; +import org.teavm.jso.JSProperty; /** * * @author Alexey Andreev */ public interface IDBCursor extends JSObject { + String DIRECTION_NEXT = "next"; + String DIRECTION_NEXT_UNIQUE = "nextunique"; + + String DIRECTION_PREVIOUS = "prev"; + + String DIRECTION_PREVIOUS_UNIQUE = "prevunique"; + + @JSProperty + IDBCursorSource getSource(); + + @JSProperty + String getDirection(); + + @JSProperty + JSObject getKey(); + + @JSProperty + JSObject getValue(); + + @JSProperty + JSObject getPrimaryKey(); + + IDBRequest update(JSObject value); + + void advance(int count); + + @JSMethod("continue") + void doContinue(); + + IDBRequest delete(); } diff --git a/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBCursorSource.java b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBCursorSource.java new file mode 100644 index 000000000..10cc7884b --- /dev/null +++ b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBCursorSource.java @@ -0,0 +1,10 @@ +package org.teavm.dom.indexeddb; + +import org.teavm.jso.JSObject; + +/** + * + * @author Alexey Andreev + */ +public interface IDBCursorSource extends JSObject { +} diff --git a/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBIndex.java b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBIndex.java index c2f459efe..379e9ad9f 100644 --- a/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBIndex.java +++ b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBIndex.java @@ -15,12 +15,49 @@ */ package org.teavm.dom.indexeddb; +import org.teavm.jso.JS; import org.teavm.jso.JSObject; +import org.teavm.jso.JSProperty; +import org.teavm.jso.JSStringArray; +import org.teavm.jso.JSType; /** * * @author Alexey Andreev */ -public interface IDBIndex extends JSObject { +public abstract class IDBIndex implements JSObject, IDBCursorSource { + @JSProperty + public abstract String getName(); + @JSProperty("keyPath") + abstract JSObject getKeyPathImpl(); + + public final String[] getKeyPath() { + JSObject result = getKeyPathImpl(); + if (JS.getType(result) == JSType.STRING) { + return new String[] { JS.unwrapString(result) }; + } else { + return JS.unwrapArray((JSStringArray)result); + } + } + + @JSProperty + public abstract boolean isMultiEntry(); + + @JSProperty + public abstract boolean isUnique(); + + public abstract IDBCursorRequest openCursor(); + + public abstract IDBCursorRequest openCursor(IDBKeyRange range); + + public abstract IDBCursorRequest openKeyCursor(); + + public abstract IDBGetRequest get(JSObject key); + + public abstract IDBGetRequest getKey(JSObject key); + + public abstract IDBCountRequest count(JSObject key); + + public abstract IDBCountRequest count(); } diff --git a/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBKeyRange.java b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBKeyRange.java new file mode 100644 index 000000000..cc835fb7b --- /dev/null +++ b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBKeyRange.java @@ -0,0 +1,48 @@ +package org.teavm.dom.indexeddb; + +import org.teavm.jso.JSBody; +import org.teavm.jso.JSObject; +import org.teavm.jso.JSProperty; + +/** + * + * @author Alexey Andreev + */ +public abstract class IDBKeyRange implements JSObject { + @JSProperty + public abstract JSObject getLower(); + + @JSProperty + public abstract JSObject getUpper(); + + @JSProperty + public abstract boolean isLowerOpen(); + + @JSProperty + public abstract boolean isUpperOpen(); + + @JSBody(params = "value", script = "return IDBKeyRange.only(value);") + public static native IDBKeyRange only(JSObject value); + + @JSBody(params = { "lower", "open" }, script = "return IDBKeyRange.lowerBound(lower, open);") + public static native IDBKeyRange lowerBound(JSObject lower, boolean open); + + public static IDBKeyRange lowerBound(JSObject lower) { + return lowerBound(lower, false); + } + + @JSBody(params = { "upper", "open" }, script = "return IDBKeyRange.upperBound(upper, open);") + public static native IDBKeyRange upperBound(JSObject upper, boolean open); + + public static IDBKeyRange upperBound(JSObject upper) { + return upperBound(upper, false); + } + + @JSBody(params = { "lower", "upper", "lowerOpen", "upperOpen" }, + script = "return IDBKeyRange.bound(lower, upper, lowerOpen, upperOpen);") + public static native IDBKeyRange bound(JSObject lower, JSObject upper, boolean lowerOpen, boolean upperOpen); + + public static IDBKeyRange bound(JSObject lower, JSObject upper) { + return bound(lower, upper, false, false); + } +} diff --git a/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBObjectStore.java b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBObjectStore.java index 4f5ae28be..6521b3613 100644 --- a/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBObjectStore.java +++ b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBObjectStore.java @@ -21,7 +21,7 @@ import org.teavm.jso.*; * * @author Alexey Andreev */ -public abstract class IDBObjectStore implements JSObject { +public abstract class IDBObjectStore implements JSObject, IDBCursorSource { @JSProperty public abstract String getName(); @@ -59,6 +59,8 @@ public abstract class IDBObjectStore implements JSObject { public abstract IDBCursorRequest openCursor(); + public abstract IDBCursorRequest openCursor(IDBKeyRange range); + public abstract IDBIndex createIndex(String name, String key); public abstract IDBIndex createIndex(String name, String[] keys); diff --git a/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBOpenDBRequest.java b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBOpenDBRequest.java index 0e737d210..6f0f4ad10 100644 --- a/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBOpenDBRequest.java +++ b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBOpenDBRequest.java @@ -23,6 +23,9 @@ import org.teavm.jso.JSProperty; * @author Alexey Andreev */ public interface IDBOpenDBRequest extends IDBRequest { + @JSProperty + IDBDatabase getResult(); + @JSProperty void setOnBlocked(EventHandler handler); diff --git a/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBTransaction.java b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBTransaction.java index 8f196f221..af8192adb 100644 --- a/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBTransaction.java +++ b/teavm-dom/src/main/java/org/teavm/dom/indexeddb/IDBTransaction.java @@ -15,12 +15,34 @@ */ package org.teavm.dom.indexeddb; +import org.teavm.dom.events.EventTarget; import org.teavm.jso.JSObject; +import org.teavm.jso.JSProperty; /** * * @author Alexey Andreev */ -public interface IDBTransaction extends JSObject { +public interface IDBTransaction extends JSObject, EventTarget { + @JSProperty + String getMode(); + @JSProperty + IDBDatabase getDb(); + + @JSProperty + IDBError getError(); + + IDBObjectStore objectStore(String name); + + void abort(); + + @JSProperty("onabort") + void setOnAbort(EventHandler handler); + + @JSProperty("oncomplete") + void setOnComplete(EventHandler handler); + + @JSProperty("onerror") + void setOnError(EventHandler handler); }