sql_util api documentation

sql_util.ci_string(s)[source]

This function creates a case-insensitive parser for a string

Parameters:

s – the string to make a case-insensitive parser for.

Returns:

case-insensitive parser for string s

Return type:

parsy.Parser

sql_util.optional_space_around(p)[source]

This function extends an existing parser with optional whitespace around it. Whitespace is stripped from the parser’s result.

Parameters:

p – the parser to extend

Returns:

a new parser with optional whitespace around it

Return type:

parsy.Parser

sql_util.parentheses_around(p)[source]

This function extends an existing parser with parentheses around it. The captured parentheses and adjacent whitespace are discarded.

Parameters:

p – the parser to extend

Returns:

a new parser with parenthesis around the old parser

Return type:

parsy.Parser

class sql_util.sql_token[source]

Bases: object

The class tokenises the sql statements captured by mysql_engine. Several regular expressions analyse and build the elements of the token. The DDL support is purposely limited to the following.

DROP PRIMARY KEY CREATE (UNIQUE) INDEX/KEY CREATE TABLE ALTER TABLE

The method post_process_key_definition ignores any foreign key definition from the sql statement as we don’t enforce any foreign key on the PostgreSQL replication.

alter_rename_table_statement = <parsy.Parser object>
alter_table_add = <parsy.Parser object>
alter_table_add_index = <parsy.Parser object>
alter_table_add_multiple = <parsy.Parser object>
alter_table_change = <parsy.Parser object>
alter_table_drop = <parsy.Parser object>
alter_table_drop_index = <parsy.Parser object>
alter_table_ignored = <parsy.Parser object>
alter_table_modify = <parsy.Parser object>
alter_table_rename_index = <parsy.Parser object>
alter_table_statement = <parsy.Parser object>
any_key_definition = <parsy.Parser object>
column_definition = <parsy.Parser object>
column_definition_in_alter_table = <parsy.Parser object>
create_index_statement = <parsy.Parser object>
create_table_statement = <parsy.Parser object>
drop_index_statement = <parsy.Parser object>
drop_primary_key_statement = <parsy.Parser object>
drop_table_statement = <parsy.Parser object>
fkey_definition = <parsy.Parser object>
group_expr = <parsy.Parser object>
idx_definition = <parsy.Parser object>
inline_expr = <parsy.Parser object>
key_part = <parsy.Parser object>
key_part_group = <parsy.Parser object>
other_key_definition = <parsy.Parser object>
parse_sql(sql_string)[source]

The method removes comments from the sql string and parses it. The statements are split in a list using the statement separator ;

For each statement, the sql parser tries to parse it with a set of specific statement parsers. If any supported statement is found, it is parsed and returned as a non-empty dict or a list of dicts by the parser.

Look at self.sql_parser for the different statements that are supported by the parser.

Different match are performed on the statement. RENAME TABLE CREATE TABLE DROP TABLE ALTER TABLE ALTER INDEX DROP PRIMARY KEY TRUNCATE TABLE

Each successful parse builds a dictionary with at least two keys “name” and “command”.

Each statement parse comes with specific addictional keys.

When the token dictionary is complete is added to the class list tokenised

Parameters:

sql_string – The sql string with the sql statements.

pk_definition = <parsy.Parser object>
quote_cols(cols)[source]

The method adds the “ quotes to the column names. The string is converted to a list using the split method with the comma separator. The columns are then stripped and quoted with the “”. Finally the list elements are rejoined in a string which is returned. The method is used in build_key_dic to sanitise the column names.

Parameters:

cols – The columns string

Returns:

The columns quoted between “.

Return type:

text

rename_table_item = <parsy.Parser object>
rename_table_statement = <parsy.Parser object>
reset_lists()[source]

The method resets the lists to empty lists after a successful tokenisation.

simple_expr = <parsy.Parser object>
truncate_table_statement = <parsy.Parser object>
uk_definition = <parsy.Parser object>