-
Notifications
You must be signed in to change notification settings - Fork 0
/
Menu.py
executable file
·304 lines (255 loc) · 8.73 KB
/
Menu.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
from SliceField import SliceField
from SliceEnv import SliceEnv
from SliceCondition import SliceCondition
from SliceQuery import SliceQuery
class Menu :
def __init__ ( self ) :
self.methodList = ( self.createDatabase, self.addRecord, self.deleteRecord,
self.bulkLoad, self.join, self.runQuery, self.report1, self.report2 )
self.env = SliceEnv ()
def getDatabaseServices ( self ) :
choice = self.getMenu ()
while choice != 9 :
self.methodList [choice - 1] ()
i = input ( "Press any key to continue" )
choice = self.getMenu ()
def createDatabase ( self ) :
while True :
try :
tableName = input ( "Enter Table Name: " )
while True :
fieldCount = input ( "Enter the number of columns: " )
try :
fieldCount = int ( fieldCount )
if fieldCount < 1 :
raise ValueError
else :
break
except ValueError as e :
print ( "You have entered a wrong value for number of columns" )
print ( "Please enter a correct value" )
schema = list ()
while fieldCount != 0 :
while True :
try :
columnName = input ( "Enter Column Name: " )
while True :
columnType = input ( "Enter the type for %s column: " % columnName )
if columnType == "STRING" :
schema.append ( SliceField ( columnName, SliceField.STRING ) )
break
elif columnType == "INT" :
schema.append ( SliceField ( columnName, SliceField.INT ) )
break
elif columnType == "FLOAT" :
schema.append ( SliceField ( columnName, SliceField.DOUBLE ) )
break
else :
print ( "You have Entered an incorrect type for %s column" % columnName )
print ( "Please try again" )
break
except Exception as e:
print ( "The following error occurred: %s" %e )
print ( "Please try again" )
fieldCount = fieldCount - 1
indexColumn = input ( "Enter an index column ( or press enter key to skip it) : " )
db = self.env.create ( tableName, schema, indexColumn )
self.env.close ()
break
except Exception as e:
print ( "The following error occurred: %s" %e )
print ( "Please try again" )
print ( "%s database has been created" % tableName )
def addRecord ( self ) :
while True :
try :
dbName = input ( "Enter the name of the database to whom you would like to add record: " )
db = self.env.open ( dbName )
break
except Exception as e :
print ( "The following error occurred: %s" %e )
print ( "Please try again" )
newRecord = db.createRecord ()
#Catch exception
while True :
try :
for column in newRecord.recordSchema :
columnName = column.field[0]
while True :
try :
newData = input ( "Enter a value for %s: " % columnName )
newRecord.set ( columnName, newData )
break
except Exception as e :
print ( "The following error occurred: %s" %e )
print ( "Please try again" )
db.set ( newRecord )
break
except Exception as e :
print ( "The following error occurred: %s" %e )
print ( "Please try again" )
db.commit ()
print ( "A new record have been added to the database named %s" % dbName )
def deleteRecord ( self ) :
while True :
try :
dbName = input ( "Enter the name of the database from whom you would like to delete record: " )
db = self.env.open ( dbName )
break
except Exception as e :
print ( "The following error occurred: %s" %e )
print ( "Please try again" )
if db.index == None :
print ( "%s do not have an index column. Hence, you are not permitted to delete a record from %s" % (dbName,dbName) )
else :
indexColumnName = db.schema[ db.index ][0]
key = input ( "Enter a value for %s to delete the corresponding record: " % indexColumnName )
try :
result = db.delete ( key )
except Exception as e :
print ( "The following error occurred: %s" %e )
return
if result == True :
db.commit ()
print ( "The record with %s having a value of %s has been deleted from %s" % ( indexColumnName, key, dbName ) )
else :
print ( "No record with %s having a value of %s was found in %s" % ( indexColumnName, key, dbName ) )
def bulkLoad ( self ) :
while True :
try :
dbName = input ( "Enter the name of the database into whom you would like to load the data: " )
db = self.env.open ( dbName )
break
except Exception as e :
print ( "The following error occurred: %s" %e )
print ( "Please try again" )
while True :
try :
fileName = input ( "Enter the name of the bulk load file: " )
db.load ( fileName )
break
except Exception as e :
print ( "The following error occurred relating to your bulk load file: %s" %e )
print ( "Please try again" )
print ( "The data has been loaded from %s into %s database" % ( fileName, dbName ) )
def join ( self ) :
while True :
try :
lhsName = input ( "Enter first database name: " )
lhs = self.env.open ( lhsName )
break
except Exception as e :
print ( "The following error occurred: %s" %e )
print ( "Please try again" )
while True :
try :
rhsName = input ( "Enter second database name: " )
rhs = self.env.open ( rhsName )
break
except Exception as e :
print ( "The following error occurred: %s" %e )
print ( "Please try again" )
userInput = input ( "Enter the join column (press enter to perform default join): " )
try :
if userInput == '' :
joinResult = lhs.join ( rhs )
else :
joinResult = lhs.join ( rhs, userInput )
except Exception as e:
print ( "The following error occurred: %s" %e )
return
print ( "Join result is as follows" )
self.printRecords ( joinResult )
def runQuery ( self ) :
while True :
try :
dbName = input ( "Enter the name of the database on which you would like to perform a query: " )
db = self.env.open ( dbName )
break
except Exception as e :
print ( "The following error occurred: %s" %e )
print ( "Please try again" )
resultColumn = list ()
while True :
value = input ( "Enter the name of a column to display" )
if value != '' :
break
print ( "The result of the query must have atleast one column" )
while value != '' :
resultColumn.append ( value )
value = input ( "Enter the name of a column to display (or press enter key if you have entered all the needed columns): " )
operatingColumn = input ( "Enter the name the column to be used for conditional checking records: " )
operator = input ( "Enter the operation you would like to perform: " )
while True :
if operator == "EQ" :
operator = SliceCondition.EQ
break
elif operator == "LT" :
operator = SliceCondition.LT
break
elif operator == "GT" :
operator = SliceCondition.GT
break
else :
print ( "You have entered a wrong operation" )
print ( "Please enter one among EQ, LT and GT operation" )
operator = input ( "Enter the operation you would like to perform: " )
literalValue = input ( "Enter the value you would like compare the %s values against: " % operatingColumn )
condition = SliceCondition ( operatingColumn, operator, literalValue )
query = SliceQuery ( resultColumn, dbName, condition )
try :
queryResult = db.query ( query )
except Exception as e:
print ( "Your query failed due to the following error: %s" %e )
return
self.printRecords ( queryResult )
def printRecords ( self, re ) :
schema = re[0].recordSchema
widthList = list()
for value in schema :
colName, colType = value.field
if colType == SliceField.INT or colType == SliceField.DOUBLE :
widthList.append (7)
print ( "{0:<7}".format ( colName ), end = '' )
else :
widthList.append (35)
print ( "{0:<35}".format ( colName ), end = '' )
print ()
print ( "{0:-<{width}}".format ( '-', width = sum (widthList) ) )
for value in re :
currentRecord = value.record
for i, element in enumerate ( currentRecord ) :
print ( "{0:<{width}}".format ( element, width = widthList[i] ), end = '' )
print ()
def report1 ( self ) :
pass
def report2 ( self ) :
pass
def getMenu ( self ) :
print ()
print ( "Slice database testing menu" )
print ( "1. Create Database" )
print ( "2. Add Record" )
print ( "3. Delete Record" )
print ( "4. Bulk Load" )
print ( "5. Natural Join" )
print ( "6. Run Query" )
print ( "7. Report 1" )
print ( "8. Report 2" )
print ( "9. Exit" )
print ()
while True :
choice = input ( "Enter your choice: ")
try :
choice = int ( choice )
if choice < 1 or choice > 9 :
raise ValueError
break
except ValueError as e :
print ( "You have entered a wrong choice" )
print ( "Please try again" )
print ()
return choice
if __name__ == "__main__" :
obj = Menu ()
obj.getDatabaseServices ()