],
)
- def _assert_result(self, select, result, params=()):
- with config.db.connect() as conn:
- eq_(conn.execute(select, params).fetchall(), result)
+ def _assert_result(
+ self, connection, select, result, params=(), set_=False
+ ):
+ if set_:
+ query_res = connection.execute(select, params).fetchall()
+ eq_(len(query_res), len(result))
+ eq_(set(query_res), set(result))
+
+ else:
+ eq_(connection.execute(select, params).fetchall(), result)
def _assert_result_str(self, select, result, params=()):
conn = config.db.connect(close_with_result=True)
eq_(conn.exec_driver_sql(select, params).fetchall(), result)
- def test_simple_limit(self):
+ def test_simple_limit(self, connection):
table = self.tables.some_table
+ stmt = select(table).order_by(table.c.id)
self._assert_result(
- select(table).order_by(table.c.id).limit(2),
+ connection,
+ stmt.limit(2),
[(1, 1, 2), (2, 2, 3)],
)
+ self._assert_result(
+ connection,
+ stmt.limit(3),
+ [(1, 1, 2), (2, 2, 3), (3, 3, 4)],
+ )
@testing.requires.fetch_first
- def test_simple_fetch(self):
+ def test_simple_fetch(self, connection):
table = self.tables.some_table
self._assert_result(
+ connection,
select(table).order_by(table.c.id).fetch(2),
[(1, 1, 2), (2, 2, 3)],
)
+ self._assert_result(
+ connection,
+ select(table).order_by(table.c.id).fetch(3),
+ [(1, 1, 2), (2, 2, 3), (3, 3, 4)],
+ )
@testing.requires.offset
- def test_simple_offset(self):
+ def test_simple_offset(self, connection):
table = self.tables.some_table
self._assert_result(
+ connection,
select(table).order_by(table.c.id).offset(2),
[(3, 3, 4), (4, 4, 5), (5, 4, 6)],
)
+ self._assert_result(
+ connection,
+ select(table).order_by(table.c.id).offset(3),
+ [(4, 4, 5), (5, 4, 6)],
+ )
@testing.requires.offset
- def test_simple_limit_offset(self):
+ def test_simple_limit_offset(self, connection):
table = self.tables.some_table
self._assert_result(
+ connection,
select(table).order_by(table.c.id).limit(2).offset(1),
[(2, 2, 3), (3, 3, 4)],
)
+ self._assert_result(
+ connection,
+ select(table).order_by(table.c.id).limit(3).offset(2),
+ [(3, 3, 4), (4, 4, 5), (5, 4, 6)],
+ )
@testing.requires.fetch_first
- def test_simple_fetch_offset(self):
+ def test_simple_fetch_offset(self, connection):
table = self.tables.some_table
self._assert_result(
+ connection,
select(table).order_by(table.c.id).fetch(2).offset(1),
[(2, 2, 3), (3, 3, 4)],
)
+ self._assert_result(
+ connection,
+ select(table).order_by(table.c.id).fetch(3).offset(2),
+ [(3, 3, 4), (4, 4, 5), (5, 4, 6)],
+ )
+
@testing.requires.fetch_no_order_by
- def test_fetch_offset_no_order(self):
+ def test_fetch_offset_no_order(self, connection):
table = self.tables.some_table
- with config.db.connect() as conn:
- eq_(
- set(conn.execute(select(table).fetch(10))),
- set([(1, 1, 2), (2, 2, 3), (3, 3, 4), (4, 4, 5), (5, 4, 6)]),
- )
+ self._assert_result(
+ connection,
+ select(table).fetch(10),
+ [(1, 1, 2), (2, 2, 3), (3, 3, 4), (4, 4, 5), (5, 4, 6)],
+ set_=True,
+ )
+
+ @testing.requires.offset
+ def test_simple_offset_zero(self, connection):
+ table = self.tables.some_table
+ self._assert_result(
+ connection,
+ select(table).order_by(table.c.id).offset(0),
+ [(1, 1, 2), (2, 2, 3), (3, 3, 4), (4, 4, 5), (5, 4, 6)],
+ )
+
+ self._assert_result(
+ connection,
+ select(table).order_by(table.c.id).offset(1),
+ [(2, 2, 3), (3, 3, 4), (4, 4, 5), (5, 4, 6)],
+ )
@testing.requires.offset
def test_limit_offset_nobinds(self):
self._assert_result_str(sql, [(2, 2, 3), (3, 3, 4)])
@testing.requires.bound_limit_offset
- def test_bound_limit(self):
+ def test_bound_limit(self, connection):
table = self.tables.some_table
self._assert_result(
+ connection,
select(table).order_by(table.c.id).limit(bindparam("l")),
[(1, 1, 2), (2, 2, 3)],
params={"l": 2},
)
+ self._assert_result(
+ connection,
+ select(table).order_by(table.c.id).limit(bindparam("l")),
+ [(1, 1, 2), (2, 2, 3), (3, 3, 4)],
+ params={"l": 3},
+ )
+
@testing.requires.bound_limit_offset
- def test_bound_offset(self):
+ def test_bound_offset(self, connection):
table = self.tables.some_table
self._assert_result(
+ connection,
select(table).order_by(table.c.id).offset(bindparam("o")),
[(3, 3, 4), (4, 4, 5), (5, 4, 6)],
params={"o": 2},
)
+ self._assert_result(
+ connection,
+ select(table).order_by(table.c.id).offset(bindparam("o")),
+ [(2, 2, 3), (3, 3, 4), (4, 4, 5), (5, 4, 6)],
+ params={"o": 1},
+ )
+
@testing.requires.bound_limit_offset
- def test_bound_limit_offset(self):
+ def test_bound_limit_offset(self, connection):
table = self.tables.some_table
self._assert_result(
+ connection,
select(table)
.order_by(table.c.id)
.limit(bindparam("l"))
params={"l": 2, "o": 1},
)
+ self._assert_result(
+ connection,
+ select(table)
+ .order_by(table.c.id)
+ .limit(bindparam("l"))
+ .offset(bindparam("o")),
+ [(3, 3, 4), (4, 4, 5), (5, 4, 6)],
+ params={"l": 3, "o": 2},
+ )
+
@testing.requires.fetch_first
- def test_bound_fetch_offset(self):
+ def test_bound_fetch_offset(self, connection):
table = self.tables.some_table
self._assert_result(
+ connection,
select(table)
.order_by(table.c.id)
.fetch(bindparam("f"))
params={"f": 2, "o": 1},
)
+ self._assert_result(
+ connection,
+ select(table)
+ .order_by(table.c.id)
+ .fetch(bindparam("f"))
+ .offset(bindparam("o")),
+ [(3, 3, 4), (4, 4, 5), (5, 4, 6)],
+ params={"f": 3, "o": 2},
+ )
+
@testing.requires.sql_expression_limit_offset
- def test_expr_offset(self):
+ def test_expr_offset(self, connection):
table = self.tables.some_table
self._assert_result(
+ connection,
select(table)
.order_by(table.c.id)
.offset(literal_column("1") + literal_column("2")),
)
@testing.requires.sql_expression_limit_offset
- def test_expr_limit(self):
+ def test_expr_limit(self, connection):
table = self.tables.some_table
self._assert_result(
+ connection,
select(table)
.order_by(table.c.id)
.limit(literal_column("1") + literal_column("2")),
)
@testing.requires.sql_expression_limit_offset
- def test_expr_limit_offset(self):
+ def test_expr_limit_offset(self, connection):
table = self.tables.some_table
self._assert_result(
+ connection,
select(table)
.order_by(table.c.id)
.limit(literal_column("1") + literal_column("1"))
)
@testing.requires.fetch_first
- def test_expr_fetch_offset(self):
+ def test_expr_fetch_offset(self, connection):
table = self.tables.some_table
self._assert_result(
+ connection,
select(table)
.order_by(table.c.id)
.fetch(literal_column("1") + literal_column("1"))
)
@testing.requires.sql_expression_limit_offset
- def test_simple_limit_expr_offset(self):
+ def test_simple_limit_expr_offset(self, connection):
table = self.tables.some_table
self._assert_result(
+ connection,
select(table)
.order_by(table.c.id)
.limit(2)
[(3, 3, 4), (4, 4, 5)],
)
+ self._assert_result(
+ connection,
+ select(table)
+ .order_by(table.c.id)
+ .limit(3)
+ .offset(literal_column("1") + literal_column("1")),
+ [(3, 3, 4), (4, 4, 5), (5, 4, 6)],
+ )
+
@testing.requires.sql_expression_limit_offset
- def test_expr_limit_simple_offset(self):
+ def test_expr_limit_simple_offset(self, connection):
table = self.tables.some_table
self._assert_result(
+ connection,
select(table)
.order_by(table.c.id)
.limit(literal_column("1") + literal_column("1"))
[(3, 3, 4), (4, 4, 5)],
)
+ self._assert_result(
+ connection,
+ select(table)
+ .order_by(table.c.id)
+ .limit(literal_column("1") + literal_column("1"))
+ .offset(1),
+ [(2, 2, 3), (3, 3, 4)],
+ )
+
@testing.requires.fetch_ties
- def test_simple_fetch_ties(self):
+ def test_simple_fetch_ties(self, connection):
table = self.tables.some_table
- with config.db.connect() as conn:
- eq_(
- set(
- conn.execute(
- select(table)
- .order_by(table.c.x.desc())
- .fetch(1, with_ties=True)
- )
- ),
- set([(4, 4, 5), (5, 4, 6)]),
- )
+ self._assert_result(
+ connection,
+ select(table).order_by(table.c.x.desc()).fetch(1, with_ties=True),
+ [(4, 4, 5), (5, 4, 6)],
+ set_=True,
+ )
+
+ self._assert_result(
+ connection,
+ select(table).order_by(table.c.x.desc()).fetch(3, with_ties=True),
+ [(3, 3, 4), (4, 4, 5), (5, 4, 6)],
+ set_=True,
+ )
@testing.requires.fetch_ties
@testing.requires.fetch_offset_with_options
- def test_fetch_offset_ties(self):
+ def test_fetch_offset_ties(self, connection):
table = self.tables.some_table
- with config.db.connect() as conn:
- fa = conn.execute(
- select(table)
- .order_by(table.c.x)
- .fetch(2, with_ties=True)
- .offset(2)
- ).fetchall()
- eq_(fa[0], (3, 3, 4))
- eq_(set(fa), set([(3, 3, 4), (4, 4, 5), (5, 4, 6)]))
+ fa = connection.execute(
+ select(table)
+ .order_by(table.c.x)
+ .fetch(2, with_ties=True)
+ .offset(2)
+ ).fetchall()
+ eq_(fa[0], (3, 3, 4))
+ eq_(set(fa), set([(3, 3, 4), (4, 4, 5), (5, 4, 6)]))
@testing.requires.fetch_ties
@testing.requires.fetch_offset_with_options
- def test_fetch_offset_ties_exact_number(self):
+ def test_fetch_offset_ties_exact_number(self, connection):
table = self.tables.some_table
self._assert_result(
+ connection,
select(table)
.order_by(table.c.x)
.fetch(2, with_ties=True)
[(2, 2, 3), (3, 3, 4)],
)
+ self._assert_result(
+ connection,
+ select(table)
+ .order_by(table.c.x)
+ .fetch(3, with_ties=True)
+ .offset(3),
+ [(4, 4, 5), (5, 4, 6)],
+ )
+
@testing.requires.fetch_percent
- def test_simple_fetch_percent(self):
+ def test_simple_fetch_percent(self, connection):
table = self.tables.some_table
self._assert_result(
+ connection,
select(table).order_by(table.c.id).fetch(20, percent=True),
[(1, 1, 2)],
)
@testing.requires.fetch_percent
@testing.requires.fetch_offset_with_options
- def test_fetch_offset_percent(self):
+ def test_fetch_offset_percent(self, connection):
table = self.tables.some_table
self._assert_result(
+ connection,
select(table)
.order_by(table.c.id)
.fetch(40, percent=True)
@testing.requires.fetch_ties
@testing.requires.fetch_percent
- def test_simple_fetch_percent_ties(self):
+ def test_simple_fetch_percent_ties(self, connection):
table = self.tables.some_table
- with config.db.connect() as conn:
- fa = conn.execute(
- select(table)
- .order_by(table.c.x.desc())
- .fetch(20, percent=True, with_ties=True)
- ).fetchall()
-
- eq_(len(fa), 2)
- eq_(set(fa), set([(4, 4, 5), (5, 4, 6)]))
+ self._assert_result(
+ connection,
+ select(table)
+ .order_by(table.c.x.desc())
+ .fetch(20, percent=True, with_ties=True),
+ [(4, 4, 5), (5, 4, 6)],
+ set_=True,
+ )
@testing.requires.fetch_ties
@testing.requires.fetch_percent
@testing.requires.fetch_offset_with_options
- def test_fetch_offset_percent_ties(self):
+ def test_fetch_offset_percent_ties(self, connection):
table = self.tables.some_table
- with config.db.connect() as conn:
- fa = conn.execute(
- select(table)
- .order_by(table.c.x)
- .fetch(40, percent=True, with_ties=True)
- .offset(2)
- ).fetchall()
- eq_(fa[0], (3, 3, 4))
- eq_(set(fa), set([(3, 3, 4), (4, 4, 5), (5, 4, 6)]))
+ fa = connection.execute(
+ select(table)
+ .order_by(table.c.x)
+ .fetch(40, percent=True, with_ties=True)
+ .offset(2)
+ ).fetchall()
+ eq_(fa[0], (3, 3, 4))
+ eq_(set(fa), set([(3, 3, 4), (4, 4, 5), (5, 4, 6)]))
class JoinTest(fixtures.TablesTest):
"anon_2.col2 AS col2, ROWNUM AS ora_rn FROM (SELECT "
"sometable.col1 AS col1, sometable.col2 AS "
"col2 FROM sometable) anon_2 WHERE ROWNUM <= "
- "[POSTCOMPILE_param_1]) anon_1 WHERE ora_rn > "
+ "[POSTCOMPILE_param_1] + [POSTCOMPILE_param_2]) anon_1 "
+ "WHERE ora_rn > "
"[POSTCOMPILE_param_2]",
- checkparams={"param_1": 30, "param_2": 20},
+ checkparams={"param_1": 10, "param_2": 20},
)
c = s.compile(dialect=oracle.OracleDialect())
self.assert_compile(
s,
"SELECT anon_1.col1, anon_1.col2 FROM "
- "(SELECT /*+ FIRST_ROWS([POSTCOMPILE_ora_frow_1]) */ "
+ "(SELECT /*+ FIRST_ROWS([POSTCOMPILE_param_1]) */ "
"anon_2.col1 AS col1, "
"anon_2.col2 AS col2, ROWNUM AS ora_rn FROM (SELECT "
"sometable.col1 AS col1, sometable.col2 AS "
"col2 FROM sometable) anon_2 WHERE ROWNUM <= "
- "[POSTCOMPILE_param_1]) anon_1 WHERE ora_rn > "
+ "[POSTCOMPILE_param_1] + [POSTCOMPILE_param_2]) anon_1 "
+ "WHERE ora_rn > "
"[POSTCOMPILE_param_2]",
- checkparams={"ora_frow_1": 10, "param_1": 30, "param_2": 20},
+ checkparams={"param_1": 10, "param_2": 20},
dialect=oracle.OracleDialect(optimize_limits=True),
)
"ROWNUM AS ora_rn "
"FROM (SELECT sometable.col1 AS col1, "
"sometable.col2 AS col2 FROM sometable) anon_3 "
- "WHERE ROWNUM <= [POSTCOMPILE_param_1]) anon_2 "
+ "WHERE ROWNUM <= [POSTCOMPILE_param_1] + [POSTCOMPILE_param_2]) "
+ "anon_2 "
"WHERE ora_rn > [POSTCOMPILE_param_2]) anon_1",
- checkparams={"param_1": 30, "param_2": 20},
+ checkparams={"param_1": 10, "param_2": 20},
)
self.assert_compile(
"ROWNUM AS ora_rn "
"FROM (SELECT sometable.col1 AS col1, "
"sometable.col2 AS col2 FROM sometable) anon_3 "
- "WHERE ROWNUM <= [POSTCOMPILE_param_1]) anon_2 "
+ "WHERE ROWNUM <= [POSTCOMPILE_param_1] + [POSTCOMPILE_param_2]) "
+ "anon_2 "
"WHERE ora_rn > [POSTCOMPILE_param_2]) anon_1",
)
c = s2.compile(dialect=oracle.OracleDialect())
"sometable.col1 AS col1, sometable.col2 AS "
"col2 FROM sometable ORDER BY "
"sometable.col2) anon_2 WHERE ROWNUM <= "
- "[POSTCOMPILE_param_1]) anon_1 "
+ "[POSTCOMPILE_param_1] + [POSTCOMPILE_param_2]) anon_1 "
"WHERE ora_rn > [POSTCOMPILE_param_2]",
- checkparams={"param_1": 30, "param_2": 20},
+ checkparams={"param_1": 10, "param_2": 20},
)
c = s.compile(dialect=oracle.OracleDialect())
eq_(len(c._result_columns), 2)
s = select(t).with_for_update().limit(10).order_by(t.c.col2)
self.assert_compile(
s,
- "SELECT /*+ FIRST_ROWS([POSTCOMPILE_ora_frow_1]) */ "
+ "SELECT /*+ FIRST_ROWS([POSTCOMPILE_param_1]) */ "
"anon_1.col1, anon_1.col2 FROM (SELECT "
"sometable.col1 AS col1, sometable.col2 AS "
"col2 FROM sometable ORDER BY "
"sometable.col2) anon_1 WHERE ROWNUM <= [POSTCOMPILE_param_1] "
"FOR UPDATE",
- checkparams={"param_1": 10, "ora_frow_1": 10},
+ checkparams={"param_1": 10},
dialect=oracle.OracleDialect(optimize_limits=True),
)
"sometable.col1 AS col1, sometable.col2 AS "
"col2 FROM sometable ORDER BY "
"sometable.col2) anon_2 WHERE ROWNUM <= "
- "[POSTCOMPILE_param_1]) anon_1 "
+ "[POSTCOMPILE_param_1] + [POSTCOMPILE_param_2]) anon_1 "
"WHERE ora_rn > [POSTCOMPILE_param_2] FOR "
"UPDATE",
- checkparams={"param_1": 30, "param_2": 20},
+ checkparams={"param_1": 10, "param_2": 20},
)
def test_limit_six(self):
"col1, anon_2.col2 AS col2, ROWNUM AS ora_rn FROM "
"(SELECT sometable.col1 AS col1, sometable.col2 AS col2 "
"FROM sometable ORDER BY sometable.col2) anon_2 WHERE "
- "ROWNUM <= :param_1 + :param_2 + :param_3) anon_1 "
+ "ROWNUM <= [POSTCOMPILE_param_1] + :param_2 + :param_3) anon_1 "
"WHERE ora_rn > :param_2 + :param_3",
checkparams={"param_1": 10, "param_2": 10, "param_3": 20},
)
"ROWNUM AS ora_rn "
"FROM (SELECT mytable.myid AS myid, mytable.name AS name "
"FROM mytable WHERE mytable.myid = :myid_1) anon_2 "
- "WHERE ROWNUM <= [POSTCOMPILE_param_1]) anon_1 "
+ "WHERE ROWNUM <= [POSTCOMPILE_param_1] + [POSTCOMPILE_param_2]) "
+ "anon_1 "
"WHERE ora_rn > [POSTCOMPILE_param_2] "
"FOR UPDATE OF anon_1.name NOWAIT",
- checkparams={"param_1": 60, "param_2": 50, "myid_1": 7},
+ checkparams={"param_1": 10, "param_2": 50, "myid_1": 7},
)
def test_for_update_of_w_limit_offset_adaption_col_unpresent(self):
"ROWNUM AS ora_rn, anon_2.name AS name "
"FROM (SELECT mytable.myid AS myid, mytable.name AS name "
"FROM mytable WHERE mytable.myid = :myid_1) anon_2 "
- "WHERE ROWNUM <= [POSTCOMPILE_param_1]) anon_1 "
+ "WHERE "
+ "ROWNUM <= [POSTCOMPILE_param_1] + [POSTCOMPILE_param_2]) anon_1 "
"WHERE ora_rn > [POSTCOMPILE_param_2] "
"FOR UPDATE OF anon_1.name NOWAIT",
- checkparams={"param_1": 60, "param_2": 50, "myid_1": 7},
+ checkparams={"param_1": 10, "param_2": 50, "myid_1": 7},
)
def test_for_update_of_w_limit_offset_adaption_partial_col_unpresent(self):
"mytable.bar AS bar, "
"mytable.foo AS foo FROM mytable "
"WHERE mytable.myid = :myid_1) anon_2 "
- "WHERE ROWNUM <= [POSTCOMPILE_param_1]) anon_1 "
+ "WHERE ROWNUM <= [POSTCOMPILE_param_1] + [POSTCOMPILE_param_2]) "
+ "anon_1 "
"WHERE ora_rn > [POSTCOMPILE_param_2] "
"FOR UPDATE OF anon_1.foo, anon_1.bar NOWAIT",
- checkparams={"param_1": 60, "param_2": 50, "myid_1": 7},
+ checkparams={"param_1": 10, "param_2": 50, "myid_1": 7},
)
def test_limit_preserves_typing_information(self):
"anon_2.col1 AS col1, anon_2.col2 AS col2, ROWNUM AS ora_rn "
"FROM (SELECT sometable.col1 AS col1, sometable.col2 AS col2 "
"FROM sometable) anon_2 "
- "WHERE ROWNUM <= [POSTCOMPILE_param_1]) anon_1 "
+ "WHERE ROWNUM <= [POSTCOMPILE_param_1] + "
+ "[POSTCOMPILE_param_2]) anon_1 "
"WHERE ora_rn > [POSTCOMPILE_param_2]",
dialect=dialect,
)
"ROWNUM AS ora_rn "
"FROM (SELECT sometable.col1 AS col1, sometable.col2 AS col2 "
"FROM sometable) anon_2 "
- "WHERE ROWNUM <= [POSTCOMPILE_param_1]) anon_1 "
+ "WHERE ROWNUM <= [POSTCOMPILE_param_1] + "
+ "[POSTCOMPILE_param_2]) anon_1 "
"WHERE ora_rn > [POSTCOMPILE_param_2]",
dialect=dialect,
- checkparams={"param_1": 20, "param_2": 10},
+ checkparams={"param_1": 10, "param_2": 10},
)
def test_long_labels_legacy_ident_length(self):
"thirdtable.userid(+) = "
"myothertable.otherid AND mytable.myid = "
"myothertable.otherid ORDER BY mytable.name) anon_2 "
- "WHERE ROWNUM <= [POSTCOMPILE_param_1]) anon_1 "
+ "WHERE ROWNUM <= [POSTCOMPILE_param_1] + [POSTCOMPILE_param_2]) "
+ "anon_1 "
"WHERE ora_rn > [POSTCOMPILE_param_2]",
- checkparams={"param_1": 15, "param_2": 5},
+ checkparams={"param_1": 10, "param_2": 5},
dialect=oracle.dialect(use_ansi=False),
)