Add quote utility function

This commit is contained in:
Konsta Vesterinen
2014-08-22 13:20:56 +03:00
parent 84f244c56f
commit 8274b495fe
7 changed files with 55 additions and 2 deletions

View File

@@ -4,6 +4,12 @@ Changelog
Here you can see the full list of changes between each SQLAlchemy-Utils release.
0.26.12 (2014-08-22)
^^^^^^^^^^^^^^^^^^^^
- Added quote utility function
0.26.11 (2014-08-21)
^^^^^^^^^^^^^^^^^^^^

View File

@@ -82,6 +82,12 @@ naturally_equivalent
.. autofunction:: naturally_equivalent
quote
^^^^^
.. autofunction:: quote
sort_query
^^^^^^^^^^

View File

@@ -44,7 +44,7 @@ for name, requirements in extras_require.items():
setup(
name='SQLAlchemy-Utils',
version='0.26.11',
version='0.26.12',
url='https://github.com/kvesteri/sqlalchemy-utils',
license='BSD',
author='Konsta Vesterinen, Ryan Leckey, Janne Vanhala, Vesa Uimonen',

View File

@@ -75,7 +75,7 @@ from .types import (
from .models import Timestamp
__version__ = '0.26.11'
__version__ = '0.26.12'
__all__ = (

View File

@@ -33,6 +33,7 @@ from .orm import (
has_changes,
identity,
naturally_equivalent,
quote,
table_name,
)
@@ -64,6 +65,7 @@ __all__ = (
'naturally_equivalent',
'non_indexed_foreign_keys',
'QuerySorterException',
'quote',
'render_expression',
'render_statement',
'sort_query',

View File

@@ -333,6 +333,31 @@ def remote_column_names(prop):
yield remote.name
def quote(mixed, ident):
"""
Conditionally quote an identifier.
::
from sqlalchemy_utils import quote
engine = create_engine('sqlite:///:memory:')
quote(engine, 'order')
# '"order"'
quote(engine, 'some_other_identifier')
# 'some_other_identifier'
:param mixed: SQLAlchemy Session / Connection / Engine object.
:param ident: identifier to conditionally quote
"""
dialect = get_bind(mixed).dialect
return dialect.preparer(dialect).quote(ident)
def query_labels(query):
"""
Return all labels for given SQLAlchemy query object.

View File

@@ -0,0 +1,14 @@
from sqlalchemy_utils.functions import quote
from tests import TestCase
class TestQuote(TestCase):
def test_quote_with_preserved_keyword(self):
assert quote(self.connection, 'order') == '"order"'
assert quote(self.session, 'order') == '"order"'
assert quote(self.engine, 'order') == '"order"'
def test_quote_with_non_preserved_keyword(self):
assert quote(self.connection, 'some_order') == 'some_order'
assert quote(self.session, 'some_order') == 'some_order'
assert quote(self.engine, 'some_order') == 'some_order'