2 from typing
import List
6 from prettytable
import PrettyTable
13 def table_visual(self, title: list, val: List[dict]) -> PrettyTable:
16 msg=
'The database gets no return from the SQL of the field type in the table,'
17 ' and the SQL is written incorrectly, please check the SQL',
18 obj=SqlResultError, raise_exception=
True)
20 table = PrettyTable(title)
22 [table.add_row(list(i.values()))
for i
in val]
25 def check(self, res, fields) -> bool:
30 'auto_increment':
"自增",
31 'DEFAULT_GENERATED':
"自动追加日期",
32 'DEFAULT_GENERATED on update CURRENT_TIMESTAMP':
"自动更新时间为最后一次更改时间"
36 def deal(self, res, fields):
42 def res(self, tb_name, db_name, extool) -> dict:
47 COLUMN_NAME AS name, -- 名称
48 DATA_TYPE AS typer, -- 类型
49 CHARACTER_MAXIMUM_LENGTH AS length, -- 长度
50 NUMERIC_SCALE AS num_scale, -- 数字小数点
51 IS_NULLABLE AS is_null, -- 是否允许为空
52 COLUMN_KEY AS c_key, -- 是否为键
53 EXTRA as def_val, -- 默认值
54 COLUMN_COMMENT as comment -- 描述
55 FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = %s AND TABLE_NAME = %s"""
57 res = extool.select(sql=sql, params=[db_name, tb_name])
70 你可以使用find('table').by('args').order_by('args').desc().end()方式执行sql
87 自定义方言除了可使用默认已存在的方法,还可以使用自定义sql方言拼接
100 if repository
is None:
102 msg=
'Repository is null,Place use repository of ORM framework',
103 obj=AttributeError, LogObject=self.
repository.log_obj, raise_exception=
True)
125 def find(self, *args, **kwargs):
130 find('param1',asses=['p'],h_func=True)
132 asses:将对应的字段转成另一个别名,不需要转换的使用None标识
133 h_func:不将字段转换成 `%s` 格式
137 self.
exmode = AOrm.Mode.FIND
141 if 'alias' in kwargs.keys():
142 alias = kwargs[
'alias']
145 if 'h_func' in kwargs.keys():
146 func_flag = kwargs[
'h_func']
153 if _all
or 'all'.upper() == args[0].upper():
163 fields = self.
ParseUtil.parse_key(*args, is_field=
True)
165 fields = self.
ParseUtil.parse_key(*args, is_field=
False)
167 if alias
is not None:
168 fs = fields.split(
',')
169 if len(fs) != len(alias):
171 ALog.log_error(obj=TypeError,
172 msg=
'The length of the anonymous parameter does not match the length of the field',
173 raise_exception=
True)
174 for i, v
in enumerate(fs):
175 if alias[i]
is not None:
184 if alias
is not None:
188 if 'poly' not in kwargs.keys():
191 self.
args += kwargs[
'poly']
198 find('all').order_by('param')
199 find('all').order_by('param').end()
200 find('all').order_by('p1','p2').desc().limit(10,20)
202 return self.
by_opera(field=self.
sqlFields.order_by_str, args_list=args, **kwargs)
208 select shop_id,count(*) as count from comments group by shop_id having count>1;
210 return self.
by_opera(field=self.
sqlFields.group_by_str, args_list=args, **kwargs)
218 if not kwargs.get(
'text',
False):
221 if not kwargs.get(
'text',
False):
234 find('ALL').where(param='%s') - 默认符号为等于 ==
235 find('ALL').where(param='==%s')
236 find('ALL').where(param='>%d')
237 find('ALL').where(param='<%d')
238 find('ALL').where(param='<=%d')
239 find('ALL').where(param='>=%.2f')
240 find('ALL').where(param='!=%.2f')
242 find('ALL').where(param='+%d/%d==%d')
243 find('ALL').where(param='-%.2f*%d==12')
244 find('ALL').where(param='*10-1==12')
245 find('ALL').where(param='/10+1==12')
248 for key, value
in kwargs.items():
253 sps = cp_key.split(
'__')
255 if len(str(value)) > 2
and str(value)[0:2]
in self.
sqlFields.symbol:
257 value = str(value)[2:len(str(value))]
267 if not len(sps) == 1:
269 sym = sps[len(sps) - 1]
271 self.
ParseUtil,
'adapter', raise_exception=
True)
272 cp_key = cp_key[:cp_key.rfind(
'__' + sym)]
273 self.
ParseUtil.adapter.funcs[sym](self, cp_key, value)
275 elif not len(sps) == 1:
277 sym = sps[len(sps) - 1]
279 self.
ParseUtil,
'adapter', raise_exception=
True)
280 cp_key = cp_key[:cp_key.rfind(
'__' + sym)]
281 self.
ParseUtil.adapter.funcs[sym](self, cp_key, value)
285 '{}{}{}{}%s'.format(self.
sqlFields.left_subscript,
295 def on(self, from_where, to_where, symbol='='):
312 find('all').limit(start=10,end=20)
313 find('all').limit(end=10)
318 limit_param =
'{}{}{}'.format(
321 limit_param =
'{}{}{}{}{}'.format(self.
sqlFields.space, start, self.
sqlFields.comma, end,
331 find('all').desc().end()
332 find('all').order_by('param').desc().limit(10,20)
337 'There is no `order by` field before calling `desc` field,You have an error in your SQL syntax',
338 AttributeError, LogObject=self.
repository.log_obj, raise_exception=
True)
347 update('table').set('param','value').end()
348 update('table').set('param1','value1').where('param2=value2').end()
351 _size = len(kwargs.keys())
352 for key, value
in kwargs.items():
369 update('table').set('param1','value1').and().set('param2','value2')
370 update('table').set('param1','value1').and().set('param2','value2').end()
371 update('table').set('param1','value1').and().set('param2','value2').where('param3=value3').end()
376 def run(self, need_sql=False, serializer=True) -> QuerySet:
382 print_sql =
'print_sql' in conf.keys()
and conf[
'print_sql']
is True
383 last_id =
'last_id' in conf.keys()
and conf[
'last_id']
is True
384 sql +=
''.join(self.
args)
387 if self.
exmode == AOrm.Mode.FIND:
399 _result_objs.append(_obj)
431 如果没有被from包含,则在末尾加上from __table_name__关键字
449 将最后一个参数包含的指定字符替换为指定字符
452 1] = str(self.
args[len(self.
args) - 1]).replace(sym, rep)
456 return self.
run(**kwargs)
462 new_args = self.
args.copy()
463 new_args.append(
' ) ')
464 other.args.append(
' ( ')
465 other.args += new_args
472 new_args = other.args.copy()
473 new_args.append(
' ) ')
492 return self.
alias(name)
495 sql =
''.join(self.
args)
499 """sudo apt install containerd