{"id":15964463,"url":"https://github.com/prawn-cake/pg_requests","last_synced_at":"2026-05-03T23:35:55.638Z","repository":{"id":57452179,"uuid":"59882210","full_name":"prawn-cake/pg_requests","owner":"prawn-cake","description":"PostgreSQL-specific query builder library","archived":false,"fork":false,"pushed_at":"2017-11-05T20:08:52.000Z","size":49,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-09-20T13:35:34.358Z","etag":null,"topics":["database","postgresql","python","sql-query"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/prawn-cake.png","metadata":{"files":{"readme":"README.md","changelog":"Changelog.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-05-28T07:13:39.000Z","updated_at":"2017-11-01T17:58:52.000Z","dependencies_parsed_at":"2022-09-02T08:32:38.227Z","dependency_job_id":null,"html_url":"https://github.com/prawn-cake/pg_requests","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/prawn-cake/pg_requests","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prawn-cake%2Fpg_requests","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prawn-cake%2Fpg_requests/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prawn-cake%2Fpg_requests/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prawn-cake%2Fpg_requests/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/prawn-cake","download_url":"https://codeload.github.com/prawn-cake/pg_requests/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prawn-cake%2Fpg_requests/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32589262,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-03T22:12:39.696Z","status":"ssl_error","status_checked_at":"2026-05-03T22:09:10.534Z","response_time":103,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","postgresql","python","sql-query"],"created_at":"2024-10-07T17:02:01.188Z","updated_at":"2026-05-03T23:35:55.624Z","avatar_url":"https://github.com/prawn-cake.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"pg_requests\n========\n[![Build Status](https://travis-ci.org/prawn-cake/pg_requests.svg?branch=master)](https://travis-ci.org/prawn-cake/pg_requests)\n[![Coverage Status](https://coveralls.io/repos/github/prawn-cake/pg_requests/badge.svg?branch=master)](https://coveralls.io/github/prawn-cake/pg_requests?branch=master)\n\nPostgreSQL-specific query builder.\n\n## Why?\n\nIn some cases using ORM or well-known frameworks is not applicable when we are \nforced to work with database and plain sql directly. \n\nIt's quite easy to write simple sql queries until it becomes cumbersome: \nlong multi-line sql queries, wrong arguments evaluation creates risks for sql injections, \nbuilding up sql depends on input parameters and extra conditions make manual way unmaintainable and barely checkable\n\nThe library provides handy way to build sql queries for PostgreSQL\n\n## Requirements\n\n* python2.7, python3.4+\n\n\n## Querying\n\nAll queries are executed in a safe manner, \ni.e every query is a tuple of sql template string and tuple of parameters, e.g \n    \n    ('SELECT * FROM users WHERE ( name = %s )', ('Mr.Robot',))\n    \nAnd this is already prepared query for `psycopg2.cursor` execution, rely on that it also excludes sql-injection chances. \n\n**Starting point**\n    \n    from pg_requests import query_facade as qf\n\n### Select\n    \n    qf.select(\u003ctable_name\u003e)\\\n      .fields(\u003cfield args\u003e)\\\n      .filter(\u003cdjango-style filter conditions\u003e)\n\n#### Example\n    \n    result_set = qf.select('users')\\\n        .fields('id', 'name')\\\n        .filter(created_at__gt=datetime.now() - timedelta(days=30))\\\n        .order_by('created_at').desc()\\\n        .limit(10)\\\n        .offset(20)\\\n        .execute(cursor).fetchall()\n\n##### Q-object\n\nQ-object mimics django Q object and allows to build complex querying conditions (especially OR conditions for WHERE clause)\n    \n    from pg_requests import Q\n    \n    ...\n    users = qf.select('users')\\\n            .filter(Q(name='Mr.Robot') | Q(login='anonymous'))\\\n            .filter(Q(name='John'))\n            .execute(cursor)\n            .fetchall()\n\n#### Complex conditions\n\n#### Join\n    \n    # USING syntax\n    qf.select('users').join('customers', using=('id', )).execute(cursor)\n    \n    # The following SQL will be executed\n    SELECT * FROM users INNER JOIN customers USING (id)\n    \n    # Using different join types\n    from pg_requests.operators import JOIN\n    \n    qf.select('users')\\\n        .join('customers', join_type=JOIN.RIGHT_OUTER, using=('id', ))\\\n        .filter(users__name='Mr.Robot')\n        .execute(cursor)\n    \n    # Query value\n    ('SELECT * FROM users RIGHT OUTER JOIN customers USING (id) WHERE ( users.name = %s )', ('Mr.Robot',))\n\n###### {table}.{field} Key evaluation\n\nTo use explicit form *{table}.{field}* you have to use the syntax:\n\n    qf.select('users')\\\n        .join('customers', using=('id', )).filter(users__name='Mr.Robot').execute(cursor)\n\n#### Functions\n\n##### Aggregation\n    \n    ...\n    from pg_requests.functions import fn\n    \n    qf.select('users').fields(fn.COUNT('*')).filter(name='Mr.Robot')\n    \n    # Query tuple\n    ('SELECT COUNT(*) FROM users WHERE ( name = %s )', ('Mr.Robot',))\n\n##### Calling stored procedures (user-defined database functions)\n\n    qf.call_fn('my_user_function', args=(1, 'str value', False))    \n    \n    # Query tuple\n    ('SELECT * FROM my_user_function(%s, %s, %s)', (1, 'str value', False))\n\n\n### Insert\n\n**IMPORTANT: all the mutations require connection commit (or autocommit=True) option (fair for psycopg2 cursors)**\n\n\n#### Example\n\n    qf.insert('users').data(name='x', login='y').returning('id').execute(cursor)\n    \n    # NOTE: If psycopg2 is used and no autocommit=True is enabled\n    cursor.connection.commit()\n    \n    \n### Update\n\n#### Example\n    \n    # Basic example\n    qf.update('users').data(users='Mr.Robot').filter(id=1).execute(cursor)\n    \n    # Update from foreign table (with JOIN)\n    qf.update('users')._from('customers').data(users__value='customers.value')\\\n        .filter(users__id='customers.id').execute(cursor)\n        \n    # NOTE: If psycopg2 is used and no autocommit=True is enabled\n    cursor.connection.commit()\n        \n**NOTE:** In the last example we use {table}__{field} syntax key evaluation\n\n\n##### F-object\n\nF-object is used to refer directly to a table field, useful for example for increments, i.e such SQL update query\n\n    UPDATE users \n    SET count = count + 1\n    WHERE name = 'John';\n\nCan be implemented as\n    \n    from pg_requests import F\n    \n    ...\n    qf.update('users').data(count=F('count') + 1).filter(name='John').execute(cursor)\n    cursor.connection.commit()\n\n\n### Delete\n\nNot implemented","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprawn-cake%2Fpg_requests","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprawn-cake%2Fpg_requests","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprawn-cake%2Fpg_requests/lists"}