LibrePCB Developers Documentation
Loading...
Searching...
No Matches
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 <QtCore>
31
32#include <optional>
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
123 template <typename ElementType>
124 int addElement(int libId, const FilePath& fp, const Uuid& uuid,
125 const Version& version, bool deprecated,
126 const QString& generatedBy) {
127 static_assert(std::is_same<ElementType, Symbol>::value ||
128 std::is_same<ElementType, Package>::value ||
129 std::is_same<ElementType, Component>::value,
130 "Unsupported ElementType");
131 return addElement(getElementTable<ElementType>(), libId, fp, uuid, version,
132 deprecated, generatedBy);
133 }
134
147 template <typename ElementType>
148 int addCategory(int libId, const FilePath& fp, const Uuid& uuid,
149 const Version& version, bool deprecated,
150 const std::optional<Uuid>& parent) {
151 static_assert(std::is_same<ElementType, ComponentCategory>::value ||
152 std::is_same<ElementType, PackageCategory>::value,
153 "Unsupported ElementType");
154 return addCategory(getElementTable<ElementType>(), libId, fp, uuid, version,
155 deprecated, parent);
156 }
157
171 int addDevice(int libId, const FilePath& fp, const Uuid& uuid,
172 const Version& version, bool deprecated,
173 const QString& generatedBy, const Uuid& component,
174 const Uuid& package);
175
184 int addPart(int devId, const QString& mpn, const QString& manufacturer);
185
193 int addPartAttribute(int partId, const Attribute& attribute);
194
204 template <typename ElementType>
205 void removeElement(const FilePath& fp) {
206 removeElement(getElementTable<ElementType>(), fp);
207 }
208
217 template <typename ElementType>
219 removeAllElements(getElementTable<ElementType>());
220 }
221
233 template <typename ElementType>
234 int addTranslation(int elementId, const QString& locale,
235 const std::optional<ElementName>& name,
236 const std::optional<QString>& description,
237 const std::optional<QString>& keywords) {
238 return addTranslation(getElementTable<ElementType>(), elementId, locale,
239 name, description, keywords);
240 }
241
247 template <typename ElementType>
249 removeAllTranslations(getElementTable<ElementType>());
250 }
251
260 template <typename ElementType>
261 int addToCategory(int elementId, const Uuid& category) {
262 static_assert(std::is_same<ElementType, Symbol>::value ||
263 std::is_same<ElementType, Package>::value ||
264 std::is_same<ElementType, Component>::value ||
265 std::is_same<ElementType, Device>::value,
266 "Unsupported ElementType");
267 return addToCategory(getElementTable<ElementType>(), elementId, category);
268 }
269
280 template <typename ElementType>
281 int addResource(int elementId, const QString& name, const QString& mediaType,
282 const QUrl& url) {
283 static_assert(std::is_same<ElementType, Component>::value ||
284 std::is_same<ElementType, Device>::value,
285 "Unsupported ElementType");
286 return addResource(getElementTable<ElementType>(), elementId, name,
287 mediaType, url);
288 }
289
298 int addAlternativeName(int pkgId, const ElementName& name,
299 const SimpleString& reference);
300
301 // Helper Functions
302
309 template <typename ElementType>
310 static QString getElementTable() noexcept;
311
319 template <typename ElementType>
320 static QString getCategoryTable() noexcept;
321
322 // Operator Overloadings
324 delete;
325
326private: // Methods
327 int addElement(const QString& elementsTable, int libId, const FilePath& fp,
328 const Uuid& uuid, const Version& version, bool deprecated,
329 const QString& generatedBy);
330 int addCategory(const QString& categoriesTable, int libId, const FilePath& fp,
331 const Uuid& uuid, const Version& version, bool deprecated,
332 const std::optional<Uuid>& parent);
333 void removeElement(const QString& elementsTable, const FilePath& fp);
334 void removeAllElements(const QString& elementsTable);
335 int addTranslation(const QString& elementsTable, int elementId,
336 const QString& locale,
337 const std::optional<ElementName>& name,
338 const std::optional<QString>& description,
339 const std::optional<QString>& keywords);
340 void removeAllTranslations(const QString& elementsTable);
341 int addToCategory(const QString& elementsTable, int elementId,
342 const Uuid& category);
343 int addResource(const QString& elementsTable, int elementId,
344 const QString& name, const QString& mediaType,
345 const QUrl& url);
346 QString filePathToString(const FilePath& fp) const noexcept;
347 static QString nonEmptyOrNull(const QString& s) noexcept;
348 static QString nonNull(const QString& s) noexcept;
349
350private: // Data
353};
354
355/*******************************************************************************
356 * End of File
357 ******************************************************************************/
358
359} // namespace librepcb
360
361#endif
The Attribute class.
Definition attribute.h:46
This class represents absolute, well-formatted paths to files or directories.
Definition filepath.h:127
The SQLiteDatabase class.
Definition sqlitedatabase.h:43
The Uuid class is a replacement for QUuid to get UUID strings without {} braces.
Definition uuid.h:56
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:352
int addAlternativeName(int pkgId, const ElementName &name, const SimpleString &reference)
Add an alternative name to a previously added package.
Definition workspacelibrarydbwriter.cpp:418
int addTranslation(int elementId, const QString &locale, const std::optional< ElementName > &name, const std::optional< QString > &description, const std::optional< QString > &keywords)
Add a translation for a library element.
Definition workspacelibrarydbwriter.h:234
int addDevice(int libId, const FilePath &fp, const Uuid &uuid, const Version &version, bool deprecated, const QString &generatedBy, const Uuid &component, const Uuid &package)
addElement() specialized for devices
Definition workspacelibrarydbwriter.cpp:367
int addPartAttribute(int partId, const Attribute &attribute)
Add an attribute to a previously added part.
Definition workspacelibrarydbwriter.cpp:402
static QString nonEmptyOrNull(const QString &s) noexcept
Definition workspacelibrarydbwriter.cpp:617
QString filePathToString(const FilePath &fp) const noexcept
Definition workspacelibrarydbwriter.cpp:612
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:350
int addPart(int devId, const QString &mpn, const QString &manufacturer)
Add a part to a previously added device.
Definition workspacelibrarydbwriter.cpp:390
void removeElement(const FilePath &fp)
Remove a library element.
Definition workspacelibrarydbwriter.h:205
int addToCategory(int elementId, const Uuid &category)
Add a library element to a category.
Definition workspacelibrarydbwriter.h:261
int addCategory(int libId, const FilePath &fp, const Uuid &uuid, const Version &version, bool deprecated, const std::optional< Uuid > &parent)
addElement() specialized for categories
Definition workspacelibrarydbwriter.h:148
void addInternalData(const QString &key, int value)
Add an integer value to the "internal" table.
Definition workspacelibrarydbwriter.cpp:323
FilePath mLibrariesRoot
Definition workspacelibrarydbwriter.h:351
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:332
int addResource(int elementId, const QString &name, const QString &mediaType, const QUrl &url)
Add a resource for a library element.
Definition workspacelibrarydbwriter.h:281
static QString nonNull(const QString &s) noexcept
Definition workspacelibrarydbwriter.cpp:621
~WorkspaceLibraryDbWriter() noexcept
Definition workspacelibrarydbwriter.cpp:56
int addElement(int libId, const FilePath &fp, const Uuid &uuid, const Version &version, bool deprecated, const QString &generatedBy)
Add a library element.
Definition workspacelibrarydbwriter.h:124
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:218
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:248
Definition occmodel.cpp:76
type_safe::constrained_type< QString, SimpleStringConstraint, SimpleStringVerifier > SimpleString
Definition simplestring.h:79
type_safe::constrained_type< QString, ElementNameConstraint, ElementNameVerifier > ElementName
Definition elementname.h:84
Definition uuid.h:186