|
@@ -4,7 +4,11 @@
|
|
|
# All rights reserved
|
|
|
#
|
|
|
# THIS SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY
|
|
|
-from sqlite3 import Cursor
|
|
|
+from psycopg import Cursor
|
|
|
+from psycopg.sql import (
|
|
|
+ SQL,
|
|
|
+ Literal,
|
|
|
+)
|
|
|
import time
|
|
|
from typing import Any, Callable
|
|
|
from .TransactionView import (
|
|
@@ -144,3 +148,16 @@ class QueryManager(object):
|
|
|
|
|
|
def insert_new_product(self, product, category, group):
|
|
|
self.cursor.execute(get_insert_product_statement(product, category, group))
|
|
|
+
|
|
|
+ def get_preferred_unit(self, product):
|
|
|
+ self.cursor.execute(SQL("""SELECT
|
|
|
+ units.name AS preferred_unit
|
|
|
+FROM products
|
|
|
+JOIN units ON unit_id = units.id
|
|
|
+WHERE products.name = {product}""").format(product=Literal(product)))
|
|
|
+ res = list(cursor_as_dict(self.cursor))
|
|
|
+ if len(res) == 0:
|
|
|
+ return None
|
|
|
+ assert len(res) == 1
|
|
|
+ return res[0]['preferred_unit']
|
|
|
+
|