Source for file StorableBoolean.class.php

Documentation is available at StorableBoolean.class.php

  1. <?php
  2.  
  3. /**
  4. * This is the {@link StorablePrimitive} equivalent of {@link Boolean}.
  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: StorableBoolean.class.php,v 1.13 2007/09/04 20:25:33 adamfranco Exp $
  12. */
  13. class StorableBoolean
  14. extends Boolean
  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 StorableBoolean
  28. * @static
  29. */
  30. function createAndPopulate( $dbRow ) {
  31. $boolean = new StorableBoolean;
  32. $boolean->_setValue($dbRow["boolean_data"]);
  33. return $boolean;
  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. if ($searchType == SEARCH_TYPE_EQUALS) {
  47. return "dm_boolean.data = ".($value->value()?"1":"0");
  48. }
  49. return null;
  50. }
  51. /*******************************************************
  52. * Instance Methods
  53. *********************************************************/
  54.  
  55. /**
  56. * Set the value
  57. *
  58. * @param $value
  59. * @return void
  60. * @access private
  61. * @since 7/13/05
  62. */
  63. function _setValue ($value) {
  64. $this->_bool = ($value==1)?true:false;
  65. }
  66. /**
  67. * Inserts a new row into the Database with the data contained in the object.
  68. * @param integer $dbID The {@link DBHandler} database ID to query.
  69. * @access public
  70. * @return integer Returns the new ID of the data stored.
  71. */
  72. function insert($dbID) {
  73. $idManager = Services::getService("Id");
  74. $newID =$idManager->createId();
  75. $query = new InsertQuery();
  76. $query->setTable("dm_boolean");
  77. $query->setColumns(array("id","data"));
  78. $query->addRowOfValues(array("'".addslashes($newID->getIdString())."'", $this->value()?1:0));
  79. $dbHandler = Services::getService("DatabaseManager");
  80. $result =$dbHandler->query($query, $dbID);
  81. if (!$result || $result->getNumberOfRows() != 1) {
  82. throwError( new UnknownDBError("Storable") );
  83. return false;
  84. }
  85. return $newID->getIdString();
  86. }
  87. /**
  88. * Uses the ID passed and updates the database row with
  89. * new data.
  90. * @param integer $dbID The {@link DBHandler} database ID to query.
  91. * @param integer $dataID The ID in the database of the data to be updated.
  92. * @access public
  93. * @return void
  94. */
  95. function update($dbID, $dataID) {
  96. if (!$dataID) return false;
  97. $query = new UpdateQuery();
  98. $query->setTable("dm_boolean");
  99. $query->setColumns(array("data"));
  100. $query->setWhere("id='".addslashes($dataID)."'");
  101. $query->setValues(array($this->value()?1:0));
  102. $dbHandler = Services::getService("DatabaseManager");
  103. $result =$dbHandler->query($query, $dbID);
  104. if (!$result) {
  105. throwError( new UnknownDBError("Storable") );
  106. return false;
  107. }
  108. return true;
  109. }
  110. /**
  111. * Takes an existing {@link SelectQuery} and adds a table join and some columns so that
  112. * when it is executed the actual data can be retrieved from the row. The join condition must
  113. * be "fk_data = data_id_field", since the field "fk_data" is already part of the DataManager's
  114. * table structure.
  115. * @access public
  116. * @return void
  117. */
  118. function alterQuery( $query ) {
  119. $query->addTable("dm_boolean",LEFT_JOIN,"dm_boolean.id = fk_data");
  120. $query->addColumn("data","boolean_data","dm_boolean");
  121. }
  122. /**
  123. * Deletes the data row from the appropriate table.
  124. * @param integer $dbID The {@link DBHandler} database ID to query.
  125. * @param integer $dataID The ID in the database of the data to be deleted.
  126. * @access public
  127. * @return void
  128. */
  129. function prune($dbID, $dataID) {
  130. if (!$dataID) return;
  131. // delete ourselves from our data table
  132. $table = "dm_boolean";
  133. $query = new DeleteQuery;
  134. $query->setTable($table);
  135. $query->setWhere("id='".addslashes($dataID)."'");
  136. $dbHandler = Services::getService("DatabaseManager");
  137. $res =$dbHandler->query($query, $dbID);
  138. if (!$res) throwError( new UnknownDBError("StorablePrimitive"));
  139. }
  140. /*******************************************************
  141. * Conversion Methods
  142. *********************************************************/
  143.  
  144. /**
  145. * Convert this object to a StorableBlob
  146. *
  147. * @return object
  148. * @access public
  149. * @since 6/9/06
  150. */
  151. function asABlob () {
  152. return Blob::fromString($this->asString());
  153. }
  154. /**
  155. * Convert this object to a StorableString
  156. *
  157. * @return object
  158. * @access public
  159. * @since 6/9/06
  160. */
  161. function asAString () {
  162. return String::fromString($this->asString());
  163. }
  164. /**
  165. * Convert this object to a StorableShortString
  166. *
  167. * @return object
  168. * @access public
  169. * @since 6/9/06
  170. */
  171. function asAShortString () {
  172. return String::fromString($this->asString());
  173. }
  174. /**
  175. * Convert this object to a StorableInteger
  176. *
  177. * @return object
  178. * @access public
  179. * @since 6/9/06
  180. */
  181. function asAInteger() {
  182. return Integer::withValue($this->getValue()?1:0);
  183. }
  184. /**
  185. * Convert this object to a StorableFloat
  186. *
  187. * @return object
  188. * @access public
  189. * @since 6/9/06
  190. */
  191. function asAFloat () {
  192. return Float::withValue($this->getValue()?1:0);
  193. }
  194. /**
  195. * Convert this object to a Boolean
  196. *
  197. * @return object
  198. * @access public
  199. * @since 6/9/06
  200. */
  201. function asABoolean() {
  202. return $this;
  203. }
  204. }

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