Introduction
a simple orm in python based on [mysql,dataclass].
Install
pip install rinch-sql
Use
dict
-
table
- __init__.py
- user.py
- table2.py
-
task.py
user.py
import datetime
from dataclasses import dataclass
@dataclass
class User:
field_list_unique = ["name"]
name: str
age: int
id: int = None
create_time: datetime = None
update_time: datetime = None
__init__.py
from rinch_sql import Mysql
MYSQL_SERVER_DATABASE = {
"host": "",
"port": 3306,
"user": "",
"password": "",
"database": "",
"pool_size": 32,
}
from .user import User
mysql_user = Mysql(MYSQL_SERVER_DATABASE, User)
def mysql_user_():
return Mysql(MYSQL_SERVER_DATABASE, User)
task.py
from rinch_sql import Sql, SqlStatic
from table import mysql_user, User
user_list = mysql_user.select("")
user_list = mysql_user.select("id = 1")
sql = "SELECT id,t1.name,age from user as t1 right join tb_mail as t2 on t1.name=t2.name group by t1.name"
values = ["id", "name", "age"]
user_record_list = mysql_user.execute(sql)
user_list = [mysql_user._values_2_obj(i, values) for i in user_record_list]
sql_update = SqlStatic.update("user", ["name", "age"])
user = User(id=2)
mysql_user.execute(sql_update, ["name_new", 100])
Advice
when you have any advice, please issue in https://github.com/rinch-wu/rinch_sql