Source for file StorableInteger.class.php

Documentation is available at StorableInteger.class.php

  1. <?php
  2.  
  3. /**
  4. * This is the {@link StorablePrimitive} equivalent of {@link Integer}.
  5. *
  6. * @package harmoni.datamanager.storableprimitives
  7. *
  8. * @copyright Copyright &copy; 2005, Middlebury College
  9. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  10. *
  11. * @version $Id: StorableInteger.class.php,v 1.14 2007/09/04 20:25:33 adamfranco Exp $
  12. */
  13. class StorableInteger
  14. extends Integer
  15. /* implements StorablePrimitive */
  16. {
  17.  
  18. /*******************************************************
  19. * Class Methods
  20. *********************************************************/
  21.  
  22. /**
  23. * Takes a single database row, which would contain the columns added by alterQuery()
  24. * and extracts the values to setup the object with the appropriate data.
  25. * @param array $dbRow
  26. * @access public
  27. * @return object StorableInteger
  28. * @static
  29. */
  30. function createAndPopulate( $dbRow ) {
  31. $integer = new StorableInteger;
  32. $integer->_setValue($dbRow["integer_data"]);
  33. return $integer;
  34. }
  35. /**
  36. * Returns a string that could be inserted into an SQL query's WHERE clause, based on the
  37. * {@link Primitive} value that is passed. It is used when searching for datasets that contain a certain
  38. * field=value pair.
  39. * @param ref object $value The {@link Primitive} object to search for.
  40. * @param int $searchType One of the SEARCH_TYPE_* constants, defining what type of search this should be (ie, equals,
  41. * contains, greater than, less than, etc)
  42. * @return string or NULL if no searching is allowed.
  43. * @static
  44. */
  45. function makeSearchString($value, $searchType = SEARCH_TYPE_EQUALS) {
  46. switch ($searchType) {
  47. case SEARCH_TYPE_EQUALS:
  48. return "dm_integer.data = ".$value->asString();
  49. case SEARCH_TYPE_GREATER_THAN:
  50. return "dm_integer.data > ".$value->asString();
  51. case SEARCH_TYPE_LESS_THAN:
  52. return "dm_integer.data < ".$value->asString();
  53. case SEARCH_TYPE_GREATER_THAN_OR_EQUALS:
  54. return "dm_integer.data >= ".$value->asString();
  55. case SEARCH_TYPE_LESS_THAN_OR_EQUALS:
  56. return "dm_integer.data <= ".$value->asString();
  57. case SEARCH_TYPE_IN_LIST:
  58. $string = "dm_integer.data IN (";
  59. while ($value->hasNext()) {
  60. $valueObj =$value->next();
  61. $string .= $valueObj->asString();
  62. if ($value->hasNext())
  63. $string .= ", ";
  64. }
  65. $string .= ")";
  66. return $string;
  67. case SEARCH_TYPE_NOT_IN_LIST:
  68. $string = "dm_integer.data NOT IN (";
  69. while ($value->hasNext()) {
  70. $valueObj =$value->next();
  71. $string .= $valueObj->asString();
  72. if ($value->hasNext())
  73. $string .= ", ";
  74. }
  75. $string .= ")";
  76. return $string;
  77. }
  78. return null;
  79. }
  80. /*******************************************************
  81. * Instance Methods
  82. *********************************************************/
  83.  
  84. /**
  85. * Set the value
  86. *
  87. * @param $value
  88. * @return void
  89. * @access private
  90. * @since 7/13/05
  91. */
  92. function _setValue ($value) {
  93. $this->_value = (integer) $value;
  94. }
  95. /**
  96. * Inserts a new row into the Database with the data contained in the object.
  97. * @param integer $dbID The {@link DBHandler} database ID to query.
  98. * @access public
  99. * @return integer Returns the new ID of the data stored.
  100. */
  101. function insert($dbID) {
  102. $idManager = Services::getService("Id");
  103. $newID =$idManager->createId();
  104. $query = new InsertQuery();
  105. $query->setTable("dm_integer");
  106. $query->setColumns(array("id","data"));
  107. $query->addRowOfValues(array("'".addslashes($newID->getIdString())."'", $this->value()));
  108. $dbHandler = Services::getService("DatabaseManager");
  109. $result =$dbHandler->query($query, $dbID);
  110. if (!$result || $result->getNumberOfRows() != 1) {
  111. throwError( new UnknownDBError("Storable") );
  112. return false;
  113. }
  114. return $newID->getIdString();
  115. }
  116. /**
  117. * Uses the ID passed and updates the database row with
  118. * new data.
  119. * @param integer $dbID The {@link DBHandler} database ID to query.
  120. * @param integer $dataID The ID in the database of the data to be updated.
  121. * @access public
  122. * @return void
  123. */
  124. function update($dbID, $dataID) {
  125. if (!$dataID) return false;
  126. $query = new UpdateQuery();
  127. $query->setTable("dm_integer");
  128. $query->setColumns(array("data"));
  129. $query->setWhere("id='".addslashes($dataID)."'");
  130. $query->setValues(array($this->value()));
  131. $dbHandler = Services::getService("DatabaseManager");
  132. $result =$dbHandler->query($query, $dbID);
  133. if (!$result) {
  134. throwError( new UnknownDBError("Storable") );
  135. return false;
  136. }
  137. return true;
  138. }
  139. /**
  140. * Takes an existing {@link SelectQuery} and adds a table join and some columns so that
  141. * when it is executed the actual data can be retrieved from the row. The join condition must
  142. * be "fk_data = data_id_field", since the field "fk_data" is already part of the DataManager's
  143. * table structure.
  144. * @access public
  145. * @return void
  146. */
  147. function alterQuery( $query ) {
  148. $query->addTable("dm_integer",LEFT_JOIN,"dm_integer.id = fk_data");
  149. $query->addColumn("data","integer_data","dm_integer");
  150. }
  151. /**
  152. * Deletes the data row from the appropriate table.
  153. * @param integer $dbID The {@link DBHandler} database ID to query.
  154. * @param integer $dataID The ID in the database of the data to be deleted.
  155. * @access public
  156. * @return void
  157. */
  158. function prune($dbID, $dataID) {
  159. if (!$dataID) return;
  160. // delete ourselves from our data table
  161. $table = "dm_integer";
  162. $query = new DeleteQuery;
  163. $query->setTable($table);
  164. $query->setWhere("id='".addslashes($dataID)."'");
  165. $dbHandler = Services::getService("DatabaseManager");
  166. $res =$dbHandler->query($query, $dbID);
  167. if (!$res) throwError( new UnknownDBError("StorablePrimitive"));
  168. }
  169. }

Documentation generated on Wed, 19 Sep 2007 10:26:46 -0400 by phpDocumentor 1.3.0RC3