-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper_database.py
executable file
·82 lines (70 loc) · 2.91 KB
/
helper_database.py
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
"""
pathfinder helper_database.py
:copyright: (c) 2018 by Pavel Konovalov
Created on 02.07.2018
"""
from enum import IntEnum
class MysqlType(IntEnum):
JSON = 245
def cursor_to_json(cursor):
"""
Преобразует результат запроса к БД в JSON объект
:param cursor:
:return: JSON
"""
return [dict((cursor.description[i][0], value) for i, value in enumerate(row) if value is not None and value != '')
for row in
cursor.fetchall()]
def cursor_one_to_json(cursor):
"""
Преобразует результат запроса к БД в JSON объект
:param cursor:
:return: JSON
"""
return [dict((cursor.description[i][0], value) for i, value in enumerate(row) if value is not None and value != '')
for row in cursor.fetchall()]
def array_to_where_in(table_name, field_name, value_array, condition='and'):
"""
Перобразует массив значений в условие включения или одно значение в условие равенства
:param table_name:
:param value_array:
:param field_name:
:param condition:
:return: String
"""
if value_array is not None:
if isinstance(value_array, type([])):
if len(value_array) > 0:
return ' {} {}.{} in ({})'.format(condition, table_name, field_name, ','.join(map(str, value_array)))
else:
if value_array is not None:
return ' {} {}.{} = {}'.format(condition, table_name, field_name, value_array)
else:
return ''
else:
return ''
def array_to_where_between(table_name, field_name, value_array):
"""
Преобразует массив значений из двух элементов в условие between
:param table_name:
:param value_array:
:param field_name:
:return: String
"""
r = ''
if value_array is not None:
if isinstance(value_array, type([])):
if len(value_array) == 2:
if isinstance(value_array[0], int) and isinstance(value_array[1], int):
if value_array[0] == value_array[1]:
r = ' and {}.{} = {}'.format(table_name, field_name, value_array[0])
else:
r = ' and {}.{} between {} and {}'.format(table_name, field_name, value_array[0],
value_array[1])
else:
if isinstance(value_array[0], int) and value_array[1] is None:
r = ' and {}.{} >= {}'.format(table_name, field_name, value_array[0])
elif isinstance(value_array[1], int) and value_array[0] is None:
r = ' and {}.{} <= {}'.format(table_name, field_name, value_array[1])
return r