1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
from __future__ import unicode_literals, absolute_import from handlers import APIHandler
import logging
logger = logging.getLogger(__name__)
class QueryHandler(APIHandler): def get(self): """ 从数据库中查询数据 :return: """ sql = 'select * from TEST_EMP t where t.salary > :salary' salary = self.get_query_argument('salary', 1000) params = { 'salary': salary } rows = self.db.query(sql, **params) data = rows.as_dict() return self.success(data, msg='获取数据成功')
def post(self): sql = 'select * from TEST_EMP t where t.salary > :salary' salary = self.post_data.get('salary', 1000) params = { 'salary': salary } rows = self.db.query(sql, **params) data = [{ 'name': r.name, 'salary': r.salary } for r in rows] return self.success(data, msg='获取数据成功')
class InsertHandler(APIHandler): def post(self): logger.debug(self.post_data) sql = 'insert into TEST_EMP (EMP_NO, NAME, JOB, SALARY) values(:emp_no, :name, :job, :salary)' params = { 'emp_no': self.post_data.get('emp_no', 0), 'name': self.post_data.get('name', 'no name'), 'job': self.post_data.get('job', 'no job'), 'salary': self.post_data.get('salary', 1000) } self.db.query(sql, **params) return self.success(params, msg='插入数据成功')
class TransactionHandler(APIHandler): """ 带有事务的操作 """
def post(self): logger.debug(self.post_data) t = self.db.transaction() sql = 'insert into TEST_EMP (EMP_NO, NAME, JOB, SALARY) values(:emp_no, :name, :job, :salary)' params = { 'emp_no': self.post_data.get('emp_no', 0), 'name': self.post_data.get('name', 'no name'), 'job': self.post_data.get('job', 'no job'), 'salary': self.post_data.get('salary', 1000) } self.db.query(sql, **params) if self.post_data.get('rollback'): t.rollback() else: t.commit()
return self.success(params, msg='事务执行成功')
class ProcedureHandler(APIHandler): """ 使用存储过程 create or replace procedure sp_update_salary ( uEmpNo in int, -- 员工编号 uSalary in int -- 将工资更新为多少 ) as begin update TEST_EMP set salary = uSalary where emp_no = uEmpNo; commit; end sp_update_salary; """
def post(self): logger.debug(self.post_data) sp_name = 'sp_update_salary' params = [ self.post_data.get('emp_no', 0), self.post_data.get('salary', 1000) ]
self.db.callproc(sp_name, params) return self.success(params, msg='存储过程执行成功')
class ProcedureReturnHandler(APIHandler): """ 使用存储过程,带有返回值 存储过程 create or replace procedure sp_add_salary ( uOldSalary in int, -- 小于该工资的需要增加 uSalary in int, -- 加多少工资 uCount out number -- 更新了多少个 ) as begin select count(*) into uCount from TEST_EMP where salary <= uOldSalary; update TEST_EMP set salary = salary + uSalary where salary <= uOldSalary; commit; end sp_update_salary; """
def post(self): logger.debug(self.post_data) sp_name = 'sp_add_salary' params = [ self.post_data.get('old_salary', 1000), self.post_data.get('salary', 1000), 0 ]
ret = self.db.callproc(sp_name, params) logger.info('更新了多少数据项:%s' % ret[2]) return self.success(params, msg='存储过程执行成功')
class ProcedureReturnCursorHandler(APIHandler): """ 使用存储过程,返回值使用游标 存储过程 create or replace procedure sp_query_salary ( uSalary in int, -- 小于多少工资 rcursor out sys_refcursor -- 返回的游标 ) as begin open rcursor for select emp_no, name, salary from TEST_EMP where salary <= uSalary; end sp_query_salary; """
def post(self): logger.debug(self.post_data) sp_name = 'sp_query_salary' params = [ self.post_data.get('salary', 1000), self.db.cursor() ]
ret = self.db.callproc(sp_name, params) rows = ret[1] data = [{ 'emp_no': t[0], 'name': t[1], 'salary': t[2] } for t in rows] return self.success(data, msg='存储过程执行成功')
|