### Install sqlparse Source: https://github.com/andialbrecht/sqlparse/blob/master/docs/source/index.md Install the sqlparse library using pip. This command is used for initial setup. ```sh $ pip install sqlparse ``` -------------------------------- ### Install sqlparse using pip Source: https://github.com/andialbrecht/sqlparse/blob/master/docs/source/intro.md Install the sqlparse library system-wide using pip. This is the recommended method for most users. ```bash pip install sqlparse ``` -------------------------------- ### Install Pre-commit Hooks Source: https://github.com/andialbrecht/sqlparse/blob/master/docs/source/ui.md Install the configured pre-commit hooks into your local repository. This command should be run after setting up the .pre-commit-config.yaml file. ```bash pre-commit install ``` -------------------------------- ### Install Pre-commit Hook Source: https://github.com/andialbrecht/sqlparse/blob/master/docs/source/index.md Command to install the pre-commit hook after configuring it in the .pre-commit-config.yaml file. This enables automatic formatting on commit. ```sh $ pre-commit install ``` -------------------------------- ### Configure Lexer with Custom Regex and Keywords Source: https://github.com/andialbrecht/sqlparse/blob/master/docs/source/extending.md This example demonstrates how to add custom SQL syntax like 'ZORDER BY' and keywords like 'BAR' to the sqlparse lexer. It involves clearing existing configurations, setting new regular expressions, and adding keyword dictionaries. ```python import re import sqlparse from sqlparse import keywords from sqlparse.lexer import Lexer # get the lexer singleton object to configure it lex = Lexer.get_default_instance() # Clear the default configurations. # After this call, reg-exps and keyword dictionaries need to be loaded # to make the lexer functional again. lex.clear() my_regex = (r"ZORDER\s+BY\b", sqlparse.tokens.Keyword) # slice the default SQL_REGEX to inject the custom object lex.set_SQL_REGEX( keywords.SQL_REGEX[:38] + [my_regex] + keywords.SQL_REGEX[38:] ) # add the default keyword dictionaries lex.add_keywords(keywords.KEYWORDS_COMMON) lex.add_keywords(keywords.KEYWORDS_ORACLE) lex.add_keywords(keywords.KEYWORDS_PLPGSQL) lex.add_keywords(keywords.KEYWORDS_HQL) lex.add_keywords(keywords.KEYWORDS_MSACCESS) lex.add_keywords(keywords.KEYWORDS) # add a custom keyword dictionary lex.add_keywords({'BAR': sqlparse.tokens.Keyword}) # no configuration is passed here. The lexer is used as a singleton. sqlparse.parse("select * from foo zorder by bar;") ``` -------------------------------- ### Configure sqlparse Pre-commit Hook Source: https://github.com/andialbrecht/sqlparse/blob/master/docs/source/index.md Example configuration for using sqlparse as a pre-commit hook to automatically format SQL files. This YAML defines the repository, revision, and hook details. ```yaml repos: - repo: https://github.com/andialbrecht/sqlparse rev: 0.5.4 # Use the latest version hooks: - id: sqlformat # Optional: Add more formatting options # IMPORTANT: --in-place is required, already included by default args: [--in-place, --reindent, --keywords, upper] ``` -------------------------------- ### Format SQL statements Source: https://github.com/andialbrecht/sqlparse/blob/master/docs/source/intro.md Beautify SQL statements using the `sqlparse.format()` function. This example demonstrates uppercasing keywords and re-indenting for improved readability. Refer to the API documentation for all supported formatting options. ```python sql = 'select * from foo where id in (select id from bar);' print(sqlparse.format(sql, reindent=True, keyword_case='upper')) ``` -------------------------------- ### Build Distribution Packages Source: https://github.com/andialbrecht/sqlparse/blob/master/AGENTS.md Create distribution packages (sdist and wheel) for the project using the build module. ```bash python -m build ``` -------------------------------- ### Split and Format SQL Statements Source: https://github.com/andialbrecht/sqlparse/blob/master/docs/source/index.md Demonstrates how to split a string containing multiple SQL statements and format the first statement using sqlparse. Requires importing the library. ```python >>> import sqlparse >>> # Split a string containing two SQL statements: >>> raw = 'select * from foo; select * from bar;' >>> statements = sqlparse.split(raw) >>> statements ['select * from foo;', 'select * from bar;'] >>> # Format the first statement and print it out: >>> first = statements[0] >>> print(sqlparse.format(first, reindent=True, keyword_case='upper')) SELECT * FROM foo; ``` -------------------------------- ### Run All Tests Source: https://github.com/andialbrecht/sqlparse/blob/master/AGENTS.md Execute all tests across different Python versions using the make command. ```bash make test ``` -------------------------------- ### Run sqlparse test suite with tox Source: https://github.com/andialbrecht/sqlparse/blob/master/docs/source/intro.md Execute the test suite for sqlparse using tox. This ensures that tests are run for different Python versions and environments before submitting a pull request. ```bash tox ``` -------------------------------- ### Generate Coverage Report Source: https://github.com/andialbrecht/sqlparse/blob/master/AGENTS.md Run tests with coverage enabled and display the report. Use 'make coverage-xml' for XML output. ```bash make coverage ``` -------------------------------- ### Configure sqlparse Pre-commit Hook Source: https://github.com/andialbrecht/sqlparse/blob/master/docs/source/ui.md Add this configuration to your .pre-commit-config.yaml to enable automatic SQL formatting before commits. Replace '0.5.4' with your desired version. ```yaml repos: - repo: https://github.com/andialbrecht/sqlparse rev: 0.5.4 # Replace with the version you want to use hooks: - id: sqlformat ``` -------------------------------- ### Manually Run sqlformat Pre-commit Hook Source: https://github.com/andialbrecht/sqlparse/blob/master/docs/source/ui.md Manually trigger the sqlformat hook on all files in your repository. This is useful for formatting existing code or after changing the hook configuration. ```bash pre-commit run sqlformat --all-files ``` -------------------------------- ### Run Single Test File Source: https://github.com/andialbrecht/sqlparse/blob/master/AGENTS.md Execute tests within a specific file (e.g., test_format.py) for a given Python version. ```bash uv run --group dev --python 3.11 pytest tests/test_format.py ``` -------------------------------- ### Token Traversal: Navigation Source: https://github.com/andialbrecht/sqlparse/blob/master/AGENTS.md Navigate token lists using helper methods like token_first(), token_next(), and token_prev(). ```python token_first() token_next() token_prev() ``` -------------------------------- ### Clone sqlparse repository Source: https://github.com/andialbrecht/sqlparse/blob/master/docs/source/intro.md Clone the latest sources of the sqlparse module from its Git repository. ```bash git clone git://github.com/andialbrecht/sqlparse.git ``` -------------------------------- ### sqlparse.format() Options Source: https://github.com/andialbrecht/sqlparse/blob/master/docs/source/api.md The `format()` function in sqlparse accepts various keyword arguments to control the formatting of SQL statements. These options allow customization of keyword casing, identifier casing, comment stripping, string truncation, indentation, operator spacing, and output format. ```APIDOC ## Formatting Options for sqlparse.format() ### Description Customizes the output of the `format()` function. ### Parameters #### Keyword Arguments - **keyword_case** (string) - Optional - Changes how keywords are formatted. Allowed values are "upper", "lower", and "capitalize". - **identifier_case** (string) - Optional - Changes how identifiers are formatted. Allowed values are "upper", "lower", and "capitalize". - **strip_comments** (boolean) - Optional - If `True`, comments are removed from the statements. - **truncate_strings** (integer) - Optional - If a positive integer, string literals longer than the given value will be truncated. - **truncate_char** (string) - Optional - Default: "[...]". If long string literals are truncated, this value will be appended to the truncated string. - **reindent** (boolean) - Optional - If `True`, the indentations of the statements are changed. - **reindent_aligned** (boolean) - Optional - If `True`, the indentations of the statements are changed, and statements are aligned by keywords. - **use_space_around_operators** (boolean) - Optional - If `True`, spaces are used around all operators. - **indent_tabs** (boolean) - Optional - If `True`, tabs instead of spaces are used for indentation. - **indent_width** (integer) - Optional - Default: 2. The width of the indentation. - **wrap_after** (integer) - Optional - The column limit (in characters) for wrapping comma-separated lists. If unspecified, it puts every item in the list on its own line. - **compact** (boolean) - Optional - If `True`, the formatter tries to produce more compact output. - **output_format** (string) - Optional - If given, the output is additionally formatted to be used as a variable in a programming language. Allowed values are "python" and "php". - **comma_first** (boolean) - Optional - If `True`, comma-first notation for column names is used. ``` -------------------------------- ### Customize sqlparse Pre-commit Hook Arguments Source: https://github.com/andialbrecht/sqlparse/blob/master/docs/source/ui.md Override the default arguments for the sqlformat hook to customize formatting. Ensure '--in-place' is the first argument to modify files directly. ```yaml repos: - repo: https://github.com/andialbrecht/sqlparse rev: 0.5.4 hooks: - id: sqlformat args: [--in-place, --reindent, --keywords, upper, --identifiers, lower] ``` -------------------------------- ### Lint Code with Ruff Source: https://github.com/andialbrecht/sqlparse/blob/master/AGENTS.md Check code quality and style using Ruff. This command can also be run via 'make lint'. ```bash uv run --group dev ruff check sqlparse/ ``` -------------------------------- ### Add New Keywords to Lexer Source: https://github.com/andialbrecht/sqlparse/blob/master/AGENTS.md Extend the SQL parser with new keywords for different dialects using the Lexer.add_keywords() method. ```python Lexer.add_keywords(new_keywords) ``` -------------------------------- ### Parse a SQL Statement Source: https://github.com/andialbrecht/sqlparse/blob/master/docs/source/index.md Shows how to parse a single SQL statement and access its tokens using sqlparse. The result is a parsed statement object. ```python >>> # Parsing a SQL statement: >>> parsed = sqlparse.parse('select * from foo')[0] >>> parsed.tokens [, , >> ``` -------------------------------- ### Run Specific Test Source: https://github.com/andialbrecht/sqlparse/blob/master/AGENTS.md Execute a single named test function within a specific file and Python version. ```bash uv run --group dev --python 3.11 pytest tests/test_format.py::test_name ``` -------------------------------- ### Run Tests for Specific Python Version Source: https://github.com/andialbrecht/sqlparse/blob/master/AGENTS.md Execute tests using a specific Python version (e.g., 3.11) with uv and pytest. ```bash uv run --group dev --python 3.11 pytest tests/ ``` -------------------------------- ### Token Traversal: Find Next Token Source: https://github.com/andialbrecht/sqlparse/blob/master/AGENTS.md Find the next token based on instance type, match criteria, or token type using token_next_by(). ```python token_next_by(i=..., m=..., t=...) ``` -------------------------------- ### sqlparse Grouping Limits Source: https://github.com/andialbrecht/sqlparse/blob/master/docs/source/api.md sqlparse includes configurable limits to prevent excessive resource consumption when processing very large or deeply nested SQL structures, offering protection against potential denial of service (DoS) attacks. ```APIDOC ## Security and Performance Considerations: Grouping Limits ### Description Configurable limits to prevent excessive resource consumption and potential DoS attacks when parsing large or complex SQL statements. ### Parameters #### Constants in `sqlparse.engine.grouping` - **MAX_GROUPING_DEPTH** (integer or None) - Default: 100. Limits recursion depth during token grouping. Set to `None` to disable. - **MAX_GROUPING_TOKENS** (integer or None) - Default: 10,000. Limits the number of tokens processed in a single grouping operation. Set to `None` to disable. ### WARNING Increasing or disabling these limits may expose your application to DoS vulnerabilities when processing untrusted SQL input. Modify with extreme caution. ``` -------------------------------- ### Token Matching Source: https://github.com/andialbrecht/sqlparse/blob/master/AGENTS.md Check if a token matches a specific token type, values, or regex pattern using the match() method. ```python token.match(ttype, values, regex=False) ``` -------------------------------- ### Token Traversal: Flatten Source: https://github.com/andialbrecht/sqlparse/blob/master/AGENTS.md Recursively yield all leaf tokens from a token object. Useful for iterating through the smallest meaningful units of SQL. ```python token.flatten() ``` -------------------------------- ### Parse SQL statements into tokens Source: https://github.com/andialbrecht/sqlparse/blob/master/docs/source/intro.md Use the `sqlparse.parse()` function to parse a SQL string into a tuple of `Statement` objects. Each `Statement` object contains a `tokens` attribute which holds its sub-tokens. ```python import sqlparse sql = 'select * from "someschema"."mytable" where id = 1' parsed = sqlparse.parse(sql) parsed ``` -------------------------------- ### Split SQL statements Source: https://github.com/andialbrecht/sqlparse/blob/master/docs/source/intro.md Use the `sqlparse.split()` function to split a string containing multiple SQL statements into a list of individual statements. The function identifies statement endings by semicolons, correctly handling semicolons within constructs like BEGIN...END blocks. ```python import sqlparse sql = 'select * from foo; select * from bar;' sqlparse.split(sql) ``` -------------------------------- ### Modify sqlparse Grouping Limits Source: https://github.com/andialbrecht/sqlparse/blob/master/docs/source/api.md Adjust the maximum recursion depth and token count for SQL statement grouping. Use with caution as increasing or disabling these limits can expose your application to DoS vulnerabilities when processing untrusted SQL input. ```python import sqlparse.engine.grouping # Increase limits (use with caution) sqlparse.engine.grouping.MAX_GROUPING_DEPTH = 200 sqlparse.engine.grouping.MAX_GROUPING_TOKENS = 50000 # Disable limits completely (use with extreme caution) sqlparse.engine.grouping.MAX_GROUPING_DEPTH = None sqlparse.engine.grouping.MAX_GROUPING_TOKENS = None ``` -------------------------------- ### Access statement tokens Source: https://github.com/andialbrecht/sqlparse/blob/master/docs/source/intro.md Access the `tokens` attribute of a parsed `Statement` object to retrieve its sub-tokens. Each token object can be converted back to its string representation. ```python stmt = parsed[0] # grab the Statement object stmt.tokens ``` ```python str(stmt) # str(stmt) for Python 3 ``` ```python str(stmt.tokens[-1]) # or just the WHERE part ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.