├── demo.sql ├── gspsf32.dll ├── gspsf64.dll ├── screenshots └── ppv3 │ ├── select │ ├── sql_commainsidelist.png │ ├── align_join_condition.png │ ├── not_align_join_condition.png │ └── sql_not_commainsidelist.png │ └── createtable │ ├── options_default.png │ ├── sql_default_options.png │ ├── sql_fields_align_from_create.png │ └── sql_fields_align_from_left_parenthesis.png ├── README.md ├── formatsql.py └── format_options_default.json /demo.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE dbo.mytable ( low int, high int, myavg AS (low + high)/2 ) ; -------------------------------------------------------------------------------- /gspsf32.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sqlparser/sql-pretty-printer/HEAD/gspsf32.dll -------------------------------------------------------------------------------- /gspsf64.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sqlparser/sql-pretty-printer/HEAD/gspsf64.dll -------------------------------------------------------------------------------- /screenshots/ppv3/select/sql_commainsidelist.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sqlparser/sql-pretty-printer/HEAD/screenshots/ppv3/select/sql_commainsidelist.png -------------------------------------------------------------------------------- /screenshots/ppv3/createtable/options_default.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sqlparser/sql-pretty-printer/HEAD/screenshots/ppv3/createtable/options_default.png -------------------------------------------------------------------------------- /screenshots/ppv3/select/align_join_condition.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sqlparser/sql-pretty-printer/HEAD/screenshots/ppv3/select/align_join_condition.png -------------------------------------------------------------------------------- /screenshots/ppv3/createtable/sql_default_options.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sqlparser/sql-pretty-printer/HEAD/screenshots/ppv3/createtable/sql_default_options.png -------------------------------------------------------------------------------- /screenshots/ppv3/select/not_align_join_condition.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sqlparser/sql-pretty-printer/HEAD/screenshots/ppv3/select/not_align_join_condition.png -------------------------------------------------------------------------------- /screenshots/ppv3/select/sql_not_commainsidelist.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sqlparser/sql-pretty-printer/HEAD/screenshots/ppv3/select/sql_not_commainsidelist.png -------------------------------------------------------------------------------- /screenshots/ppv3/createtable/sql_fields_align_from_create.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sqlparser/sql-pretty-printer/HEAD/screenshots/ppv3/createtable/sql_fields_align_from_create.png -------------------------------------------------------------------------------- /screenshots/ppv3/createtable/sql_fields_align_from_left_parenthesis.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sqlparser/sql-pretty-printer/HEAD/screenshots/ppv3/createtable/sql_fields_align_from_left_parenthesis.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | sql-pretty-printer 2 | ================== 3 | 4 | SQL beautifier for databases include but not limited to Oracle, SQL Server, DB2, Sybase, MySQL, PostgreSQL, Teradata. 5 | 6 | This SQL formtter is provided as a Service(SQL FaaS). You can 7 | 8 | 1. Integrate this service into your application and web site by using sql formatter API. 9 | 2. SQL formatter widget can be easily embedded to your site 10 | 11 | ### Runt the python demo 12 | ``` 13 | python formatsql.py demo.sql format_options_default.json 14 | ``` 15 | 16 | **Please note that since gspsf.dll is win32 DLL, so please use python 32 bit version to run the python demo.** 17 | 18 | 19 | * [SQL Formatter Options](https://github.com/sqlparser/sql-pretty-printer/wiki/SQL-Coding-Standard-and-Guideline) 20 | * [SQL FaaS Home](./SQL-FaaS) 21 | * [SQL FaaS API manual](./SQL-FaaS-API-manual) 22 | * [SQL Formatter Options NOT USED](https://github.com/sqlparser/sql-pretty-printer/wiki/PP-V3-format-options) 23 | -------------------------------------------------------------------------------- /formatsql.py: -------------------------------------------------------------------------------- 1 | import ctypes 2 | import json 3 | import sys 4 | 5 | def read_file(file_path): 6 | try: 7 | with open(file_path, 'r') as file: 8 | content = file.read() 9 | return content 10 | except FileNotFoundError: 11 | return "File not found." 12 | except IOError: 13 | return "Error reading the file." 14 | 15 | # Check if two filenames are provided as command-line arguments 16 | if len(sys.argv) != 3: 17 | print("Please provide SQL file and format option file as command-line arguments.") 18 | sys.exit(1) 19 | 20 | filename_input_sqlfile = sys.argv[1] 21 | filename_input_format_option_file = sys.argv[2] 22 | 23 | # Change the path of input sql file, "c:\\prg\\gsp_vcl\\sampleFiles\\demo.sql" 24 | inputSql = read_file(filename_input_sqlfile).encode() 25 | 26 | # Change the path of format option file, "c:\\prg\\gsp_vcl\\sampleFiles\\format_options.json" 27 | inputOption = read_file(filename_input_format_option_file).encode() 28 | 29 | # Load the DLL 30 | dll = ctypes.WinDLL('.\gspsf32.dll') 31 | 32 | # Define the function prototype 33 | formatsql = dll.formatsql 34 | formatsql.argtypes = [ 35 | ctypes.c_char_p, # inputSql 36 | ctypes.c_uint, # inputSqlLength 37 | ctypes.c_char_p, # inputOption 38 | ctypes.c_uint, # inputOptionLength 39 | ctypes.c_char_p, # outputSql 40 | ctypes.c_uint, # outputSqlLength 41 | ctypes.c_char_p, # outMessage 42 | ctypes.c_uint # outMessageLength 43 | ] 44 | formatsql.restype = ctypes.c_int 45 | 46 | # Call the formatsql function 47 | 48 | PRE_ALLOCATE_OUTPUT_SQL_LEN = 1024 * 8 49 | PRE_ALLOCATE_OUTPUT_MSG_LEN = 1024 * 8 50 | 51 | outputSql = ctypes.create_string_buffer(PRE_ALLOCATE_OUTPUT_SQL_LEN) 52 | outMessage = ctypes.create_string_buffer(PRE_ALLOCATE_OUTPUT_MSG_LEN) 53 | 54 | result = formatsql( 55 | inputSql, 56 | len(inputSql), 57 | inputOption, 58 | len(inputOption), 59 | outputSql, 60 | PRE_ALLOCATE_OUTPUT_SQL_LEN, 61 | outMessage, 62 | PRE_ALLOCATE_OUTPUT_MSG_LEN 63 | ) 64 | 65 | if result > PRE_ALLOCATE_OUTPUT_SQL_LEN: 66 | PRE_ALLOCATE_OUTPUT_SQL_LEN = result 67 | outputSql = ctypes.create_string_buffer(PRE_ALLOCATE_OUTPUT_SQL_LEN) 68 | result = formatsql( 69 | inputSql, 70 | len(inputSql), 71 | inputOption, 72 | len(inputOption), 73 | outputSql, 74 | PRE_ALLOCATE_OUTPUT_SQL_LEN, 75 | outMessage, 76 | PRE_ALLOCATE_OUTPUT_MSG_LEN 77 | ) 78 | 79 | # print(outMessage.value.decode('utf-8')) 80 | outMessageJson = json.loads(outMessage.value.decode('utf-8')) 81 | retval_value = outMessageJson["retval"] 82 | retval_msg = outMessageJson["retMessage"] 83 | 84 | # Check the result 85 | if retval_value == 0: 86 | print("Success!") 87 | else: 88 | print("Error:", retval_value) 89 | print("Error message:", retval_msg) 90 | 91 | print(outputSql.value.decode('utf-8')) 92 | 93 | -------------------------------------------------------------------------------- /format_options_default.json: -------------------------------------------------------------------------------- 1 | { 2 | "dbVendor":"dbvmssql", 3 | "output":{ 4 | "output_type":"ofsql", 5 | "varname":"varname1" 6 | }, 7 | "preprocessor":{ 8 | "remove_linebreak_before_parse":false, 9 | "trim_quoted_char_of_eachline":false, 10 | "quoted_char_of_eachline":"\"" 11 | }, 12 | "formatOptions":{ 13 | 14 | "alignment":{ 15 | "select_keywords_alignoption":"aloleft", 16 | "true_left":false 17 | }, 18 | "blankline":{ 19 | "emptylines":"remove", 20 | "insert_blankline_inbatchsqls":false, 21 | }, 22 | "capitalization":{ 23 | "case_keywords":"couppercase", 24 | "case_datatype":"conochange", 25 | "case_funcname":"coinitcap", 26 | "case_builtinfunc":"coinitcap", 27 | "case_identifier":"coinitcapeachword", 28 | "case_quoted_identifier":"conochange", 29 | "case_table_name":"conochange", 30 | "case_column_name":"conochange", 31 | "case_alias_name":"conochange", 32 | "case_column_alias":"conochange", 33 | "case_prefix_identitfer":"conochange", 34 | "case_variable_name":"conochange", 35 | "identifier_prefix_list":"" 36 | }, 37 | "case_expression":{ 38 | "case_when_then_in_sameline":true, 39 | "indent_casefromswitch":2, 40 | "indent_case_then":0, 41 | "case_when_in_sameline_as_case":false, 42 | "case_then_expr_in_newline":false, 43 | "case_else_expr_in_newline":false, 44 | }, 45 | "comment":{ 46 | "remove_comment":false 47 | }, 48 | "create_table":{ 49 | "bestyle_createtable_leftbeonnewline":true, 50 | "bestyle_createtable_leftbeindentsize":2, 51 | "bestyle_createtable_rightbeonnewline":true, 52 | "bestyle_createtable_rightbeindentsize":2, 53 | "createtable_listiteminnewline":true, 54 | "createtable_fields_alignwithcreatekeyword":false, 55 | "createtable_fieldlist_align_option":"aloleft", 56 | "createtable_fieldlist_style":"asstacked", 57 | "createtable_fieldlist_comma_option":"lfaftercomma" 58 | }, 59 | "default_option":{ 60 | "align":"asstacked", 61 | "comma":"lfaftercomma", 62 | }, 63 | "execute":{ 64 | "exec_first_paramater_in_newline":true, 65 | "exec_parameters_align_value":false, 66 | "exec_parameters_style":"asstacked", 67 | "exec_parameters_comma_option":"lfaftercomma" 68 | }, 69 | "expression":{ 70 | "expr_remove_redundant_brackets":false, 71 | "expr_parenthesis_innewline":false, 72 | "expr_concat_string_max_length":40 73 | }, 74 | "indentation":{ 75 | "indent_len":2, 76 | "use_tab":false, 77 | "tab_size":2, 78 | "input_tab_size":2, 79 | "bestyle_function_bodyindent":2, 80 | "bestyle_block_leftbeonnewline":true, 81 | "bestyle_block_leftbeindentsize":2, 82 | "bestyle_block_rightbeindentsize ":2, 83 | "bestyle_blockindentsize":2, 84 | "bestyle_ifelsesinglestmtindentsize":2 85 | }, 86 | "insert":{ 87 | "insert_parenthesis_in_separate_line":false, 88 | "insert_columnlist_style":"asstacked", 89 | "insert_valuelist_style":"asstacked", 90 | "insert_indent_select":false, 91 | "insert_columns_per_line":0 92 | }, 93 | "line_number":{ 94 | "linenumber_enabled":false, 95 | "linenumber_0_based":false, 96 | "linenumber_leftmargin":0, 97 | "linenumber_rightmargin":2 98 | }, 99 | "line_width":{ 100 | "line_width":999, 101 | "compact_mode":"cpmnone" 102 | }, 103 | "parameter":{ 104 | "bestyle_function_leftbeonnewline":false, 105 | "bestyle_function_leftbeindentsize":0, 106 | "bestyle_function_rightbeonnewline":false, 107 | "bestyle_function_rightbeindentsize":0, 108 | "parameters_style":"asstacked", 109 | "parameters_comma":"lfaftercomma", 110 | "bestyle_function_firstparaminnewline":false, 111 | "parameters_align_datatype":true, 112 | "functioncall_parameters_style":"aswrapped", 113 | "functioncall_parameters_comma":"lfaftercomma" 114 | }, 115 | "refactor":{ 116 | "add_missing_semicolon":false 117 | }, 118 | "select":{ 119 | "select_columnlist_style":"asstacked", 120 | "select_columnlist_comma":"lfaftercomma", 121 | "align_alias_in_select_list":true, 122 | "treat_distinct_as_virtual_column":false, 123 | "select_fromclause_style":"asstacked", 124 | "select_fromclause_comma":"lfaftercomma", 125 | "select_item_in_newline":false, 126 | "into_clause_in_newline":false, 127 | "from_clause_in_newline":false, 128 | "align_alias_in_from_clause":false, 129 | "align_and_or_with_on_in_join_clause":false, 130 | "where_clause_in_newline":false, 131 | "and_or_under_where":false, 132 | "where_clause_align_expr":false, 133 | "group_by_clause_in_newline":false, 134 | "having_clause_in_new_line":false, 135 | "order_by_clause_in_newline":false, 136 | "comma_inside_list":true, 137 | "keep_comment_after_column":false, 138 | "select_from_clause_join_condition_in_newline":true, 139 | "align_join_condition":false, 140 | "select_from_clause_join_on_in_newline":false, 141 | "align_join_with_from_keyword":false, 142 | "select_groupby_style":"asstacked", 143 | "select_orderby_style":"asstacked", 144 | "linefeeds_and_or_option":"lfbeforeandor" 145 | }, 146 | "subquery":{ 147 | "subquery_newline_after_in":false, 148 | "subquery_newline_after_exists":false, 149 | "subquery_newline_after_comparisonoperator":false, 150 | "subquery_newline_before_comparisonoperator":false, 151 | }, 152 | "update":{ 153 | "update_columnlist_style":"asstacked", 154 | "update_set_clause_innewline":false, 155 | "update_from_clause_innewline":false 156 | }, 157 | "whitespace":{ 158 | "wspadding_operator_arithmetic":true, 159 | "wspadding_parentheses_in_function":false, 160 | "wspadding_parentheses_in_expression":true, 161 | "wspadding_parentheses_of_subquery":false, 162 | "wspadding_parentheses_in_functioncall":false, 163 | "wspadding_parentheses_of_typename":false 164 | } 165 | } 166 | } --------------------------------------------------------------------------------