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#include "../types/simplestring.h"
29
30#include <optional/tl/optional.hpp>
31
32#include <QtCore>
33
34/*******************************************************************************
35 * Namespace / Forward Declarations
36 ******************************************************************************/
37namespace librepcb {
38
39class Attribute;
40class Component;
41class ComponentCategory;
42class Device;
43class Package;
44class PackageCategory;
45class SQLiteDatabase;
46class Symbol;
47class Uuid;
48class Version;
49
50/*******************************************************************************
51 * Class WorkspaceLibraryDbWriter
52 ******************************************************************************/
53
58public:
59 // Constructors / Destructor
62 WorkspaceLibraryDbWriter(const FilePath& librariesRoot, SQLiteDatabase& db);
64
65 // General Methods
66
72 void createAllTables();
73
80 void addInternalData(const QString& key, int value);
81
93 int addLibrary(const FilePath& fp, const Uuid& uuid, const Version& version,
94 bool deprecated, const QByteArray& iconPng,
95 const QString& manufacturer);
96
107 void updateLibrary(const FilePath& fp, const Uuid& uuid,
108 const Version& version, bool deprecated,
109 const QByteArray& iconPng, const QString& manufacturer);
110
122 template <typename ElementType>
123 int addElement(int libId, const FilePath& fp, const Uuid& uuid,
124 const Version& version, bool deprecated) {
125 static_assert(std::is_same<ElementType, Symbol>::value ||
126 std::is_same<ElementType, Package>::value ||
127 std::is_same<ElementType, Component>::value,
128 "Unsupported ElementType");
129 return addElement(getElementTable<ElementType>(), libId, fp, uuid, version,
130 deprecated);
131 }
132
145 template <typename ElementType>
146 int addCategory(int libId, const FilePath& fp, const Uuid& uuid,
147 const Version& version, bool deprecated,
148 const tl::optional<Uuid>& parent) {
149 static_assert(std::is_same<ElementType, ComponentCategory>::value ||
150 std::is_same<ElementType, PackageCategory>::value,
151 "Unsupported ElementType");
152 return addCategory(getElementTable<ElementType>(), libId, fp, uuid, version,
153 deprecated, parent);
154 }
155
168 int addDevice(int libId, const FilePath& fp, const Uuid& uuid,
169 const Version& version, bool deprecated, const Uuid& component,
170 const Uuid& package);
171
180 int addPart(int devId, const QString& mpn, const QString& manufacturer);
181
189 int addPartAttribute(int partId, const Attribute& attribute);
190
200 template <typename ElementType>
201 void removeElement(const FilePath& fp) {
202 removeElement(getElementTable<ElementType>(), fp);
203 }
204
213 template <typename ElementType>
215 removeAllElements(getElementTable<ElementType>());
216 }
217
229 template <typename ElementType>
230 int addTranslation(int elementId, const QString& locale,
231 const tl::optional<ElementName>& name,
232 const tl::optional<QString>& description,
233 const tl::optional<QString>& keywords) {
234 return addTranslation(getElementTable<ElementType>(), elementId, locale,
235 name, description, keywords);
236 }
237
243 template <typename ElementType>
245 removeAllTranslations(getElementTable<ElementType>());
246 }
247
256 template <typename ElementType>
257 int addToCategory(int elementId, const Uuid& category) {
258 static_assert(std::is_same<ElementType, Symbol>::value ||
259 std::is_same<ElementType, Package>::value ||
260 std::is_same<ElementType, Component>::value ||
261 std::is_same<ElementType, Device>::value,
262 "Unsupported ElementType");
263 return addToCategory(getElementTable<ElementType>(), elementId, category);
264 }
265
276 template <typename ElementType>
277 int addResource(int elementId, const QString& name, const QString& mediaType,
278 const QUrl& url) {
279 static_assert(std::is_same<ElementType, Component>::value ||
280 std::is_same<ElementType, Device>::value,
281 "Unsupported ElementType");
282 return addResource(getElementTable<ElementType>(), elementId, name,
283 mediaType, url);
284 }
285
294 int addAlternativeName(int pkgId, const ElementName& name,
295 const SimpleString& reference);
296
297 // Helper Functions
298
305 template <typename ElementType>
306 static QString getElementTable() noexcept;
307
315 template <typename ElementType>
316 static QString getCategoryTable() noexcept;
317
318 // Operator Overloadings
320 delete;
321
322private: // Methods
323 int addElement(const QString& elementsTable, int libId, const FilePath& fp,
324 const Uuid& uuid, const Version& version, bool deprecated);
325 int addCategory(const QString& categoriesTable, int libId, const FilePath& fp,
326 const Uuid& uuid, const Version& version, bool deprecated,
327 const tl::optional<Uuid>& parent);
328 void removeElement(const QString& elementsTable, const FilePath& fp);
329 void removeAllElements(const QString& elementsTable);
330 int addTranslation(const QString& elementsTable, int elementId,
331 const QString& locale,
332 const tl::optional<ElementName>& name,
333 const tl::optional<QString>& description,
334 const tl::optional<QString>& keywords);
335 void removeAllTranslations(const QString& elementsTable);
336 int addToCategory(const QString& elementsTable, int elementId,
337 const Uuid& category);
338 int addResource(const QString& elementsTable, int elementId,
339 const QString& name, const QString& mediaType,
340 const QUrl& url);
341 QString filePathToString(const FilePath& fp) const noexcept;
342 static QString nonNull(const QString& s) noexcept;
343
344private: // Data
347};
348
349/*******************************************************************************
350 * End of File
351 ******************************************************************************/
352
353} // namespace librepcb
354
355#endif
The Attribute class.
Definition: attribute.h:46
This class represents absolute, well-formatted paths to files or directories.
Definition: filepath.h:129
The SQLiteDatabase class.
Definition: sqlitedatabase.h:43
The Uuid class is a replacement for QUuid to get UUID strings without {} braces.
Definition: uuid.h:58
The Version class represents a version number in the format "1.42.7".
Definition: version.h:58
Database write functions for librepcb::WorkspaceLibraryDb.
Definition: workspacelibrarydbwriter.h:57
SQLiteDatabase & mDb
Definition: workspacelibrarydbwriter.h:346
int addAlternativeName(int pkgId, const ElementName &name, const SimpleString &reference)
Add an alternative name to a previously added package.
Definition: workspacelibrarydbwriter.cpp:412
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:146
int addPartAttribute(int partId, const Attribute &attribute)
Add an attribute to a previously added part.
Definition: workspacelibrarydbwriter.cpp:396
QString filePathToString(const FilePath &fp) const noexcept
Definition: workspacelibrarydbwriter.cpp:607
void updateLibrary(const FilePath &fp, const Uuid &uuid, const Version &version, bool deprecated, const QByteArray &iconPng, const QString &manufacturer)
Update library metadata.
Definition: workspacelibrarydbwriter.cpp:346
int addElement(int libId, const FilePath &fp, const Uuid &uuid, const Version &version, bool deprecated)
Add a library element.
Definition: workspacelibrarydbwriter.h:123
int addPart(int devId, const QString &mpn, const QString &manufacturer)
Add a part to a previously added device.
Definition: workspacelibrarydbwriter.cpp:384
void removeElement(const FilePath &fp)
Remove a library element.
Definition: workspacelibrarydbwriter.h:201
int addToCategory(int elementId, const Uuid &category)
Add a library element to a category.
Definition: workspacelibrarydbwriter.h:257
void addInternalData(const QString &key, int value)
Add an integer value to the "internal" table.
Definition: workspacelibrarydbwriter.cpp:319
FilePath mLibrariesRoot
Definition: workspacelibrarydbwriter.h:345
int addLibrary(const FilePath &fp, const Uuid &uuid, const Version &version, bool deprecated, const QByteArray &iconPng, const QString &manufacturer)
Add a library.
Definition: workspacelibrarydbwriter.cpp:328
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:230
int addResource(int elementId, const QString &name, const QString &mediaType, const QUrl &url)
Add a resource for a library element.
Definition: workspacelibrarydbwriter.h:277
static QString nonNull(const QString &s) noexcept
Definition: workspacelibrarydbwriter.cpp:612
~WorkspaceLibraryDbWriter() noexcept
Definition: workspacelibrarydbwriter.cpp:56
static QString getElementTable() noexcept
Get the table name of an element type.
void removeAllElements()
Remove all library elements of a specific type.
Definition: workspacelibrarydbwriter.h:214
WorkspaceLibraryDbWriter(const WorkspaceLibraryDbWriter &other)=delete
void createAllTables()
Create all tables to initialize the database.
Definition: workspacelibrarydbwriter.cpp:63
static QString getCategoryTable() noexcept
Get the category table name of an element type.
void removeAllTranslations()
Remove all translations for a library element type.
Definition: workspacelibrarydbwriter.h:244
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:363
Definition: occmodel.cpp:77
type_safe::constrained_type< QString, SimpleStringConstraint, SimpleStringVerifier > SimpleString
Definition: simplestring.h:80
type_safe::constrained_type< QString, ElementNameConstraint, ElementNameVerifier > ElementName
Definition: elementname.h:84
Definition: uuid.h:183