Exemple #1
0
    def update_one(self, newtuple, keytuple, currtuple=None):
        """Execute a single-tuple UPDATE command using the primary key

        :param newtuple: Tuple with new values
        :param keytuple: Tuple with key values
        :param currtuple: previous version of newtuple
        """
        def update_one_cmd():
            if changed_values or not hasattr(self, 'update_one_cmd'):
                setlist = "SET %s" % ", ".join([
                    '%s = %%(%s)s' % (c, c)
                    for c in list(changed_values.keys())
                ])
                self.update_one_cmd = "UPDATE %s %s %s" % (
                    self.name, setlist,
                    self.where_clause(currtuple is not None))
            return self.update_one_cmd

        if currtuple:
            changed_values = tuple_values_dict(currtuple, newtuple)
            if not changed_values:
                return
        else:
            changed_values = tuple_values_dict(newtuple)
        values = self.key_values_update(keytuple, currtuple)
        values.update(changed_values)
        curs = self.db.execute(update_one_cmd(), values)
        if curs.rowcount != 1:
            self.db.rollback()
            raise DatabaseError("Failed to update %s %r" %
                                (self.extname, self))
        curs.close()
Exemple #2
0
    def update_one(self, newtuple, keytuple, currtuple=None):
        """Execute a single-tuple UPDATE command using the primary key

        :param newtuple: Tuple with new values
        :param keytuple: Tuple with key values
        :param currtuple: previous version of newtuple
        """
        def update_one_cmd():
            if changed_values or not hasattr(self, 'update_one_cmd'):
                setlist = "SET %s" % ", ".join(
                    ['%s = %%(%s)s' % (c, c) for c in
                     list(changed_values.keys())])
                self.update_one_cmd = "UPDATE %s %s %s" % (
                    self.name, setlist,
                    self.where_clause(currtuple is not None))
            return self.update_one_cmd

        if currtuple:
            changed_values = tuple_values_dict(currtuple, newtuple)
            if not changed_values:
                return
        else:
            changed_values = tuple_values_dict(newtuple)
        values = self.key_values_update(keytuple, currtuple)
        values.update(changed_values)
        curs = self.db.execute(update_one_cmd(), values)
        if curs.rowcount != 1:
            self.db.rollback()
            raise DatabaseError("Failed to update %s %r" % (
                self.extname, self))
        curs.close()
Exemple #3
0
    def insert_one(self, newtuple, retkey=False):
        """Execute a single-tuple INSERT command

        :param newtuple: the tuple to be inserted
        :param retkey: indicates assigned key values should be returned
        """
        attrnames = [name for name, typ in newtuple._heading]
        targets = '(%s)' % ", ".join(attrnames)
        values_list = 'VALUES (%s)' % ", ".join(
            ['%%(%s)s' % name for name in attrnames])
        cmd = "INSERT INTO %s %s %s" % (self.name, targets, values_list)
        if retkey:
            cmd += " RETURNING %s" % ", ".join(self.key)
        curs = self.db.execute(cmd, tuple_values_dict(newtuple))
        if curs.rowcount != 1:
            self.db.rollback()
            raise DatabaseError("Failed to add %s %r" % (self.extname, self))
        if retkey:
            attrdict = dict(self.attributes)
            rettuple = Tuple(
                [Attribute(name, attrdict[name].type) for name in self.key])
            row = curs.fetchone()
            for attr, type_ in rettuple._heading:
                setattr(rettuple, attr, row[attr])
        curs.close()
        if retkey:
            return rettuple
Exemple #4
0
    def insert_one(self, newtuple, retkey=False):
        """Execute a single-tuple INSERT command

        :param newtuple: the tuple to be inserted
        :param retkey: indicates assigned key values should be returned
        """
        attrnames = [name for name, typ in newtuple._heading]
        targets = '(%s)' % ", ".join(attrnames)
        values_list = 'VALUES (%s)' % ", ".join(
            ['%%(%s)s' % name for name in attrnames])
        cmd = "INSERT INTO %s %s %s" % (self.name, targets, values_list)
        if retkey:
            cmd += " RETURNING %s" % ", ".join(self.key)
        curs = self.db.execute(cmd, tuple_values_dict(newtuple))
        if curs.rowcount != 1:
            self.db.rollback()
            raise DatabaseError("Failed to add %s %r" % (self.extname, self))
        if retkey:
            attrdict = dict(self.attributes)
            rettuple = Tuple([Attribute(name, attrdict[name].type)
                              for name in self.key])
            row = curs.fetchone()
            for attr, type_ in rettuple._heading:
                setattr(rettuple, attr, row[attr])
        curs.close()
        if retkey:
            return rettuple
Exemple #5
0
def test_tuple_changed_nullable_values():
    "Test tuple_values_dict with changed nullable values"
    tup1 = Tuple([Attribute('attr1', int, 123), Attribute('attr2', str, 'abc'),
                 Attribute('attr3', float, 45.67)])
    tup2 = Tuple([Attribute('attr1', int, 123),
                  Attribute('attr2', str, '', nullable=True),
                 Attribute('attr3', float, 0, nullable=True)])
    assert tuple_values_dict(tup1, tup2) == {'attr2': None, 'attr3': None}
Exemple #6
0
def test_tuple_changed_values_dict():
    "Test tuple_values_dict with second tuple"
    tup1 = Tuple([Attribute('attr1', int, 123), Attribute('attr2', str, 'abc'),
                  Attribute('attr3', float, 45.67)])
    tup2 = Tuple([Attribute('attr1', int, 123), Attribute('attr2', str, 'def'),
                  Attribute('attr3', float, 98.76),
                  Attribute('attr4', str, 'xyz')])
    assert tuple_values_dict(tup1, tup2) == {'attr2': 'def', 'attr3': 98.76,
                                             'attr4': 'xyz'}
Exemple #7
0
def test_tuple_values_dict():
    "Test tuple_values_dict function"
    tup = Tuple([Attribute('attr1', int, 123), Attribute('attr2', str, 'abc'),
                 Attribute('attr3', float, 45.67)])
    assert tuple_values_dict(tup) == {'attr1': 123, 'attr2': 'abc',
                                      'attr3': 45.67}