Skip to content

Commit

Permalink
Initial sql compilation caching support
Browse files Browse the repository at this point in the history
  • Loading branch information
aadel committed Feb 22, 2024
1 parent 4f377bd commit 71ccf7e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/sqlalchemy_solr/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@ class SolrDialect(default.DefaultDialect):
returns_unicode_strings = True
description_encoding = None
supports_native_boolean = True
supports_statement_cache = True

def __init__(self, **kw):
default.DefaultDialect.__init__(self, **kw)
Expand Down
2 changes: 2 additions & 0 deletions src/sqlalchemy_solr/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
class SolrDialect_http(SolrDialect): # pylint: disable=invalid-name
# pylint: disable=abstract-method,too-many-instance-attributes

supports_statement_cache = True

mf = MessageFormatter()

def __init__(self, **kw):
Expand Down
49 changes: 49 additions & 0 deletions tests/test_sql_compilation_caching.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from sqlalchemy import select
from sqlalchemy.sql.expression import bindparam

from tests.setup import prepare_orm
from .fixtures.fixtures import SalesFixture

class TestSQLCompilationCaching:
def index_data(self, settings):
f = SalesFixture(settings)
f.truncate_collection()
f.index()

def test_sql_compilation_caching_1(self, caplog, settings):
engine, t = prepare_orm(settings)

qry_1 = (select(t.c.CITY_s).select_from(t)).limit(1) # pylint: disable=unsubscriptable-object
qry_2 = (select(t.c.CITY_s).select_from(t)).limit(10) # pylint: disable=unsubscriptable-object

with engine.connect() as connection:
connection.execute(qry_1)
connection.execute(qry_2)

assert 'generated in' in caplog.text
assert 'cached since' in caplog.text

def test_sql_compilation_caching_2(self, caplog, settings):
engine, t = prepare_orm(settings)

qry_1 = (select(t.c.CITY_s).select_from(t)).limit(1).offset(1) # pylint: disable=unsubscriptable-object
qry_2 = (select(t.c.CITY_s).select_from(t)).limit(1).offset(2) # pylint: disable=unsubscriptable-object

with engine.connect() as connection:
connection.execute(qry_1)
connection.execute(qry_2)

assert 'generated in' in caplog.text
assert 'cached since' in caplog.text

def test_sql_compilation_caching_3(self, caplog, settings):
engine, t = prepare_orm(settings)

qry = select(t).where(t.c.CITY_s == bindparam('CITY_s')).limit(10) # pylint: disable=unsubscriptable-object

with engine.connect() as connection:
connection.execute(qry, params={'CITY_s': 'Singapore'})
connection.execute(qry, CITY_s='Boras')

assert 'generated in' in caplog.text
assert 'cached since' in caplog.text

0 comments on commit 71ccf7e

Please sign in to comment.