|
@@ -2,7 +2,7 @@
|
|
#
|
|
#
|
|
# PartMgr - Database
|
|
# PartMgr - Database
|
|
#
|
|
#
|
|
-# Copyright 2014-2022 Michael Buesch <m@bues.ch>
|
|
|
|
|
|
+# Copyright 2014-2024 Michael Buesch <m@bues.ch>
|
|
#
|
|
#
|
|
# This program is free software; you can redistribute it and/or modify
|
|
# This program is free software; you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# it under the terms of the GNU General Public License as published by
|
|
@@ -91,7 +91,7 @@ class Database:
|
|
"Part database interface."
|
|
"Part database interface."
|
|
|
|
|
|
# Database version number
|
|
# Database version number
|
|
- DB_VERSION = 0
|
|
|
|
|
|
+ DB_VERSION = 1
|
|
|
|
|
|
# User editable parameters
|
|
# User editable parameters
|
|
USER_PARAMS = {
|
|
USER_PARAMS = {
|
|
@@ -116,10 +116,12 @@ class Database:
|
|
ver = self.getGlobalParameter(
|
|
ver = self.getGlobalParameter(
|
|
"partmgr_db_version")
|
|
"partmgr_db_version")
|
|
ver = ver.getDataInt() if ver else None
|
|
ver = ver.getDataInt() if ver else None
|
|
- if ver is None or ver != self.DB_VERSION:
|
|
|
|
|
|
+ if ver is None or ver < 0 or ver > self.DB_VERSION:
|
|
self.filename = None
|
|
self.filename = None
|
|
raise PartMgrError("Invalid database "
|
|
raise PartMgrError("Invalid database "
|
|
"version")
|
|
"version")
|
|
|
|
+ if ver == 0:
|
|
|
|
+ self.__upgrade_0to1() # Upgrade DB version to 1.
|
|
self.__setUserParameterDefaults()
|
|
self.__setUserParameterDefaults()
|
|
except (sql.Error, ValueError, TypeError) as e:
|
|
except (sql.Error, ValueError, TypeError) as e:
|
|
self.filename = None
|
|
self.filename = None
|
|
@@ -200,13 +202,15 @@ class Database:
|
|
|
|
|
|
def __initTables(self):
|
|
def __initTables(self):
|
|
entityColumns = "id INTEGER PRIMARY KEY AUTOINCREMENT, "\
|
|
entityColumns = "id INTEGER PRIMARY KEY AUTOINCREMENT, "\
|
|
- "name TEXT, description TEXT, "\
|
|
|
|
|
|
+ "name TEXT, "\
|
|
|
|
+ "description TEXT, "\
|
|
"flags INTEGER, "\
|
|
"flags INTEGER, "\
|
|
"createTimeStamp INTEGER, "\
|
|
"createTimeStamp INTEGER, "\
|
|
"modifyTimeStamp INTEGER"
|
|
"modifyTimeStamp INTEGER"
|
|
tables = (
|
|
tables = (
|
|
"parameters(" + entityColumns + ", "
|
|
"parameters(" + entityColumns + ", "
|
|
- "parentType INTEGER, parent INTEGER, "
|
|
|
|
|
|
+ "parentType INTEGER, "
|
|
|
|
+ "parent INTEGER, "
|
|
"data TEXT)",
|
|
"data TEXT)",
|
|
"parts(" + entityColumns + ", "
|
|
"parts(" + entityColumns + ", "
|
|
"category INTEGER)",
|
|
"category INTEGER)",
|
|
@@ -218,17 +222,22 @@ class Database:
|
|
"footprints(" + entityColumns + ", "
|
|
"footprints(" + entityColumns + ", "
|
|
"image TEXT)",
|
|
"image TEXT)",
|
|
"stock(" + entityColumns + ", "
|
|
"stock(" + entityColumns + ", "
|
|
- "part INTEGER, category INTEGER, "
|
|
|
|
|
|
+ "part INTEGER, "
|
|
|
|
+ "category INTEGER, "
|
|
"footprint INTEGER, "
|
|
"footprint INTEGER, "
|
|
"minQuantity INTEGER, "
|
|
"minQuantity INTEGER, "
|
|
"targetQuantity INTEGER, "
|
|
"targetQuantity INTEGER, "
|
|
"quantityUnits INTEGER)",
|
|
"quantityUnits INTEGER)",
|
|
"origins(" + entityColumns + ", "
|
|
"origins(" + entityColumns + ", "
|
|
- "stockItem INTEGER, supplier INTEGER, "
|
|
|
|
|
|
+ "stockItem INTEGER, "
|
|
|
|
+ "supplier INTEGER, "
|
|
"orderCode TEXT, "
|
|
"orderCode TEXT, "
|
|
- "price FLOAT, priceTimeStamp INTEGER)",
|
|
|
|
|
|
+ "price FLOAT, "
|
|
|
|
+ "priceTimeStamp INTEGER, "
|
|
|
|
+ "priceFact FLOAT DEFAULT 1.0)",
|
|
"storages(" + entityColumns + ", "
|
|
"storages(" + entityColumns + ", "
|
|
- "stockItem INTEGER, location INTEGER, "
|
|
|
|
|
|
+ "stockItem INTEGER, "
|
|
|
|
+ "location INTEGER, "
|
|
"quantity INTEGER)",
|
|
"quantity INTEGER)",
|
|
)
|
|
)
|
|
c = self.db.cursor()
|
|
c = self.db.cursor()
|
|
@@ -236,6 +245,13 @@ class Database:
|
|
c.execute("CREATE TABLE IF NOT EXISTS %s;" % table)
|
|
c.execute("CREATE TABLE IF NOT EXISTS %s;" % table)
|
|
self.__commit()
|
|
self.__commit()
|
|
|
|
|
|
|
|
+ def __upgrade_0to1(self):
|
|
|
|
+ print("Updating database version 0 to version 1.")
|
|
|
|
+ c = self.db.cursor()
|
|
|
|
+ c.execute("ALTER TABLE origins ADD COLUMN priceFact FLOAT DEFAULT 1.0;")
|
|
|
|
+ self.getGlobalParameter("partmgr_db_version").setData(1)
|
|
|
|
+ self.__commit()
|
|
|
|
+
|
|
def __sqlIsEmpty(self):
|
|
def __sqlIsEmpty(self):
|
|
try:
|
|
try:
|
|
c = self.db.cursor()
|
|
c = self.db.cursor()
|
|
@@ -1290,11 +1306,18 @@ class Database:
|
|
id = Entity.toId(origin)
|
|
id = Entity.toId(origin)
|
|
try:
|
|
try:
|
|
c = self.db.cursor()
|
|
c = self.db.cursor()
|
|
- c.execute("SELECT name, description, flags, "
|
|
|
|
|
|
+ c.execute("SELECT "
|
|
|
|
+ "name, "
|
|
|
|
+ "description, "
|
|
|
|
+ "flags, "
|
|
"createTimeStamp, "
|
|
"createTimeStamp, "
|
|
"modifyTimeStamp, "
|
|
"modifyTimeStamp, "
|
|
- "stockItem, supplier, orderCode, "
|
|
|
|
- "price, priceTimeStamp "
|
|
|
|
|
|
+ "stockItem, "
|
|
|
|
+ "supplier, "
|
|
|
|
+ "orderCode, "
|
|
|
|
+ "price, "
|
|
|
|
+ "priceTimeStamp, "
|
|
|
|
+ "priceFact "
|
|
"FROM origins "
|
|
"FROM origins "
|
|
"WHERE id=?;",
|
|
"WHERE id=?;",
|
|
(int(id),))
|
|
(int(id),))
|
|
@@ -1311,6 +1334,7 @@ class Database:
|
|
orderCode = fromBase64(data[7]),
|
|
orderCode = fromBase64(data[7]),
|
|
price = float(data[8]),
|
|
price = float(data[8]),
|
|
priceTimeStamp = int(data[9]),
|
|
priceTimeStamp = int(data[9]),
|
|
|
|
+ priceFact = float(data[10]),
|
|
id = id,
|
|
id = id,
|
|
db = self)
|
|
db = self)
|
|
except (sql.Error, ValueError, TypeError) as e:
|
|
except (sql.Error, ValueError, TypeError) as e:
|
|
@@ -1323,11 +1347,18 @@ class Database:
|
|
stockItemId = Entity.toId(stockItem)
|
|
stockItemId = Entity.toId(stockItem)
|
|
try:
|
|
try:
|
|
c = self.db.cursor()
|
|
c = self.db.cursor()
|
|
- c.execute("SELECT id, name, description, flags, "
|
|
|
|
|
|
+ c.execute("SELECT "
|
|
|
|
+ "id, "
|
|
|
|
+ "name, "
|
|
|
|
+ "description, "
|
|
|
|
+ "flags, "
|
|
"createTimeStamp, "
|
|
"createTimeStamp, "
|
|
"modifyTimeStamp, "
|
|
"modifyTimeStamp, "
|
|
- "supplier, orderCode, "
|
|
|
|
- "price, priceTimeStamp "
|
|
|
|
|
|
+ "supplier, "
|
|
|
|
+ "orderCode, "
|
|
|
|
+ "price, "
|
|
|
|
+ "priceTimeStamp, "
|
|
|
|
+ "priceFact "
|
|
"FROM origins "
|
|
"FROM origins "
|
|
"WHERE stockItem=?;",
|
|
"WHERE stockItem=?;",
|
|
(int(stockItemId),))
|
|
(int(stockItemId),))
|
|
@@ -1344,6 +1375,7 @@ class Database:
|
|
orderCode = fromBase64(d[7]),
|
|
orderCode = fromBase64(d[7]),
|
|
price = float(d[8]),
|
|
price = float(d[8]),
|
|
priceTimeStamp = int(d[9]),
|
|
priceTimeStamp = int(d[9]),
|
|
|
|
+ priceFact = float(d[10]),
|
|
id = int(d[0]),
|
|
id = int(d[0]),
|
|
db = self)
|
|
db = self)
|
|
for d in data ]
|
|
for d in data ]
|
|
@@ -1359,12 +1391,18 @@ class Database:
|
|
c = self.db.cursor()
|
|
c = self.db.cursor()
|
|
if origin.inDatabase(self):
|
|
if origin.inDatabase(self):
|
|
c.execute("UPDATE origins "
|
|
c.execute("UPDATE origins "
|
|
- "SET name=?, description=?, flags=?, "
|
|
|
|
|
|
+ "SET "
|
|
|
|
+ "name=?, "
|
|
|
|
+ "description=?, "
|
|
|
|
+ "flags=?, "
|
|
"createTimeStamp=?, "
|
|
"createTimeStamp=?, "
|
|
"modifyTimeStamp=?, "
|
|
"modifyTimeStamp=?, "
|
|
- "stockItem=?, supplier=?, "
|
|
|
|
|
|
+ "stockItem=?, "
|
|
|
|
+ "supplier=?, "
|
|
"orderCode=?, "
|
|
"orderCode=?, "
|
|
- "price=?, priceTimeStamp=? "
|
|
|
|
|
|
+ "price=?, "
|
|
|
|
+ "priceTimeStamp=?, "
|
|
|
|
+ "priceFact=? "
|
|
"WHERE id=?;",
|
|
"WHERE id=?;",
|
|
(toBase64(origin.name),
|
|
(toBase64(origin.name),
|
|
toBase64(origin.description),
|
|
toBase64(origin.description),
|
|
@@ -1376,16 +1414,23 @@ class Database:
|
|
toBase64(origin.orderCode),
|
|
toBase64(origin.orderCode),
|
|
float(origin.price),
|
|
float(origin.price),
|
|
int(origin.getPriceTimeStampInt()),
|
|
int(origin.getPriceTimeStampInt()),
|
|
|
|
+ float(origin.priceFact),
|
|
int(origin.id)))
|
|
int(origin.id)))
|
|
else:
|
|
else:
|
|
c.execute("INSERT INTO "
|
|
c.execute("INSERT INTO "
|
|
- "origins(name, description, flags, "
|
|
|
|
|
|
+ "origins("
|
|
|
|
+ "name, "
|
|
|
|
+ "description, "
|
|
|
|
+ "flags, "
|
|
"createTimeStamp, "
|
|
"createTimeStamp, "
|
|
"modifyTimeStamp, "
|
|
"modifyTimeStamp, "
|
|
- "stockItem, supplier, "
|
|
|
|
|
|
+ "stockItem, "
|
|
|
|
+ "supplier, "
|
|
"orderCode, "
|
|
"orderCode, "
|
|
- "price, priceTimeStamp) "
|
|
|
|
- "VALUES(?,?,?,?,?,?,?,?,?,?);",
|
|
|
|
|
|
+ "price, "
|
|
|
|
+ "priceTimeStamp, "
|
|
|
|
+ "priceFact) "
|
|
|
|
+ "VALUES(?,?,?,?,?,?,?,?,?,?,?);",
|
|
(toBase64(origin.name),
|
|
(toBase64(origin.name),
|
|
toBase64(origin.description),
|
|
toBase64(origin.description),
|
|
int(origin.flags),
|
|
int(origin.flags),
|
|
@@ -1395,7 +1440,8 @@ class Database:
|
|
int(origin.supplier),
|
|
int(origin.supplier),
|
|
toBase64(origin.orderCode),
|
|
toBase64(origin.orderCode),
|
|
float(origin.price),
|
|
float(origin.price),
|
|
- int(origin.getPriceTimeStampInt())))
|
|
|
|
|
|
+ int(origin.getPriceTimeStampInt()),
|
|
|
|
+ float(origin.priceFact)))
|
|
origin.id = c.lastrowid
|
|
origin.id = c.lastrowid
|
|
origin.db = self
|
|
origin.db = self
|
|
self.__commit()
|
|
self.__commit()
|