/
curses_interface.py
940 lines (760 loc) · 32.5 KB
/
curses_interface.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
#!/usr/bin/env python
import curses
import math
import database_queries
from time import sleep
DEBUG = False # automate input vs. manual db connection info
# ==================================================
# Name: MyApp(object)
#
# Purpose: Serves as the primary starting point of
# the application. creates an curses object, sets
# curses options, establishes connection with db,
# and directs user to db connect screen.
# ==================================================
class MyApp(object):
def __init__(self, stdscreen):
# Init the main curses program
self.screen = stdscreen
#curses.curs_set(0)
curses.cbreak()
curses.echo()
# Connect to database
con = connectToDatabase(self.screen)
# Menu starting point
printMainMenu(self.screen, con)
# ==================================================
# Name: connectToDatabase(stdscr)
#
# Purpose: Collects db connection information from
# the user including host IP address, db name, db
# user, and password. validates success/failure of
# db connection and informs user. Upon completion,
# redirects to main menu.
# ==================================================
def connectToDatabase(stdscr):
x,y = stdscr.getmaxyx()
yTest = 130
xTest = 70
while x < xTest and y < yTest:
stdscr.clear()
stdscr.addstr(10, 5, "Please make your terminal bigger!")
stdscr.addstr(11, 5, "Make larger until you see the welcome splash screen")
x,y = stdscr.getmaxyx()
stdscr.getstr(1, 1, 0)
if x < 120 or y < 60:
stdscr.clear()
stdscr.addstr(12, 5, "Terminal is almost big enough!")
stdscr.clear()
# Collect db data and validate connection
while(1):
# Clear the curses screen
stdscr.clear()
# Overwrite connection data in debug mode
if DEBUG:
username = "root"
host = "192.168.1.3"
password = ""
database = "test"
else:
stdscr.clear()
stdscr.nodelay(0)
stdscr.keypad(1)
selection = -2
option = 0
while selection < 0:
graphics = [0]*5
graphics[option] = curses.A_REVERSE
#ASCII art creddited to: http://patorjk.com/software/taag/#p=display&f=Graffiti&t=Welcome
stdscr.addstr(10, 5," __ __ __" )
stdscr.addstr(11, 5,"/ \ / \ ____ | | ____ ____ _____ ____" )
stdscr.addstr(12, 5,"\ \/\/ // __ \| | _/ ___\/ _ \ / \_/ __ \ ")
stdscr.addstr(13, 5," \ /\ ___/| |_\ \__( <_> ) Y Y \ ___/ ")
stdscr.addstr(14, 5," \__/\ / \___ >____/\___ >____/|__|_| /\___ >")
stdscr.addstr(15, 5," \/ \/ \/ \/ \/")
stdscr.addstr(15, 5,"Press enter to start the application",graphics[0])
action = stdscr.getch()
if action == (ord('\n')):
selection = option
stdscr.clear()
# Collect necessary connection info
stdscr.addstr(10, 5, "Enter the database host address:")
host = stdscr.getstr(10, 38, 15)
stdscr.clear()
stdscr.addstr(10, 5, "Enter the database username:")
username = stdscr.getstr(10, 38, 15)
stdscr.clear()
stdscr.addstr(10, 5, "Enter the database name:")
database = stdscr.getstr(10, 38, 15)
stdscr.clear()
stdscr.addstr(10, 5, "Enter the database password:")
password = stdscr.getstr(10, 38, 15)
stdscr.clear()
# Attempt to connect with db
con = database_queries.dbConnect(username, password, database, host)
# Connection successful
if con['state'] == 0:
# Inform user of connection
stdscr.addstr(10, 5, "Connection to Database Successful! Press Enter to Continue...")
stdscr.getstr(1, 1, 0)
break
# Connection Failed
else:
selection = -2
option = 0
while selection < 0:
graphics = [0]*2
graphics[option] = curses.A_REVERSE
failure_string = "Connection to Database Failed: %s" % (con['msg'])
stdscr.addstr(10, 5, failure_string)
stdscr.addstr(13, 13, "Retry",graphics[0])
stdscr.addstr(13, 35, "Exit Program",graphics[1])
stdscr.refresh()
# Collect user's navigation selection
action = stdscr.getch()
if action == curses.KEY_RIGHT:
option = (option - 1) % 2
elif action == curses.KEY_LEFT:
option = (option + 1) % 2
elif action == (ord('\n')):
selection = option
stdscr.clear()
# If user wants to disconnect... Exit
#if selection == 0:
#add in retry statement
# Return to main menu
if selection == 1:
curses.endwin()
exit()
# Return successful connection
return con
# ==================================================
# Name: printMainMenu(stdscr,con)
#
# Purpose: Serves as the main menu of the application.
# accepts user input and navigates to submenus as
# directed. Submenus redirect to this menu as well.
# makes use of the con[] dictionary provided by
# databases_queries.py.
# ==================================================
def printMainMenu(stdscr, con):
stdscr.clear()
stdscr.nodelay(0)
stdscr.keypad(1)
selection = -2
option = 0
# Print main menu options
while selection < 0:
stdscr.border(0)
# Print main menu header information
stdscr.addstr(1, 2, "HOST IP:")
stdscr.addstr(1, 60, con['host'])
stdscr.addstr(2, 2, "DB USER:")
stdscr.addstr(2, 60, con['user'])
stdscr.addstr(3, 2, "DATABASE NAME:")
stdscr.addstr(3, 60, con['database'])
stdscr.addstr(4, 2, "----------------------------------------------------------------------------")
stdscr.addstr(6, 30, "-- MAIN MENU --")
graphics = [0]*5
graphics[option] = curses.A_REVERSE
stdscr.addstr(9, 6, "View / Edit Table",graphics[0])
stdscr.addstr(11, 6, "Create Table", graphics[1])
stdscr.addstr(13, 6, "Delete Table",graphics[2])
stdscr.addstr(15, 6, "About",graphics[3])
stdscr.addstr(17, 6, "Log Off / Exit",graphics[4])
stdscr.refresh()
# Collect user's navigation selection
action = stdscr.getch()
if action == curses.KEY_UP:
option = (option - 1) % 5
elif action == curses.KEY_DOWN:
option = (option + 1) % 5
elif action == (ord('\n')):
selection = option
stdscr.clear()
# Navigate to submenu
if selection == 0:
printViewEditSubmenu(stdscr,con)
# Navigate to CreateTable
elif selection == 1:
printCreateTableSubmenu(stdscr,con)
# Navigate to DeleteTable
elif selection == 2:
printDeleteTableSubmenu(stdscr,con)
# Navigate to About
elif selection == 3:
printAboutSubmenu(stdscr,con)
# Navigate to LogOff
elif selection == 4:
printLogOffSubMenu(stdscr,con)
# Navigate to submenu
# if user_input == ord('1'):
# printViewEditSubmenu(stdscr,con)
# if user_input == ord('2'):
# printCreateTableSubmenu(stdscr,con)
# if user_input == ord('3'):
# printDeleteTableSubmenu(stdscr,con)
# if user_input == ord('4'):
# printAboutSubmenu(stdscr,con)
# if user_input == ord('5'):
# printLogOffSubMenu(stdscr,con)
# ==================================================
# Name: printViewEditSubmenu(stdscr,con)
#
# Purpose: This is the submenu that allows a user to
# view, edit, or search an existing table in the db
# that has been connected to. user can also return
# to the main menu.
# ==================================================
def printViewEditSubmenu(stdscr,con):
stdscr.clear()
stdscr.nodelay(0)
stdscr.keypad(1)
selection = -2
option = 0
# Print View Edit options
while selection < 0:
graphics = [0]*5
graphics[option] = curses.A_REVERSE
stdscr.addstr(4, 2, "----------------------------------------------------------------------------")
stdscr.addstr(6, 20, "-- VIEW / EDIT TABLE --")
stdscr.addstr(9, 6, "View Table",graphics[0])
stdscr.addstr(11, 6, "Edit Table",graphics[1])
stdscr.addstr(13, 6, "Back to Main Menu",graphics[2])
stdscr.refresh()
# Collect user's navigation selection
action = stdscr.getch()
if action == curses.KEY_UP:
option = (option - 1) % 3
elif action == curses.KEY_DOWN:
option = (option + 1) % 3
elif action == (ord('\n')):
selection = option
stdscr.clear()
# Navigate to submenu
if selection == 0:
printViewTableSubmenu(stdscr, con)
# Navigate to CreateTable
elif selection == 1:
printEditTableSubmenu(stdscr, con)
# Navigate to DeleteTable
elif selection == 2:
printMainMenu(stdscr, con)
# if user_input == ord('1'):
# printViewTableSubmenu(stdscr,con)
# if user_input == ord('2'):
# printEditTableSubmenu(stdscr,con)
# if user_input == ord('3'):
# printMainMenu(stdscr,con)
# ==================================================
# Name: printViewTableSubmenu(stdscr,con)
#
# Purpose: Menu for viewing tables in the database.
# returns all tables in a column format. Tables,
# that exceed screen space will be provided in add'l
# spaces.
# ==================================================
def printViewTableSubmenu(stdscr,con):
stdscr.clear()
stdscr.addstr(4, 2, "----------------------------------------------------------------------------")
stdscr.addstr(6, 30, "-- VIEW TABLE --")
stdscr.addstr(22, 45, "[B] Back")
# Attempt to get tables in database so that they can be printed
con = database_queries.dbTables(con)
# Detect if no tables exist
if con['tbl_cnt'] == 0:
stdscr.addstr(9, 6, "No tables in database")
# Collect user's navigation selection
user_input = stdscr.getch()
# Navigate to submenu
if user_input == ord('b') or user_input == ord('B'):
printViewEditSubmenu(stdscr,con)
# At least 1 table exists so print them
else:
y = 8
x = 6
# Print out each table name line by line up to 6 per screen
for idx, table in enumerate(con['tables']):
table_string = "[" + str(idx + 1) + "] " + table[0]
stdscr.addstr(y, x, table_string)
y += 2
# Detect the last row that can fit on a page (6th)
if idx != 0 and idx % 5 == 0:
# Detect if there are add'l rows past multiple of 6th
if (idx + 1) < con['tbl_cnt']:
stdscr.addstr(22, 15, "[N] Next")
#Collect user's navigation selection
user_input = stdscr.getch()
# Navigate to submenu
if user_input == ord('b') or user_input == ord('B'):
printViewEditSubmenu(stdscr,con)
# Paginate
elif user_input == ord('n') or user_input == ord('N'):
stdscr.clear()
stdscr.addstr(4, 2, "----------------------------------------------------------------------------")
stdscr.addstr(6, 30, "-- VIEW TABLE --")
stdscr.addstr(22, 45, "[B] Back")
y = 8
x = 6
# Navigate to view table
elif user_input >= ord('1') and user_input <= unichr(con['tbl_cnt']):
try:
printViewTableContentsSubmenu(stdscr, con, int(chr(user_input)) - 1)
except ValueError:
printViewTableSubmenu(stdscr, con)
elif user_input == ord('\n'):
printViewTableSubmenu(stdscr, con)
# Collect user's navigation selection
user_input = stdscr.getch()
# Navigate to submenu
if user_input == ord('b') or user_input == ord('B'):
printViewEditSubmenu(stdscr,con)
elif user_input >= ord('1') and user_input <= unichr(con['tbl_cnt']):
try:
printViewTableContentsSubmenu(stdscr, con, int(chr(user_input)) - 1)
except ValueError:
printViewTableSubmenu(stdscr, con) # Navigate to view table
if user_input == ord('\n'):
printViewTableSubmenu(stdscr, con)
# ==================================================
# Name: printViewTableContentsSubmenu(stdscr, con, idx)
#
# Purpose: displays a table's contents upon selection
# from the table select menu. rows will paginate in
# instances that each row exceeds the rows allowed on
# the screen. idx is the table desired in con['tables'].
# ==================================================
def printViewTableContentsSubmenu(stdscr, con, idx):
stdscr.clear()
stdscr.addstr(4, 2, "----------------------------------------------------------------------------")
stdscr.addstr(6, 30, "-- VIEW TABLE --")
# Create the query string to retrieve the contents of the selected table
table_string = "SELECT * FROM " + str(con['tables'][idx][0])
# Conduct SELECT query; results in con['rows']
con = database_queries.dbQuery(con, table_string)
y = 12
x = 14
c = 10 # New Y Coordinate for columns
s = 14 # New X Coordinate for columns
totalRows = "Number of Rows: " + str(con['cnt'])
stdscr.addstr(8, s, totalRows)
# TODO: add borders
# Printing Attribute names. https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-column-names.html
for idx, col in enumerate(con['cols']):
if idx != 11:
#mid = int(math.ceil(s / 2))
mid = int(math.ceil((s) - 3.5))
stdscr.addstr(c, mid, "|")
col_string = str(col)
if len(col_string) >= 11: # Trimming column name if it is too long.
col_string = col_string[:10]
stdscr.addstr(c, s, col_string)
s += 15
stdscr.addstr(y, 5, "--------------------------------------------------------------------------------------------------------------------------------------------------------")
y += 2
# Print out each row line by line up to 6 per screen
if con['cnt'] == 0:
stdscr.addstr(y, 14, "No Rows in Table!")
y += 2
stdscr.addstr(y, 5, "--------------------------------------------------------------------------------------------------------------------------------------------------------")
# Collect user's navigation selection
user_input = stdscr.getch()
if user_input == ord('b') or user_input == ord('B'):
printViewTableSubmenu(stdscr,con)
else:
mybool = False
count = 0
for idx, row in enumerate(con['rows']):
#Detect the last row that can fit on a page (6th)
if idx != 0 and idx % 11 == 0:
mybool = True
# Detect if there are add'l rows past multiple of 6th
if (idx + 1) < con['row_cnt']:
y += 2
stdscr.addstr(y, 15, "[N] Next")
stdscr.addstr(y, 45, "[B] Back")
#Collect user's navigation selection
user_input = stdscr.getch()
# Navigate to submenu
if user_input == ord('b') or user_input == ord('B'):
printViewTableSubmenu(stdscr,con)
# Paginate
elif user_input == ord('n') or user_input == ord('N'):
stdscr.clear()
stdscr.addstr(4, 2, "----------------------------------------------------------------------------")
stdscr.addstr(6, 30, "-- VIEW TABLE --")
y = 12
x = 14
# Collect user's navigation selection
#user_input = stdscr.getch()
count += 1
row_count = "[" + str(count) + "] "
x = 5
stdscr.addstr(y, x, row_count)
for ndx, col in enumerate(con['cols']): #Created new counter 'ndx' for traversing within the SQL row.
row_string = str(row[col])
#DO NOT TOUCH THIS LINE
if len(row_string) >= 11: # Trimming column name if it is too long.
row_string = row_string[:10]
if ndx == 0: # Do this for the first value.
x = 14
mid = int(math.ceil((x) - 3.5))
stdscr.addstr(y, mid, "|")
stdscr.addstr(y, x, row_string)
else:
x += 15
mid = int(math.ceil((x) - 3.5))
stdscr.addstr(y, mid, "|")
stdscr.addstr(y, x, row_string)
#y += 2
y += 2
#stdscr.addstr(y, 45, "[B] Back")
# y += 2
if mybool == False:
stdscr.addstr(y, 45, "[B] Back")
y += 2
stdscr.addstr(y, 5, "--------------------------------------------------------------------------------------")
# # Detect the last row that can fit on a page (6th)
# if idx != 0 and idx % 5 == 0:
# # Detect if there are add'l rows past multiple of 6th
# if (idx + 1) < con['row_cnt']:
# stdscr.addstr(22, 15, "[N] Next")
# #Collect user's navigation selection
# user_input = stdscr.getch()
# #---------------------------------------------------------------------
# # TODO: NEED TO REFACTOR WITH HIGHLIGHT
# #---------------------------------------------------------------------
# # Navigate to submenu
# if user_input == ord('b') or user_input == ord('B'):
# printViewEditSubmenu(stdscr,con)
# # Paginate
# elif user_input == ord('n') or user_input == ord('N'):
# stdscr.clear()
# stdscr.addstr(4, 2, "----------------------------------------------------------------------------")
# stdscr.addstr(6, 30, "-- VIEW TABLE --")
# stdscr.addstr(22, 45, "[B] Back")
# y = 8
# x = 6
# # Collect user's navigation selection
# user_input = stdscr.getch()
# #---------------------------------------------------------------------
# # TODO: NEED TO REFACTOR WITH HIGHLIGHT
# #---------------------------------------------------------------------
# # Navigate to submenu
# if user_input == ord('b') or user_input == ord('B'):
# printViewEditSubmenu(stdscr,con)
# ==================================================
# Name: printEditTableSubmenu(stdscr,con)
#
# Purpose: Provides a list of tables that can be
# edited. User will select a table for editing, or
# can return to the next higher submenu.
# ==================================================
def printEditTableSubmenu(stdscr,con):
stdscr.clear()
stdscr.addstr(4, 2, "----------------------------------------------------------------------------")
stdscr.addstr(6, 30, "-- EDIT TABLE --")
stdscr.addstr(22, 45, "[B] Back")
# Attempt to get tables in database so that they can be printed
con = database_queries.dbTables(con)
# Detect if no tables exist
if con['tbl_cnt'] == 0:
stdscr.addstr(9, 6, "No tables in database")
# Collect user's navigation selection
user_input = stdscr.getch()
# Navigate to submenu
if user_input == ord('b') or user_input == ord('B'):
printViewEditSubmenu(stdscr,con)
# At least 1 table exists so print them
else:
y = 8
x = 6
# Print out each table name line by line up to 6 per screen
for idx, table in enumerate(con['tables']):
table_string = "[" + str(idx + 1) + "] " + table[0]
stdscr.addstr(y, x, table_string)
y += 2
# Detect the last row that can fit on a page (6th)
if idx != 0 and idx % 5 == 0:
# Detect if there are add'l rows past multiple of 6th
if (idx + 1) < con['tbl_cnt']:
stdscr.addstr(22, 15, "[N] Next")
#Collect user's navigation selection
user_input = stdscr.getch()
# Navigate to submenu
if user_input == ord('b') or user_input == ord('B'):
printViewEditSubmenu(stdscr,con)
# Paginate
elif user_input == ord('n') or user_input == ord('N'):
stdscr.clear()
stdscr.addstr(4, 2, "----------------------------------------------------------------------------")
stdscr.addstr(6, 30, "-- EDIT TABLE --")
stdscr.addstr(22, 45, "[B] Back")
y = 8
x = 6
# Navigate to view table
elif user_input >= ord('1') and user_input <= unichr(con['tbl_cnt']):
try:
printViewTableContentsSubmenu(stdscr, con, int(chr(user_input)) - 1)
except ValueError:
printViewTableSubmenu(stdscr, con)
elif user_input == ord('\n'):
printViewTableSubmenu(stdscr, con)
# Collect user's navigation selection
user_input = stdscr.getch()
# Navigate to submenu
if user_input == ord('b') or user_input == ord('B'):
printViewEditSubmenu(stdscr,con)
# Navigate to view table
elif user_input >= ord('1') and user_input <= unichr(con['tbl_cnt']):
try:
printViewTableContentsSubmenu(stdscr, con, int(chr(user_input)) - 1)
except ValueError:
printViewTableSubmenu(stdscr, con)
elif user_input == ord('\n'):
printViewTableSubmenu(stdscr, con)
# ==================================================
# Name: printEditTableContentsSubmenu(stdscr, con, idx)
#
# Purpose: provides an interface for users to enter
# edit queries for the desired table.
#
# NOTE: current functionality will allow any form of
# SELECT, INSERT, or DELETE query.
#
# ==================================================
def printEditTableContentsSubmenu(stdscr, con, idx):
stdscr.clear()
stdscr.refresh()
stdscr.addstr(4, 2, "----------------------------------------------------------------------------")
stdscr.addstr(6, 28, "-- EDIT TABLE --")
# Draw input table
stdscr.addstr(8, 6, "Enter a sql statement:")
stdscr.addstr(10, 5, "+")
stdscr.addstr(10, 6, "--------------------------------------------------------------------")
stdscr.addstr(10, 74, "+")
stdscr.addstr(11, 5, "|")
stdscr.addstr(12, 5, "|")
stdscr.addstr(13, 5, "|")
stdscr.addstr(11, 74, "|")
stdscr.addstr(12, 74, "|")
stdscr.addstr(13, 74, "|")
stdscr.addstr(14, 5, "+")
stdscr.addstr(14, 6, "--------------------------------------------------------------------")
stdscr.addstr(14, 74, "+")
# Collect user input
user_input = stdscr.getstr(12, 7, 66)
#---------------------------------------------------------------------
# TODO: NEED TO REFACTOR WITH HIGHLIGHT
#---------------------------------------------------------------------
# Attempt to submit query
con = database_queries.dbQuery(con, user_input)
# Edit query success; return to main menu
if queryCheck(con) == 1:
stdscr.clear()
stdscr.addstr(4, 2, "----------------------------------------------------------------------------")
stdscr.addstr(6, 28, "-- EDIT TABLE --")
stdscr.addstr(10, 5, "Query Success! Press Enter to Continue...")
stdscr.getstr(1, 1, 0)
printMainMenu(stdscr,con)
# Edit query failure; return to main menu
else:
stdscr.clear()
stdscr.addstr(4, 2, "----------------------------------------------------------------------------")
stdscr.addstr(6, 28, "-- EDIT TABLE --")
stdscr.addstr(10, 5, "Query Failure! Press Enter to Continue...")
stdscr.getstr(1, 1, 0)
printEditTableSubmenu(stdscr,con)
# ==================================================
# Name: printCreateTableSubmenu(stdscr,con)
#
# Purpose: Redirects from main menu to the create
# table menu. In current instantiation, user is
# provided with the ability to enter a "create"
# query which will be submitted to db.
#
# NOTE: user can technically enter a select, insert,
# or delete query and, upon successful submission,
# it will work.
# ==================================================
def printCreateTableSubmenu(stdscr,con):
stdscr.clear()
stdscr.addstr(4, 2, "----------------------------------------------------------------------------")
stdscr.addstr(6, 28, "-- CREATE TABLE --")
# Draw input table
stdscr.addstr(8, 6, "Enter a sql statement:")
stdscr.addstr(10, 5, "+")
stdscr.addstr(10, 6, "--------------------------------------------------------------------")
stdscr.addstr(10, 74, "+")
stdscr.addstr(11, 5, "|")
stdscr.addstr(12, 5, "|")
stdscr.addstr(13, 5, "|")
stdscr.addstr(11, 74, "|")
stdscr.addstr(12, 74, "|")
stdscr.addstr(13, 74, "|")
stdscr.addstr(14, 5, "+")
stdscr.addstr(14, 6, "--------------------------------------------------------------------")
stdscr.addstr(14, 74, "+")
# Collect user input
user_input = stdscr.getstr(12, 7, 66)
#---------------------------------------------------------------------
# TODO: NEED TO REFACTOR WITH HIGHLIGHT
#---------------------------------------------------------------------
# Attempt to submit query
con = database_queries.dbQuery(con, user_input)
# create query success; return to main menu
if queryCheck(con) == 0:
stdscr.clear()
stdscr.addstr(4, 2, "----------------------------------------------------------------------------")
stdscr.addstr(6, 28, "-- CREATE TABLE --")
stdscr.addstr(10, 5, "Query Success! Press Enter to Continue...")
stdscr.getstr(1, 1, 0)
printMainMenu(stdscr,con)
# create query failure; return to main menu
else:
stdscr.clear()
stdscr.addstr(4, 2, "----------------------------------------------------------------------------")
stdscr.addstr(6, 28, "-- CREATE TABLE --")
stdscr.addstr(10, 5, "Query Failure! Press Enter to Continue...")
stdscr.getstr(1, 1, 0)
printMainMenu(stdscr,con)
# ==================================================
# Name: queryCheck(con)
#
# Purpose: Verifies connection message and validates
# if query is success (return 1) or failure (return 0).
# ==================================================
def queryCheck(con):
# Parse query message response
response = con['msg'].split()
# Successful query
if response[0] == 'Successful':
return 1
# Failed query
else:
return 0
# ==================================================
# Name: printDeleteTableSubmenu(stdscr,con)
#
# Purpose: Redirects from main menu to the create
# table menu. In current instantiation, user is
# provided with the ability to enter a "Delete"
# query which will be submitted to db.
#
# NOTE: user can technically enter a select, insert,
# or delete query and, upon successful submission,
# it will work.
# ==================================================
def printDeleteTableSubmenu(stdscr,con):
stdscr.clear()
stdscr.addstr(4, 2, "----------------------------------------------------------------------------")
stdscr.addstr(6, 28, "-- DELETE TABLE --")
# Draw input table
stdscr.addstr(8, 6, "Enter a sql statement:")
stdscr.addstr(10, 5, "+")
stdscr.addstr(10, 6, "--------------------------------------------------------------------")
stdscr.addstr(10, 74, "+")
stdscr.addstr(11, 5, "|")
stdscr.addstr(12, 5, "|")
stdscr.addstr(13, 5, "|")
stdscr.addstr(11, 74, "|")
stdscr.addstr(12, 74, "|")
stdscr.addstr(13, 74, "|")
stdscr.addstr(14, 5, "+")
stdscr.addstr(14, 6, "--------------------------------------------------------------------")
stdscr.addstr(14, 74, "+")
# Collect user input
user_input = stdscr.getstr(12, 7, 66)
#---------------------------------------------------------------------
# TODO: NEED TO REFACTOR WITH HIGHLIGHT
#---------------------------------------------------------------------
# Attempt to submit query
con = database_queries.dbQuery(con, user_input)
# Delete query success; return to main menu
if queryCheck(con) == 1:
stdscr.clear()
stdscr.addstr(4, 2, "----------------------------------------------------------------------------")
stdscr.addstr(6, 28, "-- DELETE TABLE --")
stdscr.addstr(10, 5, "Query Success! Press Enter to Continue...")
stdscr.getstr(1, 1, 0)
printMainMenu(stdscr,con)
# Delete query failure; return to main menu
else:
stdscr.clear()
stdscr.addstr(4, 2, "----------------------------------------------------------------------------")
stdscr.addstr(6, 28, "-- DELETE TABLE --")
stdscr.addstr(10, 5, "Query Failure! Press Enter to Continue...")
stdscr.getstr(1, 1, 0)
printMainMenu(stdscr,con)
# ==================================================
# Name: printAboutSubmenu(stdscr,con)
#
# Purpose: accessed from the main menu, prints info
# about the program including version.
# ==================================================
def printAboutSubmenu(stdscr,con):
stdscr.clear()
selection = -2
option = 0
while selection < 0:
graphics = [0]*1
graphics[option] = curses.A_REVERSE
stdscr.refresh()
# Collect user's navigation selection
stdscr.addstr(4, 2, "----------------------------------------------------------------------------")
stdscr.addstr(6, 30, "-- ABOUT --")
string_line0 = "curses_database_interface v1.0"
string_line1 = "This program has been written by authors Nick Mastrokalos, Bryan Bauer,"
string_line2 = "and Darnel Clayton for Fall 2015 CS419's final software project."
stdscr.addstr(8, 4, "Version:")
stdscr.addstr(9, 4, string_line0)
stdscr.addstr(11, 4, "Notes:")
stdscr.addstr(12, 4, string_line1)
stdscr.addstr(13, 4, string_line2)
stdscr.addstr(22, 32, "Back",graphics[0])
action = stdscr.getch()
if action == (ord('\n')):
selection = option
stdscr.clear()
# Navigate to submenu
if selection == 0:
printMainMenu(stdscr,con)
# ==================================================
# Name: printLogOffSubMenu(stdscr,con)
#
# Purpose: accessed from the main menu, starts the
# process of logging off. After confirmation,
# disconnects from db and exits program. Otherwise,
# returns to the main menu.
# ==================================================
def printLogOffSubMenu(stdscr,con):
selection = -2
option = 0
while selection < 0:
graphics = [0]*2
graphics[option] = curses.A_REVERSE
stdscr.refresh()
stdscr.clear()
stdscr.addstr(4, 2, "----------------------------------------------------------------------------")
stdscr.addstr(6, 30, "-- LOG OFF --")
stdscr.addstr(10, 13, "Are you sure you'd like to log off?")
stdscr.addstr(13, 13, "Log Off",graphics[0])
stdscr.addstr(13, 35, "Return to Program",graphics[1])
# Collect user's navigation selection
action = stdscr.getch()
if action == curses.KEY_RIGHT:
option = (option - 1) % 2
elif action == curses.KEY_LEFT:
option = (option + 1) % 2
elif action == (ord('\n')):
selection = option
stdscr.clear()
# If user wants to disconnect... Exit
if selection == 0:
# Close connection with db and exit
con = database_queries.dbClose(con)
curses.endwin()
exit()
# Return to main menu
if selection == 1:
printMainMenu(stdscr,con)