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

📄 select.py

📁 SQLAlchemy. 经典的Python ORM框架。学习必看。
💻 PY
📖 第 1 页 / 共 5 页
字号:
     value_tbl.c.val1)/value_tbl.c.val1 > 2.0),             "SELECT values.id FROM values WHERE (values.val2 - values.val1) / values.val1 > :param_1"         )         self.assert_compile(             select([value_tbl.c.id], value_tbl.c.val1 / (value_tbl.c.val2 - value_tbl.c.val1) /value_tbl.c.val1 > 2.0),             "SELECT values.id FROM values WHERE values.val1 / (values.val2 - values.val1) / values.val1 > :param_1"         )    def test_extract(self):        """test the EXTRACT function"""        self.assert_compile(select([extract("month", table3.c.otherstuff)]), "SELECT extract(month FROM thirdtable.otherstuff) AS extract_1 FROM thirdtable")        self.assert_compile(select([extract("day", func.to_date("03/20/2005", "MM/DD/YYYY"))]), "SELECT extract(day FROM to_date(:to_date_1, :to_date_2)) AS extract_1")    def test_joins(self):        self.assert_compile(            join(table2, table1, table1.c.myid == table2.c.otherid).select(),            "SELECT myothertable.otherid, myothertable.othername, mytable.myid, mytable.name, \mytable.description FROM myothertable JOIN mytable ON mytable.myid = myothertable.otherid"        )        self.assert_compile(            select(             [table1],                from_obj = [join(table1, table2, table1.c.myid == table2.c.otherid)]            ),        "SELECT mytable.myid, mytable.name, mytable.description FROM mytable JOIN myothertable ON mytable.myid = myothertable.otherid")        self.assert_compile(            select(                [join(join(table1, table2, table1.c.myid == table2.c.otherid), table3, table1.c.myid == table3.c.userid)            ]),            "SELECT mytable.myid, mytable.name, mytable.description, myothertable.otherid, myothertable.othername, thirdtable.userid, thirdtable.otherstuff FROM mytable JOIN myothertable ON mytable.myid = myothertable.otherid JOIN thirdtable ON mytable.myid = thirdtable.userid"        )        self.assert_compile(            join(users, addresses, users.c.user_id==addresses.c.user_id).select(),            "SELECT users.user_id, users.user_name, users.password, addresses.address_id, addresses.user_id, addresses.street, addresses.city, addresses.state, addresses.zip FROM users JOIN addresses ON users.user_id = addresses.user_id"        )        self.assert_compile(                select([table1, table2, table3],                from_obj = [join(table1, table2, table1.c.myid == table2.c.otherid).outerjoin(table3, table1.c.myid==table3.c.userid)]                #from_obj = [outerjoin(join(table, table2, table1.c.myid == table2.c.otherid), table3, table1.c.myid==table3.c.userid)]                )                ,"SELECT mytable.myid, mytable.name, mytable.description, myothertable.otherid, myothertable.othername, thirdtable.userid, thirdtable.otherstuff FROM mytable JOIN myothertable ON mytable.myid = myothertable.otherid LEFT OUTER JOIN thirdtable ON mytable.myid = thirdtable.userid"            )        self.assert_compile(                select([table1, table2, table3],                from_obj = [outerjoin(table1, join(table2, table3, table2.c.otherid == table3.c.userid), table1.c.myid==table2.c.otherid)]                )                ,"SELECT mytable.myid, mytable.name, mytable.description, myothertable.otherid, myothertable.othername, thirdtable.userid, thirdtable.otherstuff FROM mytable LEFT OUTER JOIN (myothertable JOIN thirdtable ON myothertable.otherid = thirdtable.userid) ON mytable.myid = myothertable.otherid"            )        query = select(                [table1, table2],                or_(                    table1.c.name == 'fred',                    table1.c.myid == 10,                    table2.c.othername != 'jack',                    "EXISTS (select yay from foo where boo = lar)"                ),                from_obj = [ outerjoin(table1, table2, table1.c.myid == table2.c.otherid) ]                )        self.assert_compile(query,            "SELECT mytable.myid, mytable.name, mytable.description, myothertable.otherid, myothertable.othername \FROM mytable LEFT OUTER JOIN myothertable ON mytable.myid = myothertable.otherid \WHERE mytable.name = :mytable_name_1 OR mytable.myid = :mytable_myid_1 OR \myothertable.othername != :myothertable_othername_1 OR \EXISTS (select yay from foo where boo = lar)",            )    def test_compound_selects(self):        try:            union(table3.select(), table1.select())        except exceptions.ArgumentError, err:            assert str(err) == "All selectables passed to CompoundSelect must have identical numbers of columns; select #1 has 2 columns, select #2 has 3"            x = union(              select([table1], table1.c.myid == 5),              select([table1], table1.c.myid == 12),              order_by = [table1.c.myid],        )        self.assert_compile(x, "SELECT mytable.myid, mytable.name, mytable.description \FROM mytable WHERE mytable.myid = :mytable_myid_1 UNION \SELECT mytable.myid, mytable.name, mytable.description \FROM mytable WHERE mytable.myid = :mytable_myid_2 ORDER BY mytable.myid")        u1 = union(            select([table1.c.myid, table1.c.name]),            select([table2]),            select([table3])        )        self.assert_compile(u1,        "SELECT mytable.myid, mytable.name \FROM mytable UNION SELECT myothertable.otherid, myothertable.othername \FROM myothertable UNION SELECT thirdtable.userid, thirdtable.otherstuff FROM thirdtable")        assert u1.corresponding_column(table2.c.otherid) is u1.c.myid        # TODO - why is there an extra space before the LIMIT ?        self.assert_compile(            union(                select([table1.c.myid, table1.c.name]),                select([table2]),                order_by=['myid'],                offset=10,                limit=5            )        ,    "SELECT mytable.myid, mytable.name \FROM mytable UNION SELECT myothertable.otherid, myothertable.othername \FROM myothertable ORDER BY myid  LIMIT 5 OFFSET 10"        )        self.assert_compile(            union(                select([table1.c.myid, table1.c.name, func.max(table1.c.description)], table1.c.name=='name2', group_by=[table1.c.myid, table1.c.name]),                table1.select(table1.c.name=='name1')            )            ,            "SELECT mytable.myid, mytable.name, max(mytable.description) AS max_1 FROM mytable \WHERE mytable.name = :mytable_name_1 GROUP BY mytable.myid, mytable.name UNION SELECT mytable.myid, mytable.name, mytable.description \FROM mytable WHERE mytable.name = :mytable_name_2"        )        self.assert_compile(            union(                select([literal(100).label('value')]),                select([literal(200).label('value')])                ),                "SELECT :param_1 AS value UNION SELECT :param_2 AS value"        )        self.assert_compile(            union_all(                select([table1.c.myid]),                union(                    select([table2.c.otherid]),                    select([table3.c.userid]),                )            )            ,            "SELECT mytable.myid FROM mytable UNION ALL (SELECT myothertable.otherid FROM myothertable UNION \SELECT thirdtable.userid FROM thirdtable)"        )        # This doesn't need grouping, so don't group to not give sqlite unnecessarily hard time        self.assert_compile(            union(                except_(                    select([table2.c.otherid]),                    select([table3.c.userid]),                ),                select([table1.c.myid])            )            ,            "SELECT myothertable.otherid FROM myothertable EXCEPT SELECT thirdtable.userid FROM thirdtable \UNION SELECT mytable.myid FROM mytable"        )    @testing.uses_deprecated('//get_params')    def test_binds(self):        for (             stmt,             expected_named_stmt,             expected_positional_stmt,             expected_default_params_dict,             expected_default_params_list,             test_param_dict,             expected_test_params_dict,             expected_test_params_list             ) in [              (                  select(                      [table1, table2],                     and_(                         table1.c.myid == table2.c.otherid,                         table1.c.name == bindparam('mytablename')                     )),                     """SELECT mytable.myid, mytable.name, mytable.description, myothertable.otherid, myothertable.othername FROM mytable, myothertable WHERE mytable.myid = myothertable.otherid AND mytable.name = :mytablename""",                     """SELECT mytable.myid, mytable.name, mytable.description, myothertable.otherid, myothertable.othername FROM mytable, myothertable WHERE mytable.myid = myothertable.otherid AND mytable.name = ?""",                 {'mytablename':None}, [None],                 {'mytablename':5}, {'mytablename':5}, [5]             ),             (                 select([table1], or_(table1.c.myid==bindparam('myid'), table2.c.otherid==bindparam('myid'))),                 "SELECT mytable.myid, mytable.name, mytable.description FROM mytable, myothertable WHERE mytable.myid = :myid OR myothertable.otherid = :myid",                 "SELECT mytable.myid, mytable.name, mytable.description FROM mytable, myothertable WHERE mytable.myid = ? OR myothertable.otherid = ?",                 {'myid':None}, [None, None],                 {'myid':5}, {'myid':5}, [5,5]             ),             (                 text("SELECT mytable.myid, mytable.name, mytable.description FROM mytable, myothertable WHERE mytable.myid = :myid OR myothertable.otherid = :myid"),                 "SELECT mytable.myid, mytable.name, mytable.description FROM mytable, myothertable WHERE mytable.myid = :myid OR myothertable.otherid = :myid",                 "SELECT mytable.myid, mytable.name, mytable.description FROM mytable, myothertable WHERE mytable.myid = ? OR myothertable.otherid = ?",                 {'myid':None}, [None, None],                 {'myid':5}, {'myid':5}, [5,5]             ),             (                 select([table1], or_(table1.c.myid==bindparam('myid', unique=True), table2.c.otherid==bindparam('myid', unique=True))),                 "SELECT mytable.myid, mytable.name, mytable.description FROM mytable, myothertable WHERE mytable.myid = :myid_1 OR myothertable.otherid = :myid_2",                 "SELECT mytable.myid, mytable.name, mytable.description FROM mytable, myothertable WHERE mytable.myid = ? OR myothertable.otherid = ?",                 {'myid_1':None, 'myid_2':None}, [None, None],                 {'myid_1':5, 'myid_2': 6}, {'myid_1':5, 'myid_2':6}, [5,6]             ),             (                bindparam('test', type_=String) + text("'hi'"),                ":test || 'hi'",                "? || 'hi'",                {'test':None}, [None],                {}, {'test':None}, [None]             ),             (                 select([table1], or_(table1.c.myid==bindparam('myid'), table2.c.otherid==bindparam('myotherid'))).params({'myid':8, 'myotherid':7}),                 "SELECT mytable.myid, mytable.name, mytable.description FROM mytable, myothertable WHERE mytable.myid = :myid OR myothertable.otherid = :myotherid",                 "SELECT mytable.myid, mytable.name, mytable.description FROM mytable, myothertable WHERE mytable.myid = ? OR myothertable.otherid = ?",                 {'myid':8, 'myotherid':7}, [8, 7],                 {'myid':5}, {'myid':5, 'myotherid':7}, [5,7]             ),             (                 select([table1], or_(table1.c.myid==bindparam('myid', value=7, unique=True), table2.c.otherid==bindparam('myid', value=8, unique=True))),                 "SELECT mytable.myid, mytable.name, mytable.description FROM mytable, myothertable WHERE mytable.myid = :myid_1 OR myothertable.otherid = :myid_2",                 "SELECT mytable.myid, mytable.name, mytable.description FROM mytable, myothertable WHERE mytable.myid = ? OR myothertable.otherid = ?",                 {'myid_1':7, 'myid_2':8}, [7,8],                 {'myid_1':5, 'myid_2':6}, {'myid_1':5, 'myid_2':6}, [5,6]             ),             ]:                self.assert_compile(stmt, expected_named_stmt, params=expected_default_params_dict)                self.assert_compile(stmt, expected_positional_stmt, dialect=sqlite.dialect())                nonpositional = stmt.compile()

⌨️ 快捷键说明

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