为PostgreSQL准备的语句和AsyncPg建立查询。

buildpg的Python项目详细描述


BuildStatusCoveragepypi

为PostgreSQL准备的语句和AsyncPg建立查询。

很多更强大的特性,包括全子句构造、多值、逻辑函数, 查询漂亮的打印和不同的变量替换-下面只是一个非常快速的总结。 请检查代码和测试以获取示例。

生成查询

简单变量替换:

frombuildpgimportrenderrender('select * from mytable where x=:foo and y=:bar',foo=123,bar='whatever')>>'select * from mytable where x=$1 and y=$2',[123,'whatever']

使用V替换常量:

frombuildpgimportV,renderrender('select * from mytable where :col=:foo',col=V('x'),foo=456)>>'select * from mytable where x=$1',[456]

复杂逻辑:

frombuildpgimportV,funcs,renderwhere_logic=V('foo.bar')==123ifspam_value:where_logic&=V('foo.spam')<=spam_valueifexclude_cake:where_logic&=funcs.not_(V('foo.cake').in_([1,2,3]))render('select * from foo :where',where=where_logic)>>'select * from foo foo.bar = $1 AND foo.spam <= $2 AND not(foo.cake in $3)',[123,123,['x','y']]

值用法:

frombuildpgimportValues,renderrender('insert into the_table (:values__names) values :values',values=Values(a=123,b=456,c='hello'))>>'insert into the_table (a, b, c) values ($1, $2, $3)',[123,456,'hello']

使用asyncpg

作为asyncpg的包装:

importasynciofrombuildpgimportasyncpgasyncdefmain():asyncwithasyncpg.create_pool_b('postgres://postgres@localhost:5432/db')aspool:awaitpool.fetchval_b('select spam from mytable where x=:foo and y=:bar',foo=123,bar='whatever')>>42asyncio.get_event_loop().run_until_complete(main())

池和连接都有所有常用查询方法的*_b变体:

  • execute_b
  • executemany_b
  • fetch_b
  • fetchval_b
  • fetchrow_b
  • cursor_b

运算符

Python operator/functionSQL operator
^{tt9}$^{tt10}$
^{tt11}$^{tt12}$
^{tt13}$^{tt13}$
^{tt15}$^{tt15}$
^{tt17}$^{tt17}$
^{tt19}$^{tt19}$
^{tt21}$^{tt21}$
^{tt23}$^{tt23}$
^{tt25}$^{tt25}$
^{tt27}$^{tt27}$
^{tt29}$^{tt29}$
^{tt31}$^{tt31}$
^{tt33}$^{tt33}$
^{tt35}$^{tt36}$
^{tt27}$^{tt27}$
^{tt39}$^{tt40}$
^{tt41}$^{tt42}$
^{tt43}$^{tt44}$
^{tt45}$^{tt46}$
^{tt47}$^{tt48}$
^{tt49}$^{tt50}$
^{tt51}$^{tt52}$
^{tt53}$^{tt54}$
^{tt55}$^{tt56}$
^{tt57}$^{tt58}$
^{tt59}$^{tt60}$
^{tt61}$^{tt62}$
^{tt63}$^{tt64}$
^{tt65}$^{tt66}$
^{tt67}$^{tt68}$
^{tt69}$^{tt70}$
^{tt71}$^{tt72}$
^{tt73}$^{tt74}$
^{tt75}$^{tt76}$
^{tt77}$^{tt78}$
^{tt79}$^{tt80}$
^{tt81}$^{tt82}$

用法:

frombuildpgimportV,S,renderdefshow(component):sql,params=render(':c',c=component)print(f'sql="{sql}" params={params}')show(V('foobar').contains([1,2,3]))#> sql="foobar @> $1" params=[[1, 2, 3]]show(V('foobar')==4)#> sql="foobar = $1" params=[4]show(~V('foobar'))#> sql="not(foobar)" params=[]show(S(625).sqrt())#> sql="|/ $1" params=[625]show(V('foo').is_not('true'))#> sql="foo is not true" params=[]

功能

Python functionSQL function
^{tt83}$^{tt84}$
^{tt85}$^{tt86}$
^{tt87}$^{tt88}$
^{tt89}$^{tt90}$
^{tt91}$^{tt91}$
^{tt93}$^{tt94}$
^{tt95}$^{tt95}$
^{tt97}$^{tt98}$
^{tt99}$^{tt100}$
^{tt101}$^{tt102}$
^{tt103}$^{tt104}$
^{tt105}$^{tt106}$
^{tt107}$^{tt108}$
^{tt109}$^{tt110}$
^{tt111}$^{tt112}$
^{tt113}$^{tt114}$
^{tt115}$^{tt116}$
^{tt117}$^{tt118}$…
^{tt119}$^{tt120}$…
^{tt121}$^{tt122}$
^{tt123}$^{tt124}$

用法:

frombuildpgimportV,render,funcsdefshow(component):sql,params=render(':c',c=component)print(f'sql="{sql}" params={params}')show(funcs.AND(V('x')==4,V('y')>6))#> sql="x = $1 AND y > $2" params=[4, 6]show(funcs.position('foo','this has foo in it'))#> sql="position($1 in $2)" params=['foo', 'this has foo in it']

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
使用jaxrpc的Java eclipse WebService客户端   java编程方式在对象上写入名称   java Spring批处理:重试后跳过   java Android错误:错误:任务执行失败:应用程序:transformClassesWithDexForDebug'   带有清单文件nullPointerException的java Android元数据   spring Java Quartz调度作业停止运行   JavaMockito:如何在不调用实际方法的情况下,模拟带有参数和无效返回类型的静态方法?   java Tomcat连接池问题无法在关闭的连接上调用方法   java如何交换列表中的项目?   java如何停止线程并通过Toast在线程中正确显示文本?   java为什么连续写入OutputStream时偏移量0不会导致重复字节?   java我无法生成头文件   不兼容的返回类型错误java   修改值后键值对的java Jolt转换规范   java有自动更新Javadoc的工具吗?   java线程如何在ints自身实例类中共享变量   java继承一个非gwt模块   java Hibernate xml配置   使用netty4异步调用的java链接HTTP请求响应