forked from stz0117/MiniSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
API.py
142 lines (115 loc) · 4.46 KB
/
API.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import Catalog
import Index
import time
import Buffer
# import RecordManager
def initialize(path: str):
Catalog.__initialize__(path)
Index.__initialize__(path)
Buffer.__initialize__()
def save():
Catalog.__finalize__()
Index.__finalize__()
Buffer.__finalize__()
print("All tables have been saved.")
# an example of upcoming args
# S
# [{'attribute_name': 'ID', 'type': 'int', 'type_len': 0, 'unique': True},
# {'attribute_name': 'name', 'type': 'char', 'type_len': 12, 'unique': True},
# {'attribute_name': 'age', 'type': 'int', 'type_len': 0, 'unique': False},
# {'attribute_name': 'gender', 'type': 'char', 'type_len': 1, 'unique': False}]
# ID
def create_table(table_name: str, attributes: list, pk: str):
time_start = time.time()
Catalog.exists_table(table_name)
Index.create_table(table_name)
Catalog.create_table(table_name, attributes, pk)
Buffer.create_table(table_name)
time_end = time.time()
print("Successfully create table '%s', time elapsed : %fs." %
(table_name, time_end - time_start))
# e.g. name_index S name
# FAKE!
def create_index(index_name: str, table_name: str, indexed_attr: str):
Catalog.exists_index(index_name)
Catalog.create_index(index_name, table_name, indexed_attr)
Index.create_index(index_name, table_name, indexed_attr)
def drop_table(table_name: str):
time_start = time.time()
Catalog.not_exists_table(table_name)
Catalog.drop_table(table_name)
Buffer.drop_table(table_name)
Index.delete_table(table_name)
time_end = time.time()
print("Successfully drop table '%s', time elapsed : %fs." %
(table_name, time_end - time_start))
def drop_index(index_name: str):
Catalog.not_exists_index(index_name)
Catalog.drop_index(index_name)
# e.g.
# student
# ['*']
# [{'operator': '>', 'l_op': 'sage', 'r_op': 20},
# {'operator': '=', 'l_op': 'sgender', 'r_op': 'F'},
# {'operator': '<', 'l_op': 'sscore', 'r_op': 89.5}]
def select(table_name: str, attributes: list, where: list = None):
time_start = time.time()
Catalog.not_exists_table(table_name)
Catalog.check_select_statement(table_name, attributes, where)
#Index.select_from_table(table_name, attributes, where)
col_dic = Catalog.get_column_dic(table_name)
results = Buffer.find_record(table_name, col_dic, where)
numlist = []
if attributes == ['*']:
attributes = list(col_dic.keys())
numlist = list(col_dic.values())
else:
for att in attributes:
numlist.append(col_dic[att])
print_select(attributes, numlist, results)
time_end = time.time()
print(" time elapsed : %fs." % (time_end - time_start))
def print_select(columns_list, columns_list_num, results):
print('-' * (17 * len(columns_list_num) + 1))
for i in columns_list:
if len(str(i)) > 14:
output = str(i)[0:14]
else:
output = str(i)
print('|', output.center(15), end='')
print('|')
print('-' * (17 * len(columns_list_num) + 1))
for i in results:
for j in columns_list_num:
if len(str(i[j])) > 14:
output = str(i[j])[0:14]
else:
output = str(i[j])
print('|', output.center(15), end='')
print('|')
print('-' * (17 * len(columns_list_num) + 1))
print("Returned %d entries," % len(results), end='')
# e.g. student ['12345678', 'wy', 22, 'M']
def insert(table_name: str, values: list):
time_start = time.time()
Catalog.not_exists_table(table_name)
Catalog.check_types_of_table(table_name, values)
linenum =Buffer.insert_record(table_name, values)
Index.insert_into_table(table_name, values,linenum)
time_end = time.time()
print(" time elapsed : %fs." % (time_end - time_start))
# e.g. student [{'operator': '=', 'l_op': 'sno', 'r_op': '88888888'}]
def delete(table_name: str, where: list = None):
time_start = time.time()
Catalog.not_exists_table(table_name)
Catalog.check_select_statement(table_name, ['*'], where) # 从insert中借用的方法
col = Catalog.get_column_dic(table_name)
pklist=Buffer.delete_record(table_name, col, where)
Index.delete_from_table(table_name, pklist)
time_end = time.time()
print(" time elapsed : %fs." % (time_end - time_start))
def show_table(table_name: str):
Catalog.not_exists_table(table_name)
Catalog.show_table(table_name) # 显示属性名、类型、大小(char)、是否unique
def show_tables():
Catalog.show_tables()