sqldbclient

A SQL client software package, mainly for use in Jupyter Notebook environment


License
Other
Install
pip install sqldbclient==0.1.1

Documentation

Sql DB Client

packageversion_ docs_ totaldownloads_ monthdownloads_

Description

Sql DB Client is a Python interface for interacting with a database.

Its main goal is to provide a Python-based alternative to basic database client software applications (e.g. DBeaver), especially in terms of executing SQL queries. This package mostly aims at SQL scripts executing since other types of database related activities (e.g. database navigation) can be done more conveniently with specifically designed graphical UI.

Based on powerful Python packages such as sqlalchemy, pandas and sqlparse, it provides easy-to-use interface for executing SQL code along with other additional functionalities:

  • keeping track of all executed queries, their execution information and results
  • parsing SQL queries (e.g. automatically adding LIMIT clause to prevent memory overflow)
  • performing transaction by simply using with operator

sqldbclient is especially helpful for data analysts and engineers who are used to work with Python and its packages inside Jupyter Notebook environment, since it's meant for an interactive use with the goal of analyzing, visualizing and interpreting data. Note that a SQL query result will be shown and saved as a pandas DataFrame object.

The module is compatible with Python 3.6+ and released under the terms of the MIT License.

Visit the project page at https://github.com/YuriyKozhev/SqlDbClient for further information about this project.

Quick Start

The latest released version of Sql DB Client can be obtained from the Python Package Index (PyPI). You can install sqldbclient using pip:

SqlExecutor, main tool to execute SQL queries, is configured and created using SqlExecutorConf object.

Let's create pg_executor, a new SqlExecutor instance for a PostgreSQL database.

Now, let's create a new table using transaction.

With SqlExecutor instance, it can be as easy as using its instance as a context manager.

Finally, let's check out data from the source we have just created.

If the logging is set up to show warnings (by default), first we will see the following message

indicating that SqlExecutor automatically added LIMIT clause to the query.

The next message will be an ExecutedSqlQuery instance.

After that, a Pandas DataFrame object will be displayed as an output.

.. date_day sales_total

0

2023-01-01

5332

1

2023-02-01

8676

2

2023-03-01

1345

Then, we can reference the DataFrame object using the UUID assigned to ExecutedSqlQuery to calculate the overall sum for sales_total field.

By storing results of executed queries in a SQLite database, we assure that they will be accessible after restarting the program, or even can used in another Jupyter notebook (as long as the SQLite database file is present in the same directory as a notebook).

Features

Here are some modules one most likely will use in their program.

sql_executor

Main class, SqlExecutor, inherits all functionalities from SqlHistoryManager, SqlQueryPreparator and SqlTransactionManager:

  • SqlHistoryManager
    • stores information about query executions and their results in local SQLite database
    • provides easy access to saved data via UUID
    • performs database cleaning to keep its size limited
  • SqlQueryPreparator
    • validates that there is exactly 1 statement in a query which is being executed
    • determines query type
    • formats query
    • automatically adds LIMIT clause to query
  • SqlTransactionManager
    • provides context manager for performing transactions

Moreover, SqlExecutor keeps configuration (sqlalchemy engine parameters, default LIMIT clause value, file name for history database) and provides single method for executing SQL queries.

sql_asyncio

Note

The following tools are available only with sqlalchemy version >= 1.4 installed, since the support for asynchronous engines was added in that release.

SqlAsyncExecutor is a simplified version of SqlExecutor, which provides a single method to execute queries asynchronously. It may be useful for the case when one needs to execute queries in parallel or to schedule an execution without blocking the main program.

SqlQueryPreparator is a wrapper around SqlAsyncExecutor with builtin tasks queue, which is used to store and obtain results of asynchronous executions. All queries are immediately scheduled for execution once they are added to the queue.

db_inspector

Note

This module is under development, and currently provides minimal functionality.

Provides a wrapper around sqlalchemy inspect function.

Apart from standard sqlalchemy.engine.reflection.Inspector methods, a DBInspector instance has the following functionalities:

  • creates text representation of table columns
  • provides get_views method to get consistent result throughout different sqlalchemy versions

dialects.postgresql

SqlViewFactory collects all available data about a regular or materialized view and all its dependencies into a Python View object. SqlViewMaterializer applies changes made to a View instance to a corresponding database object and all its dependencies. Together, SqlViewFactory and SqlViewMaterializer provide a tool which helps redefine a view in a database without the need of manually dropping it and its dependencies and then recreating them all. It also takes care of all the permissions that recreated objects had, that is the permissions will be automatically restored along with the view and its dependencies. Note that all the necessary steps will be executed in a separate transaction, which ensures that the whole operation either will be completed fully or will not be done at all.

Warning

'INSTEAD OF' view triggers are not supported yet and will not be automatically restored during view recreation.

utils

Here are some helpful tools to:

Resources

More information about available modules, classes and functions can be found on the documentation page.

Project page

https://github.com/YuriyKozhev/SqlDBClient

Bug tracker

https://github.com/YuriyKozhev/SqlDBClient/issues

Documentation

https://sqldbclient.readthedocs.io/