getNumber()) { //print "Can not select without Primary-Key!"; return false; } $sql = "SELECT * FROM CUSTOMER WHERE (NUMBER = ".$valueObject->getNumber().") "; if ($this->singleQuery(&$conn, $sql, &$valueObject)) return true; else return false; } /** * LoadAll-method. This will read all contents from database table and * build an Vector containing valueObjects. Please note, that this method * will consume huge amounts of resources if table has lot's of rows. * This should only be used when target tables have only small amounts * of data. * * @param conn This method requires working database connection. */ function loadAll(&$conn) { $sql = "SELECT * FROM CUSTOMER ORDER BY NAME ASC "; $searchResults = $this->listQuery(&$conn, $sql); return $searchResults; } /** * create-method. This will create new row in database according to supplied * valueObject contents. Make sure that values for all NOT NULL columns are * correctly specified. Also, if this table does not use automatic surrogate-keys * the primary-key must be specified. After INSERT command this method will * read the generated primary-key back to valueObject if automatic surrogate-keys * were used. * * @param conn This method requires working database connection. * @param valueObject This parameter contains the class instance to be created. * If automatic surrogate-keys are not used the Primary-key * field must be set for this to work properly. */ function create(&$conn, &$valueObject) { $sql = "INSERT INTO CUSTOMER ( NUMBER, NAME, ADDRESS, "; $sql = $sql."CREATED, BALANCE) VALUES (".$valueObject->getNumber().", "; $sql = $sql."'".$valueObject->getName()."', "; $sql = $sql."'".$valueObject->getAddress()."', "; $sql = $sql."'".$valueObject->getCreated()."', "; $sql = $sql."".$valueObject->getBalance().") "; $result = $this->databaseUpdate(&$conn, $sql); return true; } /** * save-method. This method will save the current state of valueObject to database. * Save can not be used to create new instances in database, so upper layer must * make sure that the primary-key is correctly specified. Primary-key will indicate * which instance is going to be updated in database. If save can not find matching * row, NotFoundException will be thrown. * * @param conn This method requires working database connection. * @param valueObject This parameter contains the class instance to be saved. * Primary-key field must be set for this to work properly. */ function save(&$conn, &$valueObject) { $sql = "UPDATE CUSTOMER SET NAME = '".$valueObject->getName()."', "; $sql = $sql."ADDRESS = '".$valueObject->getAddress()."', "; $sql = $sql."CREATED = '".$valueObject->getCreated()."', "; $sql = $sql."BALANCE = ".$valueObject->getBalance().""; $sql = $sql." WHERE (NUMBER = ".$valueObject->getNumber().") "; $result = $this->databaseUpdate(&$conn, $sql); if ($result != 1) { //print "PrimaryKey Error when updating DB!"; return false; } return true; } /** * delete-method. This method will remove the information from database as identified by * by primary-key in supplied valueObject. Once valueObject has been deleted it can not * be restored by calling save. Restoring can only be done using create method but if * database is using automatic surrogate-keys, the resulting object will have different * primary-key than what it was in the deleted object. If delete can not find matching row, * NotFoundException will be thrown. * * @param conn This method requires working database connection. * @param valueObject This parameter contains the class instance to be deleted. * Primary-key field must be set for this to work properly. */ function delete(&$conn, &$valueObject) { if (!$valueObject->getNumber()) { //print "Can not delete without Primary-Key!"; return false; } $sql = "DELETE FROM CUSTOMER WHERE (NUMBER = ".$valueObject->getNumber().") "; $result = $this->databaseUpdate(&$conn, $sql); if ($result != 1) { //print "PrimaryKey Error when updating DB!"; return false; } return true; } /** * deleteAll-method. This method will remove all information from the table that matches * this Dao and ValueObject couple. This should be the most efficient way to clear table. * Once deleteAll has been called, no valueObject that has been created before can be * restored by calling save. Restoring can only be done using create method but if database * is using automatic surrogate-keys, the resulting object will have different primary-key * than what it was in the deleted object. (Note, the implementation of this method should * be different with different DB backends.) * * @param conn This method requires working database connection. */ function deleteAll(&$conn) { $sql = "DELETE FROM CUSTOMER"; $result = $this->databaseUpdate(&$conn, $sql); return true; } /** * coutAll-method. This method will return the number of all rows from table that matches * this Dao. The implementation will simply execute "select count(primarykey) from table". * If table is empty, the return value is 0. This method should be used before calling * loadAll, to make sure table has not too many rows. * * @param conn This method requires working database connection. */ function countAll(&$conn) { $sql = "SELECT count(*) FROM CUSTOMER"; $allRows = 0; $result = $conn->execute($sql); if ($row = $conn->nextRow($result)) $allRows = $row[0]; return $allRows; } /** * searchMatching-Method. This method provides searching capability to * get matching valueObjects from database. It works by searching all * objects that match permanent instance variables of given object. * Upper layer should use this by setting some parameters in valueObject * and then call searchMatching. The result will be 0-N objects in vector, * all matching those criteria you specified. Those instance-variables that * have NULL values are excluded in search-criteria. * * @param conn This method requires working database connection. * @param valueObject This parameter contains the class instance where search will be based. * Primary-key field should not be set. */ function searchMatching(&$conn, &$valueObject) { $first = true; $sql = "SELECT * FROM CUSTOMER WHERE 1=1 "; if ($valueObject->getNumber() != 0) { if ($first) { $first = false; } $sql = $sql."AND NUMBER = ".$valueObject->getNumber()." "; } if ($valueObject->getName() != "") { if ($first) { $first = false; } $sql = $sql."AND NAME LIKE '".$valueObject->getName()."%' "; } if ($valueObject->getAddress() != "") { if ($first) { $first = false; } $sql = $sql."AND ADDRESS LIKE '".$valueObject->getAddress()."%' "; } if ($valueObject->getCreated() != "") { if ($first) { $first = false; } $sql = $sql."AND CREATED = '".$valueObject->getCreated()."' "; } if ($valueObject->getBalance() != 0) { if ($first) { $first = false; } $sql = $sql."AND BALANCE = ".$valueObject->getBalance()." "; } $sql = $sql."ORDER BY NAME ASC "; // Prevent accidential full table results. // Use loadAll if all rows must be returned. if ($first) return array(); $searchResults = $this->listQuery(&$conn, $sql); return $searchResults; } /** * getDaogenVersion will return information about * generator which created these sources. */ function getDaogenVersion() { return "DaoGen version 2.2.1"; } /** * databaseUpdate-method. This method is a helper method for internal use. It will execute * all database handling that will change the information in tables. SELECT queries will * not be executed here however. The return value indicates how many rows were affected. * This method will also make sure that if cache is used, it will reset when data changes. * * @param conn This method requires working database connection. * @param stmt This parameter contains the SQL statement to be excuted. */ function databaseUpdate(&$conn, &$sql) { $result = $conn->execute($sql); return $result; } /** * databaseQuery-method. This method is a helper method for internal use. It will execute * all database queries that will return only one row. The resultset will be converted * to valueObject. If no rows were found, NotFoundException will be thrown. * * @param conn This method requires working database connection. * @param stmt This parameter contains the SQL statement to be excuted. * @param valueObject Class-instance where resulting data will be stored. */ function singleQuery(&$conn, &$sql, &$valueObject) { $result = $conn->execute($sql); if ($row = $conn->nextRow($result)) { $valueObject->setNumber($row[0]); $valueObject->setName($row[1]); $valueObject->setAddress($row[2]); $valueObject->setCreated($row[3]); $valueObject->setBalance($row[4]); } else { //print " Object Not Found!"; return false; } return true; } /** * databaseQuery-method. This method is a helper method for internal use. It will execute * all database queries that will return multiple rows. The resultset will be converted * to the List of valueObjects. If no rows were found, an empty List will be returned. * * @param conn This method requires working database connection. * @param stmt This parameter contains the SQL statement to be excuted. */ function listQuery(&$conn, &$sql) { $searchResults = array(); $result = $conn->execute($sql); while ($row = $conn->nextRow($result)) { $temp = new Customer(); $temp->setNumber($row[0]); $temp->setName($row[1]); $temp->setAddress($row[2]); $temp->setCreated($row[3]); $temp->setBalance($row[4]); array_push($searchResults, $temp); } return $searchResults; } } ?>