You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

234 lines
6.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCETICE project - Copyright 2009 by Lucio Asnaghi.
  4. JUCETICE is based around the JUCE library - "Jules' Utility Class Extensions"
  5. Copyright 2007 by Julian Storer.
  6. ------------------------------------------------------------------------------
  7. JUCE and JUCETICE can be redistributed and/or modified under the terms of
  8. the GNU General Public License, as published by the Free Software Foundation;
  9. either version 2 of the License, or (at your option) any later version.
  10. JUCE and JUCETICE are distributed in the hope that they will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with JUCE and JUCETICE; if not, visit www.gnu.org/licenses or write to
  16. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  17. Boston, MA 02111-1307 USA
  18. ==============================================================================
  19. */
  20. #if JUCE_SUPPORT_SQLITE
  21. extern "C" {
  22. #include <sqlite3.h>
  23. }
  24. BEGIN_JUCE_NAMESPACE
  25. //==============================================================================
  26. SqliteResultset::SqliteResultset (SqliteConnection* connection_,
  27. void* statement_,
  28. const String& errorText_)
  29. : connection (connection_),
  30. statement ((sqlite3_stmt*) statement_),
  31. errorText (errorText_)
  32. {
  33. }
  34. SqliteResultset::~SqliteResultset ()
  35. {
  36. if (statement)
  37. sqlite3_finalize ((sqlite3_stmt*) statement);
  38. if (connection)
  39. connection->removeResultset (this, false);
  40. }
  41. bool SqliteResultset::isValid () const
  42. {
  43. return statement != 0;
  44. }
  45. const String SqliteResultset::getErrorText () const
  46. {
  47. return errorText;
  48. }
  49. bool SqliteResultset::next ()
  50. {
  51. return (sqlite3_step ((sqlite3_stmt*) statement) == SQLITE_ROW);
  52. }
  53. int SqliteResultset::columnCount ()
  54. {
  55. return sqlite3_column_count ((sqlite3_stmt*) statement);
  56. }
  57. int SqliteResultset::getColumnType (const int columnIndex)
  58. {
  59. int columnType = sqlite3_column_type ((sqlite3_stmt*) statement, columnIndex);
  60. switch (columnType) {
  61. case SQLITE_NULL: return SqliteResultset::Null;
  62. case SQLITE_INTEGER: return SqliteResultset::Integer;
  63. case SQLITE_FLOAT: return SqliteResultset::Double;
  64. case SQLITE_TEXT: return SqliteResultset::Text;
  65. case SQLITE_BLOB: return SqliteResultset::Blob;
  66. default: return SqliteResultset::Unknown;
  67. }
  68. }
  69. const String SqliteResultset::getColumnName (int columnIndex)
  70. {
  71. return String (sqlite3_column_name ((sqlite3_stmt*) statement, columnIndex));
  72. }
  73. bool SqliteResultset::columnIsNull (const int columnIndex)
  74. {
  75. return sqlite3_column_type ((sqlite3_stmt*) statement, columnIndex) == SQLITE_NULL;
  76. }
  77. int SqliteResultset::columnAsInteger (const int columnIndex)
  78. {
  79. return sqlite3_column_int ((sqlite3_stmt*) statement, columnIndex);
  80. }
  81. double SqliteResultset::columnAsDouble (const int columnIndex)
  82. {
  83. return sqlite3_column_double ((sqlite3_stmt*) statement, columnIndex);
  84. }
  85. const String SqliteResultset::columnAsText (const int columnIndex)
  86. {
  87. return String ((const char*) sqlite3_column_text ((sqlite3_stmt*) statement, columnIndex));
  88. }
  89. const void* SqliteResultset::columnAsBlob (const int columnIndex, int& bytesRead)
  90. {
  91. bytesRead = sqlite3_column_bytes ((sqlite3_stmt*) statement, columnIndex);
  92. return sqlite3_column_blob ((sqlite3_stmt*) statement, columnIndex);
  93. }
  94. //==============================================================================
  95. SqliteConnection::SqliteConnection ()
  96. : database (0)
  97. {
  98. }
  99. SqliteConnection::~SqliteConnection ()
  100. {
  101. if (database)
  102. sqlite3_close ((sqlite3*) database);
  103. }
  104. const String SqliteConnection::openDatabaseFile (const File& file)
  105. {
  106. String errorText;
  107. if (database)
  108. sqlite3_close ((sqlite3*) database);
  109. if (file.existsAsFile ())
  110. {
  111. sqlite3* db = 0;
  112. if (sqlite3_open ((const char*) file.getFullPathName(), &db))
  113. {
  114. errorText = "Error connecting to " + file.getFullPathName();
  115. }
  116. else
  117. {
  118. database = db;
  119. }
  120. }
  121. else
  122. {
  123. errorText = "Database file " + file.getFullPathName() + " does not exists";
  124. }
  125. return errorText;
  126. }
  127. SqliteResultset* SqliteConnection::executeQuery (const String& sqlText)
  128. {
  129. sqlite3_stmt* statement = 0;
  130. String errorText;
  131. if (database)
  132. {
  133. int rc = sqlite3_prepare_v2 ((sqlite3*) database, (const char*) sqlText, -1, &statement, 0);
  134. if (rc != SQLITE_OK)
  135. {
  136. errorText = (const char*) sqlite3_errmsg ((sqlite3*) database);
  137. }
  138. }
  139. ScopedLock sl (queriesLock);
  140. SqliteResultset* queryResultSet = new SqliteResultset (this, statement, errorText);
  141. queries.add (queryResultSet);
  142. return queryResultSet;
  143. }
  144. const StringPairArray SqliteConnection::getTableSchema (const String& tableName)
  145. {
  146. jassertfalse; // not implemented !
  147. StringPairArray schema;
  148. #if 0
  149. char* error;
  150. char** resultSet;
  151. int numRows, numColumns;
  152. String query;
  153. query << "PRAGMA table_info(\"";
  154. query << tableName;
  155. query << "\");";
  156. int resultCode = sqlite3_get_table(
  157. (sqlite3*) database, // An open database
  158. (const char*) query, // SQL to be executed
  159. &resultSet, // Result written to a char*[] that this points to
  160. &numRows, // Number of result rows written here
  161. &numColumns, // Number of result columns written here
  162. &error // Error msg written here
  163. );
  164. if (resultCode == SQLITE_OK)
  165. {
  166. for (int row = numColumns; row < numRows; row += 6)
  167. {
  168. schema[String (resultSet[row + 1])] = String (resultSet[row + 2]);
  169. }
  170. }
  171. sqlite3_free_table (resultSet);
  172. #endif
  173. return schema;
  174. }
  175. void SqliteConnection::removeResultset (SqliteResultset* resultSet, const bool deleteObject)
  176. {
  177. ScopedLock sl (queriesLock);
  178. if (queries.contains (resultSet))
  179. queries.removeObject (resultSet, deleteObject);
  180. }
  181. END_JUCE_NAMESPACE
  182. #endif // JUCE_SUPPORT_SQLITE