Permalink
Browse files

query execution fixes for Titanium

adding titanium test

extract titanium code to persistence.store.titanium.js and added a type mapper to properly support BIGINT on titanium

cleaning up titanium test app
  • Loading branch information...
1 parent a45b84b commit d1d16e074d9be1b30aea4cb0fa6e247de9f41dcf @staugaard staugaard committed with zefhemel Feb 5, 2011
View
@@ -0,0 +1 @@
+test/titanium/build
@@ -0,0 +1,191 @@
+try {
+ if(!window) {
+ window = {};
+ //exports.console = console;
+ }
+} catch(e) {
+ window = {};
+ exports.console = console;
+}
+
+var persistence = (window && window.persistence) ? window.persistence : {};
+
+if(!persistence.store) {
+ persistence.store = {};
+}
+
+persistence.store.titanium = {};
+
+persistence.store.titanium.config = function(persistence, dbname) {
+ var conn = null;
+
+ /**
+ * Create a transaction
+ *
+ * @param callback,
+ * the callback function to be invoked when the transaction
+ * starts, taking the transaction object as argument
+ */
+ persistence.transaction = function (callback) {
+ if(!conn) {
+ throw new Error("No ongoing database connection, please connect first.");
+ } else {
+ conn.transaction(callback);
+ }
+ };
+
+ ////////// Low-level database interface, abstracting from HTML5 and Gears databases \\\\
+ persistence.db = persistence.db || {};
+
+ persistence.db.conn = null;
+
+ persistence.db.titanium = {};
+
+ persistence.db.titanium.connect = function (dbname) {
+ var that = {};
+ var conn = Titanium.Database.open(dbname);
+
+ that.transaction = function (fn) {
+ fn(persistence.db.titanium.transaction(conn));
+ };
+ return that;
+ };
+
+ persistence.db.titanium.transaction = function (conn) {
+ var that = {};
+ that.executeSql = function (query, args, successFn, errorFn) {
+
+ if(persistence.debug) {
+ console.log(query, args);
+ }
+ try {
+ var executeVarArgs = [query];
+ if (args) {
+ executeVarArgs = executeVarArgs.concat(args);
+ };
+ var rs = Function.apply.call(conn.execute, conn, executeVarArgs);
+ if (successFn) {
+ var results = [];
+ if (rs) {
+ while (rs.isValidRow()) {
+ var result = {};
+ for ( var i = 0; i < rs.fieldCount(); i++) {
+ result[rs.fieldName(i)] = rs.field(i);
+ }
+ results.push(result);
+ rs.next();
+ }
+ rs.close();
+ };
+ successFn(results);
+ }
+ } catch(e) {
+ if (errorFn) {
+ errorFn(null, e);
+ };
+ }
+ };
+ return that;
+ };
+
+ ///////////////////////// SQLite dialect
+
+ persistence.store.titanium.sqliteDialect = {
+ // columns is an array of arrays, e.g.
+ // [["id", "VARCHAR(32)", "PRIMARY KEY"], ["name", "TEXT"]]
+ createTable: function(tableName, columns) {
+ var tm = persistence.typeMapper;
+ var sql = "CREATE TABLE IF NOT EXISTS `" + tableName + "` (";
+ var defs = [];
+ for(var i = 0; i < columns.length; i++) {
+ var column = columns[i];
+ defs.push("`" + column[0] + "` " + tm.columnType(column[1]) + (column[2] ? " " + column[2] : ""));
+ }
+ sql += defs.join(", ");
+ sql += ')';
+ return sql;
+ },
+
+ // columns is array of column names, e.g.
+ // ["id"]
+ createIndex: function(tableName, columns, options) {
+ options = options || {};
+ return "CREATE "+(options.unique?"UNIQUE ":"")+"INDEX IF NOT EXISTS `" + tableName + "__" + columns.join("_") +
+ "` ON `" + tableName + "` (" +
+ columns.map(function(col) { return "`" + col + "`"; }).join(", ") + ")";
+ },
+
+ typeMapper: {
+ idType: persistence.store.sql.defaultTypeMapper.idType,
+ classNameType: persistence.store.sql.defaultTypeMapper.classNameType,
+ inVar: persistence.store.sql.defaultTypeMapper.inVar,
+ outVar: persistence.store.sql.defaultTypeMapper.outVar,
+ outId: persistence.store.sql.defaultTypeMapper.outId,
+ inIdVar: persistence.store.sql.defaultTypeMapper.inIdVar,
+ outIdVar: persistence.store.sql.defaultTypeMapper.outIdVar,
+ entityIdToDbId: persistence.store.sql.defaultTypeMapper.entityIdToDbId,
+ zeroPaddingMap: ['0000000000000000',
+ '000000000000000',
+ '00000000000000',
+ '0000000000000',
+ '000000000000',
+ '00000000000',
+ '0000000000',
+ '000000000',
+ '00000000',
+ '0000000',
+ '000000',
+ '00000',
+ '0000',
+ '000',
+ '00',
+ '0'],
+ zeroPadded: function(val) {
+ var result = val.toString();
+ if (result.length < 16) {
+ return persistence.store.titanium.sqliteDialect.typeMapper.zeroPaddingMap[result.length] + result;
+ } else {
+ return result;
+ };
+ },
+ columnType: function(type) {
+ if (type === 'BIGINT') {
+ return 'TEXT';
+ } else {
+ return persistence.store.sql.defaultTypeMapper.columnType(type);
+ };
+ },
+ dbValToEntityVal: function(val, type){
+ if (val === null || val === undefined) {
+ return val;
+ } else if (type === 'BIGIN') {
+ return parseInt(val);
+ } else {
+ return persistence.store.sql.defaultTypeMapper.dbValToEntityVal(val, type);
+ }
+ },
+ entityValToDbVal: function(val, type){
+ if (val === undefined || val === null) {
+ return null;
+ } else if (type === 'BIGINT') {
+ return persistence.store.titanium.sqliteDialect.typeMapper.zeroPadded(val);
+ } else {
+ return persistence.store.sql.defaultTypeMapper.entityValToDbVal(val, type);
+ };
+ }
+ }
+ };
+
+ // Configure persistence for generic sql persistence, using sqliteDialect
+ persistence.store.sql.config(persistence, persistence.store.titanium.sqliteDialect);
+
+ // Make the connection
+ conn = persistence.db.titanium.connect(dbname);
+ if(!conn) {
+ throw new Error("No supported database found");
+ }
+};
+
+try {
+ exports.persistence = persistence;
+} catch(e) {}
@@ -42,9 +42,7 @@ persistence.store.websql.config = function(persistence, dbname, description, siz
persistence.db.conn = null;
// window object does not exist on Qt Declarative UI (http://doc.trolltech.org/4.7-snapshot/declarativeui.html)
- if (typeof Titanium !== 'undefined') {
- persistence.db.implementation = "titanium";
- } else if (window && window.openDatabase) {
+ if (window && window.openDatabase) {
persistence.db.implementation = "html5";
} else if (window && window.google && google.gears) {
persistence.db.implementation = "gears";
@@ -165,62 +163,13 @@ persistence.store.websql.config = function(persistence, dbname, description, siz
return that;
};
- persistence.db.titanium = {};
-
- persistence.db.titanium.connect = function (dbname) {
- var that = {};
- var conn = Titanium.Database.open(dbname);
-
- that.transaction = function (fn) {
- fn(persistence.db.titanium.transaction(conn));
- };
- return that;
- };
-
- persistence.db.titanium.transaction = function (conn) {
- var that = {};
- that.executeSql = function (query, args, successFn, errorFn) {
-
- if(persistence.debug) {
- console.log(query, args);
- }
- try {
- var executeVarArgs = [query];
- if (args) {
- executeVarArgs = executeVarArgs.concat(args);
- };
- var rs = Function.apply.call(conn.execute, conn, executeVarArgs);
- if (rs && successFn) {
- var results = [];
- while (rs.isValidRow()) {
- var result = {};
- for ( var i = 0; i < rs.fieldCount(); i++) {
- result[rs.fieldName(i)] = rs.field(i);
- }
- results.push(result);
- rs.next();
- }
- rs.close();
- successFn(results);
- }
- } catch(e) {
- if (errorFn) {
- errorFn(null, e);
- };
- }
- };
- return that;
- };
-
persistence.db.connect = function (dbname, description, size) {
if (persistence.db.implementation == "html5") {
return persistence.db.html5.connect(dbname, description, size);
} else if (persistence.db.implementation == "html5-sync") {
return persistence.db.html5Sync.connect(dbname, description, size);
} else if (persistence.db.implementation == "gears") {
return persistence.db.gears.connect(dbname);
- } else if (persistence.db.implementation == "titanium") {
- return persistence.db.titanium.connect(dbname);
}
};
@@ -15,6 +15,7 @@ $(document).ready(function(){
done: "BOOL",
counter: "INT",
dateAdded: "DATE",
+ dateAddedInMillis: "BIGINT",
metaData: "JSON"
});
@@ -64,45 +65,50 @@ $(document).ready(function(){
}
});
- test("Property default values", 5, function() {
+ test("Property default values", 6, function() {
var t1 = new Task();
QUnit.strictEqual(t1.name, "", "TEXT properties default to ''");
QUnit.strictEqual(t1.done, false, "BOOL properties default to false");
QUnit.strictEqual(t1.counter, 0, "INT properties default to 0");
QUnit.strictEqual(t1.dateAdded, null, "DATE properties default to null");
+ QUnit.strictEqual(t1.dateAddedInMillis, 0, "BIGINT properties default to 0");
QUnit.strictEqual(t1.metaData, null, "JSON properties default to null");
});
- test("Property value assignment", 5, function() {
+ test("Property value assignment", 6, function() {
var t1 = new Task();
var now = new Date();
var meta = {starRating: 5};
t1.name = "Task 1";
t1.done = false;
t1.counter = 7;
t1.dateAdded = now;
+ t1.dateAddedInMillis = now.getTime();
t1.metaData = meta;
QUnit.strictEqual(t1.name, 'Task 1', "Assignment for TEXT properties");
QUnit.strictEqual(t1.done, false, "Assignment for BOOL properties");
QUnit.strictEqual(t1.counter, 7, "Assignment for INT properties");
QUnit.strictEqual(t1.dateAdded, now, "Assignment for DATE properties");
+ QUnit.strictEqual(t1.dateAddedInMillis, now.getTime(), "Assignment for BIGINT properties");
QUnit.strictEqual(t1.metaData, meta, "Assignment for JSON properties");
});
- test("Property constructor property value assignment", 5, function() {
+ test("Property constructor property value assignment", 6, function() {
var now = new Date();
var meta = {starRating: 5};
var t1 = new Task({
name: "Task 1",
done: false,
counter: 7,
dateAdded: now,
+ dateAddedInMillis: now.getTime(),
metaData: meta
});
QUnit.strictEqual(t1.name, 'Task 1', "Assignment for TEXT properties");
QUnit.strictEqual(t1.done, false, "Assignment for BOOL properties");
QUnit.strictEqual(t1.counter, 7, "Assignment for INT properties");
QUnit.strictEqual(t1.dateAdded, now, "Assignment for DATE properties");
+ QUnit.strictEqual(t1.dateAddedInMillis, now.getTime(), "Assignment for BIGINT properties");
QUnit.strictEqual(t1.metaData, meta, "Assignment for JSON properties");
});
@@ -117,6 +123,7 @@ $(document).ready(function(){
equals(t1db.done, false, "BOOL properties default to false");
equals(t1db.counter, 0, "INT properties default to 0");
equals(t1db.dateAdded, null, "DATE properties default to null");
+ equals(t1db.dateAddedInMillis, 0, "BIGINT properties default to 0");
equals(t1db.metaData, null, "JSON properties default to null");
start();
});
@@ -131,16 +138,18 @@ $(document).ready(function(){
done: false,
counter: 7,
dateAdded: now,
+ dateAddedInMillis: 1296802544867,
metaData: meta
});
persistence.add(t1);
persistence.flush(function() {
- //persistence.clean();
+ persistence.clean();
Task.all().one(function(t1db) {
equals(t1db.name, 'Task 1', "Persistence of TEXT properties");
equals(t1db.done, false, "Persistence of BOOL properties");
equals(t1db.counter, 7, "Persistence of INT properties");
equals(Math.round(t1db.dateAdded.getTime()/1000)*1000, Math.round(now.getTime()/1000)*1000, "Persistence of DATE properties");
+ equals(t1db.dateAddedInMillis, 1296802544867, "Persistence of BIGINT properties");
same(t1db.metaData, meta, "Persistence of JSON properties");
start();
});
View
@@ -0,0 +1 @@
+tmp
@@ -0,0 +1,5 @@
+var win = Titanium.UI.createWindow({
+ url:'runner.js',
+ title: 'Unit Test'
+});
+win.open();
Oops, something went wrong.

0 comments on commit d1d16e0

Please sign in to comment.