| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- <?php
- // @docs in Schema_TableFactory
- class Schema_TableBase {
- public $idDatabase;
- public $tblName;
- public function __construct($idDatabase, $tblName) {
- $this->idDatabase = $idDatabase;
- $this->tblName = strtolower($tblName);
- $this->_types = array();
- $this->_restrictions = array();
- $this->initTypes();
- }
- public function initTypes() {// TODO: override
- // parent::initTypes();// always call parent method
- // example: $this->_types['WWW'] = 'p5:www_link';
- // example: $this->_restrictions['WWW'] = ['maxLength' => 255]
- // TODO:? $this->_types['WWW'] = new Type_Link($params = ['maxLength' => 255, ...]);
- }
- public function fixTypes($types) {
- if (empty($this->_types)) return $types;
- foreach ($types as $fldName => $type) {
- if (array_key_exists($fldName, $this->_types)) {
- $types[$fldName]['simpleType'] = $this->_types[$fldName];
- }
- if (array_key_exists($fldName, $this->_restrictions)) {
- $types[$fldName]['restrictions'] = $this->_restrictions[$fldName];
- }
- // TODO: add default xsdElementType('simpleType', 'complexType'), xsdType(p5Type:...), ... for every field
- }
- return $types;
- }
- public function getStruct() {
- if (!empty($this->_struct)) return $this->_struct;
- $storage = DB::getStorage($this->idDatabase);
- $struct = $storage->getTableStruct($this->tblName);
- $this->_struct = $struct;
- foreach ($this->_struct as $fldName => $field) {
- $this->_struct[$fldName] = $this->convertStorageTypeToXsdP5Type($storage->getType(), $field);
- }
- return $this->_struct;
- }
- public function convertStorageTypeToXsdP5Type($storageType, $field) {
- /* cat SE/schema/types.xsd |grep 'xs:simpleType name='
- <xs:simpleType name="string">
- <xs:simpleType name="text">
- <xs:simpleType name="enumeration">
- <xs:simpleType name="set">
- <xs:simpleType name="integer">
- <xs:simpleType name="decimal">
- <xs:simpleType name="double">
- <xs:simpleType name="float">
- <xs:simpleType name="binary">
- <xs:simpleType name="hexBinary">
- <xs:simpleType name="date">
- <xs:simpleType name="dateZero">
- <xs:simpleType name="dateTime">
- <xs:simpleType name="dateTimeZero">
- <xs:simpleType name="time">
- <xs:simpleType name="timeZero">
- <xs:simpleType name="year">
- <xs:simpleType name="yearZero">
- <xs:simpleType name="geometry">
- <xs:simpleType name="polygon">
- <xs:simpleType name="point">
- <xs:simpleType name="lineString">
- */
- $field['p5_isComplexType'] = false;
- $field['p5_type'] = 'unknown';// TODO: default type or leave undefined?
- $field['p5_restrictions'] = array();
- $field['p5_props'] = array();
- if ('mysql' == $storageType) {
- switch ($field['type']) {
- case 'tinyint':
- case 'smallint':
- case 'bigint':
- case 'int': {
- $field['p5_type'] = 'integer';
- } break;
- case 'decimal': {
- $field['p5_type'] = $field['type'];
- } break;
- case 'float':
- case 'double': {
- $field['p5_type'] = $field['type'];
- } break;
- case 'char':
- case 'varchar': {
- $field['p5_type'] = 'string';
- } break;
- case 'mediumtext':
- case 'longtext':
- case 'text': {
- $field['p5_type'] = $field['type'];
- } break;
- case 'datetime': {
- $field['p5_type'] = 'dateTime';
- } break;
- case 'date': {
- $field['p5_type'] = $field['type'];
- } break;
- case 'year': {
- $field['p5_type'] = $field['type'];
- } break;
- case 'time': {
- $field['p5_type'] = $field['type'];
- } break;
- case 'timestamp': {
- $field['p5_type'] = 'integer';
- } break;
- case 'point':
- case 'polygon':
- case 'geometry': {
- $field['p5_type'] = $field['type'];
- } break;
- case 'linestring': {
- $field['p5_type'] = 'lineString';
- } break;
- case 'multipoint': {
- $field['p5_type'] = 'multiPoint';// TODO: create multiPoint in types.xsd
- } break;
- case 'multipolygon': {
- $field['p5_type'] = 'multiPolygon';// TODO: create multiPolygon in types.xsd
- } break;
- case 'multilinestring': {
- $field['p5_type'] = 'multiLineString';// TODO: create multiLineString in types.xsd
- } break;
- case 'enum': {
- $field['p5_type'] = 'string';
- $enumValues = substr($field['raw_storage_type'], 6, -2);// "enum('R','W','X','C')"
- $enumValues = explode("','", $enumValues);
- $field['p5_restrictions']['enumeration'] = $enumValues;
- } break;
- case 'set': {
- $field['p5_type'] = 'set';
- $enumValues = substr($field['raw_storage_type'], 5, -2);// "set('R','W','X','C')"
- $enumValues = explode("','", $enumValues);
- // TODO: enumerations with all combinations
- // $baseComb = array(); foreach ($enumValues as $enumValue) { if ('' !== $enumValue) $baseComb[] = $enumValue; };
- // $sizeCombinations = count($baseComb);
- // $allComb = pow(2, $sizeCombinations);
- // //DBG::_(true, true, "baseCombinations", $baseComb, __CLASS__, __FUNCTION__, __LINE__);
- // for ($b = 1; $b < $allComb; $b++) {
- // $curValues = array();
- // $valBinary = sprintf("%0{$sizeCombinations}b", $b);
- // $binValues = str_split($valBinary);
- // //DBG::_(true, true, "curValues({$b}/{$valBinary})", $curValues, __CLASS__, __FUNCTION__, __LINE__);
- // foreach ($binValues as $idx => $isSet) {
- // if ($isSet) $curValues[] = $baseComb[$idx];
- // }
- // $enumValues[] = implode(",", $curValues);
- // }
- //$field['p5_restrictions']['enumeration'] = $enumValues;// TODO: use p5_props
- $field['p5_props']['multiple_enumeration'] = $enumValues;// TODO: use p5_props
- } break;
- case 'tinyblob':
- case 'mediumblob':
- case 'longblob':
- case 'blob':
- case 'binary':
- case 'varbinary': {
- $field['p5_type'] = 'hexBinary';
- } break;
- default:
- DBG::_(true, true, "TODO: storageType({$storageType}) field({$field['name']})", $field, __CLASS__, __FUNCTION__, __LINE__);
- }
- } else if ('pgsql' == $storageType) {
- } else {
- }
- return $field;
- }
- }
|