Description:
There is duplicate code on the creation of the Generators in both saveInternal and updateInternal functions of the save handler.
Additionally there are two bugs.
1. There is no exception if the generator class does not exist, producing a fatal error on the persistence check.
2. generators are re-instantiated for each saving of an object, producing considerable overhead.
A patch is attached, that moves the duplicate code into a private getIdGenerator() method which also fixes both bugs stated above.
Environment:
Operating System:
PHP Version: (please be specific, like '4.4.3' or '5.1.5')
Database and version:
Browser (and version):
Attachments
ezcPoSaveUpdateInternalCodeDuplicationRemoval.diff (4.2 kb) added by Benjamin Eberlei
Index: PersistentObject/src/handlers/save_handler.php
===================================================================
--- PersistentObject/src/handlers/save_handler.php (revision 10038)
+++ PersistentObject/src/handlers/save_handler.php (working copy)
@@ -319,21 +339,7 @@
}
// Set up and execute the query.
- $q = $this->database->createInsertQuery();
- $q->insertInto( $this->database->quoteIdentifier( $def->table ) );
- foreach ( $castedState as $name => $value )
- {
- if ( $name !== $def->idProperty->propertyName )
- {
- // Set each of the properties.
- $q->set(
- $this->database->quoteIdentifier(
- $def->properties[$name]->columnName
- ),
- $q->bindValue( $value, null, $def->properties[$name]->databaseType )
- );
- }
- }
+ $q = $this->buildInsertQuery($def, $castedState);
// Atomic operation
$this->database->beginTransaction();
@@ -417,21 +411,81 @@
}
// Set up and execute the query
- $q = $this->database->createUpdateQuery();
- $q->update( $this->database->quoteIdentifier( $def->table ) );
+ $q = $this->buildUpdateQuery($def, $state);
+
+ $this->session->performQuery( $q, true );
+ }
+
+ /**
+ * Build insert query based on a definition and a state.
+ *
+ * @param ezcPersistentObjectDefinition $def
+ * @param array $state
+ * @return ezcQueryInsert
+ */
+ private function buildInsertQuery($def, $state)
+ {
+ $q = $this->database->createInsertQuery();
+ $q->insertInto( $this->database->quoteIdentifier( $def->table ) );
+ $q = $this->bindNonIdPropertyValuesToQuery($q, $def, $state);
+ return $q;
+ }
+
+ /**
+ * Bind Non Id Properties to the given Query
+ *
+ * @param ezcQueryUpdate|ezcQueryInsert $q
+ * @param ezcPersistentObjectDefinition $def
+ * @param array $state
+ * @return ezcQueryUpdate|ezcQueryInsert
+ */
+ private function bindNonIdPropertyValuesToQuery($q, $def, $state)
+ {
foreach ( $state as $name => $value )
{
- // Skip the id field.
- if ( $name != $def->idProperty->propertyName )
+ if ( $name !== $def->idProperty->propertyName )
{
// Set each of the properties.
$q->set(
$this->database->quoteIdentifier(
- $def->properties[$name]->columnName ),
- $q->bindValue( $value, null, $def->properties[$name]->databaseType )
- );
+ $def->properties[$name]->columnName
+ ),
+ $q->bindValue( $value, null, $def->properties[$name]->databaseType )
+ );
}
}
+ return $q;
+ }
+
+ /**
+ * Build an Update query given a definition and a state.
+ *
+ * @param ezcPersistentObjectDefinition $def
+ * @param array $state
+ * @return ezcQueryUpdate
+ */
+ private function buildUpdateQuery($def, $state)
+ {
+ // Set up and execute the query
+ $q = $this->database->createUpdateQuery();
+ $q->update( $this->database->quoteIdentifier( $def->table ) );
+ $q = $this->bindNonIdPropertyValuesToQuery($q, $def, $state);
+ $q = $this->buildUpdateWhereQuery($q, $def, $state);
+ return $q;
+ }
+
+ /**
+ * Build where clause for update query.
+ *
+ * @param ezcQueryUpdate $q
+ * @param ezcPersistentObjectDefinition $def
+ * @param array $state
+ * @return ezcQueryUpdate
+ */
+ private function buildUpdateWhereQuery($q, $def, $state)
+ {
+ $idValue = $state[$def->idProperty->propertyName];
+
$q->where(
$q->expr->eq(
$this->database->quoteIdentifier(
@@ -441,7 +495,7 @@
)
);
- $this->session->performQuery( $q, true );
+ return $q;
}
/**
Attachments (1)
Links (1)
History
History (9)