-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSchema.php
137 lines (114 loc) · 3.9 KB
/
Schema.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/**
* Created by PhpStorm.
* User: execut
* Date: 9/20/17
* Time: 2:05 PM
*/
namespace execut\oData;
use Kily\Tools1C\OData\Client;
use yii\db\ColumnSchema;
use yii\db\TableSchema;
class Schema extends \yii\db\Schema
{
/**
* @var Client
*/
public $client = null;
public function loadTableSchema($name) {
}
public function getCustomColumnsTypes() {
return $this->client->customColumnsTypes;
}
protected function getMetadata() {
$cacheKey = __CLASS__;
$cache = \yii::$app->cache;
if ($result = $cache->get($cacheKey)) {
return $result;
}
if ($this->client->isConnectionError()) {
return false;
}
$metadata = $this->client->getMetadata();
$result = array_merge($metadata['EntityType'], $metadata['ComplexType']);
$cache->set($cacheKey, $result);
return $result;
}
public function getTableSchema($name, $refresh = false)
{
$cache = \yii::$app->cache;
$cacheKey = __CLASS__ . __FUNCTION__ . var_export($this->getCustomColumnsTypes(), true) . '14' . $name;
if ($schema = $cache->get($cacheKey)) {
return $schema;
}
$metadata = $this->getMetadata();
$name = preg_replace('/\(\)$/', '', str_replace('/', '_', $name));
$name = str_replace('_BalanceAndTurnovers', '_BalanceAndTurnover', $name);
foreach ($metadata as $params) {
if ($params['@attributes']['Name'] === $name) {
break;
}
}
if (empty($params['Key'])) {
$primaryKey = ['Ref_Key', 'LineNumber'];
} else if (empty($params['Key']['PropertyRef']['@attributes'])) {
$primaryKey = [];
foreach ($params['Key']['PropertyRef'] as $properyRef) {
$primaryKey[] = $properyRef['@attributes']['Name'];
}
} else {
$primaryKey = $params['Key']['PropertyRef']['@attributes'];
$primaryKey = array_values($primaryKey);
}
$columns = [];
foreach ($params['Property'] as $property) {
$property = $property['@attributes'];
if (strpos($property['Type'], 'Collection') !== false) {
continue;
}
$customColumnsTypes = $this->getCustomColumnsTypes();
if (!empty($customColumnsTypes[$name]) && !empty($customColumnsTypes[$name][$property['Name']])) {
$type = $customColumnsTypes[$name][$property['Name']];
} else {
$type = $this->getColumnDbType($property['Type']);
}
$column = new ColumnSchema([
'name' => $property['Name'],
'type' => $this->getColumnAbstractType($property['Type']),
'allowNull' => $property['Nullable'] === 'true',
'phpType' => $this->getColumnPhpType($property['Type']),
'dbType' => $type,
]);
$columns[$column->name] = $column;
}
$tableSchema = new TableSchema([
'name' => $name,
'fullName' => $name,
'primaryKey' => $primaryKey,
'columns' => $columns
]);
$cache->set($cacheKey, $tableSchema);
return $tableSchema;
}
protected function getColumnDbType($type) {
return $type;
}
protected function getColumnAbstractType($type) {
return $this->getColumnPhpType($type);
}
protected function getColumnPhpType($type) {
$typesMap = [
'Edm.Guid' => 'string',
'Edm.Boolean' => 'boolean',
'Edm.String' => 'string',
'Edm.Int16' => 'int',
'Edm.Int32' => 'int',
'Edm.Int64' => 'int',
'Edm.Double' => 'double',
];
if (empty($typesMap[$type])) {
return $type;
}
return $typesMap[$type];
}
}