-
Notifications
You must be signed in to change notification settings - Fork 89
/
joint_sql.py
326 lines (289 loc) · 9.78 KB
/
joint_sql.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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
v0.1.2 2016/12/19 yilai 建表改成utf8mb4字符集
v0.1.1 2016/09/21 yilai 加了些参数
v0.1.0 2016/07/20 yilai created
"""
from datetime import datetime,date,timedelta
from decimal import Decimal
import traceback
import logging
from pymysqlreplication import BinLogStreamReader
from pymysqlreplication.row_event import (
DeleteRowsEvent,
UpdateRowsEvent,
WriteRowsEvent,
)
from pymysqlreplication.event import(
QueryEvent,
XidEvent
)
from constant import Constant
logger = logging.getLogger("__main__")
def joint_insert_sql(schema,table,pk,row,add_schema_name=False):
"""
拼接insert语句
:param schema:
:param table:
:param pk: 主键列名,数组类型
:param row: 分析binlog的数据,包括字段名和值
:return: sql
"""
logger.debug(u"schema={0},table={1},pk={2},row={3}".format(schema,table,pk,row))
#sql=u"update {0} set {1} where {2}"
table_name=generate_table_name(schema,table,False,add_schema_name)
(columns,values)=generate_two_array_column_and_value(row["values"])
statment_column=",".join(columns)
statment_value=",".join(values)
sql=u"insert into "+table_name+"("+statment_column+ ") values("+statment_value+")"
logger.debug(sql)
return sql
def joint_update_sql(schema,table,pk,row,update_pk=True,add_schema_name=False):
"""
拼接update语句
:param schema:
:param table:
:param pk: 主键列名,数组类型
:param row: 分析binlog的数据,包括字段名和值
:param update_pk 是否产生更新pk的语句
:return: sql
"""
logger.debug(u"schema={0},table={1},pk={2},row={3}".format(schema,table,pk,row))
#sql=u"update {0} set {1} where {2}"
table_name=generate_table_name(schema,table,False,add_schema_name)
#TODO ,key没有加escape
if update_pk:
statment_dict=row["before_values"]
else:
(statment_dict,null)=split_dict_column_value_pair(pk,row["before_values"])
(null,pk_dict)=split_dict_column_value_pair(pk,row["after_values"])
statment=generate_array_column_value_pair(statment_dict)
statment=",".join(statment)
where =generate_array_column_value_pair(pk_dict)
where = " and ".join(where)
sql=u"update "+table_name+" set"+statment+ " where "+where
logger.debug(sql)
return sql
def joint_delete_sql(schema,table,pk,row,add_schema_name=False):
"""
拼接delete语句
:param schema:
:param table:
:param pk: 主键列名,数组类型
:param row: 分析binlog的数据,包括字段名和值
:return: sql
"""
logger.debug(u"schema={0},table={1},pk={2},row={3}".format(schema,table,pk,row))
if pk is None or pk is "":
raise ValueError(u"could not find primary key for table,schema={0},table={1}".format(schema,table))
table_name=generate_table_name(schema,table,False,add_schema_name)
temp=generate_dict_pk(pk,row["values"])
where =generate_array_column_value_pair(temp)
where = " and ".join(where)
sql=u"delete from "+table_name + " where "+where
logger.debug(sql)
return sql
def generate_dict_pk(pk,row):
"""
产生基于主键的dict类型的{列名,列值}
:param pk: 可能是string或者tuple
:param row:
:return:{列名,列值}
"""
dt={}
if isinstance(pk,unicode):
dt[pk]=row[pk]
else:
for key in pk :
dt[key]=row[key]
return dt
def split_dict_column_value_pair(pk,row):
"""
产生基于主键的dict类型的{列名,列值}
:param pk: 可能是string或者tuple
:param row:
:return: 其他列字典{列名,列值} ,主键字典{列名,列值}
"""
pk_orgi={}
pk_dt={}
other_dt={}
if isinstance(pk,unicode):
pk_orgi[pk]=""
else:
for key in pk:
pk_orgi[key]=""
for key in row:
if pk_orgi.has_key(key):
pk_dt[key]=row[key]
else:
other_dt[key]=row[key]
return (other_dt,pk_dt)
def generate_table_name(schema,table,keep_data=False,add_schema_name=True):
"""
拼接表名
:param schema:
:param table:
:param keep_data:是否产生保留数据的表名
:return:
"""
content=""
if keep_data:
content=Constant.KEEP_DATA_TABLE_TEMPLATE.format(table)
else:
if add_schema_name:
content=u'`'+ schema+ '`'+ ".`" + table + "`"
else:
content=u'`'+ table + "`"
return content
def generate_array_column_value_pair(row):
"""
产生 “列=列值” 的数组
:param row: 格式为row["列名"]=列值
:return:“列=列值” 的数组
"""
logger.debug(u"dump row={0}".format(row))
content=[]
for key in row:
logger.debug(u"dump row ele: {0}={1}".format(key,row[key]))
ele="`"+key+"`"+"="+to_string(row[key],"'")
content.append(ele)
return content
def to_string(val,prefix=""):
"""
把任何类型转换成string
:param val:
:param prefix:对于字符串,加prefix前缀
:return:
"""
#修复为null的bug
if val is None:
return "null"
if isinstance(val,unicode):
ret= u"%s%s%s" % (prefix,escape_unicode(val),prefix)
#ret= u"'%s'" % escape_unicode(val)
elif isinstance(val,str):
ret= u"%s%s%s" % (prefix,escape_string(val),prefix)
elif isinstance(val,datetime) or isinstance(val,date) or isinstance(val,timedelta):
ret=u"'"+str(val)+u"'"
else:
ret=str(val)
return ret
def escape_string(value):
"""escape_string escapes *value* but not surround it with quotes.
"""
value = value.replace('\\', '\\\\')
value = value.replace('\0', '\\0')
value = value.replace('\n', '\\n')
value = value.replace('\r', '\\r')
value = value.replace('\032', '\\Z')
value = value.replace("'", "\\'")
value = value.replace('"', '\\"')
return value
_escape_table = [unichr(x) for x in range(128)]
_escape_table[0] = u'\\0'
_escape_table[ord('\\')] = u'\\\\'
_escape_table[ord('\n')] = u'\\n'
_escape_table[ord('\r')] = u'\\r'
_escape_table[ord('\032')] = u'\\Z'
_escape_table[ord('"')] = u'\\"'
_escape_table[ord("'")] = u"\\'"
#_escape_table[ord("'")] = u"\'"
def escape_unicode(value, mapping=None):
"""escapes *value* without adding quote.
Value should be unicode
"""
return value.translate(_escape_table)
def generate_two_array_column_and_value(row):
"""
产生 “列 的和“列值”两个数组
:param row: 格式为row["列名"]=列值
:return:
"""
logger.debug(u"dump row={0}".format(row))
columns=[]
values=[]
for key in row:
logger.debug(u"dump row ele: {0}={1},type={2}".format(key,row[key],type(row[key])))
columns.append("`"+key+"`")
values.append(to_string(row[key],"'"))
return (columns,values)
def joint_keep_data_sql(op,op_timestamp,table,row,keep_current=False):
"""
:param op: 操作类型,insert,update,delete
:param op_timestamp:
:param table
:param row: 数据
:param keep_current: 是否去当前值
:return: insert的语句
"""
op_datetime=datetime.fromtimestamp(op_timestamp)
content_dict={}
content_dict[u"op"]=op
content_dict[u"op_datetime"]=op_datetime
if (op=="update"):
generate_keep_data_dict(content_dict,"bfr",row["before_values"])
generate_keep_data_dict(content_dict,"aft",row["after_values"])
elif (op=="insert"):
generate_keep_data_dict(content_dict,"aft",row["values"])
else:
generate_keep_data_dict(content_dict,"bfr",row["values"])
"""
if (op=="update"):
new_row=row["before_values"]
for key in new_row:
content_dict[u"bfr_"+key]=new_row[key]
new_row=row["after_values"]
for key in new_row:
content_dict["aft"+key]=new_row[key]
elif (op=="insert"):
new_row=row["values"]
for key in new_row:
content_dict["aft"+key]=new_row[key]
else:
new_row=row["values"]
for key in new_row:
content_dict["bfr"+key]=new_row[key]
"""
table_name=generate_table_name("",table,True)
(columns,values)=generate_two_array_column_and_value(content_dict)
statment_column=",".join(columns)
statment_value=",".join(values)
sql=u"insert into "+table_name+"("+statment_column+ ") values("+statment_value+")"
logger.debug(sql)
return sql
def generate_keep_data_dict(target,prefix,row):
for key in row:
target[u""+prefix+"_"+key]=row[key]
return target
def join_create_table(schema,table,colums):
"""
产生建表语句
:param schema:
:param table:
:param colums:列定义的数组
:return:
"""
#TODO timestamp的时区有问题吗?应该不会,python会自动转换为当前的时区
columns_def=[u"op varchar(64)",u"op_datetime datetime"]
table_name=generate_table_name("",table,True)
for row in colums:
check_mysql_type(row["COLUMN_TYPE"])
columns_def_ele=u"bfr_"+row["COLUMN_NAME"] + " "+row["COLUMN_TYPE"]
columns_def.append(columns_def_ele)
for row in colums:
columns_def_ele=u"aft_"+row["COLUMN_NAME"] + " "+row["COLUMN_TYPE"]
columns_def.append(columns_def_ele)
columns_def_str=",".join(columns_def)
sql=u"CREATE TABLE "+table_name+" ("+columns_def_str+")"+" ENGINE=InnoDB DEFAULT CHARSET=utf8mb4"
return sql
def check_mysql_type(mysql_type):
"""
检查mysql的类型,因为每种类型都需要详细的测试,防止产生的sql有问题
:param mysql_type:
:return:
"""
#mysql_type=""
ele=mysql_type.split(" ")[0].split("(")[0].lower()
if not Constant.ALLOW_TYPE.has_key(ele):
raise ValueError(u"find unsupport mysql type to flash,type={0}".format(mysql_type))