Coder Social home page Coder Social logo

snapaddy / databank Goto Github PK

View Code? Open in Web Editor NEW
5.0 5.0 0.0 146 KB

Databank is an easy-to-use Python library for making raw SQL queries in a multi-threaded environment.

License: MIT License

Python 100.00%
database mysql postgresql sql sqlalchemy sqlite3

databank's People

Contributors

adam444555 avatar benedikt-budig avatar philiprekers avatar severinsimmler avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

databank's Issues

Parallelized queries

That you can run queries in parallel without worrying about async/await:

db = Database()
db.execute_many(query, batch_size=5)

which will process 5 queries in parallel.

And also something like:

db.execute(query, await=False)

if you do not want the process to be blocked.

Dry run

Implement a "dry run" method to print the SQL query to be executed with escaped parameters etc.

Support for SQLAlchemy 2.0

  .env/lib/python3.10/site-packages/databank/core.py:162: RemovedIn20Warning: Deprecated API features detected! These feature(s) are not compatible with SQLAlchemy 2.0. To prevent incompatible upgrades prior to updating applications, ensure requirements files are pinned to "sqlalchemy<2.0". Set environment variable SQLALCHEMY_WARN_20=1 to show all deprecation warnings.  Set environment variable SQLALCHEMY_SILENCE_UBER_WARNING=1 to silence this message. (Background on SQLAlchemy 2.0 at: https://sqlalche.me/e/b8d9)

Question: Treat empty strings in `params` for `execute`

When I want to insert into a table with execute_many() I can use multiple params. But sometimes it is possible that values of a dictionary contain empty strings which can't be inserted. The solution is to change them to None.

insert_queries = QueryCollection.from_file("src/queries/insert.sql")

album_params = [{
        "artist_name": "HVOB",
        "album_name": "HVOB",
        "album_release_date": "",
        "album_type": "Album",
        "album_genre": ""
    },]
db.execute_many(insert_queries["insert_album"], album_params)

My question is: Is there maybe a solution planned to automatically convert empty strings to None or is the expected solution here to convert the empty strings to None by the user?

Question: Why is `_queries` in `QueryCollection` private?

I would like to execute all queries of my QueryCollection like in the following example:

create_queries = QueryCollection.from_file("src/queries/create.sql")
for _, query in create_queries._queries.items():
    db.execute(query)

Does _queries has to be private? If not, one could access it directly (although it is obviously also now possible to do this, but not recommend because it is private ๐Ÿ˜‰ ) and looping over it.
Alternatively, one could add the following getter function:

def get_queries(self):
    return self._queries

Bug: Only one newline allowed between statements in `.sql` file

If a .sql file has more than one newline between each statement, one gets the following error:

databank.query.InvalidQueryHeader: '' is not a valid query header

Example

/* @name create_album */
CREATE TABLE IF NOT EXISTS album (
  ...
);



/* @name create_review */
CREATE TABLE IF NOT EXISTS review (
    ...
);

Solution ideas

  • Allow more than one newline in .sql files
  • Autoformat loaded .sql file before further processing
  • Document this rule

Column name duplicates unexpected behaviour

Using fetch_all() with a JOIN in the query, resulting in a result containing the same column name twice, shows only one of both resulting columns in output dictionary.

Example:

Two tables contain a column called name.
We join both tables and the result table now has two columns called name.
With fetch_all() we only get one result set for a column called name (dictionary has unique keys).

Possible fixes:

  • Throw a warning when the result set has a duplicate column name
  • Rename columns in output dictionary when the result set has a duplicate column name
  • If possible use table alias for each of the repeated column names as prefix in output dictionary key, separated by underscore

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.