コード例 #1
0
ファイル: admin.py プロジェクト: BackupTheBerlios/useless-svn
class AdminConnection(object):
    """This isn't being used anymore.  It's been updated to use
    the BasicConnection class, but it's probably going to be
    transferred to another object one day, or removed
    completely."""
    def __init__(self, user=None, host=None, dbname=None, passwd=None,
                 port=5432):
        object.__init__(self)
        self.conn = BasicConnection(user=user, host=host, dbname=dbname,
                                    passwd=passwd, port=port)
        self.cursor = StatementCursor(self.conn, name='AdminConnection')
        self.stmt = Statement('select')
        self.dbname = dbname
        self.set_path(cfg.get('database', 'export_path'))

    def set_path(self, directory):
        self.path = directory
        makepaths(self.path)
        os.system('chmod 777 %s' % self.path)
        
    def to_tsv(self, table, key=None):
        self.stmt.table = table
        query = self.stmt.select(order=key)
        tsv = file(join(self.path, table + '.tsv'), 'w')
        self.cursor.execute(query)
        fields = [x[0] for x in self.cursor.description]
        tsv.write('\t'.join(map(quote, fields))+'\n')
        row = self.cursor.fetchone()
        while row:
            line = []
            for field in row:
                if field == None:
                    field = 'NULL'
                else:
                    field = str(field)
                line.append(quote(field))
            tsv.write('\t'.join(line)+'\n')
            row = self.cursor.fetchone()
        tsv.close()
        
    def set_table(self, table):
        self.stmt.set_table(table)

    def insert(self, table):
        self.cursor.set_table(table)
        tsv = Parser(join(self.path, table + '.tsv'))
        for row in tsv:
            self.cursor.insert(data=row)

    def copyto(self, table):
        path = join(self.path, table + '.bkup')
        self.cursor.copyto(table, path)

    def copyfrom(self, table):
        path = join(self.path, table + '.bkup')
        self.cursor.copyfrom(table, path)


    def backup(self):
        map(self.copyto, self.cursor.tables())
コード例 #2
0
ファイル: main.py プロジェクト: umeboshi2/paella
def select_multisuite_union(suites, table, fields=None, clause=None):
    stmt = Statement()
    if fields is not None:
        stmt.fields = fields
    if clause is not None:
        stmt.set_clause(clause)
    stmts = [stmt.select(table='%s_%s' % (s, table)) for s in suites]
    return ' UNION '.join(stmts)
コード例 #3
0
ファイル: admin.py プロジェクト: BackupTheBerlios/useless-svn
class AdminConnection(object):
    def __init__(self, cfg, dbname):
        object.__init__(self)
        self.conn = QuickConn(cfg)
        self.cursor = StatementCursor(self.conn, name='AdminConnection')
        self.stmt = Statement('select')
        self.dbname = dbname
        self.set_path(cfg.get('database', 'export_path'))

    def set_path(self, directory):
        self.path = directory
        makepaths(self.path)
        os.system('chmod 777 %s' % self.path)
        
    def to_tsv(self, table, key=None):
        self.stmt.table = table
        query = self.stmt.select(order=key)
        tsv = file(join(self.path, table + '.tsv'), 'w')
        self.cursor.execute(query)
        fields = [x[0] for x in self.cursor.description]
        tsv.write('\t'.join(map(quote, fields))+'\n')
        row = self.cursor.fetchone()
        while row:
            line = []
            for field in row:
                if field == None:
                    field = 'NULL'
                else:
                    field = str(field)
                line.append(quote(field))
            tsv.write('\t'.join(line)+'\n')
            row = self.cursor.fetchone()
        tsv.close()
        
    def set_table(self, table):
        self.stmt.set_table(table)

    def insert(self, table):
        self.cursor.set_table(table)
        tsv = Parser(join(self.path, table + '.tsv'))
        for row in tsv:
            self.cursor.insert(data=row)

    def copyto(self, table):
        path = join(self.path, table + '.bkup')
        self.cursor.copyto(table, path)

    def copyfrom(self, table):
        path = join(self.path, table + '.bkup')
        self.cursor.copyfrom(table, path)


    def backup(self):
        map(self.copyto, self.cursor.tables())
コード例 #4
0
ファイル: cfdisk.py プロジェクト: BackupTheBerlios/paella-svn
 def approve_disk(self, diskname):
     clause = Eq('diskname', diskname)
     workspace = 'partition_workspace'
     sql = Statement('select')
     sql.table = workspace
     sql.clause = clause
     new_rows = sql.select(order='partition')
     if diskname not in [r.diskname for r in self.cursor.select(table='disks')]:
         self.cursor.insert(table='disks', data=dict(diskname=diskname))
     else:
         self.cursor.delete(table='partitions', clause=clause)
     self.cursor.execute('insert into partitions %s' % new_rows)
コード例 #5
0
ファイル: cfdisk.py プロジェクト: pombredanne/paella-svn
 def approve_disk(self, diskname):
     clause = Eq('diskname', diskname)
     workspace = 'partition_workspace'
     sql = Statement('select')
     sql.table = workspace
     sql.clause = clause
     new_rows = sql.select(order='partition')
     if diskname not in [
             r.diskname for r in self.cursor.select(table='disks')
     ]:
         self.cursor.insert(table='disks', data=dict(diskname=diskname))
     else:
         self.cursor.delete(table='partitions', clause=clause)
     self.cursor.execute('insert into partitions %s' % new_rows)
コード例 #6
0
 def __init__(self, conn=None):
     if conn is None:
         self.conn = KonsultantConnection()
     else:
         self.conn = conn
     self.stmt = Statement()
     self.mcursor = StatementCursor(self.conn)
コード例 #7
0
ファイル: admin.py プロジェクト: BackupTheBerlios/useless-svn
 def __init__(self, cfg, dbname):
     object.__init__(self)
     self.conn = QuickConn(cfg)
     self.cursor = StatementCursor(self.conn, name='AdminConnection')
     self.stmt = Statement('select')
     self.dbname = dbname
     self.set_path(cfg.get('database', 'export_path'))
コード例 #8
0
ファイル: admin.py プロジェクト: BackupTheBerlios/useless-svn
 def __init__(self, user=None, host=None, dbname=None, passwd=None,
              port=5432):
     object.__init__(self)
     self.conn = BasicConnection(user=user, host=host, dbname=dbname,
                                 passwd=passwd, port=port)
     self.cursor = StatementCursor(self.conn, name='AdminConnection')
     self.stmt = Statement('select')
     self.dbname = dbname
     self.set_path(cfg.get('database', 'export_path'))
コード例 #9
0
 def __init__(self):
     object.__init__(self)
     self.stmt = Statement()
     self.id = None
     self.idcol = None
     self.fields = []
     self.table = None
     self.hasMany = {}
     self.hasOne = {}
コード例 #10
0
 def __init__(self, dsn, name, parent=None, objname=None):
     QSqlDatabase.__init__(self, 'QPSQL7', name, parent, objname)
     self.conn = BasicConnection(**dsn)
     self.setDatabaseName(dsn['dbname'])
     self.setHostName(dsn['host'])
     self.dbuser = dsn['user']
     self.setUserName(self.dbuser)
     self.stmt = Statement()
     self.mcursor = StatementCursor(self.conn)
コード例 #11
0
def select_multisuite_union(suites, table, fields=None, clause=None):
    stmt = Statement()
    if fields is not None:
        stmt.fields = fields
    if clause is not None:
        stmt.set_clause(clause)
    stmts = [stmt.select(table='%s_%s' % (s, table)) for s in suites]
    return ' UNION '.join(stmts)
コード例 #12
0
 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)
コード例 #13
0
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)
コード例 #14
0
class DataObject(object):
    def __init__(self):
        object.__init__(self)
        self.stmt = Statement()
        self.id = None
        self.idcol = None
        self.fields = []
        self.table = None
        self.hasMany = {}
        self.hasOne = {}
        
    def select(self, fields=None, table=None, clause=None):
        if fields is None:
            fields = self.fields            
        if table is None:
            table = self.table
        if clause is None:
            clause = Eq(self.idcol, self.id)
        nfields = []
        for field in fields:
            if field in self.hasOne:
                nfields.append(self.hasOne[field].idcol)
            elif field in self.hasMany:
                #nfields.append(self.hasMany[field].idcol)
                pass
            else:
                nfields.append(field)
        #for field in self.hasMany:
            #obj = self.hasMany[field]
            #rtable = self.hasMany_tables[field]
            #clause &= In(obj.idcol, obj.select(fields=[obj.idcol], table=rtable, clause=clause))
        for field in self.hasOne:
            obj = self.hasOne[field]
        return self.stmt.select(fields=nfields, table=table, clause=clause)

    def getMany(self, manyfield, clause=None):
        if clause is None:
            clause = Eq(self.idcol, self.id)
        obj = self.hasMany[manyfield]
        rtable = self.hasMany_tables[manyfield]        
        rid = obj.idcol
        clause = In(obj.idcol, self.stmt.select(fields=[obj.idcol], table=rtable, clause=clause))
        return obj.select(fields=obj.fields, table=obj.table, clause=clause)

    def getAllfields(self, allids=None):
        if allids is None:
            allids = [self.idcol]
        else:
            if self.idcol not in allids:
                allids.append(self.idcol)
        for obj in self.hasMany:
            for field in self.hasMany[obj].getAllfields(allids):
                if field not in allids:
                    allids.append(field)
        for obj in self.hasOne:
            for field in self.hasOne[obj].getAllfields(allids):
                if field not in allids:
                    allids.append(field)
        return allids

    def getData(self, idcol, clause=None):
        ho = [f for f in self.hasOne if self.hasOne[f].idcol == idcol]
        hm = [f for f in self.hasMany if self.hasMany[f].idcol == idcol]
        print ho, hm, 'ho hum'
        if idcol == self.idcol:
            return self.select()
        else:
            if len(hm):
                for f in hm:
                    clause = In(idcol, self.select(fields=[idcol], clause='TRUE'))
                    print clause
        for m, o in self.hasMany.items():
            clause = Eq(self.idcol, self.id)
            rtable = self.hasMany_tables[m]
            sel = self.stmt.select(fields=[o.idcol], table=rtable, clause=clause)
            print In(o.idcol, sel)
コード例 #15
0
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)
コード例 #16
0
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)
コード例 #17
0
 def __init__(self, conn, name=None):
     CommandCursor.__init__(self, conn, name=name)
     self.stmt = Statement("select")