Git Product home page Git Product logo

pyquery's Introduction

PyQuery

This a simple wraper around SQL meant for python. This heavily inspired by DJANGO ORM, the difference is that THIS IS NOT AN ORM. I intend to make queries easier for django developers and python developers. Without the need for using a full ORM and mapping each single table to a python class.

Example

Simple example and quickstart on how to use

from query.connection import Connect

conn = Connect(
    'postgres', 
    port=5432,
    host='db_host', 
    database='db_name', 
    user='db_user', 
    password='db_password',
    join_relations={
        'form':{
            'depends_on': 'form'
        }
    }
)

#############
#           #
# EXAMPLE 1 #
#           #
#############
migrate_data_ids = [1,2,3]

# data is lazy loaded, this means we just evaluate the data when needed
# Creates the query structure, and doesn't query anything
fields_to_recover_data = conn.query('field').filter(id___in=migrate_data_ids)

# evaluate the data only when retrieving it, so this is when we hit the database
print(fields_to_recover_data)


#############
#           #
# EXAMPLE 2 #
#           #
#############
# In this example uses the value of a query in a second query, evaluates only once, 
# you are not using `new_results` for anything.
results = conn.query('form_value').select('id', flat=True).limit(2)
new_results = conn.query('form_value').filter(id___in=results)

# you can also use .force() to force evaluation
new_results = conn.query('form_value').filter(id___in=results.force())

ENGINE

Right now we only support postgres, but hopefully we will support more engines in the near future.

JOINS

Joins are created automatically whenever you put double underscores

connection.query('form_value').filter(form__name=2)

In this example form is a field from form_value table and name is a field from form table. Sometimes the name of your field doesn't match the name of the table, for it you must use join relations.

IMPORTANT: In order to work, form field on form_value table MUST end with _id. Also form table MUST have an id column. Without this, joins don't work.

Observation: You can use double underscores for anything, here we are covering select but this also works with order_by and select functions.

Join Relations

The user can set dynamic join_relations directly in a query or in the connection for table joins. Let's go with the following example

join_relations = {
    "form_value": {
        "form": "dynamic_forms"
    }
}   

The above example means that: "When you are making a join with the form field and the table is form_value, we use the value dynamic_forms instead"

WHAT?

Let's dig deeper. When you do something like this:

connection.query('form_value').filter(form__form__id=2)

Let's separate the string by each duble underscore, we get something like this: [form, form, id] The first form is the name of the field in form_value, but this field is not from form database, instead it is from dynamic_forms. So in the SQL world we can translate to this:

SELECT *
FROM "form_value"
    INNER JOIN "dynamic_forms" ON "dyanmic_forms"."id" = "form_value"."form_id"
    INNER JOIN "form" ON "form"."id" = "dynamic_forms"."form_id"
WHERE "form"."id" = 2

Look that the second join, correctly references to form table, so we don't need to set any join relation for this field. But on on the form field in form_value table actually referes to dynamic_forms and not form.

Since your relations will probably never crash (only if you have the same column name for a table and the same table) you are safe defining it directly in the connection.

To clarify

Okay, but what if dynamic_forms table form field references foo table? In this case we would do something like the following:

join_relations = {
    "form_value": {
        "form": "dynamic_forms"
    },
    "dynamic_forms": {
        "form": "foo"
    }
}   

pyquery's People

Contributors

nicolasmelo1 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

domielias

pyquery's Issues

Create a SQLIte dabase engine

Create a new database engine for SQLIte.

Right now the user can only use the Potgres engine, it would be nice to use a SQLIte engine so the users can have the same functionality they have with postgres but with SQLIte.

For Postgres we use the psycopg2 python library that is easy to work with, it needs to find a SQLIte python library that returns the results the same way as psycopg2 but for SQLIte.
How to

How to

Engines are created this way. You just need to follow along with the postgres engine and export the same methods.

Create a MySQL database engine

Create a new database engine for MySQL.

Right now the user can only use the Potgres engine, it would be nice to use a MySQL engine so the users can have the same functionality they have with postgres but with MySQL.

For Postgres we use the psycopg2 python library that is easy to work with, it needs to find a mysql python library that returns the results the same way as psycopg2 for MySQL.

How to

Engines are created this way. You just need to follow along with the postgres engine and export the same methods.

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.