dbmasta 0.1.0__tar.gz

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.
dbmasta-0.1.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 mastamatto
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
dbmasta-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,202 @@
1
+ Metadata-Version: 2.1
2
+ Name: dbmasta
3
+ Version: 0.1.0
4
+ Summary: A simple MariaDB client based on SQLAlchemy core
5
+ Home-page: https://github.com/mastamatto/dbConnect
6
+ Author: Matt Owen
7
+ Author-email: matt@ca-automation.app
8
+ Classifier: Development Status :: 3 - Alpha
9
+ Classifier: Intended Audience :: Developers
10
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
11
+ Classifier: License :: OSI Approved :: MIT License
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Programming Language :: Python :: 3.7
14
+ Classifier: Programming Language :: Python :: 3.8
15
+ Classifier: Programming Language :: Python :: 3.9
16
+ Classifier: Programming Language :: Python :: 3.10
17
+ Classifier: Operating System :: OS Independent
18
+ Requires-Python: >=3.7
19
+ Description-Content-Type: text/markdown
20
+ License-File: LICENSE
21
+ Requires-Dist: sqlalchemy>=2.0.27
22
+ Requires-Dist: asyncmy
23
+ Requires-Dist: pymysql
24
+
25
+ # dbConnect (simple mariadb interface)
26
+
27
+ ## Overview
28
+
29
+ This Python package provides a simple interface for interacting with MariaDB databases using SQLAlchemy Core. It abstracts some common database operations into more manageable Python methods, allowing for easy database queries, inserts, updates, and deletes.
30
+
31
+ ## Installation
32
+
33
+ To install this package, run the following pip command. **Note: this requires SQLAlchemy 2.0.27 or greater**
34
+
35
+ ```bash
36
+ pip install dbConnect
37
+ ```
38
+
39
+ Replace `yourusername/yourrepository` with the actual path to your repository.
40
+
41
+ ## Basic Usage
42
+
43
+ ### Configuration
44
+
45
+ First, configure the database client with the necessary credentials:
46
+
47
+ ```python
48
+ from dbConnect import DataBase, AsyncDataBase
49
+
50
+ # Initialize the database client
51
+ db = DataBase(
52
+ dict(
53
+ username='username',
54
+ password='password',
55
+ host='host',
56
+ port=3306,
57
+ database_name='database_name'
58
+ )
59
+ )
60
+ # Async Version
61
+ db = AsyncDataBase(
62
+ dict(
63
+ username='username',
64
+ password='password',
65
+ host='host',
66
+ port=3306,
67
+ database_name='database_name'
68
+ )
69
+ )
70
+
71
+
72
+ # Initiliaze using environment variables
73
+ db = DataBase.env()
74
+ # Async Version
75
+ db = AsyncDataBase.env()
76
+ ```
77
+
78
+ ### Executing Queries
79
+
80
+ You can execute a simple SELECT query to fetch data:
81
+
82
+ ```python
83
+ import datetime as dt
84
+
85
+ # Create parameters
86
+ params = {
87
+ "date": db.before(dt.date(2024,1,1), inclusive=True)
88
+ }
89
+
90
+ # Execute the query
91
+ dbr = db.select("database", "table", params)
92
+
93
+ # Examine the results
94
+ if dbr.successful:
95
+ print(dbr.records)
96
+ else:
97
+ print(dbr.error_info)
98
+ ```
99
+
100
+ ### Complex Queries
101
+
102
+ The following query would generate this text:
103
+
104
+ ```python
105
+ import datetime as dt
106
+
107
+ # Create parameters
108
+ params = {
109
+ "_OR_": db.or_(
110
+ [
111
+ {"date": db.after(dt.date(2020,1,1)), "category": "sales"},
112
+ {"date": db.before(dt.date(2020,1,1)), "category": db.not_(db.in_, ["purchases","adjustments","sales"])},
113
+ ]
114
+ ),
115
+ "_AND_": db.and_(
116
+ [
117
+ {"keyfield": db.starts_with("SJ")},
118
+ {"keyfield": db.not_(db.ends_with("2E"))}
119
+ ]
120
+ )
121
+ "status": "under_review"
122
+ }
123
+
124
+ # Execute the query
125
+ dbr = db.select("database", "table", params)
126
+
127
+ # Examine the results
128
+ if dbr.successful:
129
+ print(dbr.records)
130
+ else:
131
+ print(dbr.error_info)
132
+ ```
133
+
134
+ The raw text of the query can be retrieved from the attribute `dbr.raw_query` from the DataBaseResponse object,
135
+ which the DataBase.select method returns. The text in the above example would be as follows:
136
+
137
+ ```sql
138
+ SELECT * FROM `database`.`table`
139
+ WHERE ((`date` > '2020-01-01' and `category`='sales') or
140
+ (`date` < '2020-01-01' and `category` not in ('sales')))
141
+ AND `keyfield` LIKE 'SJ%' AND `keyfield` NOT LIKE '%2E'
142
+ AND `status`='under_review';
143
+ ```
144
+
145
+ Or in simple terms...
146
+ Get all records `under_review` where the keyfield starts with `SJ`, but doesn't end with `2E`. Pull these if either:
147
+
148
+ - dated after `2020-01-01` and categorized as a `sale`
149
+ - dated before `2020-01-01` and not categorized as `sale`,`purchase` or `adjustment`.
150
+
151
+
152
+ ### Result Modification from `DataBase.select`
153
+
154
+ In addition to complex conditions for filtering records, you can:
155
+
156
+ - sort records
157
+
158
+ ```pythoN
159
+ db.select(..., order_by="column_name", reverse=True)
160
+ ```
161
+
162
+ - limit and offset results
163
+
164
+ ```python
165
+ # for offset pagination
166
+ db.select(..., limit=100, offset=0)
167
+ ```
168
+
169
+ - filter columns
170
+
171
+ ```python
172
+ # only receive the data for the fields you provide
173
+ db.select(..., columns=["keyfield", "name", "date"])
174
+ ```
175
+
176
+ - get textual output (without executing)
177
+
178
+ ```python
179
+ # this will not execute the query, but will return the raw query needed to execute
180
+ raw_textual_query = db.select(..., textual=True)
181
+ print(raw_textual_query)
182
+ new_query = f"INSERT INTO `filteredtable` ({raw_textual_query[:-1]});
183
+ dbr = db.run(new_query)
184
+ ```
185
+
186
+ - get model output by providing a model factory
187
+
188
+ ```python
189
+ from pydantic import BaseModel
190
+ import datetime as dt
191
+
192
+ class Record(BaseModel):
193
+ keyfield: str
194
+ date: dt.date
195
+ status: str
196
+
197
+ model_factory = lambda row: Record(**row)
198
+
199
+ # only receive the data for the fields you provide
200
+ dbr = db.select(..., response_model=model_factory)
201
+ # each record will be an instance of Record
202
+ ```
@@ -0,0 +1,178 @@
1
+ # dbConnect (simple mariadb interface)
2
+
3
+ ## Overview
4
+
5
+ This Python package provides a simple interface for interacting with MariaDB databases using SQLAlchemy Core. It abstracts some common database operations into more manageable Python methods, allowing for easy database queries, inserts, updates, and deletes.
6
+
7
+ ## Installation
8
+
9
+ To install this package, run the following pip command. **Note: this requires SQLAlchemy 2.0.27 or greater**
10
+
11
+ ```bash
12
+ pip install dbConnect
13
+ ```
14
+
15
+ Replace `yourusername/yourrepository` with the actual path to your repository.
16
+
17
+ ## Basic Usage
18
+
19
+ ### Configuration
20
+
21
+ First, configure the database client with the necessary credentials:
22
+
23
+ ```python
24
+ from dbConnect import DataBase, AsyncDataBase
25
+
26
+ # Initialize the database client
27
+ db = DataBase(
28
+ dict(
29
+ username='username',
30
+ password='password',
31
+ host='host',
32
+ port=3306,
33
+ database_name='database_name'
34
+ )
35
+ )
36
+ # Async Version
37
+ db = AsyncDataBase(
38
+ dict(
39
+ username='username',
40
+ password='password',
41
+ host='host',
42
+ port=3306,
43
+ database_name='database_name'
44
+ )
45
+ )
46
+
47
+
48
+ # Initiliaze using environment variables
49
+ db = DataBase.env()
50
+ # Async Version
51
+ db = AsyncDataBase.env()
52
+ ```
53
+
54
+ ### Executing Queries
55
+
56
+ You can execute a simple SELECT query to fetch data:
57
+
58
+ ```python
59
+ import datetime as dt
60
+
61
+ # Create parameters
62
+ params = {
63
+ "date": db.before(dt.date(2024,1,1), inclusive=True)
64
+ }
65
+
66
+ # Execute the query
67
+ dbr = db.select("database", "table", params)
68
+
69
+ # Examine the results
70
+ if dbr.successful:
71
+ print(dbr.records)
72
+ else:
73
+ print(dbr.error_info)
74
+ ```
75
+
76
+ ### Complex Queries
77
+
78
+ The following query would generate this text:
79
+
80
+ ```python
81
+ import datetime as dt
82
+
83
+ # Create parameters
84
+ params = {
85
+ "_OR_": db.or_(
86
+ [
87
+ {"date": db.after(dt.date(2020,1,1)), "category": "sales"},
88
+ {"date": db.before(dt.date(2020,1,1)), "category": db.not_(db.in_, ["purchases","adjustments","sales"])},
89
+ ]
90
+ ),
91
+ "_AND_": db.and_(
92
+ [
93
+ {"keyfield": db.starts_with("SJ")},
94
+ {"keyfield": db.not_(db.ends_with("2E"))}
95
+ ]
96
+ )
97
+ "status": "under_review"
98
+ }
99
+
100
+ # Execute the query
101
+ dbr = db.select("database", "table", params)
102
+
103
+ # Examine the results
104
+ if dbr.successful:
105
+ print(dbr.records)
106
+ else:
107
+ print(dbr.error_info)
108
+ ```
109
+
110
+ The raw text of the query can be retrieved from the attribute `dbr.raw_query` from the DataBaseResponse object,
111
+ which the DataBase.select method returns. The text in the above example would be as follows:
112
+
113
+ ```sql
114
+ SELECT * FROM `database`.`table`
115
+ WHERE ((`date` > '2020-01-01' and `category`='sales') or
116
+ (`date` < '2020-01-01' and `category` not in ('sales')))
117
+ AND `keyfield` LIKE 'SJ%' AND `keyfield` NOT LIKE '%2E'
118
+ AND `status`='under_review';
119
+ ```
120
+
121
+ Or in simple terms...
122
+ Get all records `under_review` where the keyfield starts with `SJ`, but doesn't end with `2E`. Pull these if either:
123
+
124
+ - dated after `2020-01-01` and categorized as a `sale`
125
+ - dated before `2020-01-01` and not categorized as `sale`,`purchase` or `adjustment`.
126
+
127
+
128
+ ### Result Modification from `DataBase.select`
129
+
130
+ In addition to complex conditions for filtering records, you can:
131
+
132
+ - sort records
133
+
134
+ ```pythoN
135
+ db.select(..., order_by="column_name", reverse=True)
136
+ ```
137
+
138
+ - limit and offset results
139
+
140
+ ```python
141
+ # for offset pagination
142
+ db.select(..., limit=100, offset=0)
143
+ ```
144
+
145
+ - filter columns
146
+
147
+ ```python
148
+ # only receive the data for the fields you provide
149
+ db.select(..., columns=["keyfield", "name", "date"])
150
+ ```
151
+
152
+ - get textual output (without executing)
153
+
154
+ ```python
155
+ # this will not execute the query, but will return the raw query needed to execute
156
+ raw_textual_query = db.select(..., textual=True)
157
+ print(raw_textual_query)
158
+ new_query = f"INSERT INTO `filteredtable` ({raw_textual_query[:-1]});
159
+ dbr = db.run(new_query)
160
+ ```
161
+
162
+ - get model output by providing a model factory
163
+
164
+ ```python
165
+ from pydantic import BaseModel
166
+ import datetime as dt
167
+
168
+ class Record(BaseModel):
169
+ keyfield: str
170
+ date: dt.date
171
+ status: str
172
+
173
+ model_factory = lambda row: Record(**row)
174
+
175
+ # only receive the data for the fields you provide
176
+ dbr = db.select(..., response_model=model_factory)
177
+ # each record will be an instance of Record
178
+ ```
@@ -0,0 +1,3 @@
1
+ # -*- coding: utf-8 -*-
2
+ from .async_db_client import AsyncDataBase
3
+ from .db_client import DataBase
@@ -0,0 +1 @@
1
+ from .base import AsyncDataBase