def save(self): # If it doesn't have an ID (Either nonexistant or not set), then call the # create() method to do an INSERT instead of an UPDATE. try: if not self.id: self.create() except AttributeError: self.create() base = 'UPDATE %s SET ' % self.Table diff = self.diff() fields = '' if not diff: return None # Build field and value list if there is a difference for key in diff: field = self._get_field_by_name(key) value = field.query(diff[key]) fields += ', ' + field.name + ' = ' + Query.format_type(value) base += fields[2:] + ' WHERE id = ' + str(self.id) Query.query(base) # Right now, just spit back the instance of self return self
def create(self): base = 'INSERT INTO %s ' % self.Table names = [] values = [] for f in self.Structure: try: # Try to get the attribute, else raise an exception if it can't be null. name = f.name value = Query.format_type(f.query(self.__getattribute__(name))) names.append(name);values.append(value) except AttributeError: primary = self.get_primary() if not f.null and f.name != primary.name: raise Exception, 'Field "%s" cannot be null' % f.name # Build the set of fields and VALUES. base += '(%s) ' % (', '.join(names)) base += 'VALUES (%s)' % (', '.join(values)) # Execute and dump the current instance's attribs into the _original. self.Query.query(base) self._original = self._build_original() # Spit yourself back! return self