Python wechat ordering applet course video
https://edu.csdn.net/course/detail/36074
Python practical quantitative transaction financial management system
https://edu.csdn.net/course/detail/35475
People who have developed web systems must be familiar with a large number of curd s. In order to improve efficiency, we usually use some orm frameworks instead of directly operating the database. However, the existing orm framework often has two common problems (the same in all languages): 1 api complexity: even experienced developers need to write the original SQL statement before using it, and then call the api. The efficiency is not high, and the troubleshooting is not transparent enough. If you use native SQL, you usually need to write your own code to process the result set. 2. The operation granularity of the database using the orm framework is relatively coarse. Sometimes, in order to improve the query efficiency of the database, it is clear that one statement can complete the work, and multiple statements will be sent through the framework. This also increases the load of the system and reduces the concurrent throughput of the system.
So called, I want to be able to build a framework by myself, which not only allows developers to write sql freely, but also maintains the characteristics of orm, and automatically processes the query result set into my predefined format. The key is to be easy to use without external dependence. After about half a month of trying, I finally wrote gzero.
1, How to introduce
go get gitee.com/learnhow/gzero@v0.8.1
The latest version is v0 8.1, the source code is only 400 lines, and some simple unit tests are done. Later, it may be further improved on the basis of this version according to your feedback.
2, Start using
- Define model
// Define the employee structure, which contains an ID card information and login information type Emp struct { ID uint `zero:"primaryKey"` Name string JobNumber *string ILogin Login `zero:"prefix:login\_"` IdCard *IdentityCard `zero:"prefix:idcard\_"` } // login information type Login struct { ID uint Code string EmpFk uint } // ID card information type IdentityCard struct { Code string }
Since it is an orm, it should reflect the association relationship between models in our structure. gzero identifies the primary key through the primaryKey. It will judge according to the primary key when processing the result set, and supports composite primary keys at the same time. Pointer type is not supported for primary key field.
- Create connection
ctx := Open("mysql", "root:123456@tcp(127.0.0.1:13306)/helloworld?charset=utf8&parseTime=True&loc=Local", Cfg{})
gzero calls database/sql internally. Ctx is not a singleton object. You can call the Open method repeatedly. But * SQL DB will only be created once. At present, I only do simple processing.
- Write sql and install it into the object
ctx.Sql("SELECT name, job\_number FROM t\_emp WHERE id = 1").Install(&emp)
The default structure field of gzero is the hump type, which is mapped to the serpentine field of table structure. For example, JobNumber corresponds to a job in the database_ number. You can also redefine the mapped field name through "column". If multiple structures contain fields with the same name, you can use "prefix" to add a uniform prefix to the internal structure (this feature refers to gorm).
Examples of results:
3, Get multiple result sets
Write sql and install it into Slice
var emps []Emp ctx.Sql("SELECT id, name FROM t\_emp").Install(&emps)
Multiple result sets need to be received using arrays or slices. Examples of results:
4, Process data according to the hierarchy of objects
code:
var emp []Emp ctx.Sql("SELECT e.id, e.name, e.job\_number, l.emp\_fk, l.code AS login\_code FROM t\_emp e, t\_login l WHERE e.id = l.emp\_fk").Install(&emp)
For the queried data, gzero will automatically process the data according to the hierarchy of the structure
5, Automatically merge data according to primary key
Define structure:
type Dept struct { ID uint `zero:"primaryKey"` Name string Emps []Emp `zero:"prefix:emp\_"` }
Query and install data:
var depts []Dept ctx.Sql("SELECT d.id, d.name, e.name AS emp\_name, l.code AS login\_code FROM t\_dept d, t\_emp e, t\_login l WHERE d.id = e.dept\_fk AND l.emp\_fk = e.id").Install(&depts)
The prefix of structure can be specified by alias in sql statement. The query results are as follows:
6, Summary
At present, the function of gzero is very simple, which is to process the query result set according to the hierarchical relationship pre-defined by the developer. After that, I intend to add the function of generating objects into sql to further improve the development efficiency of crud.