TableBase.php 5.9 KB

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