构建postgresql预编译语句和asyncpg的查询
项目描述
buildpg
构建postgresql预编译语句和asyncpg的查询
许多更强大的功能,包括完整的子句构建、多个值、逻辑函数、查询美化打印和不同的变量替换 - 以下只是一个非常简短的总结。请检查代码和测试用例以获取示例。
构建查询
简单的变量替换
from buildpg import render
render('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 替换常量
from buildpg import V, render
render('select * from mytable where :col=:foo', col=V('x'), foo=456)
>> 'select * from mytable where x=$1', [456]
复杂逻辑
from buildpg import V, funcs, render
where_logic = V('foo.bar') == 123
if spam_value:
   where_logic &= V('foo.spam') <= spam_value
if exclude_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']]
值的使用
from buildpg import Values, render
render('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 的包装器
import asyncio
from buildpg import asyncpg
async def main():
   async with asyncpg.create_pool_b('postgres://postgres@localhost:5432/db') as pool:
       await pool.fetchval_b('select spam from mytable where x=:foo and y=:bar', foo=123, bar='whatever')
       >> 42
asyncio.run(main())
池和连接都具有所有常见查询方法的 *_b 变体
- execute_b
- executemany_b
- fetch_b
- fetchval_b
- fetchrow_b
- cursor_b
运算符
| Python运算符/函数 | SQL运算符 | 
|---|---|
| & | AND | 
| ` | ` | 
| = | = | 
| != | != | 
| < | < | 
| <= | <= | 
| > | > | 
| >= | >= | 
| + | + | 
| - | - | 
| * | * | 
| / | / | 
| % | % | 
| ** | ^ | 
| - | - | 
| ~ | not(...) | 
| sqrt | ` | 
| abs | @ | 
| contains | @> | 
| contained_by | <@ | 
| overlap | && | 
| like | LIKE | 
| ilike | ILIKE | 
| cat | ` | 
| in_ | in | 
| from_ | from | 
| at_time_zone | AT TIME ZONE | 
| matches | @@ | 
| is_ | is | 
| is_not | is not | 
| for_ | for | 
| factorial | ! | 
| cast | :: | 
| asc | ASC | 
| desc | DESC | 
| comma | , | 
| on | ON | 
| as_ | AS | 
| 空值优先 | 空值优先 | 
| 空值后置 | 空值后置 | 
用法
from buildpg import V, S, render
def show(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函数 | SQL函数 | 
|---|---|
| AND(*args) | <arg1> and <arg2> ... | 
| OR(*args) | <arg1> or <arg2> ... | 
| NOT(arg) | not(<arg>) | 
| comma_sep(*args) | <arg1>, <arg2>, ... | 
| count(expr) | count(expr) | 
| any(arg) | any(<arg1>) | 
| now() | now() | 
| cast(v, cast_type) | <v>::<cast_type> | 
| upper(string) | upper(<string>) | 
| lower(string) | lower(<string>) | 
| length(string) | length(<string>) | 
| left(string, n) | left(<string>, <n>) | 
| right(string, n) | right(<string>, <n>) | 
| extract(expr) | extract(<expr>) | 
| sqrt(n) | ` | 
| abs(n) | @<n> | 
| factorial(n) | !<n> | 
| position(substring, string) | position(<substring> in <st... | 
| substring(string, pattern, escape-None) | substring(<string> from <pa... | 
| to_tsvector(arg1, document-None) | to_tsvector(<arg1>) | 
| to_tsquery(arg1, text-None) | to_tsquery(<arg1>) | 
用法
from buildpg import V, render, funcs
def show(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']
          项目详情
下载文件
下载适用于您的平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。
源分发
         buildpg-0.4.tar.gz  (12.5 kB 查看哈希值)
      
    构建分发
         buildpg-0.4-py3-none-any.whl  (11.7 kB 查看哈希值)