Exemplo n.º 1
0
 def _update(self):
     valid = self.validate()
     values = []
     for k, v in self.data.items():
         if k != 'uid':
             # don't pass null values
             if v != None:
                 values.append(
                     str(k) + "='" + dbConnect.escape(str(v)) + "'")
             else:
                 values.append(str(k) + "=NULL")
     sql = "UPDATE %s SET %s WHERE uid='%s'" % (
         self.db_table(), ','.join(values), self.data['uid'])
     log.info(sql)
     dbConnect.query_connection(self.db_connection_name(), sql)
Exemplo n.º 2
0
 def update(self, **kwargs):
     if not self.data.has_key('uid') or not self.data['uid']:
         raise ORMException, "Record cannot be updated: missing uid"
     values = []
     for k, v in kwargs.iteritems():
         if k != 'uid':
             # don't pass null values
             if v != None:
                 values.append(
                     str(k) + "='" + dbConnect.escape(str(v)) + "'")
             else:
                 values.append(str(k) + "=NULL")
     sql = "UPDATE %s SET %s WHERE uid='%s'" % (
         self.db_table(), ','.join(values), self.data['uid'])
     log.info(sql)
     dbConnect.query_connection(self.db_connection_name(), sql)
Exemplo n.º 3
0
 def _execute_select(cls, sql, tokens=None):
     log.info("ORM: Connected to: %s" % (cls.DB_ATTRIBUTES))
     records = dbConnect.query_connection(cls.db_connection_name(), sql,
                                          tokens)
     results = []
     for i in records:
         instance = cls()
         instance.data = i
         results.append(instance)
     return results
Exemplo n.º 4
0
    def save(self):
        if self.data.has_key('uid') and self.data['uid']:
            return self._update()
        # validate the data in the object
        # Note this only runs if self.VALIDATION is set
        self.validate()
        # is there a defined unique_key? if so check only for those fields
        if 'UNIQUE_KEY' in dir(self):
            values = []
            for k, v in self.data.items():
                if k in self.UNIQUE_KEY:
                    # don't pass null values
                    if v != None:
                        values.append(
                            str(k) + "='" + dbConnect.escape(str(v)) + "'")
                    else:
                        values.append(str(k) + "=NULL")
        else:
            # is there an exact duplcate of this record
            # by exact we mean everything but a creation_date
            # already in the table?
            values = []
            for k, v in self.data.items():
                if k != 'uid':
                    if k != 'creation_date':
                        # don't pass null values
                        if v != None:
                            values.append(
                                str(k) + "='" + dbConnect.escape(str(v)) + "'")
                        else:
                            values.append(str(k) + "=NULL")
        sql = "SELECT uid FROM %s WHERE %s ORDER BY uid DESC LIMIT 1" % (
            self.db_table(), ' and '.join(values))
        records = dbConnect.query_connection(self.db_connection_name(), sql)
        for row in records:
            self.data['uid'] = row['uid']
            return

        # if not, create the record
        self._create()
        return
Exemplo n.º 5
0
 def _create(self):
     attrs = []
     values = []
     for k, v in self.data.items():
         if k != 'uid':
             attrs.append(str(k))
             if v == 'now()':
                 now = dateutil.mysql_now()
                 values.append("'" + dbConnect.escape(str(now)) + "'")
             elif v != None:
                 values.append("'" + dbConnect.escape(str(v)) + "'")
             else:
                 values.append("NULL")
     sql = "INSERT INTO %s (%s) VALUES (%s)" % (
         self.db_table(), ','.join(attrs), ','.join(values))
     log.info("_CREATE: %s" % (sql))
     cursor = dbConnect.query_connection(self.db_connection_name(), sql)
     # if id is passed in then assume we aren't dealing with auto-increment
     uid = cursor.lastrowid
     self.data['uid'] = uid
     self._is_new = False
Exemplo n.º 6
0
 def delete(self):
     if self.data['uid']:
         dbConnect.query_connection(
             self.db_connection_name(), "DELETE FROM %s WHERE uid='%s'" %
             (self.db_table(), self.data['uid']))