Package: que-py
Que: SQL for Sneks 🐍Que allows you to get generate your SQL queries on the fly, without the
overhead of a fully-fledged ORM.MotivationsQue was born out of a need for dynamically generated SQL for an ASGI web
service. I found my self wishing for the convenience of dynamic querying
with an ORM such as SQLAlchemy, but the performance of a fully
asynchronous database client. Que attempts to fill this void. Choose the
connection client you prefer and let Que worry about the SQL.What Is It?Que looks to solve a single purpose: generate SQL-compliant queries in
pure-Python. Que has absolutely no hard dependendencies and does not
enforce the use of a specific database client or dialect.Still want to use SQLAlchemy for your connection? Go for it. Want to use
PyMySQL or psycopg2? Que won't stop you. Want to use an asyncio
framework such as aiopg? You have excellent taste! This library was
written just for you.DesignThe focus of Que issimplicity, just look at what it takes for a
simpleSELECT:>>>importque>>>select=que.Select(table='foo')>>>selectSelect(table='foo',schema=None,filters=FilterList([]),fields=FieldList([]))>>>sql,args=select.to_sql()>>>print(sql)SELECT*FROMfooQue works with the DBAPI client of your choice by parametrizing your sql
and formatting your arguments for you:>>>importque>>>fields=[que.Field('bar')]>>>filters=[que.Filter(que.Field('id',1))]>>>select=que.Select(table='foo',filters=filters,fields=fields)>>>sql,args=select.to_sql()>>>print(sql)SELECTbarFROMfooWHEREid=:1>>>args[1]>>>sql,args=select.to_sql(style=que.NameParamStyle.NAME)>>>print(sql)SELECTbarFROMfooWHEREid=:id>>>args{'id':1}Que works to normalize the API for your SQL operations, so that
initializing anINSERTorUPDATEis functionally the same as
initializing aSELECT:>>>importque>>>importdataclasses>>>importdatetime>>>>>>@dataclasses.dataclass...classFoo:...bar:str...id:int=None...created:datetime.datetime=None...>>>new_foo=Foo('blah')>>>fields=que.data_to_fields(new_foo,exclude=None)>>>insert=que.Insert(table='foo',fields=fields)>>>sql,args=insert.to_sql(que.NameParamStyle.NAME)>>>print(sql)INSERTINTOfoo(:colbar)VALUES(:valbar)>>>args{'colbar':'bar','valbar':'blah'}QuickStartQue has no dependencies and is exceptionally light-weight (currently
only ~30Kb!), comprising of only a few hundred lines of code.
Installation is as simple aspip3 install que-py.Then you're good to go!import queand rock on 🤘ExamplesA simple client for generating your SQL and inserting new entries:importdataclassesimportsqlite3importque@dataclasses.dataclassclassSpam:flavor:strid:int=Nonecreated_on:int=NoneclassSpamClient:"""A database client for tracking spam flavors."""def__init__(self):self.conn=sqlite3.connect('sqlite://spam.db')definsert_spam(self,spam:Spam):fields=que.data_to_fields(spam,exclude=None)insert=que.Insert('spam',fields=fields)sql,args=insert.to_sql()returnself.conn.execute(sql,args)defget_spam(self,**kwargs):fields=que.data_to_fields(kwargs)filters=[que.Filter(x)forxinfields]select=que.Select('spam',filters=filters)returnself.conn.execute(*select.to_sql())defupdate_spam(self,spam:Spam):fields=[que.Field('flavor',spam.flavor)]filters=[que.Filter(que.Field('id',spam.id))]update=que.Update('spam',filters=filters,fields=fields)returnself.conn.execute(*update.to_sql())defdelete_spam(self,spam:Spam):filters=[que.Filter(que.Field('id',spam.id))]delete=que.Delete('spam',filters=filters)returnself.conn.execute(*delete.to_sql())DocumentationFull documentation coming soon!Happy Querying 🐍How to ContributeCheck for open issues or open a fresh issue to start a discussion
around a feature idea or a bug.Create a branch on Github for your issue or forkthe repositoryon GitHub to
start making your changes to themasterbranch.Write a test which shows that the bug was fixed or that the feature
works as expected.Send a pull request and bug the maintainer until it gets merged and
published. :)