f6b43a5b7f494015c2f979324a8c7989a6f007a4.svn-base 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Ext JS Library 2.0 Copyright(c) 2006-2007, Ext JS, LLC. licensing@extjs.com
  3. *
  4. * http://extjs.com/license
  5. */
  6. Ext.data.AirDB = Ext.extend(Ext.data.SqlDB, {
  7. open : function(db, cb, scope) {
  8. this.conn = new air.SQLConnection();
  9. var file = air.File.applicationResourceDirectory.resolve(db);
  10. this.conn.addEventListener(air.SQLEvent.OPEN, this.onOpen
  11. .createDelegate(this, [cb, scope]));
  12. this.conn.addEventListener(air.SQLEvent.CLOSE, this.onClose
  13. .createDelegate(this));
  14. this.conn.open(file, true);
  15. },
  16. close : function() {
  17. this.conn.close();
  18. },
  19. onOpen : function(cb, scope) {
  20. this.openState = true;
  21. Ext.callback(cb, scope, [this]);
  22. this.fireEvent('open', this);
  23. },
  24. onClose : function() {
  25. this.fireEvent('close', this);
  26. },
  27. onError : function(e, stmt, type, cb, scope) {
  28. Ext.callback(cb, scope, [false, e, stmt]);
  29. },
  30. onResult : function(e, stmt, type, cb, scope) {
  31. if (type == 'exec') {
  32. Ext.callback(cb, scope, [true, e, stmt]);
  33. } else {
  34. var r = [];
  35. var result = stmt.getResult();
  36. if (result && result.data) {
  37. var len = result.data.length;
  38. for (var i = 0; i < len; i++) {
  39. r[r.length] = result.data[i];
  40. }
  41. }
  42. Ext.callback(cb, scope, [r, e, stmt]);
  43. }
  44. },
  45. createStatement : function(type, cb, scope) {
  46. var stmt = new air.SQLStatement();
  47. stmt.addEventListener(air.SQLErrorEvent.ERROR, this.onError
  48. .createDelegate(this, [stmt, type, cb, scope],
  49. true));
  50. stmt.addEventListener(air.SQLEvent.RESULT, this.onResult
  51. .createDelegate(this, [stmt, type, cb, scope],
  52. true));
  53. stmt.sqlConnection = this.conn;
  54. return stmt;
  55. },
  56. exec : function(sql, cb, scope) {
  57. var stmt = this.createStatement('exec', cb, scope);
  58. stmt.text = sql;
  59. stmt.execute();
  60. },
  61. execBy : function(sql, args, cb, scope) {
  62. var stmt = this.createStatement('exec', cb, scope);
  63. stmt.text = sql;
  64. this.addParams(stmt, args);
  65. stmt.execute();
  66. },
  67. query : function(sql, cb, scope) {
  68. var stmt = this.createStatement('query', cb, scope);
  69. stmt.text = sql;
  70. stmt.execute(this.maxResults);
  71. },
  72. queryBy : function(sql, args, cb, scope) {
  73. var stmt = this.createStatement('query', cb, scope);
  74. stmt.text = sql;
  75. this.addParams(stmt, args);
  76. stmt.execute(this.maxResults);
  77. },
  78. addParams : function(stmt, args) {
  79. if (!args) {
  80. return;
  81. }
  82. for (var key in args) {
  83. if (args.hasOwnProperty(key)) {
  84. if (!isNaN(key)) {
  85. stmt.parameters[parseInt(key) + 1] = args[key];
  86. } else {
  87. stmt.parameters[':' + key] = args[key];
  88. }
  89. }
  90. }
  91. return stmt;
  92. }
  93. });