quicker/README.md

149 lines
4.2 KiB
Markdown
Raw Normal View History

2023-07-19 09:46:12 +03:00
<img width="320px" src="logo.svg"/>
2023-07-09 04:03:24 +03:00
2023-07-19 09:46:12 +03:00
**Quicker** is a pythonic 🐍 tool for querying databases.
2023-07-09 03:47:50 +03:00
2023-07-25 01:40:12 +03:00
Quicker wraps Python libraries:
2023-07-09 03:47:50 +03:00
- `mysqlclient` for MySQL.
2023-07-19 06:44:58 +03:00
- `psycopg2` for PostgreSQL.
2023-07-25 01:40:12 +03:00
- `sqlite3` from Python standard library for SQLite.
2023-07-09 03:47:50 +03:00
2023-07-19 09:46:12 +03:00
# Why is it needed?
2023-07-25 01:40:12 +03:00
At work, I periodically have to make queries to different databases and then somehow process the information received. This may be necessary for one-time use, so I don't want to write a lot of code. You may also want to do all the work right in the interactive Python shell.
2023-07-19 09:46:12 +03:00
2023-07-25 01:40:12 +03:00
Quicker interface is as simple as possible, thanks to which lazy system administrators can now effortlessly extract data from the database.
2023-07-19 09:46:12 +03:00
Of course, this library **should not be used in production**. This is just a small assistant in routine tasks.
2023-07-09 03:47:50 +03:00
2023-07-09 04:03:24 +03:00
# Installation
```
pip install git+https://git.nxhs.cloud/ge/quicker
```
2023-07-09 03:50:37 +03:00
# Usage
2023-07-09 03:47:50 +03:00
2023-07-19 09:46:12 +03:00
Quicker uses a context manager. All that is needed for work is to pass connection parameters to object and write the queries themselves. See MySQL example:
2023-07-09 03:47:50 +03:00
```python
2023-07-19 09:46:12 +03:00
from quicker import Connection
2023-07-09 03:47:50 +03:00
2023-07-19 09:46:12 +03:00
with Connection(provider='mysql', read_default_file='~/.my.cnf') as query:
users = query("SELECT * FROM `users` WHERE admin = 'N'")
2023-07-09 03:47:50 +03:00
```
2023-07-19 09:46:12 +03:00
`Connection` object initialises `Query` callable object for interacting with cursor. You can use `query("sql there..")` or `query.execute("sql...")` syntax. There are the same.
2023-07-09 03:47:50 +03:00
2023-07-19 09:46:12 +03:00
`Query` object methods and properties:
- `query()`, `execute()`. Execute SQL. There is you can use here this syntax: `query('SELECT * FROM users WHERE id = %s', (15,))`.
2023-07-09 03:47:50 +03:00
- `commit()`. Write changes into database.
2023-07-19 06:44:58 +03:00
- `cursor`. Access cursor object directly.
- `connection`. Access connection object directly.
2023-07-09 03:47:50 +03:00
2023-07-19 09:46:12 +03:00
You don't need to commit to the database, Quicker will do it for you, but if you need to, you can commit manually calling `query.commit()`. You can also turn off automatic commit when creating a `Connection` object — pass it the argument `commit=Fasle`.
2023-07-09 04:16:46 +03:00
2023-07-19 09:46:12 +03:00
That's not all — Quicker converts the received data into a list of dictionaries. The list will be empty if nothing was found for the query. If the request does not imply a response, `None` will be returned.
## MySQL example
2023-07-09 03:47:50 +03:00
2023-07-19 09:46:12 +03:00
```python
2023-07-09 03:47:50 +03:00
from quicker import Connection
config = {
'provider': 'mysql',
'host': '127.0.0.1',
'port': 3306,
'user': 'myuser',
'database': 'mydb',
'password': 'example',
}
with Connection(**config) as query:
2023-07-19 09:46:12 +03:00
query(
"""
CREATE TABLE IF NOT EXISTS users (
id int AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
"""
)
query(
"INSERT INTO users VALUES (NULL, %s, %s, current_timestamp)",
('john', 'john@exmpl.org',)
)
query.commit()
users = query("SELECT * FROM users")
print('ID\t NAME\t EMAIL')
for user in users:
print(user['id'], '\t', user['name'], '\t', user['email'])
2023-07-09 03:47:50 +03:00
```
2023-07-19 09:46:12 +03:00
## PostgreSQL example
2023-07-09 03:47:50 +03:00
```python
2023-07-19 09:46:12 +03:00
import logging
2023-07-09 04:16:46 +03:00
2023-07-09 03:47:50 +03:00
from quicker import Connection
2023-07-19 09:46:12 +03:00
logging.basicConfig(level=logging.DEBUG)
2023-07-19 06:44:58 +03:00
2023-07-19 09:46:12 +03:00
config = {
'provider': 'postgres',
'host': '127.0.0.1',
'port': 5432,
'user': 'myuser',
'database': 'mydb',
'password': 'example',
}
2023-07-19 06:44:58 +03:00
2023-07-19 09:46:12 +03:00
with Connection(**config) as query:
query(
"""
CREATE TABLE IF NOT EXISTS users (
id serial PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
"""
)
query(
"INSERT INTO users VALUES ((SELECT MAX(id)+1 FROM users), %s, %s, current_timestamp)",
('phil', 'phil@exmpl.org',)
)
2023-07-19 06:44:58 +03:00
```
2023-07-19 09:46:12 +03:00
## Logging
For logging add following code in your module:
```python
2023-07-19 06:44:58 +03:00
import logging
logging.basicConfig(level=logging.DEBUG)
```
2023-07-19 09:46:12 +03:00
## Direct access to Cursor object
2023-07-19 06:44:58 +03:00
2023-07-19 09:46:12 +03:00
```python
2023-07-25 01:40:12 +03:00
from quicker import Connection, mklist
2023-07-19 06:44:58 +03:00
# config declaration here...
with Connection(**config) as db:
2023-07-19 09:46:12 +03:00
db.cursor.execute('SELECT id, name, email FROM users WHERE name = %s', ('John',))
2023-07-19 06:44:58 +03:00
users = db.cursor.fetchall()
2023-07-19 09:46:12 +03:00
# Note: "users" is tuple! Convert it to list of dicts!
2023-07-19 06:44:58 +03:00
colnames = [desc[0] for desc in db.cursor.description]
2023-07-25 01:40:12 +03:00
users_list = mklist(colnames, users)
```