PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/PHPFrame/Mapper/ObjectRelationalToolbox.php

https://github.com/chrismcband/PHPFrame
PHP | 185 lines | 93 code | 20 blank | 72 comment | 16 complexity | 03c0607bd53fb0f948840b0206a43a03 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * PHPFrame/Mapper/ObjectRelationalToolbox.php
  4. *
  5. * PHP version 5
  6. *
  7. * @category PHPFrame
  8. * @package Mapper
  9. * @author Lupo Montero <lupo@e-noise.com>
  10. * @copyright 2010 The PHPFrame Group
  11. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  12. * @link http://github.com/PHPFrame/PHPFrame
  13. */
  14. /**
  15. * Object Relational Toolbox Class
  16. *
  17. * @category PHPFrame
  18. * @package Mapper
  19. * @author Lupo Montero <lupo@e-noise.com>
  20. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  21. * @link http://github.com/PHPFrame/PHPFrame
  22. * @since 1.0
  23. */
  24. class PHPFrame_ObjectRelationalToolbox
  25. {
  26. /**
  27. * Create a database table to store a given persistent object
  28. *
  29. * @param PHPFrame_Database $db Reference to a database
  30. * object.
  31. * @param PHPFrame_PersistentObject $obj An instance of the persistent
  32. * object we want to create the
  33. * table for.
  34. * @param string $table_name [Optional] Table name to
  35. * use. If none specified we
  36. * use the object's class name.
  37. * @param bool $drop [Optional] Default value is
  38. * FALSE. When set to true
  39. * existing table will be
  40. * dropped.
  41. *
  42. * @return void
  43. * @throws Exception on failure
  44. * @since 1.0
  45. */
  46. public function createTable(
  47. PHPFrame_Database $db,
  48. PHPFrame_PersistentObject $obj,
  49. $table_name=null,
  50. $drop=false
  51. ) {
  52. if (is_null($table_name)) {
  53. $table_name = get_class($obj);
  54. }
  55. if ($db->hasTable($table_name) && !$drop) {
  56. $msg = "Could not create table. Table '".$table_name."' already ";
  57. $msg .= "exists in database. Use the '\$drop' argument in ";
  58. $msg .= get_class($this)."::".__FUNCTION__."() to drop the table ";
  59. $msg .= "before trying to create it again.";
  60. throw new RuntimeException($msg);
  61. }
  62. if ($drop) {
  63. $db->dropTable($table_name);
  64. }
  65. $table = new PHPFrame_DatabaseTable($db, $table_name);
  66. foreach ($obj->getFilters() as $key=>$filter) {
  67. $column = new PHPFrame_DatabaseColumn(array(
  68. "name"=>$key,
  69. "type"=>PHPFrame_DatabaseColumn::TYPE_BLOB
  70. ));
  71. $options = $filter->getOptions();
  72. if ($filter instanceof PHPFrame_BoolFilter) {
  73. $column->setType(PHPFrame_DatabaseColumn::TYPE_BOOL);
  74. } elseif ($filter instanceof PHPFrame_IntFilter) {
  75. $range = $options["max_range"] - $options["min_range"];
  76. if ($range <= 255) { // 1 byte int
  77. $column->setType(PHPFrame_DatabaseColumn::TYPE_TINYINT);
  78. } elseif ($range <= 65535) { // 2 byte int
  79. $column->setType(PHPFrame_DatabaseColumn::TYPE_SMALLINT);
  80. } elseif ($range <= 16777215) { // 3 byte int
  81. $column->setType(PHPFrame_DatabaseColumn::TYPE_MEDIUMINT);
  82. } elseif ($range <= 4294967295) { // 4 byte int
  83. $column->setType(PHPFrame_DatabaseColumn::TYPE_INT);
  84. } else { // 8 byte int
  85. $column->setType(PHPFrame_DatabaseColumn::TYPE_BIGINT);
  86. }
  87. } elseif ($filter instanceof PHPFrame_FloatFilter) {
  88. $column->setType(PHPFrame_DatabaseColumn::TYPE_FLOAT);
  89. } elseif ($filter instanceof PHPFrame_EnumFilter) {
  90. $column->setType(PHPFrame_DatabaseColumn::TYPE_ENUM);
  91. $column->setEnums($filter->getOption("enums"));
  92. } elseif ($filter instanceof PHPFrame_DateFilter) {
  93. $format = $options["format"];
  94. if ($format == PHPFrame_DateFilter::FORMAT_DATE) {
  95. $column->setType(PHPFrame_DatabaseColumn::TYPE_DATE);
  96. } elseif ($format == PHPFrame_DateFilter::FORMAT_DATETIME) {
  97. $column->setType(PHPFrame_DatabaseColumn::TYPE_DATETIME);
  98. } elseif ($format == PHPFrame_DateFilter::FORMAT_TIME) {
  99. $column->setType(PHPFrame_DatabaseColumn::TYPE_TIME);
  100. }
  101. } elseif ($filter instanceof PHPFrame_URLFilter) {
  102. $column->setType(PHPFrame_DatabaseColumn::TYPE_VARCHAR);
  103. } elseif ($filter instanceof PHPFrame_StringFilter) {
  104. if ($options["max_length"] > 0) {
  105. $column->setType(PHPFrame_DatabaseColumn::TYPE_VARCHAR);
  106. $column->setLength($options["max_length"]);
  107. } else {
  108. $column->setType(PHPFrame_DatabaseColumn::TYPE_TEXT);
  109. }
  110. }
  111. $column->setNull($obj->allowsNull($key));
  112. $def_values = iterator_to_array($obj);
  113. if (!is_null($def_values[$key])) {
  114. $column->setDefault($def_values[$key]);
  115. }
  116. if ($key == "id") {
  117. $column->setKey(PHPFrame_DatabaseColumn::KEY_PRIMARY);
  118. $column->setExtra(PHPFrame_DatabaseColumn::EXTRA_AUTOINCREMENT);
  119. }
  120. $table->addColumn($column);
  121. }
  122. $db->createTable($table);
  123. }
  124. /**
  125. * Create the php code to represent a given database table as a persistent
  126. * object.
  127. *
  128. * @param PHPFrame_Database $db A reference to a database object.
  129. * @param string $table_name The name of the table we want to
  130. * create the persistent object class
  131. * for.
  132. *
  133. * @return string
  134. * @throws Exception on failure
  135. * @since 1.0
  136. * @todo This method needs to be implemented.
  137. */
  138. public function createPersistentObjectClass(
  139. PHPFrame_Database $db,
  140. $table_name
  141. ) {
  142. //...
  143. }
  144. /**
  145. * Check whether a database table is valid to store a given persistent
  146. * object.
  147. *
  148. * @param PHPFrame_Database $db A reference to a database
  149. * object.
  150. * @param string $table_name The table name.
  151. * @param PHPFrame_PersistentObject $obj Reference to a persistent
  152. * of the type we want to check.
  153. *
  154. * @return bool
  155. * @since 1.0
  156. * @todo This method needs to be implemented.
  157. */
  158. public function isValid(
  159. PHPFrame_Database $db,
  160. $table_name,
  161. PHPFrame_PersistentObject $obj
  162. ) {
  163. //...
  164. }
  165. }