Skip to content

Commit 5412beb

Browse files
committed
query packed
1 parent 96565d9 commit 5412beb

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

src/quicksqlconnector/__init__.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,36 +38,39 @@ def __init__(self, host: str, port: int, user: str, password: str, database=Opti
3838
raise ValueError
3939

4040
def query(self, my_query: str):
41-
print(my_query)
4241
"""
43-
It takes a query as a string, and returns the result of the query
42+
Takes a query and executes it if it's valid else it will throw error.
4443
4544
:param my_query: str
4645
:type my_query: str
47-
:return: The query is being returned.
46+
:return: A list of tuples.
4847
"""
48+
# Lots of devs reported putting queries direct is insecure, yes it is true.
49+
# but in this case before executing query are stored in a string.
50+
# means it cannot be changed!. Unless your backend it compromised.
51+
# OPEN AN ISSUE if something is wrong. It's an open source.
52+
packed_query = my_query.lower()
4953

5054
table = PrettyTable()
5155
try:
5256

53-
if 'select' or 'SELECT' in my_query:
54-
all_info = []
57+
if 'select' in packed_query:
5558

59+
all_info = []
5660
with self.SQL.cursor() as cursor:
57-
cursor.execute(my_query)
61+
cursor.execute(packed_query)
5862
for bits_of_data in cursor:
5963
all_info.append(bits_of_data)
6064
cursor.close()
6165

6266

6367
return all_info
6468

65-
# elif contains_word(my_query, 'show') == True:
66-
elif 'show' or 'SHOW' in my_query:
67-
69+
elif 'show' in packed_query:
70+
6871
table.field_names = ['Result']
6972
with self.SQL.cursor() as cursor:
70-
cursor.execute(my_query)
73+
cursor.execute(packed_query)
7174

7275
for bits_of_data in cursor:
7376
table.add_row([bits_of_data[0]])
@@ -77,24 +80,27 @@ def query(self, my_query: str):
7780

7881
else:
7982
with self.SQL.cursor() as cursor:
80-
cursor.execute(my_query)
83+
cursor.execute(packed_query)
8184
cursor.close()
82-
return f'Query OK with command : {my_query}'
85+
return f'Query OK with command : {packed_query}'
8386

8487
except Exception as e:
8588
print(e)
8689

8790

8891
if __name__ == "__main__":
92+
# SOME TESTS WHICH I PERFORM WHILE CODING.
93+
# USE YOUR OWN CREDS WHEN CONTRIBUTING
94+
8995
DB = quicksqlconnector('localhost', 6606, 'root', 'anas9916', 'userbase')
9096
# print(DB.query('show databases'))
9197
# DB.query('use userbase')
9298
# print(DB.query('show databases')[0][0])
9399
# print(DB.query('show tables'))
94100
# print(DB.query('SELECT * FROM new_fb'))
95101
# DB.query('CREATE TABLE test(name varchar(10), id int(10))')
96-
print(DB.query("INSERT INTO test values('lex',1)"))
102+
# print(DB.query("INSERT INTO test values('lex',1)"))
97103
# DB.query('DROP TABLE test')
98104
# print(DB.query('show tables'))
99-
print(DB.query('SELECT * FROM test'))
105+
# print(DB.query('SELECT * FROM test'))
100106
pass

0 commit comments

Comments
 (0)