A simple ORM MySQL operation Library, running on Python3.
Project description
mysqlorm
TL:DR
A simple ORM MySQL operation Library, running on Python3. Automatic long connection,support chain call, more secure statement generation and the MySQL query can be constructed more elegantly.
Installation
Install via pip
(sudo) pip(3) install mysqlorm
Usage
Begin by importing the mysqlorm module:
from mysqlorm import ORMModel, MySQLConnect
You need to link the database before use:
mysql_connect_config = {
"host": "host",
"user": "user",
"passwd": "passwd",
"db": "db",
}
MySQLConnect.connect(mysql_connect_config)
Create a class to inherit ormmodel and set table_name attribute:
(If no table_name attribute is set, use the lowercase class name by default.)
class ExampleModel(ORMModel):
table_name = "exampletable"
How to use
-
Insert:
ExampleModel.insert({"field1": "value1", "field2", "value2"}) # single insert ExampleModel.insert(( # batch insert, can use tuple or list {"field1": "value1", "field2", "value2"}, {"field1": "value1", "field2", "value2"}))
-
Where:
Support call chaining. The
wheremethod can pass 2 or 3 parameters. If two parameters are passed, the comparison symbol uses the equal sign by default. Call chaining useandconnection condition, you must useorWheremethod toorconditionExampleModel.where("field", "value").where("field", ">", "value") ExampleModel.where("field", "value").orWhere("field", ">", "value")
Support batch afferent condition, can use tuple or list. Use
andconnection conditionExampleModel.where((("filed1", "value1"), ("filed2", "value2")))
Support aggregation condition query. Or you can use lambda method.
def condition(query): return query.where("field", "value").where("field1", ">", "value") ExampleModel.where(condition).orWhere( lambda query: return query.where("field", "value").where("field1", "<", "value") )
The
likecondition uses the "LOCATE()" implementation, because the%symbol is a special symbol in Python will cause some problems. So do not pass in thewheremethod the parameter with the%symbol.ExampleModel.where("file", 'like', "value")
You can use
whereInandwhereNotInmethod. Can use tuple or list.ExampleModel.whereIn("field", TupleOrList).whereNotIn("field", TupleOrList)
You can use
whereBetweenandwhereNotBetween.ExampleModel.whereBetween("field", "from_condition", "to_condition" ).whereNotBetween("field", "from_condition", "to_condition")
You can use
whereNullandwhereNotNull.ExampleModel.whereNull("field1").whereNotNull("field2")
-
Query:
You can use
selectmethod to defining query fields. If a parameter is not passed, use by default*. The default is to queryid. Or can not useselectmethod.query = ExampleModel.select("field1", "field2", "field3")# n field parameters can be passed in.
All queries support the
wheremethod conditional queries.query.where("field", "value")
You can use
whenmethod. Execute the query when the value is true or skip. The second parameter can be a function, like the use of thewheremethod.import random a = random.choice(range(1, 10)) b = random.choice(range(1, 10)) query.when(a != b, condition).when(a == b, lambda query: return query.where("field", "value"))
You can use
orderBymethod to sort resault. TheorderBymethod can pass 1 or 2 parameters. If one parameter passed, use 'ASC' by default.query.orderBy("field")
You can use
groupBymethod But you must pay attention to the problems caused bysql_mode=only_full_group_byquery.groupBy("field")
You can use
limitmethod Thewheremethod can pass 1 or 2 parameters. If one parameter is passed, offer use 0 by default.query.limit(0, 10)
The query returns are all model instances. Can easy to operate on a single instance.
query.all() # to get all data query.find(1) # to find data from id query.get() # to get data what query according to conditions query.first() # to get first raw data what query according to conditions
-
Update:
# The `update` method supports the `where` conditions. ExampleModel.update({"field1": "value1", "field2", "value2"}) # batch update ExampleModel.where("field", "value").update({"field1": "value1"})
-
Delete:
# The `delete` method supports the `where` conditions. ExampleModel.where("field", "value").delete()
-
ORMModel:
ORMModel instances can be operated on a variety of operations. You can get a ormmodel instance in this way:
example = ExampleModel(attributes=attr) # attr must be a dict that matches the database field
You can use the
savemethod to insert data.example.save()
You can use the
savemethod to update data or use thedeletemethod to delete data. But the instances must from query resault.example = ExampleModel.find(1) example.field = "new value" example.save() # update data example.delete() # delete data
Converting ormmodel instances to dict with
dictmethodexample.dict()
TODO
- Perfect the join table query.
- Add more functions for ormmodel.
- Increase the association between ormmodel.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file mysqlorm-0.2.tar.gz.
File metadata
- Download URL: mysqlorm-0.2.tar.gz
- Upload date:
- Size: 9.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
03265d61c5a077f1f79fe382a297f2d433f45210b97e1629411fd267a5b1ffb6
|
|
| MD5 |
891ae3f66db0b470984efc143983dfcd
|
|
| BLAKE2b-256 |
ae5fb7f7d28739acf47100342e3a5ade3f47164943a6b4f3443887a500396949
|