Author: johannes Date: 2007-09-26 07:44:15 -0500 (Wed, 26 Sep 2007) New Revision: 9783
Modified: trunk/gnue-appserver/src/data.py trunk/gnue-common/src/datasources/GDataSource.py trunk/gnue-common/src/datasources/drivers/Base/Connection.py trunk/gnue-common/src/datasources/drivers/Base/RecordSet.py trunk/gnue-common/src/datasources/drivers/Base/ResultSet.py trunk/gnue-common/src/datasources/drivers/DBSIG2/Connection.py trunk/gnue-common/src/datasources/drivers/file/inifile.py trunk/gnue-common/src/datasources/drivers/other/appserver.py Log: Improved handing of row-id-fields. Do not automatically add row-id-fields or primary-key-fields to read-only resultsets. Modified: trunk/gnue-appserver/src/data.py =================================================================== --- trunk/gnue-appserver/src/data.py 2007-09-26 08:16:07 UTC (rev 9782) +++ trunk/gnue-appserver/src/data.py 2007-09-26 12:44:15 UTC (rev 9783) @@ -677,7 +677,7 @@ datasource = _createDatasource (connections, database, content, order) condition = GConditions.buildCondition (conditions) - return datasource.createResultSet (condition) + return datasource.createResultSet (condition, True) # ============================================================================= @@ -910,7 +910,7 @@ if self.__cache.inserted: for (table, row) in self.__orderInserts (): fields = tables [table] [row] - self.__backend.insert (table, fields) + self.__backend.insert (table, {'gnue_id': row}, fields) if not self.__backend._need_rollback_after_exception_: self.__cache.makeClean (table, row) Modified: trunk/gnue-common/src/datasources/GDataSource.py =================================================================== --- trunk/gnue-common/src/datasources/GDataSource.py 2007-09-26 08:16:07 UTC (rev 9782) +++ trunk/gnue-common/src/datasources/GDataSource.py 2007-09-26 12:44:15 UTC (rev 9783) @@ -178,7 +178,7 @@ def __trigger_simpleQuery (self, maskDict): conditions = GConditions.buildConditionFromDict ( maskDict, GConditions.GClike) - resultSet = self.createResultSet (conditions) + resultSet = self.createResultSet (conditions, True) return [record.getFieldsAsDict () for record in resultSet] # --------------------------------------------------------------------------- @@ -265,8 +265,6 @@ # primarykey attribute: remember them and reference them if hasattr (self, 'primarykey'): self.__primarykeyFields = self.primarykey.split (',') - # Make sure the primary key is included in the field references - self.referenceFields (self.__primarykeyFields) if hasattr(self, 'rowid'): self.__exp_rowid = self.rowid @@ -314,17 +312,10 @@ primarykeyFields = self._connection._primarykeyFields_ if primarykeyFields: self.__primarykeyFields = primarykeyFields - self.referenceFields (self.__primarykeyFields) - # Include the rowid in list of field references rowidField = self._connection._rowidField_ or self.__exp_rowid if rowidField: - # TODO: checks if the rowid is available and should be used go here: - # 1. if primary key should be prefered, don't set self.__rowidField - # 2. try if rowidField is available in current table/view - if not self.__primarykeyFields: - self.__rowidField = rowidField - self.referenceField (self.__rowidField) + self.__rowidField = rowidField # Add ourselves into the "global" datasource dictionary self._topObject._datasourceDictionary [self.name.lower ()] = self @@ -640,10 +631,12 @@ resultset = self.__newResultSet (readOnly, masterRecord) + fields = self.__get_fields(readOnly) + if self.type == 'object': resultset.query ('object', self.cache, table = self.table, - fieldnames = self.__fieldReferences.keys (), + fieldnames = fields, condition = cond, sortorder = self.__sortorder and self.__sortorder or [], distinct = self.distinct) @@ -692,6 +685,8 @@ self.__masterPkFields, self.__masterFkFields): defaultData [detailfield] = masterRecord.getField (masterfield) + fields = self.__get_fields(readOnly) + # Create the ResultSet instance return self.__resultSetClass ( defaultData = defaultData, @@ -700,7 +695,7 @@ rowidField = self.__rowidField, primarykeyFields = self.__primarykeyFields, primarykeySeq = self.primarykeyseq, - boundFields = self.__fieldReferences.keys (), + boundFields = fields, requery = self.requery, readonly = readOnly, details = self.__details, @@ -711,6 +706,24 @@ # Activate a result set # --------------------------------------------------------------------------- + def __get_fields(self, readOnly): + + fields = self.__fieldReferences.keys() + if not readOnly and self.type != 'unbound': + if self.__rowidField and not self.__rowidField in fields: + fields.append(self.__rowidField) + + if self.__primarykeyFields: + for pkf in self.__primarykeyFields: + if not pkf in fields: + fields.append(pkf) + return fields + + + # --------------------------------------------------------------------------- + # Activate a result set + # --------------------------------------------------------------------------- + def _activateResultSet (self, resultSet): """ Make the given result set the active result set for this datasource. Modified: trunk/gnue-common/src/datasources/drivers/Base/Connection.py =================================================================== --- trunk/gnue-common/src/datasources/drivers/Base/Connection.py 2007-09-26 08:16:07 UTC (rev 9782) +++ trunk/gnue-common/src/datasources/drivers/Base/Connection.py 2007-09-26 12:44:15 UTC (rev 9783) @@ -226,11 +226,13 @@ # Insert a new record in the backend # --------------------------------------------------------------------------- - def insert (self, table, newfields): + def insert (self, table, oldfields, newfields): """ Insert a new record in the backend. @param table: Table name. + @param oldfields: Fieldname/Value dictionary of fields to find the existing + record (aka where-clause). @param newfields: Fieldname/Value dictionary of data to insert. @return: The rowid of the newly inserted record, or None if no rowid's are supported. @@ -242,7 +244,7 @@ checktype (newfields, dict) assert gEnter (8) - rowid = self._insert_ (table, newfields) + rowid = self._insert_ (table, oldfields, newfields) self.__pending = True assert gLeave (8, rowid) return rowid @@ -549,7 +551,7 @@ # --------------------------------------------------------------------------- - def _insert_ (self, table, newfields): + def _insert_ (self, table, oldfields, newfields): """ Insert a new record in the backend (to be implemented by descendants). @@ -558,6 +560,8 @@ record must be returned from this method. @param table: Table name. + @param oldfields: Fieldname/Value dictionary of fields to find the existing + record (aka where-clause) @param newfields: Fieldname/Value dictionary of data to insert. @return: The rowid of the newly inserted record, or None if no rowid's are supported. Modified: trunk/gnue-common/src/datasources/drivers/Base/RecordSet.py =================================================================== --- trunk/gnue-common/src/datasources/drivers/Base/RecordSet.py 2007-09-26 08:16:07 UTC (rev 9782) +++ trunk/gnue-common/src/datasources/drivers/Base/RecordSet.py 2007-09-26 12:44:15 UTC (rev 9783) @@ -194,16 +194,11 @@ self.__fields = dict.fromkeys (self.__boundFields, None) self.__initialData = self.__fields.copy () - # 3. Set the primary key fields to dirty, so they will be included in the - # insert statement in any case. - for fieldname in self.__primarykeyFields: - self.__modifiedFlags [fieldname] = True - - # 4. Get default values from DataSource (includes link to master). + # 3. Get default values from DataSource (includes link to master). for (fieldname, value) in defaultData.items (): self [fieldname] = value - # 5. Notify event listener about new record + # 4. Notify event listener about new record self.__dispatchEvent ('dsRecordInserted') self.__initializing = False @@ -586,15 +581,27 @@ modifiedFields [field] = self.__fields [field] if self.__inserted: - rowid = self.__connection.insert (self.__tablename, modifiedFields) + rowid = self.__connection.insert (self.__tablename, + self.__wherefields(), modifiedFields) + + id_field = None if self.__rowidField: - self.__fields [self.__rowidField] = rowid + id_field = self.__rowidField + else: + if len (self.__primarykeyFields) == 1 and \ + self [self.__primarykeyFields [0]] is None and \ + self.__primarykeySeq is None and rowid is not None: + id_field = self.__primarykeyFields[0] + + if id_field: + self.__fields [id_field] = rowid # Also set initialData so the requery can work - self.__initialData [self.__rowidField] = rowid + self.__initialData [id_field] = rowid # Requery all the fields that are important for inserting details. # A backend trigger could have e.g. generated a primary key. # TODO: We could save this work for cases where the detail # resultsets don't have any inserted records. + if self.__detailLinkFlags: self.__do_requery (self.__detailLinkFlags.keys ()) else: Modified: trunk/gnue-common/src/datasources/drivers/Base/ResultSet.py =================================================================== --- trunk/gnue-common/src/datasources/drivers/Base/ResultSet.py 2007-09-26 08:16:07 UTC (rev 9782) +++ trunk/gnue-common/src/datasources/drivers/Base/ResultSet.py 2007-09-26 12:44:15 UTC (rev 9783) @@ -780,10 +780,11 @@ @param otherResultSet: the other ResultSet to merge """ - if self.__primarykeyFields: + if self.__rowidField: + keyFields = [self.__rowidField] + + elif self.__primarykeyFields: keyFields = self.__primarykeyFields - elif self.__rowidField: - keyFields = [self.__rowidField] else: return Modified: trunk/gnue-common/src/datasources/drivers/DBSIG2/Connection.py =================================================================== --- trunk/gnue-common/src/datasources/drivers/DBSIG2/Connection.py 2007-09-26 08:16:07 UTC (rev 9782) +++ trunk/gnue-common/src/datasources/drivers/DBSIG2/Connection.py 2007-09-26 12:44:15 UTC (rev 9783) @@ -123,7 +123,7 @@ # --------------------------------------------------------------------------- - def _insert_ (self, table, newfields): + def _insert_ (self, table, oldfields, newfields): fields = [] values = [] parameters = {} Modified: trunk/gnue-common/src/datasources/drivers/file/inifile.py =================================================================== --- trunk/gnue-common/src/datasources/drivers/file/inifile.py 2007-09-26 08:16:07 UTC (rev 9782) +++ trunk/gnue-common/src/datasources/drivers/file/inifile.py 2007-09-26 12:44:15 UTC (rev 9783) @@ -172,7 +172,7 @@ # Insert new record # --------------------------------------------------------------------------- - def _insert_ (self, table, newfields): + def _insert_ (self, table, oldfields, newfields): parser = self.__getParser (self._getFilename (table), table) Modified: trunk/gnue-common/src/datasources/drivers/other/appserver.py =================================================================== --- trunk/gnue-common/src/datasources/drivers/other/appserver.py 2007-09-26 08:16:07 UTC (rev 9782) +++ trunk/gnue-common/src/datasources/drivers/other/appserver.py 2007-09-26 12:44:15 UTC (rev 9783) @@ -435,9 +435,9 @@ # --------------------------------------------------------------------------- - def _insert_ (self, table, newfields): + def _insert_ (self, table, oldfields, newfields): f = newfields.copy () - id = f.pop (u'gnue_id') + id = oldfields[u'gnue_id'] self._session.store (table, [id], f.keys (), [f.values ()]) # --------------------------------------------------------------------------- _______________________________________________ commit-gnue mailing list commit-gnue@gnu.org http://lists.gnu.org/mailman/listinfo/commit-gnue