📄 common.js
字号:
if(!dojo._hasResource["dojox._sql.common"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.dojo._hasResource["dojox._sql.common"] = true;dojo.provide("dojox._sql.common");dojo.require("dojox._sql._crypto");// summary:// Executes a SQL expression.// description:// There are four ways to call this:// 1) Straight SQL: dojox.sql("SELECT * FROM FOOBAR");// 2) SQL with parameters: dojox.sql("INSERT INTO FOOBAR VALUES (?)", someParam)// 3) Encrypting particular values: // dojox.sql("INSERT INTO FOOBAR VALUES (ENCRYPT(?))", someParam, "somePassword", callback)// 4) Decrypting particular values:// dojox.sql("SELECT DECRYPT(SOMECOL1), DECRYPT(SOMECOL2) FROM// FOOBAR WHERE SOMECOL3 = ?", someParam,// "somePassword", callback)//// For encryption and decryption the last two values should be the the password for// encryption/decryption, and the callback function that gets the result set.//// Note: We only support ENCRYPT(?) statements, and// and DECRYPT(*) statements for now -- you can not have a literal string// inside of these, such as ENCRYPT('foobar')//// Note: If you have multiple columns to encrypt and decrypt, you can use the following// convenience form to not have to type ENCRYPT(?)/DECRYPT(*) many times://// dojox.sql("INSERT INTO FOOBAR VALUES (ENCRYPT(?, ?, ?))", // someParam1, someParam2, someParam3, // "somePassword", callback)//// dojox.sql("SELECT DECRYPT(SOMECOL1, SOMECOL2) FROM// FOOBAR WHERE SOMECOL3 = ?", someParam,// "somePassword", callback)dojox.sql = new Function("return dojox.sql._exec(arguments);");dojo.mixin(dojox.sql, { dbName: null, // summary: // If true, then we print out any SQL that is executed // to the debug window debug: (dojo.exists("dojox.sql.debug")?dojox.sql.debug:false), open: function(dbName){ if(this._dbOpen && (!dbName || dbName == this.dbName)){ return; } if(!this.dbName){ this.dbName = "dot_store_" + window.location.href.replace(/[^0-9A-Za-z_]/g, "_"); // database names in Gears are limited to 64 characters long if(this.dbName.length > 63){ this.dbName = this.dbName.substring(0, 63); } } if(!dbName){ dbName = this.dbName; } try{ this._initDb(); this.db.open(dbName); this._dbOpen = true; }catch(exp){ throw exp.message||exp; } }, close: function(dbName){ // on Internet Explorer, Google Gears throws an exception // "Object not a collection", when we try to close the // database -- just don't close it on this platform // since we are running into a Gears bug; the Gears team // said it's ok to not close a database connection if(dojo.isIE){ return; } if(!this._dbOpen && (!dbName || dbName == this.dbName)){ return; } if(!dbName){ dbName = this.dbName; } try{ this.db.close(dbName); this._dbOpen = false; }catch(exp){ throw exp.message||exp; } }, _exec: function(params){ try{ // get the Gears Database object this._initDb(); // see if we need to open the db; if programmer // manually called dojox.sql.open() let them handle // it; otherwise we open and close automatically on // each SQL execution if(!this._dbOpen){ this.open(); this._autoClose = true; } // determine our parameters var sql = null; var callback = null; var password = null; var args = dojo._toArray(params); sql = args.splice(0, 1)[0]; // does this SQL statement use the ENCRYPT or DECRYPT // keywords? if so, extract our callback and crypto // password if(this._needsEncrypt(sql) || this._needsDecrypt(sql)){ callback = args.splice(args.length - 1, 1)[0]; password = args.splice(args.length - 1, 1)[0]; } // 'args' now just has the SQL parameters // print out debug SQL output if the developer wants that if(this.debug){ this._printDebugSQL(sql, args); } // handle SQL that needs encryption/decryption differently // do we have an ENCRYPT SQL statement? if so, handle that first if(this._needsEncrypt(sql)){ var crypto = new dojox.sql._SQLCrypto("encrypt", sql, password, args, callback); return; // encrypted results will arrive asynchronously }else if(this._needsDecrypt(sql)){ // otherwise we have a DECRYPT statement var crypto = new dojox.sql._SQLCrypto("decrypt", sql, password, args, callback); return; // decrypted results will arrive asynchronously } // execute the SQL and get the results var rs = this.db.execute(sql, args); // Gears ResultSet object's are ugly -- normalize // these into something JavaScript programmers know // how to work with, basically an array of // JavaScript objects where each property name is // simply the field name for a column of data rs = this._normalizeResults(rs); if(this._autoClose){ this.close(); } return rs; }catch(exp){ exp = exp.message||exp; console.debug("SQL Exception: " + exp); if(this._autoClose){ try{ this.close(); }catch(e){ console.debug("Error closing database: " + e.message||e); } } throw exp; } }, _initDb: function(){ if(!this.db){ try{ this.db = google.gears.factory.create('beta.database', '1.0'); }catch(exp){ dojo.setObject("google.gears.denied", true); dojox.off.onFrameworkEvent("coreOperationFailed"); throw "Google Gears must be allowed to run"; } } }, _printDebugSQL: function(sql, args){ var msg = "dojox.sql(\"" + sql + "\""; for(var i = 0; i < args.length; i++){ if(typeof args[i] == "string"){ msg += ", \"" + args[i] + "\""; }else{ msg += ", " + args[i]; } } msg += ")"; console.debug(msg); }, _normalizeResults: function(rs){ var results = []; if(!rs){ return []; } while(rs.isValidRow()){ var row = {}; for(var i = 0; i < rs.fieldCount(); i++){ var fieldName = rs.fieldName(i); var fieldValue = rs.field(i); row[fieldName] = fieldValue; } results.push(row); rs.next(); } rs.close(); return results; }, _needsEncrypt: function(sql){ return /encrypt\([^\)]*\)/i.test(sql); }, _needsDecrypt: function(sql){ return /decrypt\([^\)]*\)/i.test(sql); }});// summary:// A private class encapsulating any cryptography that must be done// on a SQL statement. We instantiate this class and have it hold// it's state so that we can potentially have several encryption// operations happening at the same time by different SQL statements.dojo.declare("dojox.sql._SQLCrypto", null, { constructor: function(action, sql, password, args, callback){ if(action == "encrypt"){ this._execEncryptSQL(sql, password, args, callback); }else{ this._execDecryptSQL(sql, password, args, callback); } }, _execEncryptSQL: function(sql, password, args, callback){ // strip the ENCRYPT/DECRYPT keywords from the SQL var strippedSQL = this._stripCryptoSQL(sql); // determine what arguments need encryption var encryptColumns = this._flagEncryptedArgs(sql, args); // asynchronously encrypt each argument that needs it var self = this; this._encrypt(strippedSQL, password, args, encryptColumns, function(finalArgs){ // execute the SQL var error = false; var resultSet = []; var exp = null; try{ resultSet = dojox.sql.db.execute(strippedSQL, finalArgs);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -