-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloose_compare.py
130 lines (114 loc) · 3.17 KB
/
loose_compare.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# File name : main.py
# Author : Aku
# Date created : 20 mai 2022
from rich.console import Console
from rich.table import Table
import pymysql
import sqlite3
import psycopg2
import itertools
value = [
"True",
"False",
"1",
"0",
"-1",
"'1'",
"'0'",
"'-1'",
"''",
"Null",
"'John'",
"'1Jhon'",
"'0John'",
"'-1John'",
"'1e1'",
"'0e1'",
"'1e0'",
"'-1e0'",
"10",
]
def printTable(title, values):
table = Table(title=title)
table.add_column("", justify="center", style="bold")
for v in value:
table.add_column(v, justify="center")
for k in values:
table.add_row(*([k] + values[k]))
console = Console()
console.print(table)
class Connector:
def __init__(self, db_type):
self.type = db_type
if db_type == "sqlite":
self.conn = Connector.connect_sqlite()
elif db_type == "postgres":
self.conn = Connector.connect_postgres()
elif db_type == "mysql":
self.conn = Connector.connect_mysql(3308)
elif db_type == "mariadb":
self.conn = Connector.connect_mysql(3306)
@staticmethod
def connect_sqlite():
conn = sqlite3.connect('test.db')
return conn
@staticmethod
def connect_mysql(port):
conn = pymysql.connect(
host='127.0.0.1',
user='root',
password='root',
db='test',
port=port
)
return conn
@staticmethod
def connect_postgres():
connect_str = "dbname='test' user='postgres' host='localhost' " + \
"password='root' port='3307'"
conn = psycopg2.connect(connect_str)
return conn
def tests(self):
dataset = itertools.product(value, repeat=2)
result = dict()
for test in dataset:
query = f"select {test[0]}={test[1]}"
c = self.conn.cursor()
try:
c.execute(query)
response = c.fetchall()
for r in response:
if r[0] == 1:
if test[0] == test[1]:
r = '[green]True[/green]'
else:
r = '[red]True[/red]'
elif r[0] == 0:
r = 'False'
else:
r = '[blue]Null[/blue]'
if test[0] in result:
result[test[0]].append(r)
else:
result[test[0]] = [r]
except Exception as e:
if test[0] in result:
result[test[0]].append('[purple]Error[/purple]')
else:
result[test[0]] = ['[purple]Error[/purple]']
self.conn.commit()
c.close()
return result
if __name__ == '__main__':
db_list = (
'mariadb',
'mysql',
'postgres',
'sqlite'
)
for db_type in db_list:
db = Connector(db_type)
result = db.tests()
printTable(db.type, result)