项目作者: fatpo

项目描述 :
异步ORM=用于tornado+mysql的业务DAO,极大提高生产效率...
高级语言: Python
项目地址: git://github.com/fatpo/torBizMysqlDao.git
创建时间: 2018-02-02T06:16:50Z
项目社区:https://github.com/fatpo/torBizMysqlDao

开源协议:MIT License

下载


torBizMysqlDao

Business DAO for tornado + mysql, great productivity gains .
Mainly used in writing business logic code, CRUD is indeed a large number of duplicate code, it is not pythonic .
So with the configuration of the program to write a specific business DAO, is pythonic.

Simulate the scene

Suppose there is a module now called: Student, you need to add or delete “Student”.
Database Table:

  1. CREATE TABLE `student` (
  2. `id` int(11) NOT NULL AUTO_INCREMENT,
  3. `name` VARCHAR(128) not NULL DEFAULT '' comment 'student's name',
  4. `age` int(11) not NULL DEFAULT 0 comment 'student's age',
  5. `del_flag` int(11) NOT NULL DEFAULT 0 comment 'soft delete flag,0-normal,1-deleted',
  6. `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP comment 'create time',
  7. `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP comment 'last update time',
  8. PRIMARY KEY (`id`)
  9. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

We define a student Info:
(or you can remove the info layer, direct control of the dict from db returns, if u want …):

  1. class StudentInfo(BaseInfo):
  2. def __init__(self, _item):
  3. self.id = 0
  4. self.name = ''
  5. self.age = 0
  6. self.del_flag = 0
  7. self.create_time = ''
  8. self.update_time = ''
  9. if isinstance(_item, dict):
  10. for key, value in _item.iteritems():
  11. self.__dict__[key] = value

then config a StudentDao:

  1. class StudentDao(BaseDao):
  2. DataInfo = StudentInfo
  3. table_name = 'student' # table name
  4. escape_list = ['name'] # the list need to be escaped
  5. quot_list = ['create_time', 'update_time'] # the list requires quoted
  6. not_append_list = ['del_flag'] # int list like: img_id
  7. append_list = ['age'] # int list, but sometimes need to += n, like: add_cnt = add_cnt+10, view_cnt=view_cnt+1

Test case-1:

get the student which id=1 and name=’tom’

  1. pool = db.app_pool
  2. with (yield pool.Connection()) as conn:
  3. yield conn.commit()
  4. context = None # the context for app request...
  5. stu_id = 1
  6. stu_name = 'tom'
  7. # get the student which id=1 and name ='tom'
  8. user = yield StudentDao.get_by_cols(context, conn, where_lst=[
  9. {
  10. 'key': stu_id,
  11. 'where_col': 'id',
  12. 'col_str': False
  13. },
  14. {
  15. 'key': stu_name,
  16. 'where_col': 'name',
  17. 'col_str': True
  18. }
  19. ], is_fetchone=True, with_del=True)

Test case-2:

update the sutdent age+=1 who’s name=’tom’

  1. pool = db.app_pool
  2. with (yield pool.Connection()) as conn:
  3. yield conn.commit()
  4. context = None # the context for app request...
  5. stu_name = 'tom'
  6. # update age += 1
  7. dic = {
  8. 'name': stu_name,
  9. 'age': (1, True)
  10. }
  11. yield StudentDao.update(context, conn, dic)
  12. yield conn.commit()