class StatementCursor(CommandCursor): """StatementCursor object. This is the main cursor used in the code, at the moment. The highlevel cursors will be based on this cursor. This cursor has a member 'stmt' which is a Statement from useless-sqlgen. """ def __init__(self, conn, name=None): CommandCursor.__init__(self, conn, name=name) self.stmt = Statement("select") def set_table(self, table): "Set the table for the statement" self.stmt.table = table def set_clause(self, items, cmp="=", join="and"): """Set the clause for the statement. This should be updated to accept clause objects, this member is old now.""" self.stmt.set_clause(items, cmp=cmp, join=join) def set_data(self, data): """Sets the data member of the statement for update and insert statements.""" self.stmt.set(data) def set_fields(self, fields): """Sets the fields of the statement for select statements.""" self.stmt.fields = fields def delete(self, table=None, clause=None): "Perform DELETE FROM table WHERE clause" query = self.stmt.delete(table=table, clause=clause) self.execute(query) def insert(self, table=None, data=None): "Perform INSERT INTO table (data.keys) VALUES (data.values)" query = self.stmt.insert(table=table, data=data) self.execute(query) def update(self, table=None, data=None, clause=None): "Perform UPDATE table SET [key=value for items in data] WHERE clause" query = self.stmt.update(table=table, data=data, clause=clause) self.execute(query) def select(self, fields=None, table=None, clause=None, group=None, having=None, order=None): "Perform SELECT fields FROM table WHERE clause" query = self.stmt.select(fields=fields, table=table, clause=clause, group=group, having=having, order=order) self.execute(query) return self.fetchall() def iselect(self, fields=None, table=None, clause=None, group=None, having=None, order=None): """iselect is a way to use this cursor as an iterator. Like, for row in cursor.iselect(): """ query = self.stmt.select(fields=fields, table=table, clause=clause, group=group, having=having, order=order) self.execute(query) self._already_selected = True return self def select_row(self, fields=None, table=None, clause=None, group=None, having=None, order=None): """Select one row from the database successfully. You can use this member when you need to return exactly one row, or raise an error. """ query = self.stmt.select(fields=fields, table=table, clause=clause, group=group, having=having, order=order) self.execute(query) rows = len(self) if rows == 1: return self.next() elif rows == 0: raise NoExistError else: raise RuntimeError, "bad row count %s" % rows def delete_file(self, conn, field, clause): """delete_file uses select_row to ensure only one file is deleted.""" row = self.select_row(fields=[field], clause=clause) conn.removefile(int(row[field].name)) def update_file(self, conn, field, clause, fileobj): self.delete_file(conn, field, clause) newfile = self.file(conn) update = {field: newfile.name} newfile.write(fileobj.read()) newfile.close() self.update(data=update, clause=clause) def open_file(self, conn, field, clause): row = self.select_row(fields=[field], clause=clause) return self.openfile(conn, row[field].name) def clear(self, **args): "Clears the statement" self.stmt.clear(**args) def fields(self, table=None): """fields here uses the fact that the stmt object may have a table set.""" if table is None: table = self.stmt.table return CommandCursor.fields(self, table)
class BaseDatabase(QSqlDatabase): def __init__(self, dsn, name, parent=None, objname=None): deprecated('useless.kdedb.BaseDatabase is deprecated') QSqlDatabase.__init__(self, 'QPSQL7', name, parent, objname) if hasattr(dsn, 'items'): #if dsn is dictionary self.conn = BasicConnection(**dsn) self.setDatabaseName(dsn['dbname']) self.setHostName(dsn['host']) self.dbuser = dsn['user'] else: #else a conn was passed as dsn self.conn = dsn self.setDatabaseName(self.conn.conn.db) self.setHostName(self.conn.conn.host) self.dbuser = self.conn.conn.user self.setUserName(self.dbuser) self.stmt = Statement() self.mcursor = StatementCursor(self.conn) def set_table(self, table): self.stmt.table = table def set_clause(self, items, cmp='=', join='and'): self.stmt.set_clause(items, cmp=cmp, join=join) def set_data(self, data): self.stmt.set(data) def set_fields(self, fields): self.stmt.fields = fields def qdelete(self, table=None, clause=None): query = self.stmt.delete(table=table, clause=clause) return self.execStatement(query) def qinsert(self, table=None, data=None): query = self.stmt.insert(table=table, data=data) return self.execStatement(query) def qupdate(self, table=None, data=None, clause=None): query = self.stmt.update(table=table, data=data, clause=clause) return self.execStatement(query) def qselect(self, fields=None, table=None, clause=None, group=None, having=None, order=None): query = self.stmt.select(fields=fields, table=table, clause=clause, group=group, having=having, order=order) return self.execStatement(query) def delete(self, table=None, clause=None): query = self.stmt.delete(table=table, clause=clause) return self.mcursor.execute(query) def insert(self, table=None, data=None): query = self.stmt.insert(table=table, data=data) return self.mcursor.execute(query) def update(self, table=None, data=None, clause=None): query = self.stmt.update(table=table, data=data, clause=clause) return self.mcursor.execute(query) def select(self, fields=None, table=None, clause=None, group=None, having=None, order=None): query = self.stmt.select(fields=fields, table=table, clause=clause, group=group, having=having, order=order) self.mcursor.execute(query) return self.mcursor.fetchall() def select_row(self, fields=None, table=None, clause=None, group=None, having=None, order=None): query = self.stmt.select(fields=fields, table=table, clause=clause, group=group, having=having, order=order) self.mcursor.execute(query) rows = len(self.mcursor) if rows == 1: return self.mcursor.next() elif rows == 0: raise NoExistError else: raise RuntimeError, 'bad row count %s' % rows def clear(self, **args): self.stmt.clear(**args) def stdclause(self, data): return reduce(and_, [Eq(k, v) for k,v in data.items()]) def insertData(self, idcol, table, data, commit=True): clause = self.stdclause(data) try: self.mcursor.select_row(fields=[idcol], table=table, clause=clause) except NoExistError: self.mcursor.insert(table=table, data=data) if commit: self.conn.commit() def identifyData(self, idcol, table, data, commit=True): clause = self.stdclause(data) self.insertData(idcol, table, data, commit=commit) return self.mcursor.select_row(fields=['*'], table=table, clause=clause)
class BaseDatabase(object): def __init__(self, conn=None): if conn is None: self.conn = KonsultantConnection() else: self.conn = conn self.stmt = Statement() self.mcursor = StatementCursor(self.conn) def set_table(self, table): self.stmt.table = table def set_clause(self, items, cmp='=', join='and'): self.stmt.set_clause(items, cmp=cmp, join=join) def set_data(self, data): self.stmt.set(data) def set_fields(self, fields): self.stmt.fields = fields def delete(self, table=None, clause=None): query = self.stmt.delete(table=table, clause=clause) return self.mcursor.execute(query) def insert(self, table=None, data=None): query = self.stmt.insert(table=table, data=data) return self.mcursor.execute(query) def update(self, table=None, data=None, clause=None): query = self.stmt.update(table=table, data=data, clause=clause) return self.mcursor.execute(query) def select(self, fields=None, table=None, clause=None, group=None, having=None, order=None): query = self.stmt.select(fields=fields, table=table, clause=clause, group=group, having=having, order=order) self.mcursor.execute(query) return self.mcursor.fetchall() def select_row(self, fields=None, table=None, clause=None, group=None, having=None, order=None): query = self.stmt.select(fields=fields, table=table, clause=clause, group=group, having=having, order=order) self.mcursor.execute(query) rows = len(self.mcursor) if rows == 1: return self.mcursor.next() elif rows == 0: raise NoExistError else: raise Error, 'bad row count %s' % rows def clear(self, **args): self.stmt.clear(**args) def stdclause(self, data): return reduce(and_, [Eq(k, v) for k,v in data.items()]) def insertData(self, idcol, table, data, commit=True): clause = self.stdclause(data) try: self.mcursor.select_row(fields=[idcol], table=table, clause=clause) except NoExistError: self.mcursor.insert(table=table, data=data) if commit: self.conn.commit() def identifyData(self, idcol, table, data, commit=True): clause = self.stdclause(data) self.insertData(idcol, table, data, commit=commit) return self.mcursor.select_row(fields=['*'], table=table, clause=clause)