TableBase.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. // @docs in Schema_TableFactory
  3. class Schema_TableBase {
  4. public $idDatabase;
  5. public $tblName;
  6. public $_types;
  7. public $_restrictions;
  8. public $_params;
  9. public function __construct($idDatabase, $tblName) {
  10. $this->idDatabase = $idDatabase;
  11. $this->tblName = strtolower($tblName);
  12. $this->_types = array();
  13. $this->_restrictions = array();
  14. $this->initTypes();
  15. }
  16. public function initTypes() {// TODO: override
  17. // parent::initTypes();// always call parent method
  18. // example: $this->_types['WWW'] = 'p5:www_link';
  19. // example: $this->_restrictions['WWW'] = ['maxLength' => 255]
  20. // TODO:? $this->_types['WWW'] = new Type_Link($params = ['maxLength' => 255, ...]);
  21. }
  22. public function getType($fieldName) {
  23. return V::get($fieldName, '', $this->_types);
  24. }
  25. public function fixTypes($types) {
  26. if (empty($this->_types)) return $types;
  27. foreach ($types as $fldName => $type) {
  28. if (array_key_exists($fldName, $this->_types)) {
  29. $types[$fldName]['simpleType'] = $this->_types[$fldName];
  30. }
  31. if (array_key_exists($fldName, $this->_restrictions)) {
  32. $types[$fldName]['restrictions'] = $this->_restrictions[$fldName];
  33. }
  34. // TODO: add default xsdElementType('simpleType', 'complexType'), xsdType(p5Type:...), ... for every field
  35. }
  36. return $types;
  37. }
  38. public function getStruct() {
  39. if (!empty($this->_struct)) return $this->_struct;
  40. $storage = DB::getStorage($this->idDatabase);
  41. $struct = $storage->getTableStruct($this->tblName);
  42. $this->_struct = $struct;
  43. foreach ($this->_struct as $fldName => $field) {
  44. $this->_struct[$fldName] = $this->convertStorageTypeToXsdP5Type($storage->getType(), $field);
  45. }
  46. return $this->_struct;
  47. }
  48. public function convertStorageTypeToXsdP5Type($storageType, $field) {
  49. /* cat SE/schema/types.xsd |grep 'xs:simpleType name='
  50. <xs:simpleType name="string">
  51. <xs:simpleType name="text">
  52. <xs:simpleType name="enumeration">
  53. <xs:simpleType name="set">
  54. <xs:simpleType name="integer">
  55. <xs:simpleType name="decimal">
  56. <xs:simpleType name="double">
  57. <xs:simpleType name="float">
  58. <xs:simpleType name="binary">
  59. <xs:simpleType name="hexBinary">
  60. <xs:simpleType name="date">
  61. <xs:simpleType name="dateZero">
  62. <xs:simpleType name="dateTime">
  63. <xs:simpleType name="dateTimeZero">
  64. <xs:simpleType name="time">
  65. <xs:simpleType name="timeZero">
  66. <xs:simpleType name="year">
  67. <xs:simpleType name="yearZero">
  68. <xs:simpleType name="geometry">
  69. <xs:simpleType name="polygon">
  70. <xs:simpleType name="point">
  71. <xs:simpleType name="lineString">
  72. */
  73. $field['p5_isComplexType'] = false;
  74. $field['p5_type'] = 'unknown';// TODO: default type or leave undefined?
  75. $field['p5_restrictions'] = array();
  76. $field['p5_props'] = array();
  77. if ('mysql' == $storageType) {
  78. switch ($field['type']) {
  79. case 'tinyint':
  80. case 'smallint':
  81. case 'bigint':
  82. case 'int': {
  83. $field['p5_type'] = 'integer';
  84. } break;
  85. case 'decimal': {
  86. $field['p5_type'] = $field['type'];
  87. } break;
  88. case 'float':
  89. case 'double': {
  90. $field['p5_type'] = $field['type'];
  91. } break;
  92. case 'char':
  93. case 'varchar': {
  94. $field['p5_type'] = 'string';
  95. } break;
  96. case 'mediumtext':
  97. case 'longtext':
  98. case 'text': {
  99. $field['p5_type'] = $field['type'];
  100. } break;
  101. case 'datetime': {
  102. $field['p5_type'] = 'dateTime';
  103. } break;
  104. case 'date': {
  105. $field['p5_type'] = $field['type'];
  106. } break;
  107. case 'year': {
  108. $field['p5_type'] = $field['type'];
  109. } break;
  110. case 'time': {
  111. $field['p5_type'] = $field['type'];
  112. } break;
  113. case 'timestamp': {
  114. $field['p5_type'] = 'integer';
  115. } break;
  116. case 'point':
  117. case 'polygon':
  118. case 'geometry': {
  119. $field['p5_type'] = $field['type'];
  120. } break;
  121. case 'linestring': {
  122. $field['p5_type'] = 'lineString';
  123. } break;
  124. case 'multipoint': {
  125. $field['p5_type'] = 'multiPoint';// TODO: create multiPoint in types.xsd
  126. } break;
  127. case 'multipolygon': {
  128. $field['p5_type'] = 'multiPolygon';// TODO: create multiPolygon in types.xsd
  129. } break;
  130. case 'multilinestring': {
  131. $field['p5_type'] = 'multiLineString';// TODO: create multiLineString in types.xsd
  132. } break;
  133. case 'enum': {
  134. $field['p5_type'] = 'string';
  135. $enumValues = substr($field['raw_storage_type'], 6, -2);// "enum('R','W','X','C')"
  136. $enumValues = explode("','", $enumValues);
  137. $field['p5_restrictions']['enumeration'] = $enumValues;
  138. } break;
  139. case 'set': {
  140. $field['p5_type'] = 'set';
  141. $enumValues = substr($field['raw_storage_type'], 5, -2);// "set('R','W','X','C')"
  142. $enumValues = explode("','", $enumValues);
  143. // TODO: enumerations with all combinations
  144. // $baseComb = array(); foreach ($enumValues as $enumValue) { if ('' !== $enumValue) $baseComb[] = $enumValue; };
  145. // $sizeCombinations = count($baseComb);
  146. // $allComb = pow(2, $sizeCombinations);
  147. // //DBG::_(true, true, "baseCombinations", $baseComb, __CLASS__, __FUNCTION__, __LINE__);
  148. // for ($b = 1; $b < $allComb; $b++) {
  149. // $curValues = array();
  150. // $valBinary = sprintf("%0{$sizeCombinations}b", $b);
  151. // $binValues = str_split($valBinary);
  152. // //DBG::_(true, true, "curValues({$b}/{$valBinary})", $curValues, __CLASS__, __FUNCTION__, __LINE__);
  153. // foreach ($binValues as $idx => $isSet) {
  154. // if ($isSet) $curValues[] = $baseComb[$idx];
  155. // }
  156. // $enumValues[] = implode(",", $curValues);
  157. // }
  158. //$field['p5_restrictions']['enumeration'] = $enumValues;// TODO: use p5_props
  159. $field['p5_props']['multiple_enumeration'] = $enumValues;// TODO: use p5_props
  160. } break;
  161. case 'tinyblob':
  162. case 'mediumblob':
  163. case 'longblob':
  164. case 'blob':
  165. case 'binary':
  166. case 'varbinary': {
  167. $field['p5_type'] = 'hexBinary';
  168. } break;
  169. default:
  170. DBG::_(true, true, "TODO: storageType({$storageType}) field({$field['name']})", $field, __CLASS__, __FUNCTION__, __LINE__);
  171. }
  172. } else if ('pgsql' == $storageType) {
  173. } else {
  174. }
  175. return $field;
  176. }
  177. public function getPrimaryKeyField() {// TODO: use this method in TableAcl, Data_Source, etc.
  178. return 'ID';
  179. }
  180. public function fixItemFieldValue($fieldName, /* array */ $item) {
  181. return $value;
  182. }
  183. public function getFieldParam($fieldName, $paramKey) {
  184. return (empty($this->_params[$fieldName]))
  185. ? null
  186. : (empty($this->_params[$fieldName][$paramKey]))
  187. ? null
  188. : $this->_params[$fieldName][$paramKey]
  189. ;
  190. }
  191. }