[ Debian Janitor ]
New upstream release.
Debian Janitor
2 years ago
0 | dist: xenial | |
0 | 1 | language: python |
1 | 2 | python: |
2 | 3 | - "2.7" |
3 | 4 | - "3.5" |
4 | services: | |
5 | - postgresql | |
6 | addons: | |
7 | postgresql: "10" | |
8 | apt: | |
9 | packages: | |
10 | - postgresql-10 | |
11 | - postgresql-client-10 | |
12 | - postgresql-server-dev-10 | |
13 | install: "pip install tox" | |
5 | - "3.7" | |
6 | matrix: | |
7 | include: | |
8 | - python: "3.7" | |
9 | env: TOXENV=flake8 | |
10 | - python: "3.7" | |
11 | env: TOXENV=docs | |
12 | - python: "3.7" | |
13 | env: TOXENV=check-manifest | |
14 | before_install: | |
15 | # don't install postgresql for flake8 and docs environments | |
16 | - if test -z "$TOXENV"; then sudo apt-get -y install --no-install-recommends postgresql-11 postgresql-client-11 postgresql-server-dev-11; fi | |
17 | install: "pip install tox-travis" | |
14 | 18 | script: tox |
0 | include README.md | |
0 | include README.rst | |
1 | include CHANGELOG | |
2 | include LICENSE | |
3 | include tox.ini | |
4 | include test_requirements.txt | |
5 | recursive-include docs *.rst Makefile conf.py make.bat | |
6 | recursive-include tests *.csv *.sql |
2 | 2 | |
3 | 3 | .. image:: https://badge.fury.io/py/anosql.svg |
4 | 4 | :target: https://badge.fury.io/py/anosql |
5 | :alt: pypi package version | |
5 | 6 | |
6 | 7 | .. image:: http://readthedocs.org/projects/anosql/badge/?version=latest |
7 | 8 | :target: http://anosql.readthedocs.io/en/latest/?badge=latest |
9 | 10 | |
10 | 11 | .. image:: https://travis-ci.org/honza/anosql.svg?branch=master |
11 | 12 | :target: https://travis-ci.org/honza/anosql |
13 | :alt: Travid build status | |
12 | 14 | |
13 | 15 | A Python library for using SQL |
14 | 16 | |
15 | 17 | Inspired by the excellent `Yesql`_ library by Kris Jenkins. In my mother |
16 | 18 | tongue, *ano* means *yes*. |
17 | 19 | |
18 | If you are on python3.6+ or need ``anosql`` to work with ``asyncio`` based database drivers. | |
19 | See the related project `aiosql <https://github.com/nackjicholson/aiosql>`_. | |
20 | If you are on python3.6+ or need ``anosql`` to work with ``asyncio``-based database drivers, see the related project, `aiosql <https://github.com/nackjicholson/aiosql>`_. | |
21 | ||
22 | Complete documentation is available at `Read The Docs <https://anosql.readthedocs.io/en/latest/>`_. | |
20 | 23 | |
21 | 24 | Installation |
22 | 25 | ------------ |
39 | 42 | -- Get all the greetings in the database |
40 | 43 | SELECT * FROM greetings; |
41 | 44 | |
42 | -- name: $select-users | |
45 | -- name: select-users | |
43 | 46 | -- Get all the users from the database, |
44 | 47 | -- and return it as a dict |
45 | 48 | SELECT * FROM USERS; |
58 | 61 | |
59 | 62 | # Or, Sqlite3... |
60 | 63 | conn = sqlite3.connect('cool.db') |
61 | queries = anosql.from_path('queries.sql', 'sqlite3) | |
64 | queries = anosql.from_path('queries.sql', 'sqlite3') | |
62 | 65 | |
63 | 66 | queries.get_all_greetings(conn) |
64 | 67 | # => [(1, 'Hi')] |
81 | 84 | |
82 | 85 | .. code-block:: sql |
83 | 86 | |
84 | -- name: get-greetings-for-language-and-length | |
85 | -- Get all the greetings in the database | |
87 | -- name: get-greetings-for-language | |
88 | -- Get all the greetings in the database for given language | |
86 | 89 | SELECT * |
87 | 90 | FROM greetings |
88 | 91 | WHERE lang = %s; |
92 | 95 | .. code-block:: python |
93 | 96 | |
94 | 97 | visitor_language = "en" |
95 | queries.get_all_greetings(conn, visitor_language) | |
98 | queries.get_greetings_for_language(conn, visitor_language) | |
96 | 99 | |
97 | 100 | |
98 | 101 | |
105 | 108 | .. code-block:: sql |
106 | 109 | |
107 | 110 | -- name: get-greetings-for-language-and-length |
108 | -- Get all the greetings in the database | |
111 | -- Get all the greetings in the database for given language and length | |
109 | 112 | SELECT * |
110 | 113 | FROM greetings |
111 | 114 | WHERE lang = :lang |
121 | 124 | |
122 | 125 | visitor_language = "en" |
123 | 126 | |
124 | greetings_for_texting = queries.get_all_greetings( | |
127 | greetings_for_texting = queries.get_greetings_for_language_and_length( | |
125 | 128 | conn, lang=visitor_language, length_limit=140) |
126 | 129 | |
127 | 130 | Update/Insert/Delete |
145 | 148 | Adding custom query loaders. |
146 | 149 | **************************** |
147 | 150 | |
148 | Out of the box ``anosql`` supports SQLite and PostgreSQL via the stdlib ``sqlite3`` database driver | |
149 | and ``psycopg2``. If you would like to extend ``anosql`` to communicate with another type of databases | |
150 | you may create a driver adapeter class and register it with ``anosql.register_driver_adapter()``. | |
151 | Out of the box, ``anosql`` supports SQLite and PostgreSQL via the stdlib ``sqlite3`` database driver | |
152 | and ``psycopg2``. If you would like to extend ``anosql`` to communicate with other types of databases, | |
153 | you may create a driver adapter class and register it with ``anosql.core.register_driver_adapter()``. | |
151 | 154 | |
152 | 155 | Driver adapters are duck-typed classes which adhere to the below interface. Looking at ``anosql/adapters`` package |
153 | 156 | is a good place to get started by looking at how the ``psycopg2`` and ``sqlite3`` adapters work. |
155 | 158 | To register a new loader:: |
156 | 159 | |
157 | 160 | import anosql |
161 | import anosql.core | |
158 | 162 | |
159 | 163 | class MyDbAdapter(): |
160 | 164 | def process_sql(self, name, op_type, sql): |
180 | 184 | pass |
181 | 185 | |
182 | 186 | |
183 | anosql.register_driver_adapter("mydb", MyDbAdapter) | |
187 | anosql.core.register_driver_adapter("mydb", MyDbAdapter) | |
184 | 188 | |
185 | 189 | # To use make a connection to your db, and pass "mydb" as the db_type: |
186 | 190 | import mydbdriver |
191 | 195 | |
192 | 196 | conn.close() |
193 | 197 | |
194 | If your adapter constructor takes arguments you can register a function which can build | |
198 | If your adapter constructor takes arguments, you can register a function which can build | |
195 | 199 | your adapter instance:: |
196 | 200 | |
197 | 201 | def adapter_factory(): |
1 | 1 | from anosql.exceptions import SQLLoadException, SQLParseException |
2 | 2 | |
3 | 3 | __all__ = ["from_path", "from_str", "SQLOperationType", "SQLLoadException", "SQLParseException"] |
4 |
40 | 40 | cur.execute(sql, parameters) |
41 | 41 | |
42 | 42 | @staticmethod |
43 | def insert_update_delete_many(conn, _query_name, sql, parmeters): | |
43 | def insert_update_delete_many(conn, _query_name, sql, parameters): | |
44 | 44 | with conn.cursor() as cur: |
45 | cur.executemany(sql, parmeters) | |
45 | cur.executemany(sql, parameters) | |
46 | 46 | |
47 | 47 | @staticmethod |
48 | 48 | def insert_returning(conn, _query_name, sql, parameters): |
22 | 22 | var_pattern = re.compile( |
23 | 23 | r'(?P<dblquote>"[^"]+")|' |
24 | 24 | r"(?P<quote>\'[^\']+\')|" |
25 | r"(?P<lead>[^:]):(?P<var_name>[\w-]+)(?P<trail>[^:])" | |
25 | r"(?P<lead>[^:]):(?P<var_name>[\w-]+)(?P<trail>[^:]?)" | |
26 | 26 | ) |
27 | 27 | """ |
28 | 28 | Pattern: Identifies variable definitions in SQL code. |
0 | anosql (1.0.1-2) UNRELEASED; urgency=medium | |
0 | anosql (1.0.2-1) UNRELEASED; urgency=medium | |
1 | 1 | |
2 | 2 | [ Ondřej Nový ] |
3 | 3 | * Bump Standards-Version to 4.4.1. |
18 | 18 | [ Stefano Rivera ] |
19 | 19 | * Run tests against all supported Python versions. |
20 | 20 | |
21 | -- Ondřej Nový <onovy@debian.org> Fri, 18 Oct 2019 15:27:26 +0200 | |
21 | [ Debian Janitor ] | |
22 | * New upstream release. | |
23 | ||
24 | -- Ondřej Nový <onovy@debian.org> Sun, 07 Nov 2021 02:27:58 -0000 | |
22 | 25 | |
23 | 26 | anosql (1.0.1-1) unstable; urgency=medium |
24 | 27 |
18 | 18 | # import os |
19 | 19 | # import sys |
20 | 20 | # sys.path.insert(0, os.path.abspath('.')) |
21 | import pkg_resources | |
21 | 22 | |
22 | 23 | # -- General configuration ------------------------------------------------ |
23 | 24 | |
56 | 57 | # built documents. |
57 | 58 | # |
58 | 59 | # The short X.Y version. |
59 | version = u'1.0.0' | |
60 | version = pkg_resources.get_distribution('anosql').version | |
60 | 61 | # The full version, including alpha/beta/rc tags. |
61 | release = u'1.0.0' | |
62 | release = version | |
62 | 63 | |
63 | 64 | # The language for content autogenerated by Sphinx. Refer to documentation |
64 | 65 | # for a list of supported languages. |
152 | 153 | # Add any paths that contain custom static files (such as style sheets) here, |
153 | 154 | # relative to this directory. They are copied after the builtin static files, |
154 | 155 | # so a file named "default.css" will overwrite the builtin "default.css". |
155 | html_static_path = ['_static'] | |
156 | html_static_path = [] | |
156 | 157 | |
157 | 158 | # Add any extra paths that contain custom files (such as robots.txt or |
158 | 159 | # .htaccess) here, relative to this directory. These files are copied |
5 | 5 | ====================== |
6 | 6 | |
7 | 7 | Name definitions are how ``anosql`` determines how to name the SQL code blocks which are loaded. |
8 | A query name definition is a normal SQL comment starting with "-- name:" and is followed by the | |
9 | name of the query. You can use ``-`` or ``_`` in your query names, but the methods in python | |
10 | will always be valid python names using underscores. | |
8 | A query name definition is a normal SQL comment starting with "\-\- name:" and is followed by the | |
9 | name of the query. You can use ``-`` or ``_`` in your query names, but the methods in Python | |
10 | will always be valid Python names using underscores. | |
11 | 11 | |
12 | 12 | .. code-block:: sql |
13 | 13 | |
17 | 17 | The above example when loaded by ``anosql.from_path`` will return an object with a |
18 | 18 | ``.get_all_blogs(conn)`` method. |
19 | 19 | |
20 | Your SQL comments will be added to your methods as python documentation, and accessible by calling | |
20 | Your SQL comments will be added to your methods as Python docstrings, and accessible by calling | |
21 | 21 | ``help()`` on them. |
22 | 22 | |
23 | 23 | .. code-block:: sql |
32 | 32 | queries = anosql.from_path("blogs.sql", "sqlite3") |
33 | 33 | help(anosql.get_all_blogs) |
34 | 34 | |
35 | output | |
35 | returns | |
36 | 36 | |
37 | 37 | .. code-block:: text |
38 | 38 | |
51 | 51 | characters trailing it. This lack of operator is actually the most basic operator which performs |
52 | 52 | SQL ``select`` statements and returns a list of rows. When writing an application you will often |
53 | 53 | need to perform other operations besides selects, like inserts, deletes, and bulk opearations. The |
54 | operators detailed in this section let you declare in your SQL, how your code should be executed | |
54 | operators detailed in this section let you declare in your SQL how your code should be executed | |
55 | 55 | by the database driver. |
56 | 56 | |
57 | 57 | Insert/Update/Delete with ``!`` |
141 | 141 | :published |
142 | 142 | ) |
143 | 143 | |
144 | Applying this to a list of blogs in python:: | |
144 | Applying this to a list of blogs in Python:: | |
145 | 145 | |
146 | 146 | queries = anosql.from_path("blogs.sql", "psycopg2") |
147 | 147 | blogs = [ |
154 | 154 | Execute SQL script statements with ``#`` |
155 | 155 | --------------------------------------------- |
156 | 156 | |
157 | Executes some sql statements as a script. These methods don't do variable substitution, or return | |
158 | any rows. An example usecase is using data definition statements like create table in order to | |
157 | Executes some SQL statements as a script. These methods don't do variable substitution, or return | |
158 | any rows. An example use case is using data definition statements like `create` table in order to | |
159 | 159 | setup your database. |
160 | 160 | |
161 | 161 | .. code-block:: sql |
3 | 3 | Extending anosql |
4 | 4 | ################ |
5 | 5 | |
6 | .. _driver-adapters: | |
7 | ||
6 | 8 | Driver Adapters |
7 | 9 | --------------- |
8 | 10 | |
9 | Database driver adapters in ``anosql`` are a duck-typed class which follow the below interface.:: | |
11 | Database driver adapters in ``anosql`` are duck-typed classes which follow the below interface.:: | |
10 | 12 | |
11 | 13 | class MyDbAdapter(): |
12 | 14 | def process_sql(self, name, op_type, sql): |
32 | 34 | pass |
33 | 35 | |
34 | 36 | |
35 | anosql.register_driver_adapter("mydb", MyDbAdapter) | |
37 | anosql.core.register_driver_adapter("mydb", MyDbAdapter) | |
36 | 38 | |
37 | 39 | If your adapter constructor takes arguments you can register a function which can build |
38 | 40 | your adapter instance:: |
40 | 42 | def adapter_factory(): |
41 | 43 | return MyDbAdapter("foo", 42) |
42 | 44 | |
43 | anosql.register_driver_adapter("mydb", adapter_factory) | |
45 | anosql.core.register_driver_adapter("mydb", adapter_factory) | |
44 | 46 | |
45 | 47 | Looking at the source of the builtin |
46 | 48 | `adapters/ <https://github.com/honza/anosql/tree/master/anosql/adapters>`_ is a great place |
23 | 23 | from worlds |
24 | 24 | where world_name = :world_name; |
25 | 25 | |
26 | By specifying ``db_driver="sqlite3"`` we can use the python stdlib ``sqlite3`` driver to execute these sql queries and | |
26 | By specifying ``db_driver="sqlite3"`` we can use the Python stdlib ``sqlite3`` driver to execute these SQL queries and | |
27 | 27 | get the results. We're also using the ``sqlite3.Row`` type for our records to make it easy to access our data via |
28 | 28 | their column names rather than as tuple indices. |
29 | 29 |
7 | 7 | |
8 | 8 | .. image:: https://badge.fury.io/py/anosql.svg |
9 | 9 | :target: https://badge.fury.io/py/anosql |
10 | :alt: pypi package version | |
10 | 11 | |
11 | 12 | .. image:: http://readthedocs.org/projects/anosql/badge/?version=latest |
12 | 13 | :target: http://anosql.readthedocs.io/en/latest/?badge=latest |
14 | :alt: Documentation status | |
13 | 15 | |
14 | 16 | .. image:: https://travis-ci.org/honza/anosql.svg?branch=master |
15 | 17 | :target: https://travis-ci.org/honza/anosql |
18 | :alt: Travis build status | |
16 | 19 | |
17 | 20 | A Python library for using SQL |
18 | 21 | |
19 | 22 | Inspired by the excellent `Yesql`_ library by Kris Jenkins. In my mother |
20 | 23 | tongue, *ano* means *yes*. |
21 | 24 | |
22 | If you are on python3.6+ or need ``anosql`` to work with ``asyncio`` based database drivers. | |
23 | See the related project `aiosql <https://github.com/nackjicholson/aiosql>`_. | |
25 | If you are on python3.6+ or need ``anosql`` to work with ``asyncio`` based database drivers, see the related project `aiosql <https://github.com/nackjicholson/aiosql>`_. | |
24 | 26 | |
25 | 27 | Installation |
26 | 28 | ------------ |
57 | 59 | |
58 | 60 | # Or, Sqlite3... |
59 | 61 | conn = sqlite3.connect('cool.db') |
60 | queries = anosql.from_path('queries.sql', 'sqlite3) | |
62 | queries = anosql.from_path('queries.sql', 'sqlite3') | |
61 | 63 | |
62 | 64 | queries.get_all_greetings(conn) |
63 | 65 | # => [(1, 'Hi')] |
75 | 77 | Parameters |
76 | 78 | ********** |
77 | 79 | |
78 | Often, you want to change parts of the query dynamically, particularly values in the WHERE clause. | |
80 | Often, you want to change parts of the query dynamically, particularly values in the ``WHERE`` clause. | |
79 | 81 | You can use parameters to do this: |
80 | 82 | |
81 | 83 | .. code-block:: sql |
82 | 84 | |
83 | -- name: get-greetings-for-language-and-length | |
84 | -- Get all the greetings in the database | |
85 | -- name: get-greetings-for-language | |
86 | -- Get all the greetings in the database for a given language | |
85 | 87 | SELECT * |
86 | 88 | FROM greetings |
87 | 89 | WHERE lang = %s; |
91 | 93 | .. code-block:: python |
92 | 94 | |
93 | 95 | visitor_language = "en" |
94 | queries.get_all_greetings(conn, visitor_language) | |
96 | queries.get_all_greetings_for_language(conn, visitor_language) | |
95 | 97 | |
96 | 98 | |
97 | 99 | |
102 | 104 | |
103 | 105 | .. code-block:: sql |
104 | 106 | |
105 | -- name: get-greetings-for-language-and-length | |
106 | -- Get all the greetings in the database | |
107 | -- name: get-greetings-for-language | |
108 | -- Get all the greetings in the database for given language and length | |
107 | 109 | SELECT * |
108 | 110 | FROM greetings |
109 | 111 | WHERE lang = :lang |
4 | 4 | |
5 | 5 | setup( |
6 | 6 | name='anosql', |
7 | version='1.0.1', | |
7 | version='1.0.2', | |
8 | 8 | url='https://github.com/honza/anosql', |
9 | 9 | install_requires=[], |
10 | 10 | description='Easy SQL in Python', |
19 | 19 | firstname integer not null, |
20 | 20 | lastname text not null |
21 | 21 | ); |
22 | ||
22 | ||
23 | 23 | create table blogs ( |
24 | 24 | blogid integer not null primary key, |
25 | 25 | userid integer not null, |
192 | 192 | q.insert_some_value(postgresql) |
193 | 193 | |
194 | 194 | assert q.get_all_values(postgresql, a=1) == [(1, 2, 3)] |
195 | ||
196 | ||
197 | def test_without_trailing_semi_colon_pg(): | |
198 | """Make sure keywords ending queries are recognized even without | |
199 | semi-colons. | |
200 | """ | |
201 | _queries = ("-- name: get-by-a\n" | |
202 | "SELECT a, b, c FROM foo WHERE a = :a\n") | |
203 | q = anosql.from_str(_queries, "psycopg2") | |
204 | assert q.get_by_a.sql == "SELECT a, b, c FROM foo WHERE a = %(a)s" |
0 | 0 | [tox] |
1 | envlist = py27, py35 | |
1 | envlist = py27, py35, py37, flake8, docs, check-manifest | |
2 | 2 | skip_missing_interpreters = True |
3 | 3 | |
4 | 4 | [testenv] |
5 | 5 | deps = -rtest_requirements.txt |
6 | commands = py.test tests | |
6 | commands = {envpython} -m pytest {posargs:tests} | |
7 | ||
8 | [testenv:flake8] | |
9 | skip_install = true | |
10 | deps = flake8 | |
11 | commands = {envpython} -m flake8 {posargs} | |
12 | ||
13 | [flake8] | |
14 | max-line-length=100 | |
15 | ||
16 | [testenv:docs] | |
17 | deps = | |
18 | sphinx | |
19 | alabaster | |
20 | commands = {envpython} -m sphinx.cmd.build -W -b html docs docs/_build/html | |
21 | ||
22 | [testenv:check-manifest] | |
23 | skip_install = true | |
24 | deps = | |
25 | check-manifest | |
26 | commands = {envpython} -m check_manifest |