home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / AIMP2 / aimp_2.61.583.exe / $TEMP / YandexPackSetup.msi / fil95129609F13FE0D668146158593D164B < prev    next >
Text File  |  2010-07-12  |  2KB  |  83 lines

  1. XB.Database = function() {
  2.     this._connection = null;
  3. }
  4.  
  5. XB.Database.prototype.open = function XBDB_open(fileName) {
  6.     if (!fileName)
  7.         throw new Error("Filename is required");
  8.     
  9.     this.close();
  10.     
  11.     var dbFile = XB._base.application.directories.XBRoot;
  12.     dbFile.append(fileName);
  13.     
  14.     var storageService = XB._Cc["@mozilla.org/storage/service;1"].getService(XB._Ci.mozIStorageService);
  15.     
  16.     try {
  17.         this._connection = storageService.openDatabase(dbFile);
  18.     } catch(e) {
  19.         let journalDBFile = XB._base.application.directories.XBRoot;
  20.         journalDBFile.append(fileName + "-journal");
  21.         
  22.         try { journalDBFile.remove(false); } catch(ex) {}
  23.         
  24.         if (e.result == XB._Cr.NS_ERROR_FILE_CORRUPTED)
  25.             dbFile.remove(false);
  26.         
  27.         this._connection = storageService.openDatabase(dbFile);
  28.     }
  29. }
  30.  
  31. XB.Database.prototype.close = function XBDB_close() {
  32.     if (this._connection) {
  33.         try {
  34.             this._connection.close();
  35.         } catch(e) {}
  36.         
  37.         this._connection = null;
  38.     }
  39. }
  40.  
  41. XB.Database.prototype._createStatement = function XBDB__createStatement(query, parameters) {
  42.     let statement = this._connection.createStatement(query);
  43.     if (statement && !("executeAsync" in statement)) {
  44.         let wrappedStatement = XB._Cc["@mozilla.org/storage/statement-wrapper;1"].createInstance(XB._Ci.mozIStorageStatementWrapper);
  45.         wrappedStatement.initialize(statement);
  46.         statement = wrappedStatement;
  47.     }
  48.     
  49.     if (parameters)
  50.       for (let name in parameters)
  51.         statement.params[name] = parameters[name];
  52.     
  53.     return statement;
  54. }
  55.  
  56. XB.Database.prototype.exec = function XBDB_exec(query, parameters) {
  57.     var statement,
  58.         result = [];
  59.     
  60.     try {
  61.         statement = this._createStatement(query, parameters);
  62.         
  63.         var columns = [];
  64.         for (var i = statement.columnCount; i--;)
  65.             columns.push(statement.getColumnName(i));
  66.         
  67.         while (statement.step()) {
  68.             var row = {};
  69.             for (var i = columns.length; i--;)
  70.                 row[columns[i]] = statement.row[columns[i]];
  71.  
  72.             result.push(row);
  73.         }
  74.     } catch(e) {
  75.         XB._base.logger.error(e);
  76.         XB._base.logger.debug(e.stack);
  77.     } finally {
  78.         if (statement)
  79.             statement.reset();
  80.     }
  81.     
  82.     return result;
  83. }