Statements Structure¶
mysqlparse.parse(file_or_string)¶
Takes a file like object or string and returns
pyparsing.ParseResults representing the SQL file structure.
Assuming sql file is parsed like this:
>>> with open('001_migrate.sql') as sql_file:
>>> sql = mysqlparse.parse(sql_file)
The following properties are accessible
sql.statements[] : listall individual sql statements, separated by;are accessible through this list..statement_type : strone of:ALTERCREATE
.create_type : str, currently onlyTABLE..temporary : boolean,TrueifCREATE TEMPORARY TABLE..overwrite : boolean,FalsewhenIF NOT EXISTSis present..database_name : str,Noneor database. name if the table identifier was with a dot (e.g.db_name.tbl_name)..table_name : str, table name ofALTER TABLEstatement..ignore : boolean,Trueif it isALTER IGNORE TABLEstatement (support for it is removed as of MySQL 5.7.4)..table_options[]: listlist withkeyandvaluepairs for table options:.keykey.valuevalue
.alter_specification[] : listlist of individual column alterations..alter_action : strone of:ADD COLUMNADD INDEXMODIFY COLUMNCHANGE COLUMNDROP COLUMNDROP PRIMARY KEYDROP INDEXDROP KEYDROP FOREIGN KEY.
.column_name : str.new_column_name : strname of the new column name inMODIFY COLUMNstatements..null : boolean|str-Trueif the column is null,False- if not null andimplicitif unspecified..default : str- default value of the column..auto_increment : boolean-Trueif the column is auto increment..index_type : str-unique_keyif column is unique key,.primary_keyif column is primary key,BTREEif it is btree,HASHif it is has..key_block_size : strkey block size of index..parser_name : strname of the parser..comment : str- comment string..column_position : strone of:FIRST, another column name or (default)LAST..data_type : strone of:BITTINYINTSMALLINTMEDIUMINTINTINTEGERBIGINTREALDOUBLEFLOATDECIMALNUMERICDATETIMETIMESTAMPDATETIMEYEARCHARVARCHARBINARYVARBINARYTINYBLOBBLOBMEDIUMBLOBLONGBLOBTINYTEXTTEXTMEDIUMTEXTLONGTEXTENUMSET
.length : str- column length (as inINT(length))..decimals : str- number of decimal places of a decimal type..unsigned : boolean-Trueif column is ofUNSIGNEDtype..zerofill : boolean-Trueif column is ofZEROFILLtype..binary : boolean-Trueif column is ofBINARYtype..character_set : str- character set of the column..collate : str- column collation name.