⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 select.py

📁 SQLAlchemy. 经典的Python ORM框架。学习必看。
💻 PY
📖 第 1 页 / 共 5 页
字号:
import testenv; testenv.configure_for_tests()import datetime, re, operatorfrom sqlalchemy import *from sqlalchemy import exceptions, sql, utilfrom sqlalchemy.sql import table, columnfrom sqlalchemy.databases import sqlite, postgres, mysql, oracle, firebird, mssqlfrom testlib import *table1 = table('mytable',    column('myid', Integer),    column('name', String),    column('description', String),)table2 = table(    'myothertable',    column('otherid', Integer),    column('othername', String),)table3 = table(    'thirdtable',    column('userid', Integer),    column('otherstuff', String),)metadata = MetaData()table4 = Table(    'remotetable', metadata,    Column('rem_id', Integer, primary_key=True),    Column('datatype_id', Integer),    Column('value', String(20)),    schema = 'remote_owner')users = table('users',    column('user_id'),    column('user_name'),    column('password'),)addresses = table('addresses',    column('address_id'),    column('user_id'),    column('street'),    column('city'),    column('state'),    column('zip'))class SelectTest(TestBase, AssertsCompiledSQL):    def test_attribute_sanity(self):        assert hasattr(table1, 'c')        assert hasattr(table1.select(), 'c')        assert not hasattr(table1.c.myid.self_group(), 'columns')        assert hasattr(table1.select().self_group(), 'columns')        assert not hasattr(select([table1.c.myid]).as_scalar().self_group(), 'columns')        assert not hasattr(table1.c.myid, 'columns')        assert not hasattr(table1.c.myid, 'c')        assert not hasattr(table1.select().c.myid, 'c')        assert not hasattr(table1.select().c.myid, 'columns')        assert not hasattr(table1.alias().c.myid, 'columns')        assert not hasattr(table1.alias().c.myid, 'c')    def test_table_select(self):        self.assert_compile(table1.select(), "SELECT mytable.myid, mytable.name, mytable.description FROM mytable")        self.assert_compile(select([table1, table2]), "SELECT mytable.myid, mytable.name, mytable.description, myothertable.otherid, \myothertable.othername FROM mytable, myothertable")    def test_from_subquery(self):        """tests placing select statements in the column clause of another select, for the        purposes of selecting from the exported columns of that select."""                s = select([table1], table1.c.name == 'jack')        self.assert_compile(            select(                [s],                s.c.myid == 7            )            ,        "SELECT myid, name, description FROM (SELECT mytable.myid AS myid, mytable.name AS name, mytable.description AS description FROM mytable "\        "WHERE mytable.name = :mytable_name_1) WHERE myid = :myid_1")        sq = select([table1])        self.assert_compile(            sq.select(),            "SELECT myid, name, description FROM (SELECT mytable.myid AS myid, mytable.name AS name, mytable.description AS description FROM mytable)"        )        sq = select(            [table1],        ).alias('sq')        self.assert_compile(            sq.select(sq.c.myid == 7),            "SELECT sq.myid, sq.name, sq.description FROM \(SELECT mytable.myid AS myid, mytable.name AS name, mytable.description AS description FROM mytable) AS sq WHERE sq.myid = :sq_myid_1"        )        sq = select(            [table1, table2],            and_(table1.c.myid ==7, table2.c.otherid==table1.c.myid),            use_labels = True        ).alias('sq')        sqstring = "SELECT mytable.myid AS mytable_myid, mytable.name AS mytable_name, \mytable.description AS mytable_description, myothertable.otherid AS myothertable_otherid, \myothertable.othername AS myothertable_othername FROM mytable, myothertable \WHERE mytable.myid = :mytable_myid_1 AND myothertable.otherid = mytable.myid"        self.assert_compile(sq.select(), "SELECT sq.mytable_myid, sq.mytable_name, sq.mytable_description, sq.myothertable_otherid, \sq.myothertable_othername FROM (" + sqstring + ") AS sq")        sq2 = select(            [sq],            use_labels = True        ).alias('sq2')        self.assert_compile(sq2.select(), "SELECT sq2.sq_mytable_myid, sq2.sq_mytable_name, sq2.sq_mytable_description, \sq2.sq_myothertable_otherid, sq2.sq_myothertable_othername FROM \(SELECT sq.mytable_myid AS sq_mytable_myid, sq.mytable_name AS sq_mytable_name, \sq.mytable_description AS sq_mytable_description, sq.myothertable_otherid AS sq_myothertable_otherid, \sq.myothertable_othername AS sq_myothertable_othername FROM (" + sqstring + ") AS sq) AS sq2")    def test_nested_uselabels(self):        """test nested anonymous label generation.  this        essentially tests the ANONYMOUS_LABEL regex.        """        s1 = table1.select()        s2 = s1.alias()        s3 = select([s2], use_labels=True)        s4 = s3.alias()        s5 = select([s4], use_labels=True)        self.assert_compile(s5, "SELECT anon_1.anon_2_myid AS anon_1_anon_2_myid, anon_1.anon_2_name AS anon_1_anon_2_name, "\        "anon_1.anon_2_description AS anon_1_anon_2_description FROM (SELECT anon_2.myid AS anon_2_myid, anon_2.name AS anon_2_name, "\        "anon_2.description AS anon_2_description FROM (SELECT mytable.myid AS myid, mytable.name AS name, mytable.description "\        "AS description FROM mytable) AS anon_2) AS anon_1")    def test_dont_overcorrelate(self):        self.assert_compile(select([table1], from_obj=[table1, table1.select()]), """SELECT mytable.myid, mytable.name, mytable.description FROM mytable, (SELECT mytable.myid AS myid, mytable.name AS name, mytable.description AS description FROM mytable)""")    def test_exists(self):        self.assert_compile(exists([table1.c.myid], table1.c.myid==5).select(), "SELECT EXISTS (SELECT mytable.myid FROM mytable WHERE mytable.myid = :mytable_myid_1)", params={'mytable_myid':5})        self.assert_compile(select([table1, exists([1], from_obj=table2)]), "SELECT mytable.myid, mytable.name, mytable.description, EXISTS (SELECT 1 FROM myothertable) FROM mytable", params={})        self.assert_compile(select([table1, exists([1], from_obj=table2).label('foo')]), "SELECT mytable.myid, mytable.name, mytable.description, EXISTS (SELECT 1 FROM myothertable) AS foo FROM mytable", params={})        self.assert_compile(          table1.select(exists([1], table2.c.otherid == table1.c.myid).correlate(table1)),          "SELECT mytable.myid, mytable.name, mytable.description FROM mytable WHERE EXISTS (SELECT 1 FROM myothertable WHERE myothertable.otherid = mytable.myid)"        )        self.assert_compile(          table1.select(exists([1]).where(table2.c.otherid == table1.c.myid).correlate(table1)),          "SELECT mytable.myid, mytable.name, mytable.description FROM mytable WHERE EXISTS (SELECT 1 FROM myothertable WHERE myothertable.otherid = mytable.myid)"        )        self.assert_compile(          table1.select(exists([1]).where(table2.c.otherid == table1.c.myid).correlate(table1)).replace_selectable(table2, table2.alias()),          "SELECT mytable.myid, mytable.name, mytable.description FROM mytable WHERE EXISTS (SELECT 1 FROM myothertable AS myothertable_1 WHERE myothertable_1.otherid = mytable.myid)"        )        self.assert_compile(          table1.select(exists([1]).where(table2.c.otherid == table1.c.myid).correlate(table1)).select_from(table1.join(table2, table1.c.myid==table2.c.otherid)).replace_selectable(table2, table2.alias()),          "SELECT mytable.myid, mytable.name, mytable.description FROM mytable JOIN myothertable AS myothertable_1 ON mytable.myid = myothertable_1.otherid WHERE EXISTS (SELECT 1 FROM myothertable AS myothertable_1 WHERE myothertable_1.otherid = mytable.myid)"        )    def test_where_subquery(self):        s = select([addresses.c.street], addresses.c.user_id==users.c.user_id, correlate=True).alias('s')        self.assert_compile(            select([users, s.c.street], from_obj=s),            """SELECT users.user_id, users.user_name, users.password, s.street FROM users, (SELECT addresses.street AS street FROM addresses WHERE addresses.user_id = users.user_id) AS s""")        self.assert_compile(            table1.select(table1.c.myid == select([table1.c.myid], table1.c.name=='jack')),            "SELECT mytable.myid, mytable.name, mytable.description FROM mytable WHERE mytable.myid = (SELECT mytable.myid FROM mytable WHERE mytable.name = :mytable_name_1)"        )        self.assert_compile(            table1.select(table1.c.myid == select([table2.c.otherid], table1.c.name == table2.c.othername)),            "SELECT mytable.myid, mytable.name, mytable.description FROM mytable WHERE mytable.myid = (SELECT myothertable.otherid FROM myothertable WHERE mytable.name = myothertable.othername)"        )        self.assert_compile(            table1.select(exists([1], table2.c.otherid == table1.c.myid)),            "SELECT mytable.myid, mytable.name, mytable.description FROM mytable WHERE EXISTS (SELECT 1 FROM myothertable WHERE myothertable.otherid = mytable.myid)"        )        talias = table1.alias('ta')        s = subquery('sq2', [talias], exists([1], table2.c.otherid == talias.c.myid))        self.assert_compile(            select([s, table1])            ,"SELECT sq2.myid, sq2.name, sq2.description, mytable.myid, mytable.name, mytable.description FROM (SELECT ta.myid AS myid, ta.name AS name, ta.description AS description FROM mytable AS ta WHERE EXISTS (SELECT 1 FROM myothertable WHERE myothertable.otherid = ta.myid)) AS sq2, mytable")        s = select([addresses.c.street], addresses.c.user_id==users.c.user_id, correlate=True).alias('s')        self.assert_compile(            select([users, s.c.street], from_obj=s),            """SELECT users.user_id, users.user_name, users.password, s.street FROM users, (SELECT addresses.street AS street FROM addresses WHERE addresses.user_id = users.user_id) AS s""")        # test constructing the outer query via append_column(), which occurs in the ORM's Query object        s = select([], exists([1], table2.c.otherid==table1.c.myid), from_obj=table1)        s.append_column(table1)        self.assert_compile(            s,            "SELECT mytable.myid, mytable.name, mytable.description FROM mytable WHERE EXISTS (SELECT 1 FROM myothertable WHERE myothertable.otherid = mytable.myid)"        )    def test_orderby_subquery(self):        self.assert_compile(            table1.select(order_by=[select([table2.c.otherid], table1.c.myid==table2.c.otherid)]),            "SELECT mytable.myid, mytable.name, mytable.description FROM mytable ORDER BY (SELECT myothertable.otherid FROM myothertable WHERE mytable.myid = myothertable.otherid)"        )        self.assert_compile(            table1.select(order_by=[desc(select([table2.c.otherid], table1.c.myid==table2.c.otherid))]),            "SELECT mytable.myid, mytable.name, mytable.description FROM mytable ORDER BY (SELECT myothertable.otherid FROM myothertable WHERE mytable.myid = myothertable.otherid) DESC"        )    @testing.uses_deprecated('scalar option')    def test_scalar_select(self):        try:            s = select([table1.c.myid, table1.c.name]).as_scalar()            assert False        except exceptions.InvalidRequestError, err:            assert str(err) == "Scalar select can only be created from a Select object that has exactly one column expression.", str(err)        try:            # generic function which will look at the type of expression            func.coalesce(select([table1.c.myid]))

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -