Beispiel #1
0
 def test_alias(self):
     t = table("sometable", column("col1"), column("col2"))
     s = select([t.alias()])
     self.assert_compile(
         s,
         "SELECT sometable_1.col1, sometable_1.col2 "
         "FROM sometable AS sometable_1",
     )
     dialect = firebird.FBDialect()
     dialect._version_two = False
     self.assert_compile(
         s,
         "SELECT sometable_1.col1, sometable_1.col2 "
         "FROM sometable sometable_1",
         dialect=dialect,
     )
Beispiel #2
0
    def teststringadapt(self):
        """test that String with no size becomes TEXT, *all* others stay as varchar/String"""

        oracle_dialect = oracle.OracleDialect()
        mysql_dialect = mysql.MySQLDialect()
        postgres_dialect = postgres.PGDialect()
        firebird_dialect = firebird.FBDialect()

        for dialect, start, test in [
            (oracle_dialect, String(), oracle.OracleString),
            (oracle_dialect, VARCHAR(), oracle.OracleString),
            (oracle_dialect, String(50), oracle.OracleString),
            (oracle_dialect, Unicode(), oracle.OracleString),
            (oracle_dialect, UnicodeText(), oracle.OracleText),
            (oracle_dialect, NCHAR(), oracle.OracleString),
            (oracle_dialect, oracle.OracleRaw(50), oracle.OracleRaw),
            (mysql_dialect, String(), mysql.MSString),
            (mysql_dialect, VARCHAR(), mysql.MSString),
            (mysql_dialect, String(50), mysql.MSString),
            (mysql_dialect, Unicode(), mysql.MSString),
            (mysql_dialect, UnicodeText(), mysql.MSText),
            (mysql_dialect, NCHAR(), mysql.MSNChar),
            (postgres_dialect, String(), postgres.PGString),
            (postgres_dialect, VARCHAR(), postgres.PGString),
            (postgres_dialect, String(50), postgres.PGString),
            (postgres_dialect, Unicode(), postgres.PGString),
            (postgres_dialect, UnicodeText(), postgres.PGText),
            (postgres_dialect, NCHAR(), postgres.PGString),
            (firebird_dialect, String(), firebird.FBString),
            (firebird_dialect, VARCHAR(), firebird.FBString),
            (firebird_dialect, String(50), firebird.FBString),
            (firebird_dialect, Unicode(), firebird.FBString),
            (firebird_dialect, UnicodeText(), firebird.FBText),
            (firebird_dialect, NCHAR(), firebird.FBString),
        ]:
            assert isinstance(
                start.dialect_impl(dialect),
                test), "wanted %r got %r" % (test, start.dialect_impl(dialect))
Beispiel #3
0
class CompileTest(fixtures.TestBase, AssertsCompiledSQL):

    __dialect__ = firebird.FBDialect()

    def test_alias(self):
        t = table("sometable", column("col1"), column("col2"))
        s = select([t.alias()])
        self.assert_compile(
            s,
            "SELECT sometable_1.col1, sometable_1.col2 "
            "FROM sometable AS sometable_1",
        )
        dialect = firebird.FBDialect()
        dialect._version_two = False
        self.assert_compile(
            s,
            "SELECT sometable_1.col1, sometable_1.col2 "
            "FROM sometable sometable_1",
            dialect=dialect,
        )

    def test_varchar_raise(self):
        for type_ in (
                String,
                VARCHAR,
                String(),
                VARCHAR(),
                Unicode,
                Unicode(),
        ):
            type_ = sqltypes.to_instance(type_)
            assert_raises_message(
                exc.CompileError,
                "VARCHAR requires a length on dialect firebird",
                type_.compile,
                dialect=firebird.dialect(),
            )

            t1 = Table("sometable", MetaData(), Column("somecolumn", type_))
            assert_raises_message(
                exc.CompileError,
                r"\(in table 'sometable', column 'somecolumn'\)\: "
                r"(?:N)?VARCHAR requires a length on dialect firebird",
                schema.CreateTable(t1).compile,
                dialect=firebird.dialect(),
            )

    def test_function(self):
        self.assert_compile(func.foo(1, 2), "foo(:foo_1, :foo_2)")
        self.assert_compile(func.current_time(), "CURRENT_TIME")
        self.assert_compile(func.foo(), "foo")
        m = MetaData()
        t = Table("sometable", m, Column("col1", Integer),
                  Column("col2", Integer))
        self.assert_compile(
            select([func.max(t.c.col1)]),
            "SELECT max(sometable.col1) AS max_1 FROM "
            "sometable",
        )

    def test_substring(self):
        self.assert_compile(
            func.substring("abc", 1, 2),
            "SUBSTRING(:substring_1 FROM :substring_2 "
            "FOR :substring_3)",
        )
        self.assert_compile(
            func.substring("abc", 1),
            "SUBSTRING(:substring_1 FROM :substring_2)",
        )

    def test_update_returning(self):
        table1 = table(
            "mytable",
            column("myid", Integer),
            column("name", String(128)),
            column("description", String(128)),
        )
        u = update(table1,
                   values=dict(name="foo")).returning(table1.c.myid,
                                                      table1.c.name)
        self.assert_compile(
            u,
            "UPDATE mytable SET name=:name RETURNING "
            "mytable.myid, mytable.name",
        )
        u = update(table1, values=dict(name="foo")).returning(table1)
        self.assert_compile(
            u,
            "UPDATE mytable SET name=:name RETURNING "
            "mytable.myid, mytable.name, "
            "mytable.description",
        )
        u = update(table1, values=dict(name="foo")).returning(
            func.length(table1.c.name))
        self.assert_compile(
            u,
            "UPDATE mytable SET name=:name RETURNING "
            "char_length(mytable.name) AS length_1",
        )

    def test_insert_returning(self):
        table1 = table(
            "mytable",
            column("myid", Integer),
            column("name", String(128)),
            column("description", String(128)),
        )
        i = insert(table1,
                   values=dict(name="foo")).returning(table1.c.myid,
                                                      table1.c.name)
        self.assert_compile(
            i,
            "INSERT INTO mytable (name) VALUES (:name) "
            "RETURNING mytable.myid, mytable.name",
        )
        i = insert(table1, values=dict(name="foo")).returning(table1)
        self.assert_compile(
            i,
            "INSERT INTO mytable (name) VALUES (:name) "
            "RETURNING mytable.myid, mytable.name, "
            "mytable.description",
        )
        i = insert(table1, values=dict(name="foo")).returning(
            func.length(table1.c.name))
        self.assert_compile(
            i,
            "INSERT INTO mytable (name) VALUES (:name) "
            "RETURNING char_length(mytable.name) AS "
            "length_1",
        )

    def test_charset(self):
        """Exercise CHARACTER SET  options on string types."""

        columns = [
            (firebird.CHAR, [1], {}, "CHAR(1)"),
            (
                firebird.CHAR,
                [1],
                {
                    "charset": "OCTETS"
                },
                "CHAR(1) CHARACTER SET OCTETS",
            ),
            (firebird.VARCHAR, [1], {}, "VARCHAR(1)"),
            (
                firebird.VARCHAR,
                [1],
                {
                    "charset": "OCTETS"
                },
                "VARCHAR(1) CHARACTER SET OCTETS",
            ),
        ]
        for type_, args, kw, res in columns:
            self.assert_compile(type_(*args, **kw), res)

    def test_quoting_initial_chars(self):
        self.assert_compile(column("_somecol"), '"_somecol"')
        self.assert_compile(column("$somecol"), '"$somecol"')

    @testing.combinations(("no_persisted", "ignore"), ("persisted_none", None),
                          id_="ia")
    def test_column_computed(self, persisted):
        m = MetaData()
        kwargs = {"persisted": persisted} if persisted != "ignore" else {}
        t = Table(
            "t",
            m,
            Column("x", Integer),
            Column("y", Integer, Computed("x + 2", **kwargs)),
        )
        self.assert_compile(
            schema.CreateTable(t),
            "CREATE TABLE t (x INTEGER, y INTEGER GENERATED "
            "ALWAYS AS (x + 2))",
        )

    @testing.combinations(("persisted_true", True), ("persisted_false", False),
                          id_="ia")
    def test_column_computed_raises(self, persisted):
        m = MetaData()
        t = Table(
            "t",
            m,
            Column("x", Integer),
            Column("y", Integer, Computed("x + 2", persisted=persisted)),
        )
        assert_raises_message(
            exc.CompileError,
            "Firebird computed columns do not support a persistence method",
            schema.CreateTable(t).compile,
            dialect=firebird.dialect(),
        )
Beispiel #4
0
class CompileTest(TestBase, AssertsCompiledSQL):
    __dialect__ = firebird.FBDialect()

    def test_alias(self):
        t = table('sometable', column('col1'), column('col2'))
        s = select([t.alias()])
        self.assert_compile(
            s,
            "SELECT sometable_1.col1, sometable_1.col2 FROM sometable sometable_1"
        )

    def test_function(self):
        self.assert_compile(func.foo(1, 2), "foo(:foo_1, :foo_2)")
        self.assert_compile(func.current_time(), "CURRENT_TIME")
        self.assert_compile(func.foo(), "foo")

        m = MetaData()
        t = Table('sometable', m, Column('col1', Integer),
                  Column('col2', Integer))
        self.assert_compile(
            select([func.max(t.c.col1)]),
            "SELECT max(sometable.col1) AS max_1 FROM sometable")

    def test_substring(self):
        self.assert_compile(
            func.substring('abc', 1, 2),
            "SUBSTRING(:substring_1 FROM :substring_2 FOR :substring_3)")
        self.assert_compile(func.substring('abc', 1),
                            "SUBSTRING(:substring_1 FROM :substring_2)")

    def test_update_returning(self):
        table1 = table(
            'mytable',
            column('myid', Integer),
            column('name', String(128)),
            column('description', String(128)),
        )

        u = update(table1,
                   values=dict(name='foo'),
                   firebird_returning=[table1.c.myid, table1.c.name])
        self.assert_compile(
            u,
            "UPDATE mytable SET name=:name RETURNING mytable.myid, mytable.name"
        )

        u = update(table1,
                   values=dict(name='foo'),
                   firebird_returning=[table1])
        self.assert_compile(u, "UPDATE mytable SET name=:name "\
            "RETURNING mytable.myid, mytable.name, mytable.description")

        u = update(table1,
                   values=dict(name='foo'),
                   firebird_returning=[func.length(table1.c.name)])
        self.assert_compile(
            u,
            "UPDATE mytable SET name=:name RETURNING char_length(mytable.name)"
        )

    def test_insert_returning(self):
        table1 = table(
            'mytable',
            column('myid', Integer),
            column('name', String(128)),
            column('description', String(128)),
        )

        i = insert(table1,
                   values=dict(name='foo'),
                   firebird_returning=[table1.c.myid, table1.c.name])
        self.assert_compile(
            i,
            "INSERT INTO mytable (name) VALUES (:name) RETURNING mytable.myid, mytable.name"
        )

        i = insert(table1,
                   values=dict(name='foo'),
                   firebird_returning=[table1])
        self.assert_compile(i, "INSERT INTO mytable (name) VALUES (:name) "\
            "RETURNING mytable.myid, mytable.name, mytable.description")

        i = insert(table1,
                   values=dict(name='foo'),
                   firebird_returning=[func.length(table1.c.name)])
        self.assert_compile(
            i,
            "INSERT INTO mytable (name) VALUES (:name) RETURNING char_length(mytable.name)"
        )