-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_sql.py
More file actions
145 lines (135 loc) · 5.8 KB
/
Copy pathpython_sql.py
File metadata and controls
145 lines (135 loc) · 5.8 KB
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
from typing import Union
import psycopg2 as ps
from psycopg2 import OperationalError
import traceback
def get_id(database:str='') -> dict[str,str]:
if database :
return {'database': database,
'user':'postgres',
'password':'bonjour',
'host': 'localhost',
'port': 5432}
return {'user':'postgres',
'password':'bonjour'}
def log(message:str) -> bool:
try:
with open("log.txt","a",encoding="utf-8") as file:
file.write(f'\n{message}')
return True
except:
return False
def format_sql(data:Union[str, int, list[str], list[int], bool]) -> str:
if type(data) == str:
return f"'{data}'"
if type(data) == list:
if len(data) == 0:
return
return f'ARRAY[{", ".join([format_sql(x) for x in data])}]'
return str(data)
def get(database:str, tables:list[str], variable:str="", valeur:str="") -> list[tuple]:
try:
id = get_id(database)
with ps.connect(**id) as connector:
with connector.cursor() as cursor:
if len(tables) >= 3:
return [('Erreur')]
commande = f'select * from public."{tables[0]}"'
if len(tables) == 2:
commande += f' inner join public."{tables[1]}" on public."{tables[0]}".{tables[1]}_id = public."{tables[1]}".id'
if variable:
commande+= f" where {variable} = {format_sql(valeur)}"
cursor.execute(commande)
return cursor.fetchall()
except:
return [()]
def put(database:str, table:str, data:dict[str,str]) -> bool:
try:
id = get_id(database)
with ps.connect(**id) as connector:
with connector.cursor() as cursor:
cursor.execute(f"""
insert into public."{table}" values ({", ".join([format_sql(x) for x in data.values()])})
""")
connector.commit()
return True
except:
return False
def delete(database:str, table:str, id:int) -> bool:
try:
identifiants = get_id(database)
with ps.connect(**identifiants) as connector:
with connector.cursor() as cursor:
cursor.execute(f"""
delete from public."{table}" where id = {id}
""")
connector.commit()
return cursor.rowcount > 0
except:
traceback.print_exc()
def create_database(name:str='database_1') -> bool:
try:
id = get_id()
conn = ps.connect(**id)
conn.autocommit = True
cursor = conn.cursor()
cursor.execute(f"CREATE DATABASE {name}")
cursor.close()
conn.close()
return True
except:
return False
def create_table(database:str, table_name:str) -> bool:
try:
id = get_id(database)
with ps.connect(**id) as conn:
conn.autocommit = True
with conn.cursor() as cursor:
cursor.execute(f"SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = '{table_name}');")
table_exists = cursor.fetchone()[0]
if not table_exists:
cursor.execute(f"CREATE TABLE {table_name} (id SERIAL PRIMARY KEY)")
return not table_exists
except:
return False
def add_column(database:str, table:str, column_name:str, type:type) -> bool:
try:
id = get_id(database)
conversion = {int:'integer',
str:'text',
bool:'boolean',
bool:'boolean',
list[int]:'integer[]',
list[str]:'text[]'}
with ps.connect(**id) as conn:
conn.autocommit = True
with conn.cursor() as cursor:
cursor.execute(f"SELECT EXISTS (select 1 from information_schema.columns where table_name = '{table}' and column_name = '{column_name}')")
column_exists = cursor.fetchone()[0]
if not column_exists:
cursor.execute(f"""
ALTER TABLE {table}
ADD {column_name} {conversion[type]}
""")
return not column_exists
except:
traceback.print_exc()
if __name__ == '__main__':
"""User guide de la librairie"""
"""
print(create_database('database_1'))
print(create_table('database_1', 'table_1'))
print(add_column('database_1','table_1','column_1',int))
print(add_column('database_1','table_1','column_2',str))
print(add_column('database_1','table_1','column_3',bool))
print(add_column('database_1','table_1','column_4',list[int]))
print(add_column('database_1','table_1','column_5',list[str]))
print(put('database_1','table_1',{f"column_{i}":val for i,val in enumerate([4,567,'oui',False,[9,8,7,6],['i','o','a']])}))
print(put('database_1','table_1',{f"column_{i}":val for i,val in enumerate([6,82,'oui',False,[9,8,7,6],['i','o','a']])}))
print(get('database_1',['table_1']))
print(get('database_1',['table_1'],'column_1',82))
print(delete('database_1','table_1',6))
"""
print(put('ipam', 'group', {i:val for i,val in enumerate([1, 'Rennes', 0, [0], ['dl'], ['dl']])}))
print(put('ipam', 'group', {i:val for i,val in enumerate([3, 'Vannes', 1, [0], ['dl'], ['dl']])}))
data = {'id': 4, 'first_addr': '10.0.0.0', 'last_addr': '10.0.0.6', 'desc': 'yipitizoump', 'mask': '30', 'r_r': ['user'], 'w_r': ['user'], 'gp_id': '1'}
print(put('ipam', 'subnet', data))