Code Monkey home page Code Monkey logo

ooquery's Introduction

OpenObject Query

Python 2.7 and >=3.5

https://travis-ci.org/gisce/ooquery.svg?branch=master https://coveralls.io/repos/github/gisce/ooquery/badge.svg?branch=master

Parsers OpenObjectes queries like:

import pyscopg2

from oopgrade.oopgrade import get_foreign_keys
from ooquery import OOQuery

conn = psycopg2.connect("dbname=test user=postgres")
with conn.cursor() as cursor:
    def fk_function(table, field):
        fks = get_foreign_keys(cursor, table)
        return fks[field]

    q = OOQuery('account_invoice', fk_function)
    sql = q.select(['number', 'state']).where([
        ('state', '=', 'open'),
        ('partner_id.name', '=', 'Pepito')
    ])
    cursor.execute(*sql)

Support for reading from joined tables

q = OOQuery('account_invoice', fk_function)
sql = q.select(['number', 'partner_id.name', 'partner_id.vat']).where([
    ('state', '=', 'open')
])

ooquery's People

Contributors

amatmv avatar eberloso avatar ecarreras avatar gdalmau avatar giscegit avatar mroig avatar polsala avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ooquery's Issues

Support for table fields in where clauses

If we want to search comparing two values from the database, now is not possible. We want do something like:

q  = OOQuery('account_invoice')
q.select(['id', 'number']).where([
    ('account_id', '!=', 'partner_id.account_id')
])

Then to inform that a condition is not a field we should use the Literal class as following:

q  = OOQuery('account_invoice')
q.select(['id', 'number']).where([
    ('amount_total', '>', Literal(100))
])

Rename is ignored on JoinType fields

When custom joins are used, the as_ parameter doesn't have any effect on that field.

def dummy_fk(table, field):
    fks = {
        'table_2': {
            'constraint_name': 'fk_contraint_name',
            'table_name': 'table',
            'column_name': 'table_2',
            'foreign_table_name': 'table2',
            'foreign_column_name': 'id'
        }
    }
    return fks[field]

OOQuery('table', dummy_fk).select(
    [InnerJoin('table_2.name')], as_={"table_2.name": "t2_name"}
).where([])

# SELECT "b"."name" AS "table_2.name" FROM "table" AS "a" INNER JOIN "table2" AS "b" ON ("a"."table_2" = "b"."id")

Expected behaviour

OOQuery('table', dummy_fk).select(
    ['table_2.name'], as_={"table_2.name": "t2_name"}
).where([])

# SELECT "b"."name" AS "t2_name" FROM "table" AS "a" INNER JOIN "table2" AS "b" ON ("a"."table_2" = "b"."id")

Evalua "0" com a "None" en un search

Cas real trobat:

inv_obj = self.pool.get('account.invoice')
search_params = [
    ('pending_state.active', '=', True),
    ('pending_state.pending_days', '>', 0),
    ('type', '=', 'out_invoice'),
    ('state', '=', 'open')
]
q = OOQuery(inv_obj, cursor, uid)
sql = q.select(['id']).where(search_params)

Si es mira el valor de sql (fent un list(sql)) es veu que el valor dels parametres que es passen son els següents:

(True, None, 'out_invoice', 'open')

S'ha transformat el 0 a un None. Llavors el postgres no troba els registres en que el camp pending_days siguin > 0 ja que en realitat executa INTEGER > NULL que sempre és False.

Query domain could be inmutable

This line is modifying the query domain given to the parser of the where clause and leaving it empty.

expression = query.pop()

This makes that a query coded like this to fail.

params = [('id', 'in', (1,2,3,4))]
sql = OOQuery(self, cursor, uid).select(['name']).where(params)
assert params, "Params are empty!"

To avoid unexpected behavior, the could be done by consulting the query params instead of emptying it.

Support different nested typed joins

Continues #29 with a new syntax:

q = OOQuery('account_invoice')
q.select([
    LeftJoin('partner_id').RightJoin('parent_id.name')
]).where([
(RightOuterJoin('company_id.name'), '=', 'Tiny splr')
])

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.