-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_inserts.py
103 lines (85 loc) · 3.42 KB
/
generate_inserts.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# Generates SQL insert statements in order to populate a database with test data
import csv
import os
import sys
import sqlalchemy as sa
def mssql_engine(user=os.getenv('SA_USERNAME'), password=os.getenv('SA_PASSWORD'), host=os.getenv('SERVER_ADDRESS'), db=os.getenv('DB_NAME'), driver=os.getenv('DB_DRIVER')):
engine = sa.create_engine(
f'mssql+pyodbc://{user}:{password}@{host}/{db}?driver={driver}&Trusted_Connection=no&Encrypt=no')
return engine
def get_table_structure(table_name: str):
SQL_STATEMENT = """
SELECT
c.name,
t.Name 'data_type',
c.max_length,
c.precision ,
c.scale ,
c.is_nullable,
ISNULL(i.is_primary_key, 0) 'is_primary_key'
FROM
sys.columns c
INNER JOIN
sys.types t ON c.user_type_id = t.user_type_id
LEFT OUTER JOIN
sys.index_columns ic ON ic.object_id = c.object_id AND ic.column_id = c.column_id
LEFT OUTER JOIN
sys.indexes i ON ic.object_id = i.object_id AND ic.index_id = i.index_id
WHERE
c.object_id = OBJECT_ID(:table_name)
"""
engine = mssql_engine()
with engine.connect() as conn:
result = conn.execute(sa.text(SQL_STATEMENT), {
"table_name": table_name})
column_list = result.mappings().all()
return column_list
def create_insert_statement(table_name: str, column_list: str, column_values):
table = get_table_structure(table_name)
columns = (column_list.split(','))
statement = 'insert into ' + table_name + ' (' + column_list + ') values ('
for index, item in enumerate(columns):
column_value = column_values[index]
column_data = [d for d in table if d['name'] == item]
if len(column_data) > 0:
data_type = column_data[0]['data_type']
if column_value == '':
data_type = 'null'
match data_type:
case 'null':
statement = statement + 'null'
case 'date':
statement = statement + (f"'{column_value}'")
case 'datetime2':
statement = statement + (f"'{column_value}'")
case 'varchar':
statement = statement + (f"'{column_value}'")
case _: # we can leave decimals as is
statement = statement + column_value
statement = statement + ','
else:
print('Value for ' + item + ' not found!')
statement = statement[:-1]
statement = statement + ');'
return statement
def process_csv_file(source_file_name: str, target_file_name: str, table_name: str):
# Open file
with open(source_file_name) as file_obj:
# Create reader object by passing the file object to reader method
column_list = next(file_obj).replace('\n', '').replace('\r', '')
reader_obj = csv.reader(file_obj)
f = open(target_file_name, 'w')
# Iterate over each row in the csv file using reader object
for row in reader_obj:
statement = create_insert_statement(table_name, column_list, row)
f.write(statement + "\n")
# for i in range(len(row)):
# print(row[i])
f.write("GO\n")
f.close
# Parse command line parameters
source_file = sys.argv[1]
target_file = sys.argv[2]
table_name = sys.argv[3]
# Start processing
process_csv_file(source_file, target_file, table_name)