- DB parameters can now be bound using the native JS type rather than by specifying the type explicitly (e.g. Scholar.DB.query(sql, [1, 2, "hello"]) -- for use when the data is generated internally and trusted, obviously
- Don't try to display an SQLite error when it's "not an error" (i.e. when the error is in something else) - Switch to nsIFile instead of nsILocalFile to retrieve the profile directory
This commit is contained in:
parent
d239e6e4ba
commit
a3df0c39e2
|
@ -31,7 +31,7 @@ Scholar.DB = new function(){
|
||||||
* Run an SQL query
|
* Run an SQL query
|
||||||
*
|
*
|
||||||
* Optional _params_ is an array of bind parameters in the form
|
* Optional _params_ is an array of bind parameters in the form
|
||||||
* [{'int':2},{'string':'foobar'}]
|
* [1,"hello",3] or [{'int':2},{'string':'foobar'}]
|
||||||
*
|
*
|
||||||
* Returns:
|
* Returns:
|
||||||
* - Associative array (similar to mysql_fetch_assoc) for SELECT's
|
* - Associative array (similar to mysql_fetch_assoc) for SELECT's
|
||||||
|
@ -89,7 +89,9 @@ Scholar.DB = new function(){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (e){
|
catch (e){
|
||||||
throw(e + ' [QUERY: ' + sql + '] [ERROR: ' + db.lastErrorString + ']');
|
var dberr = (db.lastErrorString!='not an error')
|
||||||
|
? ' [ERROR: ' + db.lastErrorString + ']' : '';
|
||||||
|
throw(e + ' [QUERY: ' + sql + ']' + dberr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -151,7 +153,7 @@ Scholar.DB = new function(){
|
||||||
* Run a query, returning a mozIStorageStatement for direct manipulation
|
* Run a query, returning a mozIStorageStatement for direct manipulation
|
||||||
*
|
*
|
||||||
* Optional _params_ is an array of bind parameters in the form
|
* Optional _params_ is an array of bind parameters in the form
|
||||||
* [{'int':2},{'string':'foobar'}]
|
* [1,"hello",3] or [{'int':2},{'string':'foobar'}]
|
||||||
*/
|
*/
|
||||||
function statementQuery(sql,params){
|
function statementQuery(sql,params){
|
||||||
var db = _getDBConnection();
|
var db = _getDBConnection();
|
||||||
|
@ -161,26 +163,66 @@ Scholar.DB = new function(){
|
||||||
var statement = db.createStatement(sql);
|
var statement = db.createStatement(sql);
|
||||||
}
|
}
|
||||||
catch (e){
|
catch (e){
|
||||||
throw('[QUERY: ' + sql + '] [ERROR: ' + db.lastErrorString + ']');
|
var dberr = (db.lastErrorString!='not an error')
|
||||||
|
? ' [ERROR: ' + db.lastErrorString + ']' : '';
|
||||||
|
throw(e + ' [QUERY: ' + sql + ']' + dberr);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (statement && params){
|
if (statement && params){
|
||||||
for (var i=0; i<params.length; i++){
|
for (var i=0; i<params.length; i++){
|
||||||
// Int
|
// Integer
|
||||||
if (typeof params[i]['int'] != 'undefined'){
|
if (typeof params[i]['int'] != 'undefined'){
|
||||||
Scholar.debug('Binding parameter ' + (i+1) + ' of type int: ' +
|
var type = 'int';
|
||||||
params[i]['int'],5);
|
var value = params[i]['int'];
|
||||||
statement.bindInt32Parameter(i,params[i]['int']);
|
|
||||||
}
|
}
|
||||||
// String
|
// String
|
||||||
else if (typeof params[i]['string'] != 'undefined'){
|
else if (typeof params[i]['string'] != 'undefined'){
|
||||||
Scholar.debug('Binding parameter ' + (i+1) + ' of type string: "' +
|
var type = 'string';
|
||||||
params[i]['string'] + '"',5);
|
var value = params[i]['string'];
|
||||||
statement.bindUTF8StringParameter(i,params[i]['string']);
|
|
||||||
}
|
}
|
||||||
// Null
|
// Null
|
||||||
else if (typeof params[i]['null'] != 'undefined'){
|
else if (typeof params[i]['null'] != 'undefined'){
|
||||||
Scholar.debug('Binding parameter ' + (i+1) + ' of type NULL', 5);
|
var type = 'null';
|
||||||
|
}
|
||||||
|
// Automatic (trust the JS type)
|
||||||
|
else {
|
||||||
|
switch (typeof params[i]){
|
||||||
|
case 'string':
|
||||||
|
var type = 'string';
|
||||||
|
break;
|
||||||
|
case 'number':
|
||||||
|
var type = 'int';
|
||||||
|
break;
|
||||||
|
// Object
|
||||||
|
default:
|
||||||
|
// Null value will show as object
|
||||||
|
if (!params[i]){
|
||||||
|
var type = 'null';
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw('Invalid bound parameter ' + params[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var value = params[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bind the parameter as the correct type
|
||||||
|
switch (type){
|
||||||
|
case 'int':
|
||||||
|
Scholar.debug('Binding parameter ' + (i+1)
|
||||||
|
+ ' of type int: ' + value, 5);
|
||||||
|
statement.bindInt32Parameter(i, value);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'string':
|
||||||
|
Scholar.debug('Binding parameter ' + (i+1)
|
||||||
|
+ ' of type string: "' + value + '"', 5);
|
||||||
|
statement.bindUTF8StringParameter(i, value);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'null':
|
||||||
|
Scholar.debug('Binding parameter ' + (i+1)
|
||||||
|
+ ' of type NULL', 5);
|
||||||
statement.bindNullParameter(i);
|
statement.bindNullParameter(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -221,7 +263,9 @@ Scholar.DB = new function(){
|
||||||
db.commitTransaction();
|
db.commitTransaction();
|
||||||
}
|
}
|
||||||
catch(e){
|
catch(e){
|
||||||
throw(e + ' [ERROR: ' + db.lastErrorString + ']');
|
var dberr = (db.lastErrorString!='not an error')
|
||||||
|
? ' [ERROR: ' + db.lastErrorString + ']' : '';
|
||||||
|
throw(e + ' [QUERY: ' + sql + ']' + dberr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -241,7 +285,9 @@ Scholar.DB = new function(){
|
||||||
db.rollbackTransaction();
|
db.rollbackTransaction();
|
||||||
}
|
}
|
||||||
catch(e){
|
catch(e){
|
||||||
throw(e + ' [ERROR: ' + db.lastErrorString + ']');
|
var dberr = (db.lastErrorString!='not an error')
|
||||||
|
? ' [ERROR: ' + db.lastErrorString + ']' : '';
|
||||||
|
throw(e + ' [QUERY: ' + sql + ']' + dberr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -312,7 +358,7 @@ Scholar.DB = new function(){
|
||||||
// Get the profile directory
|
// Get the profile directory
|
||||||
var file = Components.classes["@mozilla.org/file/directory_service;1"]
|
var file = Components.classes["@mozilla.org/file/directory_service;1"]
|
||||||
.getService(Components.interfaces.nsIProperties)
|
.getService(Components.interfaces.nsIProperties)
|
||||||
.get("ProfD", Components.interfaces.nsILocalFile);
|
.get("ProfD", Components.interfaces.nsIFile);
|
||||||
|
|
||||||
// This makes file point to PROFILE_DIR/<scholar database file>
|
// This makes file point to PROFILE_DIR/<scholar database file>
|
||||||
file.append(SCHOLAR_CONFIG['DB_FILE']);
|
file.append(SCHOLAR_CONFIG['DB_FILE']);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user