forked from foliveirafilho/tpch-pgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload.py
More file actions
71 lines (63 loc) · 2.11 KB
/
load.py
File metadata and controls
71 lines (63 loc) · 2.11 KB
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
import os
from .sqlitedb import SQLiteDB
def clean_database(db_name, tables):
try:
conn = SQLiteDB(db_name)
try:
for table in tables:
conn.executeQuery("DROP TABLE IF EXISTS %s " % table)
except Exception as e:
print("unable to remove existing tables. %s" % e)
return 1
print("dropped existing tables")
conn.commit()
conn.close()
return 0
except Exception as e:
print("unable to connect to the database. %s" % e)
return 1
def create_schema(query_root, db_name, prep_query_dir):
try:
conn = SQLiteDB(db_name)
try:
queries = os.path.join(query_root, prep_query_dir, "create_tbl.sql")
conn.createTables(queries)
except Exception as e:
print("unable to run create tables. %s" % e)
return 1
conn.commit()
conn.close()
except Exception as e:
print("unable to connect to the database. %s" % e)
return 1
def load_tables(data_dir, db_name, tables, load_dir):
try:
conn = SQLiteDB(db_name)
try:
for table in tables[0:2]:
filepath = os.path.join(data_dir, load_dir, table.lower() + ".tbl.csv")
print(f"filepath={filepath}")
conn.copyFromCSV(filepath, separator="|", table=table.lower())
conn.commit()
except Exception as e:
print("unable to run load tables. %s" %e)
return 1
conn.close()
return 0
except Exception as e:
print("unable to connect to the database. %s" % e)
return 1
def index_tables(query_root, db_name, prep_query_dir):
try:
conn = SQLiteDB(db_name)
try:
conn.executeQueryFromFile(os.path.join(query_root, prep_query_dir, "create_idx.sql"))
conn.commit()
except Exception as e:
print("unable to run index tables. %s" % e)
return 1
conn.close()
return 0
except Exception as e:
print("unable to connect to the database. %s" % e)
return 1