MCPcopy Index your code
hub / github.com/danfengcao/binlog2sql / generate_sql_pattern

Function generate_sql_pattern

binlog2sql/binlog2sql_util.py:190–241  ·  view source on GitHub ↗
(binlog_event, row=None, flashback=False, no_pk=False)

Source from the content-addressed store, hash-verified

188
189
190def generate_sql_pattern(binlog_event, row=None, flashback=False, no_pk=False):
191 template = ''
192 values = []
193 if flashback is True:
194 if isinstance(binlog_event, WriteRowsEvent):
195 template = 'DELETE FROM `{0}`.`{1}` WHERE {2} LIMIT 1;'.format(
196 binlog_event.schema, binlog_event.table,
197 ' AND '.join(map(compare_items, row['values'].items()))
198 )
199 values = map(fix_object, row['values'].values())
200 elif isinstance(binlog_event, DeleteRowsEvent):
201 template = 'INSERT INTO `{0}`.`{1}`({2}) VALUES ({3});'.format(
202 binlog_event.schema, binlog_event.table,
203 ', '.join(map(lambda key: '`%s`' % key, row['values'].keys())),
204 ', '.join(['%s'] * len(row['values']))
205 )
206 values = map(fix_object, row['values'].values())
207 elif isinstance(binlog_event, UpdateRowsEvent):
208 template = 'UPDATE `{0}`.`{1}` SET {2} WHERE {3} LIMIT 1;'.format(
209 binlog_event.schema, binlog_event.table,
210 ', '.join(['`%s`=%%s' % x for x in row['before_values'].keys()]),
211 ' AND '.join(map(compare_items, row['after_values'].items())))
212 values = map(fix_object, list(row['before_values'].values())+list(row['after_values'].values()))
213 else:
214 if isinstance(binlog_event, WriteRowsEvent):
215 if no_pk:
216 # print binlog_event.__dict__
217 # tableInfo = (binlog_event.table_map)[binlog_event.table_id]
218 # if tableInfo.primary_key:
219 # row['values'].pop(tableInfo.primary_key)
220 if binlog_event.primary_key:
221 row['values'].pop(binlog_event.primary_key)
222
223 template = 'INSERT INTO `{0}`.`{1}`({2}) VALUES ({3});'.format(
224 binlog_event.schema, binlog_event.table,
225 ', '.join(map(lambda key: '`%s`' % key, row['values'].keys())),
226 ', '.join(['%s'] * len(row['values']))
227 )
228 values = map(fix_object, row['values'].values())
229 elif isinstance(binlog_event, DeleteRowsEvent):
230 template = 'DELETE FROM `{0}`.`{1}` WHERE {2} LIMIT 1;'.format(
231 binlog_event.schema, binlog_event.table, ' AND '.join(map(compare_items, row['values'].items())))
232 values = map(fix_object, row['values'].values())
233 elif isinstance(binlog_event, UpdateRowsEvent):
234 template = 'UPDATE `{0}`.`{1}` SET {2} WHERE {3} LIMIT 1;'.format(
235 binlog_event.schema, binlog_event.table,
236 ', '.join(['`%s`=%%s' % k for k in row['after_values'].keys()]),
237 ' AND '.join(map(compare_items, row['before_values'].items()))
238 )
239 values = map(fix_object, list(row['after_values'].values())+list(row['before_values'].values()))
240
241 return {'template': template, 'values': list(values)}
242
243
244def reversed_lines(fin):

Callers 2

Calls

no outgoing calls

Tested by 1