| 1 |
define(['dojo/_base/declare', 'dojo/Deferred', 'dojo/when', 'dojo/store/util/QueryResults', 'dojo/_base/lang', 'dojo/promise/all'], function(declare, Deferred, when, QueryResults, lang, all) { |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
var wildcardRe = /(.*)\*$/; |
| 7 |
function convertExtra(object){ |
| 8 |
|
| 9 |
return object && lang.mixin(object, JSON.parse(object.__extra)); |
| 10 |
} |
| 11 |
return declare([], { |
| 12 |
constructor: function(config){ |
| 13 |
var dbConfig = config.dbConfig; |
| 14 |
|
| 15 |
|
| 16 |
this.database = openDatabase(config.dbName || "dojo-db", '1.0', 'dojo-db', 4*1024*1024); |
| 17 |
var indexPrefix = this.indexPrefix = config.indexPrefix || "idx_"; |
| 18 |
var storeName = config.table || config.storeName; |
| 19 |
this.table = (config.table || config.storeName).replace(/[^\w]/g, '_'); |
| 20 |
var promises = []; |
| 21 |
|
| 22 |
this.indices = dbConfig.stores[storeName]; |
| 23 |
this.repeatingIndices = {}; |
| 24 |
for(var index in this.indices){ |
| 25 |
|
| 26 |
if(this.indices[index].multiEntry){ |
| 27 |
this.repeatingIndices[index] = true; |
| 28 |
} |
| 29 |
} |
| 30 |
if(!dbConfig.available){ |
| 31 |
|
| 32 |
for(var storeName in dbConfig.stores){ |
| 33 |
var storeConfig = dbConfig.stores[storeName]; |
| 34 |
var table = storeName.replace(/[^\w]/g, '_'); |
| 35 |
|
| 36 |
var idConfig = storeConfig[this.idProperty]; |
| 37 |
var indices = ['__extra', this.idProperty + ' ' + ((idConfig && idConfig.autoIncrement) ? 'INTEGER PRIMARY KEY AUTOINCREMENT' : 'PRIMARY KEY')]; |
| 38 |
var repeatingIndices = [this.idProperty]; |
| 39 |
for(var index in storeConfig){ |
| 40 |
if(index != this.idProperty){ |
| 41 |
indices.push(index); |
| 42 |
} |
| 43 |
} |
| 44 |
promises.push(this.executeSql("CREATE TABLE IF NOT EXISTS " + table+ ' (' |
| 45 |
+ indices.join(',') + |
| 46 |
')')); |
| 47 |
for(var index in storeConfig){ |
| 48 |
if(index != this.idProperty){ |
| 49 |
if(storeConfig[index].multiEntry){ |
| 50 |
|
| 51 |
|
| 52 |
repeatingIndices.push(index); |
| 53 |
var repeatingTable = table+ '_repeating_' + index; |
| 54 |
promises.push(this.executeSql("CREATE TABLE IF NOT EXISTS " + repeatingTable + ' (id,value)')); |
| 55 |
promises.push(this.executeSql("CREATE INDEX IF NOT EXISTS idx_" + repeatingTable + '_id ON ' + repeatingTable + '(id)')); |
| 56 |
promises.push(this.executeSql("CREATE INDEX IF NOT EXISTS idx_" + repeatingTable + '_value ON ' + repeatingTable + '(value)')); |
| 57 |
}else{ |
| 58 |
promises.push(this.executeSql("ALTER TABLE " + table + ' ADD ' + index).then(null, function(){ |
| 59 |
|
| 60 |
})); |
| 61 |
|
| 62 |
if(storeConfig[index].indexed !== false){ |
| 63 |
promises.push(this.executeSql("CREATE INDEX IF NOT EXISTS " + indexPrefix + |
| 64 |
table + '_' + index + ' ON ' + table + '(' + index + ')')); |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
dbConfig.available = all(promises); |
| 71 |
} |
| 72 |
this.available = dbConfig.available; |
| 73 |
}, |
| 74 |
idProperty: "id", |
| 75 |
selectColumns: ["*"], |
| 76 |
get: function(id){ |
| 77 |
|
| 78 |
return when(this.executeSql("SELECT " + this.selectColumns.join(",") + " FROM " + this.table + " WHERE " + this.idProperty + "=?", [id]), function(result){ |
| 79 |
return result.rows.length > 0 ? convertExtra(result.rows.item(0)) : undefined; |
| 80 |
}); |
| 81 |
}, |
| 82 |
getIdentity: function(object){ |
| 83 |
return object[this.idProperty]; |
| 84 |
}, |
| 85 |
remove: function(id){ |
| 86 |
return this.executeSql("DELETE FROM " + this.table + " WHERE " + this.idProperty + "=?", [id]); |
| 87 |
|
| 88 |
}, |
| 89 |
identifyGeneratedKey: true, |
| 90 |
add: function(object, directives){ |
| 91 |
|
| 92 |
var params = [], vals = [], cols = []; |
| 93 |
var extra = {}; |
| 94 |
var actionsWithId = []; |
| 95 |
var store = this; |
| 96 |
for(var i in object){ |
| 97 |
if(object.hasOwnProperty(i)){ |
| 98 |
if(i in this.indices || i == this.idProperty){ |
| 99 |
if(this.repeatingIndices[i]){ |
| 100 |
|
| 101 |
|
| 102 |
actionsWithId.push(function(id){ |
| 103 |
var array = object[i]; |
| 104 |
return all(array.map(function(value){ |
| 105 |
return store.executeSql('INSERT INTO ' + store.table + '_repeating_' + i + ' (value, id) VALUES (?, ?)', [value, id]); |
| 106 |
})); |
| 107 |
}); |
| 108 |
}else{ |
| 109 |
|
| 110 |
cols.push(i); |
| 111 |
vals.push('?'); |
| 112 |
params.push(object[i]); |
| 113 |
} |
| 114 |
}else{ |
| 115 |
extra[i] = object[i]; |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
cols.push('__extra'); |
| 121 |
vals.push('?'); |
| 122 |
params.push(JSON.stringify(extra)); |
| 123 |
|
| 124 |
var idColumn = this.idProperty; |
| 125 |
if(this.identifyGeneratedKey){ |
| 126 |
params.idColumn = idColumn; |
| 127 |
} |
| 128 |
var sql = "INSERT INTO " + this.table + " (" + cols.join(',') + ") VALUES (" + vals.join(',') + ")"; |
| 129 |
return when(this.executeSql(sql, params), function(results) { |
| 130 |
var id = results.insertId; |
| 131 |
object[idColumn] = id; |
| 132 |
|
| 133 |
return all(actionsWithId.map(function(func){ |
| 134 |
return func(id); |
| 135 |
})).then(function(){ |
| 136 |
return id; |
| 137 |
}); |
| 138 |
}); |
| 139 |
}, |
| 140 |
put: function(object, directives){ |
| 141 |
|
| 142 |
directives = directives || {}; |
| 143 |
var id = directives.id || object[this.idProperty]; |
| 144 |
var overwrite = directives.overwrite; |
| 145 |
if(overwrite === undefined){ |
| 146 |
|
| 147 |
var store = this; |
| 148 |
return this.get(id).then(function(previous){ |
| 149 |
if((directives.overwrite = !!previous)){ |
| 150 |
directives.overwrite = true; |
| 151 |
return store.put(object, directives); |
| 152 |
}else{ |
| 153 |
return store.add(object, directives); |
| 154 |
} |
| 155 |
}); |
| 156 |
} |
| 157 |
if(!overwrite){ |
| 158 |
return store.add(object, directives); |
| 159 |
} |
| 160 |
var sql = "UPDATE " + this.table + " SET "; |
| 161 |
var params = []; |
| 162 |
var cols = []; |
| 163 |
var extra = {}; |
| 164 |
var promises = []; |
| 165 |
for(var i in object){ |
| 166 |
if(object.hasOwnProperty(i)){ |
| 167 |
if(i in this.indices || i == this.idProperty){ |
| 168 |
if(this.repeatingIndices[i]){ |
| 169 |
|
| 170 |
this.executeSql('DELETE FROM ' + this.table + '_repeating_' + i + ' WHERE id=?', [id]); |
| 171 |
var array = object[i]; |
| 172 |
for(var j = 0; j < array.length; j++){ |
| 173 |
this.executeSql('INSERT INTO ' + this.table + '_repeating_' + i + ' (value, id) VALUES (?, ?)', [array[j], id]); |
| 174 |
} |
| 175 |
}else{ |
| 176 |
cols.push(i + "=?"); |
| 177 |
params.push(object[i]); |
| 178 |
} |
| 179 |
}else{ |
| 180 |
extra[i] = object[i]; |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
cols.push("__extra=?"); |
| 185 |
params.push(JSON.stringify(extra)); |
| 186 |
|
| 187 |
sql += cols.join(',') + " WHERE " + this.idProperty + "=?"; |
| 188 |
params.push(object[this.idProperty]); |
| 189 |
|
| 190 |
return when(this.executeSql(sql, params), function(result){ |
| 191 |
return id; |
| 192 |
}); |
| 193 |
}, |
| 194 |
query: function(query, options){ |
| 195 |
options = options || {}; |
| 196 |
var from = 'FROM ' + this.table; |
| 197 |
var condition; |
| 198 |
var addedWhere; |
| 199 |
var store = this; |
| 200 |
var table = this.table; |
| 201 |
var params = []; |
| 202 |
if(query.forEach){ |
| 203 |
|
| 204 |
condition = query.map(processObjectQuery).join(') OR ('); |
| 205 |
if(condition){ |
| 206 |
condition = '(' + condition + ')'; |
| 207 |
} |
| 208 |
}else{ |
| 209 |
|
| 210 |
condition = processObjectQuery(query); |
| 211 |
} |
| 212 |
if(condition){ |
| 213 |
condition = ' WHERE ' + condition; |
| 214 |
} |
| 215 |
function processObjectQuery(query){ |
| 216 |
|
| 217 |
var conditions = []; |
| 218 |
for(var i in query){ |
| 219 |
var filterValue = query[i]; |
| 220 |
var convertWildcard = function convertWildcard(value){ |
| 221 |
|
| 222 |
var wildcard = value && value.match && value.match(wildcardRe); |
| 223 |
if(wildcard){ |
| 224 |
params.push(wildcard[1] + '%'); |
| 225 |
return ' LIKE ?'; |
| 226 |
} |
| 227 |
params.push(value); |
| 228 |
return '=?'; |
| 229 |
}; |
| 230 |
if(filterValue){ |
| 231 |
if(filterValue.contains){ |
| 232 |
|
| 233 |
var repeatingTable = store.table + '_repeating_' + i; |
| 234 |
conditions.push(filterValue.contains.map(function(value){ |
| 235 |
return store.idProperty + ' IN (SELECT id FROM ' + repeatingTable + ' WHERE ' + |
| 236 |
'value' + convertWildcard(value) + ')'; |
| 237 |
}).join(' AND ')); |
| 238 |
continue; |
| 239 |
}else if(typeof filterValue == 'object' && ("from" in filterValue || "to" in filterValue)){ |
| 240 |
|
| 241 |
var fromComparator = filterValue.excludeFrom ? '>' : '>='; |
| 242 |
var toComparator = filterValue.excludeTo ? '<' : '<='; |
| 243 |
if('from' in filterValue){ |
| 244 |
params.push(filterValue.from); |
| 245 |
if('to' in filterValue){ |
| 246 |
params.push(filterValue.to); |
| 247 |
conditions.push('(' + table + '.' + i + fromComparator + '? AND ' + |
| 248 |
table + '.' + i + toComparator + '?)'); |
| 249 |
}else{ |
| 250 |
conditions.push(table + '.' + i + fromComparator + '?'); |
| 251 |
} |
| 252 |
}else{ |
| 253 |
params.push(filterValue.to); |
| 254 |
conditions.push(table + '.' + i + toComparator + '?'); |
| 255 |
} |
| 256 |
continue; |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
conditions.push(table + '.' + i + convertWildcard(filterValue)); |
| 261 |
} |
| 262 |
return conditions.join(' AND '); |
| 263 |
} |
| 264 |
|
| 265 |
if(options.sort){ |
| 266 |
condition += ' ORDER BY ' + |
| 267 |
options.sort.map(function(sort){ |
| 268 |
return table + '.' + sort.attribute + ' ' + (sort.descending ? 'desc' : 'asc'); |
| 269 |
}); |
| 270 |
} |
| 271 |
|
| 272 |
var limitedCondition = condition; |
| 273 |
if(options.count){ |
| 274 |
limitedCondition += " LIMIT " + options.count; |
| 275 |
} |
| 276 |
if(options.start){ |
| 277 |
limitedCondition += " OFFSET " + options.start; |
| 278 |
} |
| 279 |
var results = lang.delegate(this.executeSql('SELECT * ' + from + limitedCondition, params).then(function(sqlResults){ |
| 280 |
|
| 281 |
var results = []; |
| 282 |
for(var i = 0; i < sqlResults.rows.length; i++){ |
| 283 |
results.push(convertExtra(sqlResults.rows.item(i))); |
| 284 |
} |
| 285 |
return results; |
| 286 |
})); |
| 287 |
var store = this; |
| 288 |
results.total = { |
| 289 |
then: function(callback,errback){ |
| 290 |
|
| 291 |
return store.executeSql('SELECT COUNT(*) ' + from + condition, params).then(function(sqlResults){ |
| 292 |
return sqlResults.rows.item(0)['COUNT(*)']; |
| 293 |
}).then(callback,errback); |
| 294 |
} |
| 295 |
}; |
| 296 |
return new QueryResults(results); |
| 297 |
}, |
| 298 |
|
| 299 |
executeSql: function(sql, parameters){ |
| 300 |
|
| 301 |
var deferred = new Deferred(); |
| 302 |
var result, error; |
| 303 |
this.database.transaction(function(transaction){ |
| 304 |
transaction.executeSql(sql, parameters, function(transaction, value){ |
| 305 |
deferred.resolve(result = value); |
| 306 |
}, function(transaction, e){ |
| 307 |
deferred.reject(error = e); |
| 308 |
}); |
| 309 |
}); |
| 310 |
|
| 311 |
if(result){ |
| 312 |
return result; |
| 313 |
} |
| 314 |
if(error){ |
| 315 |
throw error; |
| 316 |
} |
| 317 |
return deferred.promise; |
| 318 |
} |
| 319 |
|
| 320 |
}); |
| 321 |
}); |