Skip to main content

A simple Python Library for building relational database queries using objects

Project description

Overview

Supported Versions

This is pysqlscribe, the Python library intended to make building SQL queries in your code a bit easier!

Motivation

Other query building libraries, such as pypika are fantastic but not actively maintained. Some ORM libraries such as sqlalchemy offer similar (and awesome) capabilities using the core API, but if you're not already using the library in your application, it's a bit of a large dependency to introduce for the purposes of query building.

API

pysqlscribe currently offers several APIs for building queries.

Query

A Query object can be constructed using the QueryRegistry's get_builder if you supply a valid dialect (e.g; "mysql", "postgres", "oracle). For example, "mysql" would be:

from pysqlscribe.query import QueryRegistry

query_builder = QueryRegistry.get_builder("mysql")
query = query_builder.select("test_field", "another_test_field").from_("test_table").build()

Alternatively, you can create the corresponding Query class associated with the dialect directly:

from pysqlscribe.query import MySQLQuery

query_builder = MySQLQuery()
query = query_builder.select("test_field", "another_test_field").from_("test_table").build()

Furthermore, if there are any dialects that we currently don't support, you can create your own by subclassing Query and registering it with the QueryRegistry:

from pysqlscribe.query import QueryRegistry, Query


@QueryRegistry.register("custom")
class CustomQuery(Query):
    ...

Table

An alternative method for building queries is through the Table object:

from pysqlscribe.table import MySQLTable

table = MySQLTable("test_table", "test_field", "another_test_field")
query = table.select("test_field").build()

A schema for the table can also be provided as a keyword argument, after the columns/fields:

from pysqlscribe.table import MySQLTable

table = MySQLTable("test_table", "test_field", "another_test_field", schema="test_schema")
query = table.select("test_field").build()

Additionally, in the event an invalid field is provided in the select call, we will raise an exception:

from pysqlscribe.table import MySQLTable

table = MySQLTable("test_table", "test_field", "another_test_field")
table.select("some_nonexistent_field")  # will raise InvalidFieldsException

Table also offers a create method in the event you've added a new dialect which doesn't have an associated Table implementation, or if you need to change it for different environments (e.g; sqlite for local development, mysql/postgres/oracle/etc. for deployment):

from pysqlscribe.table import Table

new_dialect_table_class = Table.create(
    "new-dialect")  # assuming you've registered "new-dialect" with the `QueryRegistry`
table = new_dialect_table_class("test_table", "test_field", "another_test_field")

You can overwrite the original fields supplied to a Table as well, which will delete the old attributes and set new ones:

from pysqlscribe.table import MySQLTable

table = MySQLTable("test_table", "test_field", "another_test_field")
table.test_field  # valid
table.fields = ['new_test_field']
table.select("new_test_field")
table.new_test_field  # now valid - but `table.test_field` is not anymore

Schema

For associating multiple Tables with a single schema, you can use the Schema:

from pysqlscribe.schema import Schema

schema = Schema("test_schema", tables=["test_table", "another_test_table"], dialect="postgres")
schema.tables  # a list of two `Table` objects

This is functionally equivalent to:

from pysqlscribe.table import PostgresTable

table = PostgresTable("test_table", schema="test_schema")
another_table = PostgresTable("another_test_table", schema="test_schema")

Instead of supplying a dialect directly to Schema, you can also set the environment variable PYQUERY_BUILDER_DIALECT:

export PYQUERY_BUILDER_DIALECT = 'postgres'
from pysqlscribe.schema import Schema

schema = Schema("test_schema", tables=["test_table", "another_test_table"])
schema.tables  # a list of two `PostgresTable` objects

Alternatively, if you already have existing Table objects you want to associate with the schema, you can supply them directly (in this case, dialect is not needed):

from pysqlscribe.schema import Schema
from pysqlscribe.table import PostgresTable

table = PostgresTable("test_table")
another_table = PostgresTable("another_test_table")
schema = Schema("test_schema", [table, another_table])

Schema also has each table set as an attribute, so in the example above, you can do the following:

schema.test_table # will return the supplied table object with the name `"test_table"`

Contributions

Contributions are always welcome, as this is a new and active project.

Local Environment Setup

This project currently uses uv for convenience, although we currently only have dev dependencies. To create your environment:

uv sync

Unit Testing

pytest is used for all unit testing. To run the unit tests locally (assuming a local environment is set up):

uv run pytest

Unit tests are executed as part of CI, and the behavior should be consistent with what is observed in local development.

Supported Dialects

This is anticipated to grow, also there are certainly operations that are missing within dialects.

  • MySQL
  • Oracle
  • Postgres
  • Sqlite

TODO

  • Support JOINs
  • Add more dialects
  • Support OFFSET for Oracle and SQLServer
  • Add a better abstraction around fields such that we can build expressions from comparisons, etc. as strings are limiting

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pysqlscribe-0.0.1.tar.gz (18.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pysqlscribe-0.0.1-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file pysqlscribe-0.0.1.tar.gz.

File metadata

  • Download URL: pysqlscribe-0.0.1.tar.gz
  • Upload date:
  • Size: 18.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.21

File hashes

Hashes for pysqlscribe-0.0.1.tar.gz
Algorithm Hash digest
SHA256 9e1601f9e1e3045b4ef3bb4044866beedebfb22e8dff270ec68beb850a837531
MD5 7bd87278af17a13e8a944c0220ba7e29
BLAKE2b-256 6527ff8eb6605b501685756ffd3bfcf432b05399c5a15c4238979d02f98527a9

See more details on using hashes here.

File details

Details for the file pysqlscribe-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pysqlscribe-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b3245dd233b3a3b8c923775bfa7cab9f8172fa9e5c33c06aac1d67b195b288fa
MD5 1126806f23f6a57274e91060fa17622c
BLAKE2b-256 e739acc667bba36ba1776042cc20eafb7fb9a6f8b44bfbcb958c833ff194067d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page