Skip to content

Commit 5d5ec90

Browse files
committed
EXISTS with columns
1 parent a72cc8b commit 5d5ec90

File tree

2 files changed

+47
-35
lines changed

2 files changed

+47
-35
lines changed

sqlalchemy_iris/base.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,12 @@ def check_constraints(cls):
394394
class IRISCompiler(sql.compiler.SQLCompiler):
395395
"""IRIS specific idiosyncrasies"""
396396

397+
def visit_exists_unary_operator(self, element, operator, within_columns_clause=False, **kw):
398+
if within_columns_clause:
399+
return "(SELECT 1 WHERE EXISTS(%s))" % self.process(element.element, **kw)
400+
else:
401+
return "EXISTS(%s)" % self.process(element.element, **kw)
402+
397403
def limit_clause(self, select, **kw):
398404
return ""
399405

@@ -556,16 +562,6 @@ def order_by_clause(self, select, **kw):
556562
else:
557563
return ""
558564

559-
def visit_column(self, column, within_columns_clause=False, **kwargs):
560-
text = super().visit_column(
561-
column, within_columns_clause=within_columns_clause, **kwargs
562-
)
563-
if within_columns_clause:
564-
return text
565-
# if isinstance(column.type, sqltypes.Text):
566-
# text = "CONVERT(VARCHAR, %s)" % (text,)
567-
return text
568-
569565
def visit_concat_op_binary(self, binary, operator, **kw):
570566
return "STRING(%s, %s)" % (
571567
self.process(binary.left, **kw),

tests/test_suite.py

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from sqlalchemy.testing.suite import CTETest as _CTETest
44
from sqlalchemy.testing.suite import DifficultParametersTest as _DifficultParametersTest
55
from sqlalchemy.testing import fixtures
6+
from sqlalchemy.orm import Session
67
from sqlalchemy import testing
78
from sqlalchemy import Table, Column, Integer, String, select
89
import pytest
@@ -28,7 +29,6 @@ class DifficultParametersTest(_DifficultParametersTest):
2829

2930

3031
class FetchLimitOffsetTest(_FetchLimitOffsetTest):
31-
3232
def test_simple_offset_no_order(self, connection):
3333
table = self.tables.some_table
3434
self._assert_result(
@@ -55,37 +55,14 @@ def test_simple_limit_offset_no_order(self, connection, cases):
5555
assert_data = [(1, 1, 2), (2, 2, 3), (3, 3, 4), (4, 4, 5), (5, 4, 6)]
5656

5757
for limit, offset in cases:
58-
expected = assert_data[offset: offset + limit]
58+
expected = assert_data[offset : offset + limit]
5959
self._assert_result(
6060
connection,
6161
select(table).limit(limit).offset(offset),
6262
expected,
6363
)
6464

6565

66-
# class MiscTest(AssertsExecutionResults, AssertsCompiledSQL, fixtures.TablesTest):
67-
68-
# __backend__ = True
69-
70-
# __only_on__ = "iris"
71-
72-
# @classmethod
73-
# def define_tables(cls, metadata):
74-
# Table(
75-
# "some_table",
76-
# metadata,
77-
# Column("id", Integer, primary_key=True),
78-
# Column("x", Integer),
79-
# Column("y", Integer),
80-
# Column("z", String(50)),
81-
# )
82-
83-
# # def test_compile(self):
84-
# # table = self.tables.some_table
85-
86-
# # stmt = select(table.c.id, table.c.x).offset(20).limit(10)
87-
88-
8966
class TransactionTest(fixtures.TablesTest):
9067
__backend__ = True
9168

@@ -134,3 +111,42 @@ def test_rollback(self, local_connection):
134111
transaction.rollback()
135112
result = connection.exec_driver_sql("select * from users")
136113
assert len(result.fetchall()) == 0
114+
115+
116+
class IRISExistsTest(fixtures.TablesTest):
117+
__backend__ = True
118+
119+
@classmethod
120+
def define_tables(cls, metadata):
121+
Table(
122+
"users",
123+
metadata,
124+
Column("user_id", Integer, primary_key=True),
125+
Column("user_name", String(20)),
126+
test_needs_acid=True,
127+
)
128+
129+
@classmethod
130+
def insert_data(cls, connection):
131+
connection.execute(
132+
cls.tables.users.insert(),
133+
[
134+
{"user_id": 1, "user_name": "admin"},
135+
],
136+
)
137+
138+
def test_exists(self):
139+
with config.db.connect() as conn:
140+
with Session(conn) as s:
141+
assert s.query(
142+
select(self.tables.users)
143+
.where(self.tables.users.c.user_name == "admin")
144+
.exists()
145+
).scalar()
146+
147+
assert not s.query(
148+
select(self.tables.users)
149+
.where(self.tables.users.c.user_name == "nope")
150+
.exists()
151+
).scalar()
152+

0 commit comments

Comments
 (0)