DbConnection.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**
  2. * Copyright (c) 2011-2018 by Andrew Mustun. All rights reserved.
  3. *
  4. * This file is part of the QCAD project.
  5. *
  6. * QCAD is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * QCAD is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with QCAD.
  18. */
  19. /**
  20. * Provides a connection to a SQLite database.
  21. *
  22. * \param connectionName The connection name to use.
  23. */
  24. function DbConnection(connectionName) {
  25. if (isNull(connectionName)) {
  26. return;
  27. }
  28. this.db = QSqlDatabase.database(connectionName, false);
  29. }
  30. DbConnection.prototype.dump = function() {
  31. var query = this.createQuery();
  32. query.execQuery(".dump");
  33. };
  34. DbConnection.prototype.transaction = function() {
  35. this.db.transaction();
  36. };
  37. DbConnection.prototype.commit = function() {
  38. this.db.commit();
  39. };
  40. DbConnection.prototype.rollback = function() {
  41. this.db.rollback();
  42. };
  43. /**
  44. * Executes the given QSqlQuery or SQL string and handles errors if appropriate.
  45. */
  46. DbConnection.prototype.execQuery = function(query) {
  47. try {
  48. var ret = false;
  49. if (isString(query)) {
  50. var sql = query;
  51. query = new QSqlQuery(this.db);
  52. ret = query.exec(sql);
  53. }
  54. else {
  55. ret = query.exec();
  56. }
  57. if (!ret) {
  58. qCritical("DbConnection.js:", "execQuery(): error:", query
  59. .lastError().text());
  60. qCritical("DbConnection.js:", "execQuery(): query failed:\n", query
  61. .executedQuery());
  62. debugger;
  63. }
  64. } catch (e) {
  65. qCritical("DbConnection.js:", "execQuery(): exception:", e);
  66. debugger;
  67. }
  68. return query;
  69. };
  70. /**
  71. * \return New QSqlQuery object.
  72. */
  73. DbConnection.prototype.createQuery = function() {
  74. return new QSqlQuery(this.db);
  75. };
  76. DbConnection.prototype.prepareQuery = function(sql) {
  77. var query = new QSqlQuery(this.db);
  78. var ret = query.prepare(sql);
  79. if (!ret) {
  80. qCritical("DbConnection.js:", "prepareQuery(): SQL prepare failed:\n",
  81. sql, "\n", query.lastError().text());
  82. debugger;
  83. }
  84. return query;
  85. };
  86. DbConnection.prototype.vacuum = function(sql) {
  87. var query = this.createQuery();
  88. query.exec("VACUUM");
  89. };
  90. /**
  91. * Converts the given QPixmap into a QByteArray for storage in a BLOB.
  92. */
  93. DbConnection.pixmapToByteArray = function(pixmap) {
  94. if (pixmap == undefined || pixmap == null) {
  95. return null;
  96. }
  97. var ba = new QByteArray();
  98. var buffer = new QBuffer(ba);
  99. buffer.open(QIODevice.WriteOnly);
  100. pixmap.save(buffer, "PNG"); // writes pixmap into ba in PNG format
  101. return ba;
  102. };