PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/GUISupport/QtSQL/Testing/Cxx/TestQtSQLDatabase.cxx

http://github.com/Kitware/VTK
C++ | 287 lines | 231 code | 21 blank | 35 comment | 32 complexity | 7836adc96555bc858116ce9d11fea411 MD5 | raw file
Possible License(s): JSON, BSD-3-Clause, LGPL-2.0, LGPL-3.0, GPL-2.0, Apache-2.0
  1. /*=========================================================================
  2. Program: Visualization Toolkit
  3. Module: TestQtSQLDatabase.cxx
  4. Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  5. All rights reserved.
  6. See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
  7. This software is distributed WITHOUT ANY WARRANTY; without even
  8. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  9. PURPOSE. See the above copyright notice for more information.
  10. =========================================================================*/
  11. /*-------------------------------------------------------------------------
  12. Copyright 2008 Sandia Corporation.
  13. Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
  14. the U.S. Government retains certain rights in this software.
  15. -------------------------------------------------------------------------*/
  16. // Tests vtkQtSQLDatabase.
  17. // Check for Qt SQL module before defining this test.
  18. #include <qglobal.h>
  19. #if (QT_EDITION & QT_MODULE_SQL)
  20. #include "vtkQtSQLDatabase.h"
  21. #include "vtkQtTableModelAdapter.h"
  22. #include "vtkRowQueryToTable.h"
  23. #include "vtkSQLQuery.h"
  24. #include "vtkStdString.h"
  25. #include "vtkTable.h"
  26. #include "vtkVariant.h"
  27. #include "vtkVariantArray.h"
  28. #include <QApplication>
  29. #include <QFile>
  30. #include <QInputDialog>
  31. #include <QStringList>
  32. #include <QTableView>
  33. int TestQtSQLDatabase(int argc, char* argv[])
  34. {
  35. QApplication app(argc, argv);
  36. // QCoreApplication app(argc, argv);
  37. // for (int i = 0; i < QCoreApplication::libraryPaths().count(); i++)
  38. // {
  39. // cerr << QCoreApplication::libraryPaths().at(i).toLatin1().data() << endl;
  40. // }
  41. bool interactive = false;
  42. // QMYSQL parameters:
  43. // QString dbtype = "QMYSQL";
  44. // QString database = "test";
  45. // QString user = "root";
  46. // bool askpass = true;
  47. // QString host = "localhost";
  48. // int port = 3306;
  49. // QSQLITE parameters:
  50. QString dbtype("QSQLITE");
  51. QString database(":memory:");
  52. QString user;
  53. bool askpass = false;
  54. QString host;
  55. int port = -1;
  56. QString queryText("SELECT name, age, weight FROM people WHERE age <= 20");
  57. for (int i = 1; i < argc; i++)
  58. {
  59. if (!strcmp(argv[i], "-I"))
  60. {
  61. interactive = true;
  62. continue;
  63. }
  64. if (!strcmp(argv[i], "-t"))
  65. {
  66. i++;
  67. dbtype = argv[i];
  68. continue;
  69. }
  70. if (!strcmp(argv[i], "-d"))
  71. {
  72. i++;
  73. database = argv[i];
  74. continue;
  75. }
  76. if (!strcmp(argv[i], "-u"))
  77. {
  78. i++;
  79. user = argv[i];
  80. continue;
  81. }
  82. if (!strcmp(argv[i], "-w"))
  83. {
  84. askpass = true;
  85. continue;
  86. }
  87. if (!strcmp(argv[i], "-h"))
  88. {
  89. i++;
  90. host = argv[i];
  91. continue;
  92. }
  93. if (!strcmp(argv[i], "-p"))
  94. {
  95. i++;
  96. port = atoi(argv[i]);
  97. continue;
  98. }
  99. if (!strcmp(argv[i], "-q"))
  100. {
  101. i++;
  102. queryText = argv[i];
  103. continue;
  104. }
  105. cerr << argv[0] << " Options:\n"
  106. << " -I (interactive, shows Qt table with query result)\n"
  107. << " -t database type (QSQLITE, QMYSQL, etc.; default: QSQLITE)\n"
  108. << " -h host (default: :memory:)\n"
  109. << " -p port (default: empty)\n"
  110. << " -d database (default: test)\n"
  111. << " -u username (default: empty)\n"
  112. << " -w (password required; default: no password required)\n"
  113. << " -q (query; default: select * from people ...)\n";
  114. return 0;
  115. }
  116. QString password;
  117. if (askpass)
  118. {
  119. password = QInputDialog::getText(nullptr, "Enter password", "Password", QLineEdit::Password);
  120. }
  121. vtkQtSQLDatabase* db = vtkQtSQLDatabase::New();
  122. db->SetDatabaseType(dbtype.toLatin1().data());
  123. db->SetDatabaseName(database.toLatin1().data());
  124. db->SetUserName(user.toLatin1().data());
  125. db->SetPort(port);
  126. if (!db->Open(password.toLatin1().data()))
  127. {
  128. cerr << "Unable to open database" << endl;
  129. return 1;
  130. }
  131. vtkSQLQuery* query = db->GetQueryInstance();
  132. bool dataExists = false;
  133. query->SetQuery("SHOW TABLES");
  134. query->Execute();
  135. if (query->NextRow())
  136. {
  137. dataExists = true; // there is a table
  138. }
  139. if (!dataExists)
  140. {
  141. QString createQuery("CREATE TABLE IF NOT EXISTS people (name TEXT, age INTEGER, weight FLOAT)");
  142. cout << createQuery.toLatin1().data() << endl;
  143. query->SetQuery(createQuery.toLatin1().data());
  144. if (!query->Execute())
  145. {
  146. cerr << "Create query failed" << endl;
  147. return 1;
  148. }
  149. for (int i = 0; i < 40; i++)
  150. {
  151. QString insertQuery =
  152. QString("INSERT INTO people VALUES('John Doe %1', %1, %2)").arg(i).arg(10 * i);
  153. cout << insertQuery.toLatin1().data() << endl;
  154. query->SetQuery(insertQuery.toLatin1().data());
  155. if (!query->Execute())
  156. {
  157. cerr << "Insert query failed" << endl;
  158. return 1;
  159. }
  160. }
  161. }
  162. query->SetQuery(queryText.toLatin1().data());
  163. cerr << endl << "Running query: " << query->GetQuery() << endl;
  164. cerr << endl << "Using vtkSQLQuery directly to execute query:" << endl;
  165. if (!query->Execute())
  166. {
  167. cerr << "Query failed" << endl;
  168. return 1;
  169. }
  170. for (int col = 0; col < query->GetNumberOfFields(); col++)
  171. {
  172. if (col > 0)
  173. {
  174. cerr << ", ";
  175. }
  176. cerr << query->GetFieldName(col);
  177. }
  178. cerr << endl;
  179. while (query->NextRow())
  180. {
  181. for (int field = 0; field < query->GetNumberOfFields(); field++)
  182. {
  183. if (field > 0)
  184. {
  185. cerr << ", ";
  186. }
  187. cerr << query->DataValue(field).ToString().c_str();
  188. }
  189. cerr << endl;
  190. }
  191. cerr << endl << "Using vtkSQLQuery to execute query and retrieve by row:" << endl;
  192. if (!query->Execute())
  193. {
  194. cerr << "Query failed" << endl;
  195. return 1;
  196. }
  197. for (int col = 0; col < query->GetNumberOfFields(); col++)
  198. {
  199. if (col > 0)
  200. {
  201. cerr << ", ";
  202. }
  203. cerr << query->GetFieldName(col);
  204. }
  205. cerr << endl;
  206. vtkVariantArray* va = vtkVariantArray::New();
  207. while (query->NextRow(va))
  208. {
  209. for (int field = 0; field < va->GetNumberOfValues(); field++)
  210. {
  211. if (field > 0)
  212. {
  213. cerr << ", ";
  214. }
  215. cerr << va->GetValue(field).ToString().c_str();
  216. }
  217. cerr << endl;
  218. }
  219. va->Delete();
  220. cerr << endl << "Using vtkRowQueryToTable to execute query:" << endl;
  221. vtkRowQueryToTable* reader = vtkRowQueryToTable::New();
  222. reader->SetQuery(query);
  223. reader->Update();
  224. vtkTable* table = reader->GetOutput();
  225. for (vtkIdType col = 0; col < table->GetNumberOfColumns(); col++)
  226. {
  227. table->GetColumn(col)->Print(cerr);
  228. }
  229. cerr << endl;
  230. for (vtkIdType row = 0; row < table->GetNumberOfRows(); row++)
  231. {
  232. for (vtkIdType col = 0; col < table->GetNumberOfColumns(); col++)
  233. {
  234. vtkVariant v = table->GetValue(row, col);
  235. cerr << "row " << row << ", col " << col << " - " << v.ToString() << " ("
  236. << vtkImageScalarTypeNameMacro(v.GetType()) << ")" << endl;
  237. }
  238. }
  239. // Put the table in a view ... just for fun
  240. if (interactive)
  241. {
  242. vtkQtTableModelAdapter* model = new vtkQtTableModelAdapter(table);
  243. QTableView* view = new QTableView();
  244. view->setModel(model);
  245. view->show();
  246. app.exec();
  247. delete view;
  248. delete model;
  249. }
  250. reader->Delete();
  251. query->Delete();
  252. db->Delete();
  253. return 0;
  254. }
  255. #else
  256. #include "vtkObject.h" // for cerr.
  257. int TestQtSQLDatabase(int, char*[])
  258. {
  259. cerr << "QT_MODULE_SQL not enabled in this edition, so nothing to test." << endl;
  260. return 0;
  261. }
  262. #endif // (QT_EDITION & QT_MODULE_SQL)