LibrePCB Developers Documentation
workspacelibrarydbwriter.h
Go to the documentation of this file.
1 /*
2  * LibrePCB - Professional EDA for everyone!
3  * Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
4  * https://librepcb.org/
5  *
6  * This program 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  * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef LIBREPCB_CORE_WORKSPACELIBRARYDBWRITER_H
21 #define LIBREPCB_CORE_WORKSPACELIBRARYDBWRITER_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 #include "../fileio/filepath.h"
27 #include "../types/elementname.h"
28 
29 #include <optional/tl/optional.hpp>
30 
31 #include <QtCore>
32 
33 /*******************************************************************************
34  * Namespace / Forward Declarations
35  ******************************************************************************/
36 namespace librepcb {
37 
38 class Component;
39 class ComponentCategory;
40 class Device;
41 class Package;
42 class PackageCategory;
43 class SQLiteDatabase;
44 class Symbol;
45 class Uuid;
46 class Version;
47 
48 /*******************************************************************************
49  * Class WorkspaceLibraryDbWriter
50  ******************************************************************************/
51 
56 public:
57  // Constructors / Destructor
58  WorkspaceLibraryDbWriter() = delete;
60  WorkspaceLibraryDbWriter(const FilePath& librariesRoot, SQLiteDatabase& db);
61  ~WorkspaceLibraryDbWriter() noexcept;
62 
63  // General Methods
64 
70  void createAllTables();
71 
78  void addInternalData(const QString& key, int value);
79 
90  int addLibrary(const FilePath& fp, const Uuid& uuid, const Version& version,
91  bool deprecated, const QByteArray& iconPng);
92 
102  void updateLibrary(const FilePath& fp, const Uuid& uuid,
103  const Version& version, bool deprecated,
104  const QByteArray& iconPng);
105 
117  template <typename ElementType>
118  int addElement(int libId, const FilePath& fp, const Uuid& uuid,
119  const Version& version, bool deprecated) {
120  static_assert(std::is_same<ElementType, Symbol>::value ||
121  std::is_same<ElementType, Package>::value ||
122  std::is_same<ElementType, Component>::value,
123  "Unsupported ElementType");
124  return addElement(getElementTable<ElementType>(), libId, fp, uuid, version,
125  deprecated);
126  }
127 
140  template <typename ElementType>
141  int addCategory(int libId, const FilePath& fp, const Uuid& uuid,
142  const Version& version, bool deprecated,
143  const tl::optional<Uuid>& parent) {
144  static_assert(std::is_same<ElementType, ComponentCategory>::value ||
145  std::is_same<ElementType, PackageCategory>::value,
146  "Unsupported ElementType");
147  return addCategory(getElementTable<ElementType>(), libId, fp, uuid, version,
148  deprecated, parent);
149  }
150 
163  int addDevice(int libId, const FilePath& fp, const Uuid& uuid,
164  const Version& version, bool deprecated, const Uuid& component,
165  const Uuid& package);
166 
176  template <typename ElementType>
177  void removeElement(const FilePath& fp) {
178  removeElement(getElementTable<ElementType>(), fp);
179  }
180 
189  template <typename ElementType>
191  removeAllElements(getElementTable<ElementType>());
192  }
193 
205  template <typename ElementType>
206  int addTranslation(int elementId, const QString& locale,
207  const tl::optional<ElementName>& name,
208  const tl::optional<QString>& description,
209  const tl::optional<QString>& keywords) {
210  return addTranslation(getElementTable<ElementType>(), elementId, locale,
211  name, description, keywords);
212  }
213 
219  template <typename ElementType>
221  removeAllTranslations(getElementTable<ElementType>());
222  }
223 
232  template <typename ElementType>
233  int addToCategory(int elementId, const Uuid& category) {
234  static_assert(std::is_same<ElementType, Symbol>::value ||
235  std::is_same<ElementType, Package>::value ||
236  std::is_same<ElementType, Component>::value ||
237  std::is_same<ElementType, Device>::value,
238  "Unsupported ElementType");
239  return addToCategory(getElementTable<ElementType>(), elementId, category);
240  }
241 
242  // Helper Functions
243 
250  template <typename ElementType>
251  static QString getElementTable() noexcept;
252 
260  template <typename ElementType>
261  static QString getCategoryTable() noexcept;
262 
263  // Operator Overloadings
265  delete;
266 
267 private: // Methods
268  int addElement(const QString& elementsTable, int libId, const FilePath& fp,
269  const Uuid& uuid, const Version& version, bool deprecated);
270  int addCategory(const QString& categoriesTable, int libId, const FilePath& fp,
271  const Uuid& uuid, const Version& version, bool deprecated,
272  const tl::optional<Uuid>& parent);
273  void removeElement(const QString& elementsTable, const FilePath& fp);
274  void removeAllElements(const QString& elementsTable);
275  int addTranslation(const QString& elementsTable, int elementId,
276  const QString& locale,
277  const tl::optional<ElementName>& name,
278  const tl::optional<QString>& description,
279  const tl::optional<QString>& keywords);
280  void removeAllTranslations(const QString& elementsTable);
281  int addToCategory(const QString& elementsTable, int elementId,
282  const Uuid& category);
283  QString filePathToString(const FilePath& fp) const noexcept;
284 
285 private: // Data
288 };
289 
290 /*******************************************************************************
291  * End of File
292  ******************************************************************************/
293 
294 } // namespace librepcb
295 
296 #endif
The Version class represents a version number in the format "1.42.7".
Definition: version.h:58
int addCategory(int libId, const FilePath &fp, const Uuid &uuid, const Version &version, bool deprecated, const tl::optional< Uuid > &parent)
addElement() specialized for categories
Definition: workspacelibrarydbwriter.h:141
SQLiteDatabase & mDb
Definition: workspacelibrarydbwriter.h:287
void updateLibrary(const FilePath &fp, const Uuid &uuid, const Version &version, bool deprecated, const QByteArray &iconPng)
Update library metadata.
Definition: workspacelibrarydbwriter.cpp:296
void removeAllTranslations()
Remove all translations for a library element type.
Definition: workspacelibrarydbwriter.h:220
int addDevice(int libId, const FilePath &fp, const Uuid &uuid, const Version &version, bool deprecated, const Uuid &component, const Uuid &package)
addElement() specialized for devices
Definition: workspacelibrarydbwriter.cpp:314
Definition: occmodel.cpp:76
static QString getCategoryTable() noexcept
Get the category table name of an element type.
Database write functions for librepcb::WorkspaceLibraryDb.
Definition: workspacelibrarydbwriter.h:55
int addToCategory(int elementId, const Uuid &category)
Add a library element to a category.
Definition: workspacelibrarydbwriter.h:233
~WorkspaceLibraryDbWriter() noexcept
Definition: workspacelibrarydbwriter.cpp:53
int addTranslation(int elementId, const QString &locale, const tl::optional< ElementName > &name, const tl::optional< QString > &description, const tl::optional< QString > &keywords)
Add a translation for a library element.
Definition: workspacelibrarydbwriter.h:206
QString filePathToString(const FilePath &fp) const noexcept
Definition: workspacelibrarydbwriter.cpp:500
void addInternalData(const QString &key, int value)
Add an integer value to the "internal" table.
Definition: workspacelibrarydbwriter.cpp:271
FilePath mLibrariesRoot
Definition: workspacelibrarydbwriter.h:286
This class represents absolute, well-formatted paths to files or directories.
Definition: filepath.h:127
void createAllTables()
Create all tables to initialize the database.
Definition: workspacelibrarydbwriter.cpp:60
The SQLiteDatabase class.
Definition: sqlitedatabase.h:43
int addLibrary(const FilePath &fp, const Uuid &uuid, const Version &version, bool deprecated, const QByteArray &iconPng)
Add a library.
Definition: workspacelibrarydbwriter.cpp:280
WorkspaceLibraryDbWriter & operator=(const WorkspaceLibraryDbWriter &rhs)=delete
The Uuid class is a replacement for QUuid to get UUID strings without {} braces.
Definition: uuid.h:56
static QString getElementTable() noexcept
Get the table name of an element type.
int addElement(int libId, const FilePath &fp, const Uuid &uuid, const Version &version, bool deprecated)
Add a library element.
Definition: workspacelibrarydbwriter.h:118
void removeAllElements()
Remove all library elements of a specific type.
Definition: workspacelibrarydbwriter.h:190
void removeElement(const FilePath &fp)
Remove a library element.
Definition: workspacelibrarydbwriter.h:177