forked from keygenqt/Vertica
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCommand.php
223 lines (199 loc) · 5.72 KB
/
Command.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
namespace yii\vertica;
use yii\base\Component;
/**
* @author Vitaliy Zarubint <[email protected]>
* @since 2.0
*/
class Command extends Component
{
/**
* @var Connection
*/
public $db;
/**
* @var array
*/
public static $attributes;
/**
* @var array
*/
public static $pk;
private $_sql;
public function __construct($config = [])
{
if (isset($config['sql'])) {
$this->_sql = $config['sql'];
}
if (isset($config['db'])) {
$this->db = $config['db'];
}
}
/**
* @return array
*/
public function search()
{
return $this->db->exec($this->_sql)->all();
}
/**
* @return array columns model
*/
public function getColumn()
{
if (!isset(self::$attributes[$this->db->table])) {
$resource = $this->db->exec('SELECT column_name FROM COLUMNS WHERE table_name=' . QueryBuilder::preparationValue($this->db->table))->resource();
self::$attributes[$this->db->table] = [];
while ($row = odbc_fetch_array($resource)) {
self::$attributes[$this->db->table][] = $row['column_name'];
}
}
return self::$attributes[$this->db->table];
}
/**
* @return array
*/
public function getColumnData()
{
return $this->db->exec('SELECT * FROM COLUMNS WHERE table_name=' . QueryBuilder::preparationValue($this->db->table))->all();
}
/**
* @return string pk name or first int
*/
public function getPk()
{
if (!isset(self::$pk[$this->db->table])) {
$resource = $this->db->exec('SELECT is_identity, column_name, data_type FROM COLUMNS WHERE table_name=' . QueryBuilder::preparationValue($this->db->table))->resource();
self::$pk[$this->db->table] = '';
while ($row = odbc_fetch_array($resource)) {
if (empty(self::$pk[$this->db->table]) && $row['data_type'] == 'int') {
self::$pk[$this->db->table] = $row['column_name'];
}
if ($row['is_identity']) {
self::$pk[$this->db->table] = $row['column_name'];
break;
}
}
}
return self::$pk[$this->db->table];
}
/**
* @return array with data tables
*/
public function getTables()
{
return $this->db->exec('SELECT * FROM tables')->all();
}
/**
* @param string $table
* @param array $columns
* @return \yii\vertica\Command
*/
public function insert($table, $columns)
{
$this->_sql = "INSERT INTO $table (" . implode(', ', array_keys($columns)) . ")";
$values = [];
foreach ($columns as $key => $value) {
$values[] = QueryBuilder::preparationValue($value);
}
$this->_sql .= ' VALUES (' . implode(', ', $values) . ')';
return $this;
}
/**
* @param type $table
* @param type $pkName
* @param type $pkValue
* @param type $attributes
* @return boolean
*/
public function update($table, $pkName, $pkValue, $attributes)
{
$set = [];
foreach ($attributes as $key => $value) {
if ($pkName == $key) {
continue;
}
$value = QueryBuilder::preparationValue($value);
$set[] = "$key=$value";
}
$this->_sql = "UPDATE $table SET " . implode(', ', $set) . " WHERE $pkName=$pkValue";
$this->db->execute($this->_sql);
return true;
}
/**
* @param string $table
* @param mixed $condition array or string
* @return \yii\vertica\Command
*/
public function delete($table = null, $condition = '')
{
if (empty($table) && !empty($this->_sql)) {
$this->_sql = 'DELETE ' . substr($this->_sql, strpos($this->_sql, 'FROM'));
} else {
if (is_array($condition)) {
$params = [];
foreach ($condition as $key => $value) {
$params[] = $key . '=' . QueryBuilder::preparationValue($value);
}
$this->_sql = "DELETE FROM $table WHERE " . implode(' AND ', $params);
} else {
$this->_sql = "DELETE FROM $table WHERE $condition";
}
}
return $this;
}
/**
* @param string $table
* @param array $columns
* @param string $options
* @return \yii\vertica\Command
*/
public function createTable($table, $columns = [], $options = null)
{
$cols = [];
foreach ($columns as $name => $type) {
if (is_string($name)) {
$cols[] = "\t" . $name . ' ' . $type;
} else {
$cols[] = "\t" . $type;
}
}
$sql = "CREATE TABLE " . $table . " (\n" . implode(",\n", $cols) . "\n)";
$this->_sql = $options === null ? $sql : $sql . ' ' . $options;
return $this;
}
/**
* @param string $table
* @return \yii\vertica\Command
*/
public function dropTable($table)
{
$this->_sql = "DROP TABLE $table";
return $this;
}
/**
* @return array
*/
public function queryOne()
{
return $this->db->exec($this->_sql)->one();
}
/**
* @return array
*/
public function queryAll()
{
return $this->db->exec($this->_sql)->all();
}
/**
* @return string
*/
public function queryScalar()
{
return $this->db->exec($this->_sql)->scalar();
}
public function execute($params = [])
{
$this->db->execute($this->_sql);
}
}