-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalue_generation.py
executable file
·66 lines (50 loc) · 2.11 KB
/
value_generation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/env python3
# These functions describe ways to get / compute values which
# aren't known at the time a employee puts together their yaml files.
# No error handling. Calling function needs to take care of that.
# Look up a value by a parsed sql query.
def generateBySQL(sqlConnection, query, k, fl, vl, verbose = False):
import sql_interop as si # Database connection.
import inspect # For reflection.
print(f"Pulling information for {k} from sql query {query}")
# Retreive the value by provided sql query.
resultList = si.fetchData(sqlConnection, query)
# Add to lists by reference.
try:
retrievedValue = resultList[0][k]
except Exception as e:
print(f"Got a result but could not retreive value. {e}")
print(resultList)
print(f"SQL query retrieved {retrievedValue} for {k}.")
# Append the new found key and value to respective lists.
fl.append(k)
vl.append(retrievedValue)
# Automatically generate a value.
# Retrieve the primary key. It is to be incremented.
def autoGenerate(sqlConnection, tableName, k, fl, vl, computation, verbose = False):
import sql_interop as si # For looking up the primary key.
import numbering as n # For incrementation functions.
# Retrieve the primary key for a specific table.
primaryKey = si.getPrimaryKey(sqlConnection, tableName)
if verbose:
print(
f"Automaticalle generating value for {primaryKey} in {tableName}."
)
# Try to get new maximum.
try:
oldMax = si.fetchData(
sqlConnection
# Build the query string simply by using variables about
# the pk and table.
, f"SELECT MAX({primaryKey}) AS OLD_MAX FROM `{tableName}`"
)[0]["OLD_MAX"]
# Append the key.
fl.append(k)
# Calculate and append the new maximum number.
newMax = n.getNextNumber(oldMax, computation)
vl.append(newMax)
print(
f"Auto generated primary key value {newMax} for table {tableName}"
)
except Exception as e:
print(f"Error while auto generating id. {e}")