-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.py
68 lines (55 loc) · 1.95 KB
/
driver.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
import argparse
import sys
import mysql.connector
from sqlgen.utilities.utility import prettyprint
config = {
"port": '4000',
'database': 'test'
}
parser = argparse.ArgumentParser()
parser.add_argument('-D', '--driver', type=str,
choices=['mysql', 'mariadb', 'tidb', 'postgres', 'sqlite', 'timescale'],
help='database server')
parser.add_argument('-H', '--host', type=str,
help='host')
parser.add_argument('-P', '--port', type=str,
help='port')
parser.add_argument('-u', '--user', type=str,
help='the user to connect the database')
parser.add_argument('-p', '--password',
help='password')
parser.add_argument('-d', '--database', type=str,
help='use the given database. Need file path if connecting to sqlite.')
args = parser.parse_args()
if len(sys.argv) < 2:
prettyprint('!', "You must give an argument. See the help message")
print('\n\n')
parser.print_help()
exit(0)
global driver
if args.driver is not None:
driver = args.driver
else:
driver = 'mysql'
try:
if driver == 'mysql':
prettyprint('*', 'Use MySQL connector')
cnx = mysql.connector.connect(**config, buffered=True, autocommit=True, consume_results=True)
cu = cnx.cursor()
elif driver == 'sqlite':
prettyprint('*', 'Use SQLite connector')
if args.database is None:
prettyprint('!', 'I need a database name. Try `-d` or `--database`')
exit(1)
con = 'file:{}?mode=rwc'
import sqlite3
cnx = sqlite3.connect(con.format(args.database), uri=True)
cu = cnx.cursor()
else:
prettyprint('>', 'I cannot connect to the given server because it is not implemented. '
'Please try another one instead.')
exit(1)
prettyprint(".", "All right, let's start!")
print("Nothing to do, exit")
except Exception:
pass