diff --git a/CACodeFramework/MainWork/CACodePojo.py b/CACodeFramework/MainWork/CACodePojo.py index ae0902f476c8e40727603adf133c417b277864be..032a1cac62c23ddc04527499240332a469047590 100644 --- a/CACodeFramework/MainWork/CACodePojo.py +++ b/CACodeFramework/MainWork/CACodePojo.py @@ -1,17 +1,124 @@ +from CACodeFramework.MainWork.CACodePureORM import CACodePureORM +from CACodeFramework.field import field_tag from CACodeFramework.util import JsonUtil +from CACodeFramework.MainWork import CACodeRepository -class POJO(object): +class POJO(CACodeRepository.Repository): + def __init__(self, config_obj=None, log_conf=None, close_log=False, **kwargs): + """ + 初始化ORM框架 + """ + self.__table_name__ = self.__table_name__ + self.__table_msg__ = self.__table_msg__ + self.fields = self.init_fields() + for key, value in kwargs.items(): + self.__setattr__(key, value) + super(POJO, self).__init__(config_obj=config_obj, + participants=self, + log_conf=log_conf, + close_log=close_log) + + def init_fields(self): + """ + 初始化字段 + """ + fields = self.__dict__ + fds = {} + for key, value in fields.items(): + # 取出这个值引用对象的父类 + try: + t_v = value.__class__.__bases__ + t_bf = field_tag.baseTag + if t_v[0] == t_bf: + fds[key] = value + except SyntaxError: + continue + return fds def to_json(self): """ 将此对象转换为json + 无视时间报错 """ - return JsonUtil.parse(self.__dict__) + return JsonUtil.parse(self) def to_dict(self): """ 将此对象转换成字典格式 """ - return self.__dict__ + return self.fields + + def eq_default(self, val): + """ + 是否等于默认值 + """ + try: + t_v = val.__class__.__bases__ + t_bf = field_tag.baseTag + return t_v[0] == t_bf + except SyntaxError: + return False + + @property + def orm(self): + """ + 转ORM框架 + """ + return CACodePureORM(self) + + +class tinyintField(field_tag.baseTag): + def __init__(self, **kwargs): + super(tinyintField, self).__init__(**kwargs) + + +class intField(field_tag.baseTag): + def __init__(self, **kwargs): + super(intField, self).__init__(**kwargs) + + +class bigintField(field_tag.baseTag): + def __init__(self, **kwargs): + super(bigintField, self).__init__(**kwargs) + + +class floatField(field_tag.baseTag): + def __init__(self, **kwargs): + super(floatField, self).__init__(**kwargs) + + +class doubleField(field_tag.baseTag): + def __init__(self, **kwargs): + super(doubleField, self).__init__(**kwargs) + + +class datetimeField(field_tag.baseTag): + def __init__(self, **kwargs): + super(datetimeField, self).__init__(**kwargs) + + +class charField(field_tag.baseTag): + def __init__(self, **kwargs): + super(charField, self).__init__(**kwargs) + + +class varcharField(field_tag.baseTag): + def __init__(self, **kwargs): + super(varcharField, self).__init__(**kwargs) + + +class textField(field_tag.baseTag): + def __init__(self, **kwargs): + super(textField, self).__init__(**kwargs) + + +class tinytextField(field_tag.baseTag): + def __init__(self, **kwargs): + super(tinytextField, self).__init__(**kwargs) + + +class longtextField(field_tag.baseTag): + def __init__(self, **kwargs): + super(longtextField, self).__init__(**kwargs) diff --git a/CACodeFramework/MainWork/CACodePureORM.py b/CACodeFramework/MainWork/CACodePureORM.py index 71fafc70d9ceedd183f91be6998b879ecfce15e8..95ae8e6a61402994d50ea2facd11e1ce3b3f9b97 100644 --- a/CACodeFramework/MainWork/CACodePureORM.py +++ b/CACodeFramework/MainWork/CACodePureORM.py @@ -1,5 +1,5 @@ # 纯净ORM -from CACodeFramework.MainWork.opera import obj_dict +from CACodeFramework.MainWork.opera import obj_dict, op_db from CACodeFramework.util.ParseUtil import ParseUtil from CACodeFramework.field.sql_fields import * @@ -21,6 +21,7 @@ class CACodePureORM(object): 初始化ORM :param repository:仓库 """ + self.parses = op_db.parses() self.args = [] self.params = [] if repository is None: @@ -40,29 +41,33 @@ class CACodePureORM(object): :param pojo:需要插入的对象 """ # 添加insert关键字 - self.args.append(insert_str) - self.args.append('{}{}'.format(self.__table_name__, left_par)) - _dict = pojo.__dict__ - keys = [] - # 解析item - for key, value in _dict.items(): - # 去除为空的键 - if value is None: - continue - keys.append('`{}`{}'.format(key, comma)) - self.params.append(value) - for i in keys: - self.args.append(i) - # 将最后一个字段的逗号改成空格 - self.rep_sym(comma, space) - # 加上右边括号 - self.args.append(right_par) - self.args.append('{}{}'.format(values_str, left_par)) - for i in keys: - self.args.append('%s{}'.format(comma)) - # 将最后一个字段的逗号改成空格 - self.rep_sym(comma, space) - self.args.append(right_par) + # self.args.append(insert_str) + # self.args.append('{}{}'.format(self.__table_name__, left_par)) + sql = obj_dict.parses().parse_insert(pojo, self.__table_name__.replace('`', '')) + self.args.append(sql['sql']) + self.params = sql['params'] + + # _dict = pojo.__dict__ + # keys = [] + # # 解析item + # for key, value in _dict.items(): + # # 去除为空的键 + # if value is None: + # continue + # keys.append('`{}`{}'.format(key, comma)) + # self.params.append(value) + # for i in keys: + # self.args.append(i) + # # 将最后一个字段的逗号改成空格 + # self.rep_sym(comma, space) + # # 加上右边括号 + # self.args.append(right_par) + # self.args.append('{}{}'.format(values_str, left_par)) + # for i in keys: + # self.args.append('%s{}'.format(comma)) + # # 将最后一个字段的逗号改成空格 + # self.rep_sym(comma, space) + # self.args.append(right_par) return self def delete(self): diff --git a/CACodeFramework/MainWork/CACodeRepository.py b/CACodeFramework/MainWork/CACodeRepository.py index 3af071fe8763953d6520509cde8b6c280cf0d91f..4fd0061f59778085aa3c13cbeb3fb3e4b7096ff7 100644 --- a/CACodeFramework/MainWork/CACodeRepository.py +++ b/CACodeFramework/MainWork/CACodeRepository.py @@ -1,5 +1,4 @@ import copy -import sys from CACodeFramework.MainWork.opera import op_db from CACodeFramework.util.Log import CACodeLog @@ -13,9 +12,9 @@ import uuid # threadLocal 避免线程干扰 t_local = threading.local() + + # 线程锁 -# 返回的结果 -_result = None class LogObj(CACodeLog): @@ -70,14 +69,18 @@ class Repository(object): """ # 移除name和msg键之后,剩下的就是对应的数据库字段 # 设置表名 + # 是否关闭打印日志 + self.close_log = close_log self.__table_name__ = self.__table_name__ self.operation = op_db.DbOperation() self.parse = op_db.parses() - self.parse.log(_obj=self, msg='Being Initialize this object') + if not self.close_log: + CACodeLog.log(_obj=self, msg='Being Initialize this object') # 模板类 self.participants = participants # 该对象的所有字段 - self.fields = participants.to_dict().keys() + fds = participants.to_dict() + self.fields = list(fds.keys()) # 配置类 self.config_obj = config_obj # 操作数据库 @@ -89,38 +92,13 @@ class Repository(object): charset=self.config_obj.charset) # 设定返回值 self._result = None - # 是否关闭打印日志 - self.close_log = close_log # 配置日志 self.log_obj = None if log_conf is not None: self.log_obj = LogObj(**log_conf) - - # ----------------COPY---------------- - - # t_local.table_name = self.__table_name__ - # # 模板类 - # t_local.participants = participants - # # 该对象的所有字段 - # t_local.fields = participants.to_dict().keys() - # # 配置类 - # t_local.config_obj = config_obj - # # 操作数据库 - # t_local.db_util = DbUtil.Db_opera(host=t_local.config_obj.host, - # port=t_local.config_obj.port, - # user=t_local.config_obj.user, - # password=t_local.config_obj.password, - # database=t_local.config_obj.database, - # charset=t_local.config_obj.charset) - # # 设定返回值 - # t_local._result = None - # # 是否关闭打印日志 - # t_local.close_log = close_log - # # 配置日志 - # t_local.log_obj = None - # if log_conf is not None: - # t_local.log_obj = LogObj(**log_conf) + # 返回的结果 + self.result = None def conversion(self): """作者:CACode 最后编辑于2021/4/12 @@ -140,13 +118,12 @@ class Repository(object): Returns: 将所有结果封装成POJO对象集合并返回数据 """ - global _result # 设置名称 name = str(uuid.uuid1()) # 开启任务 - kwargs = {'func': self.operation.__find_all__, '__table_name__': name, 't_local': t_local} - self.operation.start(*t_local.fields, **kwargs) - return _result + kwargs = {'func': self.operation.__find_all__, '__task_uuid__': name, 't_local': self} + self.result = self.operation.start(*self.fields, **kwargs) + return self.result def find_by_field(self, *args): """作者:CACode 最后编辑于2021/4/12 @@ -163,15 +140,14 @@ class Repository(object): 将所有结果封装成POJO对象集合并返回数据 """ - global _result # 设置名称 name = str(uuid.uuid1()) # 开启任务 kwargs = {'func': self.operation.__find_by_field__, '__task_uuid__': name, 't_local': self} - _result = self.operation.start(*args, **kwargs) + self.result = self.operation.start(*args, **kwargs) - return _result + return self.result def find_one(self, **kwargs): """作者:CACode 最后编辑于2021/4/12 @@ -204,12 +180,11 @@ class Repository(object): :return 返回使用find_many()的结果种第一条 """ - global _result - _result = self.find_many(**kwargs) - if _result is None or len(_result) == 0: + self.result = self.find_many(**kwargs) + if self.result is None or len(self.result) == 0: return None else: - return _result[0] + return self.result[0] def find_many(self, **kwargs): """作者:CACode 最后编辑于2021/4/12 @@ -233,15 +208,14 @@ class Repository(object): :return 将所有数据封装成POJO对象并返回 """ - global _result # 设置名称 name = str(uuid.uuid1()) # 开启任务 kwargs['func'] = self.operation.__find_many__ - kwargs['__table_name__'] = name - kwargs['t_local'] = t_local - self.operation.start(**kwargs) - return _result + kwargs['__task_uuid__'] = name + kwargs['t_local'] = self + self.result = self.operation.start(**kwargs) + return self.result def find_sql(self, **kwargs): """ @@ -256,15 +230,14 @@ class Repository(object): print_sql:是否打印sql语句 """ # kwargs['conf_obj'] = t_local.config_obj - global _result # 设置名称 name = str(uuid.uuid1()) # 开启任务 kwargs['func'] = self.operation.__find_sql__ kwargs['__task_uuid__'] = name - kwargs['t_local'] = t_local - self.operation.start(**kwargs) - return _result + kwargs['t_local'] = self + self.result = self.operation.start(**kwargs) + return self.result def update(self, **kwargs): """ @@ -295,7 +268,14 @@ class Repository(object): """ kwargs = self.parse.print_sql(**kwargs) kwargs = self.parse.last_id(**kwargs) - return t_local.db_util.insert(**kwargs) + return self.db_util.insert(**kwargs) + + def save(self, **kwargs): + """ + 将当前储存的值存入数据库 + """ + kwargs['pojo'] = self + return self.insert_one(**kwargs) def insert_one(self, **kwargs): """ @@ -307,16 +287,14 @@ class Repository(object): params:需要填充的字段 :return:rowcount,last_id if last_id=True """ - global _result # 设置名称 name = str(uuid.uuid1()) # 开启任务 kwargs['func'] = self.operation.__insert_one__ kwargs['__task_uuid__'] = name - kwargs['__table_name__'] = t_local.table_name - kwargs['t_local'] = t_local - self.operation.start(**kwargs) - return _result + kwargs['t_local'] = self + self.result = self.operation.start(**kwargs) + return self.result def insert_many(self, **kwargs): """ @@ -344,15 +322,6 @@ class Repository(object): # """ # return self - def save(self, pojo): - if pojo is list: - return self.insert_many(pojo_list=pojo) - else: - return self.insert_one(pojo=pojo) - - - - def copy(self): """ 复制对象进行操做 diff --git a/CACodeFramework/MainWork/__pycache__/Annotations.cpython-39.pyc b/CACodeFramework/MainWork/__pycache__/Annotations.cpython-39.pyc index fd6972f614e7d223bc480c83e198e2dbb765cb5e..f8c58dfeae26ae8cd288a70af163dc1c38a14678 100644 Binary files a/CACodeFramework/MainWork/__pycache__/Annotations.cpython-39.pyc and b/CACodeFramework/MainWork/__pycache__/Annotations.cpython-39.pyc differ diff --git a/CACodeFramework/MainWork/__pycache__/CACodePojo.cpython-39.pyc b/CACodeFramework/MainWork/__pycache__/CACodePojo.cpython-39.pyc index 25856d77873e83976ae67b87eba539b666bec123..67d39a40abb877adc9e874b3813652affcb8815d 100644 Binary files a/CACodeFramework/MainWork/__pycache__/CACodePojo.cpython-39.pyc and b/CACodeFramework/MainWork/__pycache__/CACodePojo.cpython-39.pyc differ diff --git a/CACodeFramework/MainWork/__pycache__/CACodePureORM.cpython-39.pyc b/CACodeFramework/MainWork/__pycache__/CACodePureORM.cpython-39.pyc index 27113013701627ab247b85a4319f59ad675df39a..85963b53b56f0e106fc91e799f0914f2dda84f19 100644 Binary files a/CACodeFramework/MainWork/__pycache__/CACodePureORM.cpython-39.pyc and b/CACodeFramework/MainWork/__pycache__/CACodePureORM.cpython-39.pyc differ diff --git a/CACodeFramework/MainWork/__pycache__/CACodeRepository.cpython-39.pyc b/CACodeFramework/MainWork/__pycache__/CACodeRepository.cpython-39.pyc index 0d74c4feaf7958f965d3770b8ea0f6d1a40fc91c..6c5f6d5ee8ca3907f3d13315f3c766b03e158e58 100644 Binary files a/CACodeFramework/MainWork/__pycache__/CACodeRepository.cpython-39.pyc and b/CACodeFramework/MainWork/__pycache__/CACodeRepository.cpython-39.pyc differ diff --git a/CACodeFramework/MainWork/exception/__pycache__/e_except.cpython-39.pyc b/CACodeFramework/MainWork/exception/__pycache__/e_except.cpython-39.pyc index a5a7230ef418aaa8aa4b43937b58fceaebfcc7e3..ada947f55639221d46c2c7efd801b2a60f578755 100644 Binary files a/CACodeFramework/MainWork/exception/__pycache__/e_except.cpython-39.pyc and b/CACodeFramework/MainWork/exception/__pycache__/e_except.cpython-39.pyc differ diff --git a/CACodeFramework/MainWork/exception/e_except.py b/CACodeFramework/MainWork/exception/e_except.py index 06cb2dc5d842b089b237595df62d8c1b01cc1efd..d76a1c846b109c8c671cd7f17597f51ee5aa6f80 100644 --- a/CACodeFramework/MainWork/exception/e_except.py +++ b/CACodeFramework/MainWork/exception/e_except.py @@ -1,6 +1,7 @@ from datetime import datetime import warnings -from . import e_fields + +from CACodeFramework.field import e_fields def warn(obj, line, msg, f_warn, LogObject=None, task_name='\t\tMAIN'): diff --git a/CACodeFramework/MainWork/opera/__pycache__/obj_dict.cpython-39.pyc b/CACodeFramework/MainWork/opera/__pycache__/obj_dict.cpython-39.pyc index 7bec501f790b66b5784e45bab64c79adf1b70a56..7475998d1dbe35583e3c538fb681aa6ac2e4e080 100644 Binary files a/CACodeFramework/MainWork/opera/__pycache__/obj_dict.cpython-39.pyc and b/CACodeFramework/MainWork/opera/__pycache__/obj_dict.cpython-39.pyc differ diff --git a/CACodeFramework/MainWork/opera/__pycache__/op_db.cpython-39.pyc b/CACodeFramework/MainWork/opera/__pycache__/op_db.cpython-39.pyc index 08e8060a6789d7353ba51321dca4f7e56d2416a5..3344c3ad6be5eda086b00a7262216aa8f98fa62d 100644 Binary files a/CACodeFramework/MainWork/opera/__pycache__/op_db.cpython-39.pyc and b/CACodeFramework/MainWork/opera/__pycache__/op_db.cpython-39.pyc differ diff --git a/CACodeFramework/MainWork/opera/obj_dict.py b/CACodeFramework/MainWork/opera/obj_dict.py index bbf914fe5c6c72c6c631f3f76d6030e537cf898d..5d00ca8882472e1153e85528a4753e5a079f138f 100644 --- a/CACodeFramework/MainWork/opera/obj_dict.py +++ b/CACodeFramework/MainWork/opera/obj_dict.py @@ -1,7 +1,8 @@ import copy import sys -from CACodeFramework.MainWork.exception import e_except, e_fields +from CACodeFramework.MainWork.exception import e_except +from CACodeFramework.field import e_fields from CACodeFramework.util.ParseUtil import ParseUtil @@ -81,15 +82,18 @@ class parses(object): :param __table_name__:表名 :return: """ - _dict = pojo.__dict__ - keys = [] - values = [] - for key, value in _dict.items(): - if value is None: - continue - keys.append(key) - values.append(value) - return ParseUtil().parse_insert(keys, values, __table_name__) + _dict = pojo.to_dict() + # 得到所有的键 + keys = pojo.fields + # 在得到值之后解析是否为空并删除为空的值和对应的字段 + cp_value = [] + values = [getattr(pojo, v) for v in keys] + for i, j in enumerate(values): + if j is None or pojo.eq_default(j): + del keys[i] + else: + cp_value.append(j) + return ParseUtil().parse_insert(keys, cp_value, __table_name__) def parse_obj(self, data: dict, participants): """ diff --git a/CACodeFramework/MainWork/opera/op_db.py b/CACodeFramework/MainWork/opera/op_db.py index de29a3fb72aef5ff04eba46c5e8495b90c5f09d4..f850c1af06cd748243550ef5d1d9c53b5d77f5f2 100644 --- a/CACodeFramework/MainWork/opera/op_db.py +++ b/CACodeFramework/MainWork/opera/op_db.py @@ -1,5 +1,7 @@ import threading +from CACodeFramework.util.Log import CACodeLog + from CACodeFramework.MainWork.opera.obj_dict import parses from CACodeFramework.field.sql_fields import * from CACodeFramework.util.ParseUtil import ParseUtil @@ -28,7 +30,7 @@ class DbOperation(object): _lock = kwargs['t_local'] name = kwargs['__task_uuid__'] if not _lock.close_log: - self.parse_util.log(_obj=kwargs['func'], msg='TASK-{} START'.format(name), name=name) + CACodeLog.log(_obj=kwargs['func'], msg='TASK-{} START'.format(name), name=name, LogObject=kwargs['log_obj']) # # 设置任务 # _kw = JsonUtil.load(JsonUtil.parse(_lock)) _kw = _lock.__dict__ @@ -36,7 +38,7 @@ class DbOperation(object): _t = threading.Thread(target=func, args=args, kwargs=kwargs, name=name) _t.start() if not _lock.close_log: - self.parse_util.log(_obj=_t, msg='TASK-{} RUNNING'.format(name), name=name) + CACodeLog.log(_obj=_t, msg='TASK-{} RUNNING'.format(name), name=name, LogObject=kwargs['log_obj']) # 等待任务完成 _t.join() # 返回结果 @@ -100,11 +102,9 @@ class DbOperation(object): if 'pojo' not in kwargs.keys(): raise SyntaxError('the key of `pojo` cannot be found in the parameters') - filed_list = self.parse_util.parse_insert(kwargs['pojo'], __table_name__=kwargs['table_name']) - - kwargs['sql'] = filed_list['sql'] + filed_list = self.parse_util.parse_insert(kwargs['pojo'], __table_name__=kwargs['__table_name__']) - kwargs['params'] = filed_list['params'] + kwargs.update(filed_list) self.result = kwargs['db_util'].insert(**kwargs) diff --git a/CACodeFramework/MainWork/exception/__pycache__/e_fields.cpython-39.pyc b/CACodeFramework/field/__pycache__/e_fields.cpython-39.pyc similarity index 53% rename from CACodeFramework/MainWork/exception/__pycache__/e_fields.cpython-39.pyc rename to CACodeFramework/field/__pycache__/e_fields.cpython-39.pyc index 2f9e343880225713fd6e91e2adad84b5c1b27baf..bd1eed38e799d2b4a63fb364ff8ab1933683d347 100644 Binary files a/CACodeFramework/MainWork/exception/__pycache__/e_fields.cpython-39.pyc and b/CACodeFramework/field/__pycache__/e_fields.cpython-39.pyc differ diff --git a/CACodeFramework/field/__pycache__/field_tag.cpython-39.pyc b/CACodeFramework/field/__pycache__/field_tag.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..78277e87aa949ab9c562dd91b64809c3d6e6ff12 Binary files /dev/null and b/CACodeFramework/field/__pycache__/field_tag.cpython-39.pyc differ diff --git a/CACodeFramework/MainWork/exception/e_fields.py b/CACodeFramework/field/e_fields.py similarity index 100% rename from CACodeFramework/MainWork/exception/e_fields.py rename to CACodeFramework/field/e_fields.py diff --git a/CACodeFramework/field/field_tag.py b/CACodeFramework/field/field_tag.py new file mode 100644 index 0000000000000000000000000000000000000000..2b51075cddbf14c01655614946dc23ac0551a4a0 --- /dev/null +++ b/CACodeFramework/field/field_tag.py @@ -0,0 +1,42 @@ +class baseTag(object): + def __init__(self, table_name=None, + name=None, + length=None, + d_point=None, + t_type='varchar', + is_null=False, + primary_key=False, + comment="", + auto_field=False, + auto_time=False, + update_auto_time=False): + """ + :param table_name:表名称 + :param name:字段名 + :param length:长度 + :param d_point:小数点 + :param t_type:类型 + :param is_null:允许为空 + :param primary_key:键 + :param comment:注释 + :param auto_field:自增长键 + :param auto_time:默认设置当前时间 + :param update_auto_time:默认设置当前时间并根据当前时间更新 + """ + self.update_auto_time = update_auto_time + self.auto_time = auto_time + self.autoField = auto_field + self.table_name = table_name + self.comment = comment + self.primary_key = primary_key + self.is_null = is_null + self.d_point = d_point + self.name = name + self.t_type = t_type + self.length = length + + def get_table(self): + """ + 获取表数据结构 + """ + return self.__dict__ diff --git a/CACodeFramework/util/DbUtil.py b/CACodeFramework/util/DbUtil.py index 71270f4d9e7657e70f08fa73036a83b0eb7d6ddf..43dfe4a3e4ad2ec3f977d30f363802b5073f6eea 100644 --- a/CACodeFramework/util/DbUtil.py +++ b/CACodeFramework/util/DbUtil.py @@ -1,7 +1,11 @@ +import sys + +from CACodeFramework.util.Log import CACodeLog from dbutils.pooled_db import PooledDB import pymysql -from CACodeFramework.MainWork.opera import op_db +from CACodeFramework.MainWork.exception import e_except +from CACodeFramework.field import e_fields def parse_kwa(db, **kwargs): @@ -22,8 +26,9 @@ def parse_kwa(db, **kwargs): else: sql = kwargs['sql'] if 'print_sql' in kwargs.keys() and kwargs['print_sql'] is True: - # print(sql) - op_db.parses().log(_obj=db, msg='Being Initialize this object') + _l = sys._getframe().f_back.f_lineno + e_except.warn(obj=db, line=_l, task_name='Print Sql', f_warn=e_fields.INFO, msg=sql) + CACodeLog.log(_obj=db, msg='Being Initialize this object') cursor.execute(sql) return cursor except Exception as e: diff --git a/CACodeFramework/util/JsonUtil.py b/CACodeFramework/util/JsonUtil.py index 72ba77260c3aa4eeac6aae7200974f5609687f22..52272bf1d8ed792d9c26a0e2e3a96cfd2b115bae 100644 --- a/CACodeFramework/util/JsonUtil.py +++ b/CACodeFramework/util/JsonUtil.py @@ -27,7 +27,7 @@ class SimplejsonDateEncoder(simplejson.JSONEncoder): return date_encoder(obj) -def parse(obj): +def parse(obj, bf=False): """作者:CACode 最后编辑于2021/4/10 将对象转换成字典格式: @@ -41,6 +41,8 @@ def parse(obj): ....... Returns: 返回字典格式 + :param obj:需要解析的对象 + :param bf:是否需要美化json """ def json_to_str(_obj): @@ -129,7 +131,11 @@ def parse(obj): obj = parse_dict(obj) elif isinstance(obj, object): obj = parse_obj(obj) - return json_to_str(obj) + # 最后的解析结果 + result = json_to_str(obj) + if bf: + return beautiful(load(result)) + return result def load(item): diff --git a/CACodeFramework/util/Log.py b/CACodeFramework/util/Log.py index 24e4f57dc9a5446a6aa22888429dae1aa0251009..3750a6fd2b1fc834677991e17066ca4e88fbccd2 100644 --- a/CACodeFramework/util/Log.py +++ b/CACodeFramework/util/Log.py @@ -1,6 +1,10 @@ import os +import sys import time +from CACodeFramework.MainWork.exception import e_except +from CACodeFramework.field import e_fields + def date_format(time_obj=time, fmt='%Y-%m-%d %H:%M:%S'): """ @@ -65,6 +69,23 @@ class CACodeLog(object): self.print_flag = print_flag self.save_flag = save_flag + @staticmethod + def log(_obj, msg, name='\t\tTask', LogObject=None): + """ + 输出任务执行日志 + + :param _obj:执行日志的对象地址 + :param msg:消息 + :param name:任务对象的值 + :param LogObject:写出文件的对象 + + """ + # 获得该函数被调用前的行号 + _l = sys._getframe().f_back.f_lineno + # 格式:时间 类型 日志名称 对象地址 被调用行号 执行类型 信息 + info = e_except.warn(obj=_obj, line=_l, task_name=name, f_warn=e_fields.INFO, msg=msg, LogObject=LogObject) + return info + def success(self, content): """ 成功日志 diff --git a/CACodeFramework/util/ParseUtil.py b/CACodeFramework/util/ParseUtil.py index c04f12661ced2d5b91e9eeab877ef0e0d5eae95f..181d0977abcc39c68141331a3db6769705fb0eb3 100644 --- a/CACodeFramework/util/ParseUtil.py +++ b/CACodeFramework/util/ParseUtil.py @@ -85,14 +85,17 @@ class ParseUtil(object): self.to_str = False values = self.parse_value(*values) # 分析需要几个隐藏值 - hides_value = '' - for i in range(len(values)): - hides_value += '%s,' + hides_value = ['%s,' for i in range(len(values))] + # for i in range(len(values)): + # hides_value += '%s,' # 去除末尾的逗号 - hides_value = hides_value[0: len(hides_value) - 1] + end = hides_value[len(hides_value) - 1] + hides_value[len(hides_value) - 1] = end[0: len(end) - 1] + # 得到最后隐藏符号的字符串表达格式 + value = ''.join(hides_value) sql = '%s`%s` (%s)%s(%s)' % ( insert_str, - str(__table_name__), fields, values_str, hides_value + str(__table_name__), fields, values_str, value ) kes = {'sql': sql} diff --git a/CACodeFramework/util/__pycache__/DbUtil.cpython-39.pyc b/CACodeFramework/util/__pycache__/DbUtil.cpython-39.pyc index 2e50e8a776a1c7a6de511de4e6e4a1ea5fccccb9..2a028cee94f31dcf080cae384e3899f75f1cb63d 100644 Binary files a/CACodeFramework/util/__pycache__/DbUtil.cpython-39.pyc and b/CACodeFramework/util/__pycache__/DbUtil.cpython-39.pyc differ diff --git a/CACodeFramework/util/__pycache__/JsonUtil.cpython-39.pyc b/CACodeFramework/util/__pycache__/JsonUtil.cpython-39.pyc index 3152b00981dbd22444de07421968ddbf6744c80c..3154293557ac5e4f020718c599e7e80e1defc054 100644 Binary files a/CACodeFramework/util/__pycache__/JsonUtil.cpython-39.pyc and b/CACodeFramework/util/__pycache__/JsonUtil.cpython-39.pyc differ diff --git a/CACodeFramework/util/__pycache__/Log.cpython-39.pyc b/CACodeFramework/util/__pycache__/Log.cpython-39.pyc index 953efffae7bed731f99cb0f4414400a096f8aa9e..f4e01472bd6234d4d7507c23591dfaf637ff13be 100644 Binary files a/CACodeFramework/util/__pycache__/Log.cpython-39.pyc and b/CACodeFramework/util/__pycache__/Log.cpython-39.pyc differ diff --git a/CACodeFramework/util/__pycache__/ParseUtil.cpython-39.pyc b/CACodeFramework/util/__pycache__/ParseUtil.cpython-39.pyc index 6f3477eda2b1ff619d3ab2a166a4815f8cb35c7a..a3b41126c44e2e642a710c8deace49d85b0b8906 100644 Binary files a/CACodeFramework/util/__pycache__/ParseUtil.cpython-39.pyc and b/CACodeFramework/util/__pycache__/ParseUtil.cpython-39.pyc differ diff --git a/test/test.py b/test/test.py index 7601f37b3261f8ef6aca0ab78e1d33332f925bec..a32a4c6bd158da52e6d7a57af925e44afd353e15 100644 --- a/test/test.py +++ b/test/test.py @@ -1,13 +1,11 @@ -import sys import threading import time -from CACodeFramework.MainWork.CACodePojo import POJO +from CACodeFramework.util.Log import CACodeLog -from CACodeFramework.MainWork.CACodeRepository import Repository +from CACodeFramework.MainWork import CACodePojo -from CACodeFramework.MainWork.Annotations import Table, Operations -from CACodeFramework.MainWork.CACodePureORM import CACodePureORM +from CACodeFramework.MainWork.Annotations import Table from CACodeFramework.util import Config, JsonUtil @@ -27,95 +25,78 @@ class ConF(Config.config): super(ConF, self).__init__(host, port, database, user, password, charset, conf=conf) -class Demo(POJO): - def __init__(self): - self.index = None - self.title = None - self.selects = None - self.success = None - - @Table(name="demo_table", msg="demo message") -# @Operations() -class TestClass(Repository): - def __init__(self): - super(TestClass, self).__init__(config_obj=ConF(), participants=Demo()) - - def _test(self): - pass - - -testClass = TestClass() -orm = CACodePureORM(testClass) +class Demo(CACodePojo.POJO): + def __init__(self, **kwargs): + self.index = CACodePojo.intField(name='index', primary_key=True) + self.title = CACodePojo.varcharField(length=255) + self.selects = CACodePojo.varcharField(length=255) + self.success = CACodePojo.varcharField(length=255) + super(Demo, self).__init__(config_obj=ConF(), close_log=True, **kwargs) def setData(): + a = [] + h = Demo() for i in range(2): - h = Demo() - h.title = "test title" - h.selects = "test selects" - h.success = "false" + h = Demo(title="test title", selects="test selects", success='false') + a.append(h) # _result = testClass.insert_one(pojo=h) - _result = orm.insert(h).end() - print(_result) + _r = Demo().orm.insert(h).end() + # h.insert_many(pojo_list=a) # _result = testClass.insert_many(pojo_list=pojos) # print('受影响行数:{}\t,\t已插入:{}'.format(_result, i)) +data_count = 0 + + def th(): def A(): - for i in range(10): - t = TestClass() - # a = t.conversion().find().end() - a = t.find_sql(sql='SELECT * FROM demo_table') - print('id:', t) - print(a) + for i in range(100): + d = Demo() + a = d.find_sql(sql='SELECT * FROM demo_table') + global data_count + data_count += len(a) def B(): - for i in range(10): - t = TestClass() - b = t.find_many(sql='SELECT * FROM demo_table') - print(b) + for i in range(100): + b = Demo().find_many(sql='SELECT * FROM demo_table') + global data_count + data_count += len(b) def C(): - for i in range(10): - t = TestClass() - c = t.find_all() - print(c) + for i in range(100): + c = Demo().find_all() + global data_count + data_count += len(c) def D(): - for i in range(10): - t = TestClass() - d = t.find_by_field('title', 'selects') - print(d) - - t1 = time.time() - # _a = threading.Thread(target=A) - # _b = threading.Thread(target=B) - # _c = threading.Thread(target=C) + for i in range(100): + d = Demo().find_by_field('title', 'selects') + global data_count + data_count += len(d) + + _a = threading.Thread(target=A) + _b = threading.Thread(target=B) + _c = threading.Thread(target=C) _d = threading.Thread(target=D) - - # _a.start() - # _b.start() - # _c.start() - _d.start() - - # _a.join() - # _b.join() - # _c.join() - _d.join() - t2 = time.time() - print(t2 - t1) - - -def copy(): - return testClass.copy() + return _a, _b, _c, _d if __name__ == '__main__': # setData() - # th() - # print(copy()) - # print(copy()) - _r = orm.find('COUNT(*)', asses=['c'], h_func=True).end() - print(_r[0].c) + # c = Demo().orm.find('count(*)', asses=['c'], h_func=True).end()[0] + # print('count:', c.c) + # t1 = time.time() + # t = th() + # for i in t: + # i.start() + # i.join() + # t2 = time.time() + # print('time:', t2 - t1) + # print('data count:', data_count) + # print('average:', data_count / (t2 - t1)) + + _r = Demo().orm.update().set(success='true').where(index=17036).end() + print(_r)