BezdarSQL 1.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
bezdarsql/__init__.py ADDED
@@ -0,0 +1,181 @@
1
+ from .config import *
2
+ import psycopg2
3
+
4
+
5
+ def select(table, value='*', filter_by=None, count=1):
6
+ request = f'select {value} from {table.__tablename__}'
7
+ filters = filter_by
8
+ if filters:
9
+ request += ' where '
10
+ for index, fil in enumerate(filters):
11
+ if index > 1:
12
+ request += f'and {fil.owner.__tablename__}.{fil.name}={repr(filters[fil])} '
13
+ else:
14
+ request += f'{fil.owner.__tablename__}.{fil.name}={repr(filters[fil])} '
15
+ else:
16
+ request += ';'
17
+
18
+ try:
19
+ with psycopg2.connect(
20
+ host=host,
21
+ port=port,
22
+ user=user,
23
+ database=db_name,
24
+ password=password,
25
+ ) as connection:
26
+ connection.autocommit = True
27
+ with connection.cursor() as cursor:
28
+ cursor.execute(request + ';')
29
+
30
+ if count >= 0:
31
+ results = cursor.fetchmany(count)
32
+ elif count == -1:
33
+ results = cursor.fetchall()
34
+
35
+ attrsintable = [i for i in table.__dict__ if not '__' in i]
36
+ objects = []
37
+ for result in results:
38
+ obj = table()
39
+ for index, attr in enumerate(attrsintable):
40
+ obj.__dict__[attr] = result[index]
41
+ objects.append(obj)
42
+
43
+ return objects
44
+
45
+ except Exception as _e:
46
+ raise _e
47
+
48
+
49
+ def select_join(tables, value='*', filter_on=(), count=1):
50
+ request = f'select {value} from '
51
+ for index, table in enumerate(tables):
52
+ if index == 0:
53
+ request += f'{table.__tablename__} '
54
+ else:
55
+ request += f'join {table.__tablename__} on '
56
+ for indexx, fil in enumerate(filter_on):
57
+ if indexx > 1:
58
+ request += f'and {fil.owner.__tablename__}.{fil.name}={filter_on[fil].owner.__tablename__}.{filter_on[fil].name} '
59
+ else:
60
+ request += f'{fil.owner.__tablename__}.{fil.name}={filter_on[fil].owner.__tablename__}.{filter_on[fil].name} '
61
+
62
+ try:
63
+ with psycopg2.connect(
64
+ host=host,
65
+ port=port,
66
+ user=user,
67
+ database=db_name,
68
+ password=password,
69
+ ) as connection:
70
+ connection.autocommit = True
71
+ with connection.cursor() as cursor:
72
+ cursor.execute(request + ';')
73
+
74
+ if count >= 0:
75
+ results = cursor.fetchmany(count)
76
+ elif count == -1:
77
+ results = cursor.fetchall()
78
+
79
+ objects = []
80
+ for result in results:
81
+ for index, table in enumerate(tables):
82
+ attrsintable = [i for i in table.__dict__ if not '__' in i]
83
+ obj = table()
84
+ for index, attr in enumerate(attrsintable):
85
+ obj.__dict__[attr] = result[index]
86
+ objects.append(obj)
87
+ result = result[len(attrsintable):]
88
+
89
+ return objects
90
+
91
+ except Exception as _e:
92
+ raise _e
93
+
94
+
95
+ def insert(table_obj):
96
+ request = f'insert into {table_obj.__tablename__} ('
97
+ attrs = [i for i in table_obj.__dict__ if '__' not in i]
98
+ for index, attr in enumerate(attrs):
99
+ value = getattr(table_obj, attr)
100
+ if hasattr(value, 'autoincrement'):
101
+ if not value.autoincrement:
102
+ request += f'{attr}' + (', ' if not index + 1 == len(attrs) else '')
103
+ else:
104
+ request += f'{attr}' + (', ' if not index + 1 == len(attrs) else '')
105
+
106
+ request += ') values ('
107
+ for index, attr in enumerate(attrs):
108
+ value = getattr(table_obj, attr)
109
+ if hasattr(value, 'autoincrement'):
110
+ if not value.autoincrement:
111
+ request += repr(value) + (', ' if not index + 1 == len(attrs) else ');')
112
+ else:
113
+ request += repr(value) + (', ' if not index + 1 == len(attrs) else ');')
114
+
115
+ try:
116
+ with psycopg2.connect(
117
+ host=host,
118
+ port=port,
119
+ user=user,
120
+ database=db_name,
121
+ password=password,
122
+ ) as connection:
123
+ connection.autocommit = True
124
+ with connection.cursor() as cursor:
125
+ cursor.execute(request)
126
+ return True
127
+
128
+ except Exception as _e:
129
+ print('error', _e)
130
+ return False
131
+
132
+
133
+ def update(table, **kwargs):
134
+ request = f'update {table.__tablename__} set '
135
+ values = kwargs['values']
136
+ for index, value in enumerate(values):
137
+ request += f'{value}={repr(values[value])} ' + (', ' if index + 1 != len(values) else 'where ')
138
+
139
+ where_s = kwargs['where']
140
+ for index, where in enumerate(where_s):
141
+ request += f'{where}={repr(where_s[where])} ' + (', ' if index + 1 != len(where_s) else ';')
142
+
143
+ try:
144
+ with psycopg2.connect(
145
+ host=host,
146
+ port=port,
147
+ user=user,
148
+ database=db_name,
149
+ password=password,
150
+ ) as connection:
151
+ connection.autocommit = True
152
+ with connection.cursor() as cursor:
153
+ cursor.execute(request)
154
+ return True
155
+
156
+ except Exception as _e:
157
+ print('error', _e)
158
+ return False
159
+
160
+
161
+ def delete(table, **kwargs):
162
+ request = f'delete from {table.__tablename__} where '
163
+ where_s = kwargs['where']
164
+ for index, where in enumerate(where_s):
165
+ request += f'{where}={repr(where_s[where])} ' + ('and ' if index + 1 != len(where_s) else ';')
166
+
167
+ try:
168
+ with psycopg2.connect(
169
+ host=host,
170
+ port=port,
171
+ user=user,
172
+ database=db_name,
173
+ password=password,
174
+ ) as connection:
175
+ connection.autocommit = True
176
+ with connection.cursor() as cursor:
177
+ cursor.execute(request)
178
+ return True
179
+ except Exception as _e:
180
+ print('error', _e)
181
+ return False
bezdarsql/base.py ADDED
@@ -0,0 +1,16 @@
1
+ import dataclasses
2
+
3
+
4
+ class Column:
5
+ def __init__(self, **kwargs):
6
+ self.autoincrement = kwargs.get('autoincrement')
7
+
8
+
9
+ def __set_name__(self, owner, name):
10
+ self.owner = owner
11
+ self.name = name
12
+
13
+
14
+ @dataclasses.dataclass
15
+ class Base:
16
+ __tablename__ = None
bezdarsql/config.py ADDED
@@ -0,0 +1,9 @@
1
+ import os
2
+ from dotenv import load_dotenv
3
+
4
+ load_dotenv(override=True)
5
+ host = os.getenv('HOST')
6
+ user = os.getenv('USER')
7
+ password = os.getenv('PASSWORD')
8
+ db_name = os.getenv('DB_NAME')
9
+ port = os.getenv('PORT')
@@ -0,0 +1,7 @@
1
+ Metadata-Version: 2.4
2
+ Name: BezdarSQL
3
+ Version: 1.1
4
+ Summary: My little SQL ORM for the ones who called bezdars
5
+ Author-email: boliklevik@gmail.com
6
+ Dynamic: author-email
7
+ Dynamic: summary
@@ -0,0 +1,7 @@
1
+ bezdarsql/__init__.py,sha256=tlWXWfosQw-e_RZ0amoQZMrsKb3mZmmLzXngfjfHmVI,6116
2
+ bezdarsql/base.py,sha256=rck1yKK4jYmA8R5MFeZJSMQn5ycCDdSD6X2tbxMN9Lw,283
3
+ bezdarsql/config.py,sha256=7ILEYbi1Bs_oMy5uesejLEV1mEGgxZtTjE1XTwbZXL0,208
4
+ bezdarsql-1.1.dist-info/METADATA,sha256=ZOADphl2bbPwSOdC_ZSzUlS4OTB6QGWgHDaAfPnc5Cc,184
5
+ bezdarsql-1.1.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
6
+ bezdarsql-1.1.dist-info/top_level.txt,sha256=6Hm17vWV3AZOm7VuPVlThNi5KBr6BFO7-e9s7NifPsI,10
7
+ bezdarsql-1.1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (80.10.2)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ bezdarsql