addressable-sql-queries


Nameaddressable-sql-queries JSON
Version 0.0.1.dev1 PyPI version JSON
download
home_page
SummaryIndividualize the queries of a SQL script and make them easily addressable
upload_time2023-05-03 15:48:02
maintainer
docs_urlNone
authorJean Oustry
requires_python>=3.10
licenseMIT License Copyright (c) 2023 Jean Oustry Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords sql addressable queries addressable query parser preprocessor script chunk script manipulation
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # What is this library ?
This library allows to make a SQL script (or possibly any script) addressable.
Once preprocessed or parsed, you can get any chunk of it (which could be a single query for instance) with its line number, key, or index of the chunk.

# Example & How to use:
<details>
<summary>
With a target SQL script : (toggle)
</summary>

*```raw_script.sql```* :
```SQL
CREATE TEMPORARY VIEW fibonacci (a, index_) AS
WITH RECURSIVE inner_fibo(b, c, index_) AS (
    SELECT 0, 1, 0
    UNION
    SELECT c, c + b, index_ + 1
    FROM inner_fibo
)
SELECT b, index_ FROM inner_fibo;

SELECT * FROM fibonacci LIMIT 1 OFFSET 3;
SELECT * FROM fibonacci LIMIT 1 OFFSET 4;
SELECT * FROM fibonacci LIMIT 1 OFFSET 5;
SELECT * FROM fibonacci LIMIT 1 OFFSET 10;


CREATE TEMPORARY TABLE test_table (a integer) STRICT;
```
</details>
We modify the above script by adding the following delimiter :
<pre> -- % key</pre>
where key is the string that will reference a particular chunk of the script that we want to separate and make addressable.
Note the prepended space. The delimiter is modifiable.
For instance :

*```raw_script.sql```* :
```SQL
 -- % fibonacci_view_creation
CREATE TEMPORARY VIEW fibonacci (a, index_) AS
WITH RECURSIVE inner_fibo(b, c, index_) AS (
    SELECT 0, 1, 0
    UNION
    SELECT c, c + b, index_ + 1
    FROM inner_fibo
)
SELECT b, index_ FROM inner_fibo;

 -- % contiguous_view_tests
SELECT * FROM fibonacci LIMIT 1 OFFSET 3;
SELECT * FROM fibonacci LIMIT 1 OFFSET 4;
SELECT * FROM fibonacci LIMIT 1 OFFSET 5;
 -- % testing_10nth_value_of_view
SELECT * FROM fibonacci LIMIT 1 OFFSET 10;


CREATE TEMPORARY TABLE test_table (a integer) STRICT;
```

Then we preprocess the script :
```python
import addressable_sql_queries
with (open("raw_script.sql") as input_file,
    open("preprocessed_script.py", "w") as output_file):
    output_file.write(
        addressable_sql_queries.preprocess_to_str(input_file.read()))
```

This step can also be done with :
```bash
python -m addressable_sql_queries path/to/raw_script.sql -o path/to/preprocessed_script.py
```

As all keys in the example above are not only valid but usable python ids, you can then access chunks of this script like this :
```python
from preprocessed_script import *
print(contiguous_view_tests)
```

Which will outputs :
```
 -- % contiguous_view_tests
SELECT * FROM fibonacci LIMIT 1 OFFSET 3;
SELECT * FROM fibonacci LIMIT 1 OFFSET 4;
SELECT * FROM fibonacci LIMIT 1 OFFSET 5;

```

Even when keys are invalid ids, you can access the chunks of the script by these means :
```python
from preprocessed_script import queries as sql_queries
print(sql_queries[0]) # This will print the chunk before the first delimiter, which is nothing in this case : "\n".
print(sql_queries[None]) # Another way to do the same thing. None is always the first key.
print(sql_queries["fibonacci_view_creation"])
"""This will print :
 -- % fibonacci_view_creation
CREATE TEMPORARY VIEW fibonacci (a, index_) AS
WITH RECURSIVE inner_fibo(b, c, index_) AS (
    SELECT 0, 1, 0
    UNION
    SELECT c, c + b, index_ + 1
    FROM inner_fibo
)
SELECT b, index_ FROM inner_fibo;


"""
print(sql_queries[1]) # This will print the same thing as above (the second chunk).
print(sql_queries.having_line(4)) # This will print the chunk containing the line 4 (counting from 1), and thus does the same thing as above.
```

## Parser mode example :
Alternatively, parser mode can be used :
```python
from addressable_sql_queries import Query
q = Query.fromFile("raw_script.sql")
print(q[3])
"""This will print :
 -- % testing_10nth_value_of_view
SELECT * FROM fibonacci LIMIT 1 OFFSET 10;


CREATE TEMPORARY TABLE test_table (a integer) STRICT;
"""
```

Query object can also be built directly from a string.

Note : in the case where the first line is a delimiter, thus making the first and second chunk shares the first line (starting at 1), the having_line function will return the second, non empty chunk.

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "addressable-sql-queries",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": "",
    "keywords": "SQL,addressable queries,addressable query,parser,preprocessor,script chunk,script manipulation",
    "author": "Jean Oustry",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/cc/5f/9b25d8976f10bc8ea0f82521df691bb3c23b05f8f729c8713cf5372d2300/addressable_sql_queries-0.0.1.dev1.tar.gz",
    "platform": null,
    "description": "# What is this library ?\nThis library allows to make a SQL script (or possibly any script) addressable.\nOnce preprocessed or parsed, you can get any chunk of it (which could be a single query for instance) with its line number, key, or index of the chunk.\n\n# Example & How to use:\n<details>\n<summary>\nWith a target SQL script : (toggle)\n</summary>\n\n*```raw_script.sql```* :\n```SQL\nCREATE TEMPORARY VIEW fibonacci (a, index_) AS\nWITH RECURSIVE inner_fibo(b, c, index_) AS (\n    SELECT 0, 1, 0\n    UNION\n    SELECT c, c + b, index_ + 1\n    FROM inner_fibo\n)\nSELECT b, index_ FROM inner_fibo;\n\nSELECT * FROM fibonacci LIMIT 1 OFFSET 3;\nSELECT * FROM fibonacci LIMIT 1 OFFSET 4;\nSELECT * FROM fibonacci LIMIT 1 OFFSET 5;\nSELECT * FROM fibonacci LIMIT 1 OFFSET 10;\n\n\nCREATE TEMPORARY TABLE test_table (a integer) STRICT;\n```\n</details>\nWe modify the above script by adding the following delimiter :\n<pre> -- % key</pre>\nwhere key is the string that will reference a particular chunk of the script that we want to separate and make addressable.\nNote the prepended space. The delimiter is modifiable.\nFor instance :\n\n*```raw_script.sql```* :\n```SQL\n -- % fibonacci_view_creation\nCREATE TEMPORARY VIEW fibonacci (a, index_) AS\nWITH RECURSIVE inner_fibo(b, c, index_) AS (\n    SELECT 0, 1, 0\n    UNION\n    SELECT c, c + b, index_ + 1\n    FROM inner_fibo\n)\nSELECT b, index_ FROM inner_fibo;\n\n -- % contiguous_view_tests\nSELECT * FROM fibonacci LIMIT 1 OFFSET 3;\nSELECT * FROM fibonacci LIMIT 1 OFFSET 4;\nSELECT * FROM fibonacci LIMIT 1 OFFSET 5;\n -- % testing_10nth_value_of_view\nSELECT * FROM fibonacci LIMIT 1 OFFSET 10;\n\n\nCREATE TEMPORARY TABLE test_table (a integer) STRICT;\n```\n\nThen we preprocess the script :\n```python\nimport addressable_sql_queries\nwith (open(\"raw_script.sql\") as input_file,\n    open(\"preprocessed_script.py\", \"w\") as output_file):\n    output_file.write(\n        addressable_sql_queries.preprocess_to_str(input_file.read()))\n```\n\nThis step can also be done with :\n```bash\npython -m addressable_sql_queries path/to/raw_script.sql -o path/to/preprocessed_script.py\n```\n\nAs all keys in the example above are not only valid but usable python ids, you can then access chunks of this script like this :\n```python\nfrom preprocessed_script import *\nprint(contiguous_view_tests)\n```\n\nWhich will outputs :\n```\n -- % contiguous_view_tests\nSELECT * FROM fibonacci LIMIT 1 OFFSET 3;\nSELECT * FROM fibonacci LIMIT 1 OFFSET 4;\nSELECT * FROM fibonacci LIMIT 1 OFFSET 5;\n\n```\n\nEven when keys are invalid ids, you can access the chunks of the script by these means :\n```python\nfrom preprocessed_script import queries as sql_queries\nprint(sql_queries[0]) # This will print the chunk before the first delimiter, which is nothing in this case : \"\\n\".\nprint(sql_queries[None]) # Another way to do the same thing. None is always the first key.\nprint(sql_queries[\"fibonacci_view_creation\"])\n\"\"\"This will print :\n -- % fibonacci_view_creation\nCREATE TEMPORARY VIEW fibonacci (a, index_) AS\nWITH RECURSIVE inner_fibo(b, c, index_) AS (\n    SELECT 0, 1, 0\n    UNION\n    SELECT c, c + b, index_ + 1\n    FROM inner_fibo\n)\nSELECT b, index_ FROM inner_fibo;\n\n\n\"\"\"\nprint(sql_queries[1]) # This will print the same thing as above (the second chunk).\nprint(sql_queries.having_line(4)) # This will print the chunk containing the line 4 (counting from 1), and thus does the same thing as above.\n```\n\n## Parser mode example :\nAlternatively, parser mode can be used :\n```python\nfrom addressable_sql_queries import Query\nq = Query.fromFile(\"raw_script.sql\")\nprint(q[3])\n\"\"\"This will print :\n -- % testing_10nth_value_of_view\nSELECT * FROM fibonacci LIMIT 1 OFFSET 10;\n\n\nCREATE TEMPORARY TABLE test_table (a integer) STRICT;\n\"\"\"\n```\n\nQuery object can also be built directly from a string.\n\nNote : in the case where the first line is a delimiter, thus making the first and second chunk shares the first line (starting at 1), the having_line function will return the second, non empty chunk.\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2023 Jean Oustry  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.",
    "summary": "Individualize the queries of a SQL script and make them easily addressable",
    "version": "0.0.1.dev1",
    "project_urls": null,
    "split_keywords": [
        "sql",
        "addressable queries",
        "addressable query",
        "parser",
        "preprocessor",
        "script chunk",
        "script manipulation"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e2fb5b9fa74b4fa65b49967cb334cf699c849168e9d6e3684da709b4023c9855",
                "md5": "3b620ad3089367133f0cd947414b3e3f",
                "sha256": "501202d2a7cbcdc3f3e888b63695ec129794c55de24ab26ae33bafd56790ce8f"
            },
            "downloads": -1,
            "filename": "addressable_sql_queries-0.0.1.dev1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "3b620ad3089367133f0cd947414b3e3f",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 9406,
            "upload_time": "2023-05-03T15:48:00",
            "upload_time_iso_8601": "2023-05-03T15:48:00.480192Z",
            "url": "https://files.pythonhosted.org/packages/e2/fb/5b9fa74b4fa65b49967cb334cf699c849168e9d6e3684da709b4023c9855/addressable_sql_queries-0.0.1.dev1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "cc5f9b25d8976f10bc8ea0f82521df691bb3c23b05f8f729c8713cf5372d2300",
                "md5": "d1abde7c61c832aea446be4daf569560",
                "sha256": "3d6784c6c06f1261f63925328048d990ed19858333dc905cc89782360de7da86"
            },
            "downloads": -1,
            "filename": "addressable_sql_queries-0.0.1.dev1.tar.gz",
            "has_sig": false,
            "md5_digest": "d1abde7c61c832aea446be4daf569560",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 12714,
            "upload_time": "2023-05-03T15:48:02",
            "upload_time_iso_8601": "2023-05-03T15:48:02.736468Z",
            "url": "https://files.pythonhosted.org/packages/cc/5f/9b25d8976f10bc8ea0f82521df691bb3c23b05f8f729c8713cf5372d2300/addressable_sql_queries-0.0.1.dev1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-05-03 15:48:02",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "addressable-sql-queries"
}
        
Elapsed time: 0.07519s