Пример #1
0
def delete(cls, args=None, uuid=None):
    args = args or {}
    if uuid:
        args['uuid'] = uuid
    sql, values = Query(cls).where(args).delete()
    connection.my_connection().execute(sql, values)
    return True
Пример #2
0
def execute(sql):
    try:
        connection.my_connection().execute(sql)
    except Exception as e:
        print(e)
    finally:
        connection.my_connection().close()
Пример #3
0
def create_database(models):
    if not isinstance(models, list):
        models = [models]

    all_queries = ""
    for table in models:
        all_queries += table.create_table_sql()

    connection.my_connection().execute(all_queries)
Пример #4
0
def exist(cls, where=None, uuid=None):
    where = where or {}
    if uuid:
        where['uuid'] = uuid

    q = Query(cls).select(["count(*)"]).where(where).limit(1)
    sql, where = q.query()
    connection.my_connection().execute(sql, where)
    results = connection.my_connection().fetchall()
    return results[0][0] > 0
Пример #5
0
def clear():
    connection.my_connection().execute(
        "SELECT t.table_name FROM " "information_schema.tables " "t where t.table_schema = 'public'"
    )

    result = connection.my_connection().fetchall()
    sql = ""
    for i in result:
        sql += "TRUNCATE " + i[0] + " CASCADE;"

    connection.my_connection().execute(sql)
Пример #6
0
def update(instance, where=None):
    where = where or {}
    values = instance.to_json()
    q, v = Query(instance.__class__).values(values).where(where).update()
    connection.my_connection().execute(q, v)
    return True
Пример #7
0
def insert(instance):
    values = instance.to_json() 
    q, v = Query(instance.__class__).values(values).insert()
    connection.my_connection().execute(q, v)
    return True
Пример #8
0
def query(query):
    sql, args = query.query()
    connection.my_connection().execute(sql, args)
    results = connection.my_connection().fetchall()
    class_with_fields = query.split_select(connection.my_connection().description())
    return __objects_resulted_from_join(results, class_with_fields)
Пример #9
0
 def drop_all(self):
     sql = "DROP TABLE tb_Person; DROP TABLE tb_Age;"
     connection.my_connection().execute(sql)
Пример #10
0
 def drop_person(self):
     sql = "DROP TABLE tb_Person;"
     connection.my_connection().execute(sql)
Пример #11
0
def reset():
    try:
        clear()
    finally:
        connection.my_connection().close()