def _save(self, storable, factory): """ Internal function that is responsible for doing the actual saving. """ if not(storable.is_dirty()): return False table = storable.get_table() data = storable.get_data() locks_allowed = (not bool(self.req)) or self.req.app.config.get('use_db_locks', True) use_locks = False primary_key = factory.get_primary_key() if(storable.is_new()): if(factory.uses_guids()): data[primary_key] = self.fetch_id(storable) elif(locks_allowed): use_locks = True self.pool.runOperation('LOCK TABLES `%s` WRITE' % table) else: raise RuntimeError("Broken program logic is trying to lock an unlockable database.") query = sql.build_insert(table, data) else: query = sql.build_update(table, data, {primary_key:storable.get_id()}) self.log(query) try: self.pool.runOperation(query) if not(factory.uses_guids()): if not(storable.get_id()): rows = self.pool.runQuery('SELECT MAX(%s) AS `id` FROM `%s`' % (primary_key, table)) new_id = rows[0]['id'] storable.set_id(new_id) finally: if(use_locks): self.pool.runOperation('UNLOCK TABLES') storable.clean() storable.set_new(False) storable.set_factory(factory) return True
def do_save(self, attribs): attribs['data'] = cPickle.dumps(attribs['data'], 1) if(self.is_clean()): del attribs['data'] update_query = sql.build_update('session', attribs, {'id':self.id()}) self.debug(repr(update_query)) self._pool.runOperation(update_query) elif(self.is_new()): attribs['id'] = self.id() insert_query = sql.build_insert('session', attribs) self.debug(repr(insert_query)) self._pool.runOperation(insert_query) else: attribs['id'] = self.id() #replace_query = sql.build_replace('session', attribs) update_query = sql.build_update('session', attribs, {'id':self.id()}) self.debug(repr(update_query)) self._pool.runOperation(update_query)
def update_storable(self, req, form, storable): """ @see: L{modu.editable.define.definition.get_element()} """ form_data = req.data[form.name] store = storable.get_store() item_id = storable.get_id() delete_query = sql.build_delete(self['ntof'], {self['ntof_n_id']:item_id}) store.pool.runOperation(delete_query) if(self.name in form_data): values = form_data[self.name].value if(isinstance(values, dict)): values = values[self.name + '-autocomplete'] if not(isinstance(values, list)): values = [values] data = [{self['ntof_n_id']:item_id, self['ntof_f_id']:getattr(val, 'value', val)} for val in values] insert_query = sql.build_insert(self['ntof'], data, **self.get('ntof_extras', {})) store.pool.runOperation(insert_query) elif(self.get('required', False)): # A conundrum... # It's got to be a postwrite field, because a new record would # have to be saved before we could insert a record elsewhere with # a foreign key (supposing for a minute we weren't use MySQL, argh) # # This means that it's impossible for this field to stop the writing # of the record at this point, thus 'required' is currently meaningless. # # Should there be a way for a postwrite field to validate separately, # before the write? # # I think the way it was supposed to work in Procuro was that if you # are using GUIDs, you can fill the field at creation time, otherwise # you saw a field that told you to save before editing (lame). return False return True
def test_build_insert_raw(self): query = sql.build_insert('table', {'col2':'col2_data', 'col1':'col1_data'}); expecting = "INSERT INTO `table` (`col1`, `col2`) VALUES ('col1_data', 'col2_data')" self.failUnlessEqual(query, expecting, 'Got "%s" when expecting "%s"' % (sql, expecting))
def test_build_insert_dot_syntax(self): query = sql.build_insert('db.table', {'col2':'col2_data', 'col1':sql.RAW("ENCRYPT('something')")}); expecting = "INSERT INTO db.`table` (`col1`, `col2`) VALUES (ENCRYPT('something'), 'col2_data')" self.failUnlessEqual(query, expecting, 'Got "%s" when expecting "%s"' % (sql, expecting))
def test_build_multiple_insert(self): query = sql.build_insert('table', [{'col2':'col2_data', 'col1':sql.RAW("ENCRYPT('something')")}, {'col2':'col2_data', 'col1':sql.RAW("ENCRYPT('something')")}]); expecting = "INSERT INTO `table` (`col1`, `col2`) VALUES (ENCRYPT('something'), 'col2_data'), (ENCRYPT('something'), 'col2_data')" self.failUnlessEqual(query, expecting, 'Got "%s" when expecting "%s"' % (sql, expecting))
def test_build_insert2(self): query = sql.build_insert('table', col2='col2_data', col1=sql.RAW("ENCRYPT('something')")); expecting = "INSERT INTO `table` (`col1`, `col2`) VALUES (ENCRYPT('something'), 'col2_data')" self.failUnlessEqual(query, expecting, 'Got "%s" when expecting "%s"' % (sql, expecting))