Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
68 lines (52 loc) · 2.4 KB
/
Copy pathmain.py
File metadata and controls
68 lines (52 loc) · 2.4 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
'''
Main program for CS 457 Final Project
Spring 2015
Daniel Klein
'''
import re
from DataLoader import DataLoader
from Table import Table
from Database import Database
from QueryExecutor import QueryExecutor
from ResultWriter import ResultWriter
DATA_DIR = ""
INPUT_FILENAME = "input.txt"
OUTPUT_FILENAME = "results.txt"
def main():
print("**************************************************")
print("**************************************************")
print("**** Daniel Klein ****")
print("**** CS 457 ****")
print("**** Final Project ****")
print("**************************************************")
print("**************************************************")
print("**** Initializing database from data files... ****")
database = DataLoader(DATA_DIR).load()
print("**** Database initialized. ****")
print("**** ****")
with open(INPUT_FILENAME, 'r') as input_file:
print("**** Reading input file... ****")
file_text = input_file.read().strip()
print("**** Input file loaded. ****")
print("**** ****")
queries = file_text.split(';')
executor = QueryExecutor(database)
with open(OUTPUT_FILENAME, 'w') as output_file:
writer = ResultWriter(output_file)
for query in queries:
# get the entire query on one line, remove excess whitespace,
# and add back the semicolon
clean_query = query.replace('\n', ' ').strip() + ";"
if not re.search(r"\w", clean_query): # the blank line at the end...
continue
print("**** Executing query: {}".format(clean_query))
print("**** ****")
(col_names, rows) = executor.perform_query(clean_query)
writer.write_result(col_names, rows)
print("**** Result saved to file. ****")
print("**** ****")
print("**** All queries completed. ****")
print("**** Results are in {} ****".format(OUTPUT_FILENAME))
print("**************************************************")
return 0;
main()