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
def execute(sql): try: connection.my_connection().execute(sql) except Exception as e: print(e) finally: connection.my_connection().close()
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)
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
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)
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
def insert(instance): values = instance.to_json() q, v = Query(instance.__class__).values(values).insert() connection.my_connection().execute(q, v) return True
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)
def drop_all(self): sql = "DROP TABLE tb_Person; DROP TABLE tb_Age;" connection.my_connection().execute(sql)
def drop_person(self): sql = "DROP TABLE tb_Person;" connection.my_connection().execute(sql)
def reset(): try: clear() finally: connection.my_connection().close()