/* * DB connection and schema management class */ Scholar.DB = new function(){ // Private members var _connection; var _transactionRollback; var _transactionNestingLevel = 0; // Privileged methods this.query = query; this.valueQuery = valueQuery; this.rowQuery = rowQuery; this.columnQuery = columnQuery; this.statementQuery = statementQuery; this.getColumns = getColumns; this.getColumnHash = getColumnHash; this.beginTransaction = beginTransaction; this.commitTransaction = commitTransaction; this.rollbackTransaction = rollbackTransaction; this.transactionInProgress = transactionInProgress; this.tableExists = tableExists; ///////////////////////////////////////////////////////////////// // // Privileged methods // ///////////////////////////////////////////////////////////////// /* * Run an SQL query * * Optional _params_ is an array of bind parameters in the form * [1,"hello",3] or [{'int':2},{'string':'foobar'}] * * Returns: * - Associative array (similar to mysql_fetch_assoc) for SELECT's * - lastInsertId for INSERT's * - TRUE for other successful queries * - FALSE on error */ function query(sql,params){ var db = _getDBConnection(); try { // Parse out the SQL command being used var op = sql.match(/^[^a-z]*[^ ]+/i); if (op){ op = op.toString().toLowerCase(); } // If SELECT statement, return result if (op=='select'){ // Until the native dataset methods work (or at least exist), // we build a multi-dimensional associative array manually var statement = statementQuery(sql,params); var dataset = new Array(); while (statement.executeStep()){ var row = new Array(); for(var i=0; i file.append(SCHOLAR_CONFIG['DB_FILE']); _connection = store.openDatabase(file); return _connection; } function _getTypedValue(statement, i){ var type = statement.getTypeOfIndex(i); switch (type){ case statement.VALUE_TYPE_INTEGER: var func = statement.getInt32; break; case statement.VALUE_TYPE_TEXT: var func = statement.getUTF8String; break; case statement.VALUE_TYPE_NULL: return null; case statement.VALUE_TYPE_FLOAT: var func = statement.getDouble; break; case statement.VALUE_TYPE_BLOB: var func = statement.getBlob; break; } return func(i); } }