ctkPluginDatabase_p.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) 2010 German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #ifndef CTKPLUGINDATABASE_P_H
  16. #define CTKPLUGINDATABASE_P_H
  17. #include <QtSql>
  18. #include <QList>
  19. // CTK class forward declarations
  20. class ctkPluginStorage;
  21. class ctkPluginArchive;
  22. class ctkPluginDatabase {
  23. public:
  24. ctkPluginDatabase(ctkPluginStorage* storage);
  25. virtual ~ctkPluginDatabase();
  26. /**
  27. * Opens the plugin database. If the database does not
  28. * yet exist, it is created using the path from getDatabasePath().
  29. *
  30. * @see setDatabasePath(const QString&)
  31. * @see getDatabasePath()
  32. * @see ctkPluginDatabaseException
  33. *
  34. * @throws ctkPluginDatabaseException
  35. */
  36. void open();
  37. /**
  38. * Closes the plugin database. Throws a ctkPluginDatabaseException
  39. * of type DB_CONNECTION_INVALID if the database is invalid.
  40. *
  41. * @throws ctkPluginDatabaseException
  42. */
  43. void close();
  44. /**
  45. * Checks if the database is open
  46. */
  47. bool isOpen() const;
  48. /**
  49. * Sets the path of the service database to \a databasePath
  50. */
  51. void setDatabasePath(const QString &databasePath);
  52. /**
  53. * Returns the path of the plugin database
  54. */
  55. QString getDatabasePath() const;
  56. /**
  57. * Get a Qt resource cached in the database. The resource path \a res
  58. * must be relative to the plugin specific resource prefix, but may
  59. * start with a '/'.
  60. *
  61. * @param pluginId The id of the plugin from which to get the resource
  62. * @param res The path to the resource in the plugin
  63. * @return The byte array of the cached resource
  64. *
  65. * @throws ctkPluginDatabaseException
  66. */
  67. QByteArray getPluginResource(long pluginId, const QString& res) const;
  68. /**
  69. * Get a list of resource entries under the given path.
  70. *
  71. * @param pluginId The id of the plugin from which to get the entries
  72. * @param path A resource path relative to the plugin specific resource prefix.
  73. * @return A QStringList containing the cached resource entries.
  74. *
  75. * @throws ctkPluginDatabaseException
  76. */
  77. QStringList findResourcesPath(long pluginId, const QString& path) const;
  78. /**
  79. * Inserts a new plugin into the database. This method assumes that
  80. * the an entry with the same \a location and \a localPath does not
  81. * yet exist in the database.
  82. *
  83. * @param location The URL to the plugin.
  84. * @param localPath The path to the plugin library on the local file system.
  85. * @param createArchive If \c true (default) a new ctkPluginArchive instance is returned.
  86. *
  87. * @throws ctkPluginDatabaseException
  88. */
  89. ctkPluginArchive* insertPlugin(const QUrl& location, const QString& localPath, bool createArchive = true);
  90. /**
  91. * Removes all persisted data related to the given ctkPluginArchive.
  92. *
  93. * @throws ctkPluginDatabaseException
  94. */
  95. void removeArchive(const ctkPluginArchive* pa);
  96. /**
  97. * Reads the persisted plugin data and returns a ctkPluginArchive object
  98. * for each plugin which is not in state UNINSTALLED.
  99. *
  100. * @throws ctkPluginDatabaseException
  101. */
  102. QList<ctkPluginArchive*> getPluginArchives() const;
  103. private:
  104. enum TransactionType{Read, Write};
  105. /**
  106. * Helper method that creates the database tables:
  107. *
  108. * @throws ctkPluginDatabaseException
  109. */
  110. void createTables();
  111. bool dropTables();
  112. /**
  113. * Helper method that checks if all the expected tables exist in the database.
  114. *
  115. * Returns true if they all exist and false if any of them don't
  116. */
  117. bool checkTables() const;
  118. /**
  119. * Checks the database connection.
  120. *
  121. * @throws ctkPluginDatabaseException
  122. */
  123. void checkConnection() const;
  124. /**
  125. * Compares the persisted plugin modification time with the
  126. * file system modification time and updates the database
  127. * if the persisted data is outdated.
  128. *
  129. * This should only be called once when the database is initially opened.
  130. */
  131. void updateDB();
  132. /**
  133. * Helper function that executes the sql query specified in \a statement.
  134. * It is assumed that the \a statement uses positional placeholders and
  135. * corresponding parameters are placed in the list of \a bindValues.
  136. *
  137. * Aside: This function may be safely called standalone or within an explicit
  138. * transaction. If called standalone, it's single query is implicitly
  139. * wrapped in it's own transaction.
  140. *
  141. * @throws ctkPluginDatabaseException
  142. */
  143. void executeQuery(QSqlQuery* query, const QString &statement, const QList<QVariant> &bindValues = QList<QVariant>()) const;
  144. /**
  145. * Begins a transcaction based on the \a type which can be Read or Write.
  146. *
  147. * @throws ctkPluginDatabaseException
  148. */
  149. void beginTransaction(QSqlQuery* query, TransactionType);
  150. /**
  151. * Commits a transaction
  152. *
  153. * @throws ctkPluginDatabaseException
  154. */
  155. void commitTransaction(QSqlQuery* query);
  156. /**
  157. * Rolls back a transaction
  158. *
  159. * @throws ctkPluginDatabaseException
  160. */
  161. void rollbackTransaction(QSqlQuery* query);
  162. QString m_databasePath;
  163. QString m_connectionName;
  164. bool m_isDatabaseOpen;
  165. bool m_inTransaction;
  166. ctkPluginStorage* m_PluginStorage;
  167. };
  168. #endif // CTKPLUGINDATABASE_P_H