|
@@ -1,18 +1,18 @@
|
1
|
1
|
<?php
|
2
|
2
|
interface DatabaseInterface {
|
3
|
3
|
public function getSql($name);
|
4
|
|
- public function connectDatabase($hostname,$username,$password,$database,$port,$socket,$charset);
|
|
4
|
+ public function connect($hostname,$username,$password,$database,$port,$socket,$charset);
|
5
|
5
|
public function query($sql,$params);
|
6
|
|
- public function fetch_assoc($result);
|
7
|
|
- public function fetch_row($result);
|
8
|
|
- public function insert_id($result);
|
9
|
|
- public function affected_rows($result);
|
|
6
|
+ public function fetchAssoc($result);
|
|
7
|
+ public function fetchRow($result);
|
|
8
|
+ public function insertId($result);
|
|
9
|
+ public function affectedRows($result);
|
10
|
10
|
public function close($result);
|
11
|
|
- public function fetch_fields($result);
|
12
|
|
- public function add_limit_to_sql($sql,$limit,$offset);
|
|
11
|
+ public function fetchFields($result);
|
|
12
|
+ public function addLimitToSql($sql,$limit,$offset);
|
13
|
13
|
public function likeEscape($string);
|
14
|
|
- public function is_binary_type($field);
|
15
|
|
- public function base64_encode($string);
|
|
14
|
+ public function isBinaryType($field);
|
|
15
|
+ public function base64Encode($string);
|
16
|
16
|
public function getDefaultCharset();
|
17
|
17
|
}
|
18
|
18
|
|
|
@@ -81,7 +81,7 @@ class MySQL implements DatabaseInterface {
|
81
|
81
|
return isset($this->queries[$name])?$this->queries[$name]:false;
|
82
|
82
|
}
|
83
|
83
|
|
84
|
|
- public function connectDatabase($hostname,$username,$password,$database,$port,$socket,$charset) {
|
|
84
|
+ public function connect($hostname,$username,$password,$database,$port,$socket,$charset) {
|
85
|
85
|
$db = mysqli_connect($hostname,$username,$password,$database,$port,$socket);
|
86
|
86
|
if (mysqli_connect_errno()) {
|
87
|
87
|
throw new \Exception('Connect failed. '.mysqli_connect_error());
|
|
@@ -113,19 +113,19 @@ class MySQL implements DatabaseInterface {
|
113
|
113
|
return mysqli_query($db,$sql);
|
114
|
114
|
}
|
115
|
115
|
|
116
|
|
- public function fetch_assoc($result) {
|
|
116
|
+ public function fetchAssoc($result) {
|
117
|
117
|
return mysqli_fetch_assoc($result);
|
118
|
118
|
}
|
119
|
119
|
|
120
|
|
- public function fetch_row($result) {
|
|
120
|
+ public function fetchRow($result) {
|
121
|
121
|
return mysqli_fetch_row($result);
|
122
|
122
|
}
|
123
|
123
|
|
124
|
|
- public function insert_id($result) {
|
|
124
|
+ public function insertId($result) {
|
125
|
125
|
return mysqli_insert_id($this->db);
|
126
|
126
|
}
|
127
|
127
|
|
128
|
|
- public function affected_rows($result) {
|
|
128
|
+ public function affectedRows($result) {
|
129
|
129
|
return mysqli_affected_rows($this->db);
|
130
|
130
|
}
|
131
|
131
|
|
|
@@ -133,11 +133,11 @@ class MySQL implements DatabaseInterface {
|
133
|
133
|
return mysqli_free_result($result);
|
134
|
134
|
}
|
135
|
135
|
|
136
|
|
- public function fetch_fields($result) {
|
|
136
|
+ public function fetchFields($result) {
|
137
|
137
|
return mysqli_fetch_fields($result);
|
138
|
138
|
}
|
139
|
139
|
|
140
|
|
- public function add_limit_to_sql($sql,$limit,$offset) {
|
|
140
|
+ public function addLimitToSql($sql,$limit,$offset) {
|
141
|
141
|
return "$sql LIMIT $limit OFFSET $offset";
|
142
|
142
|
}
|
143
|
143
|
|
|
@@ -145,12 +145,12 @@ class MySQL implements DatabaseInterface {
|
145
|
145
|
return addcslashes($string,'%_');
|
146
|
146
|
}
|
147
|
147
|
|
148
|
|
- public function is_binary_type($field) {
|
|
148
|
+ public function isBinaryType($field) {
|
149
|
149
|
//echo "$field->name: $field->type ($field->flags)\n";
|
150
|
150
|
return (($field->flags & 128) && ($field->type>=249) && ($field->type<=252));
|
151
|
151
|
}
|
152
|
152
|
|
153
|
|
- public function base64_encode($string) {
|
|
153
|
+ public function base64Encode($string) {
|
154
|
154
|
return base64_encode($string);
|
155
|
155
|
}
|
156
|
156
|
|
|
@@ -243,7 +243,7 @@ class PostgreSQL implements DatabaseInterface {
|
243
|
243
|
return isset($this->queries[$name])?$this->queries[$name]:false;
|
244
|
244
|
}
|
245
|
245
|
|
246
|
|
- public function connectDatabase($hostname,$username,$password,$database,$port,$socket,$charset) {
|
|
246
|
+ public function connect($hostname,$username,$password,$database,$port,$socket,$charset) {
|
247
|
247
|
$e = function ($v) { return str_replace(array('\'','\\'),array('\\\'','\\\\'),$v); };
|
248
|
248
|
$conn_string = '';
|
249
|
249
|
if ($hostname || $socket) {
|
|
@@ -296,20 +296,20 @@ class PostgreSQL implements DatabaseInterface {
|
296
|
296
|
return @pg_query($db,$sql);
|
297
|
297
|
}
|
298
|
298
|
|
299
|
|
- public function fetch_assoc($result) {
|
|
299
|
+ public function fetchAssoc($result) {
|
300
|
300
|
return pg_fetch_assoc($result);
|
301
|
301
|
}
|
302
|
302
|
|
303
|
|
- public function fetch_row($result) {
|
|
303
|
+ public function fetchRow($result) {
|
304
|
304
|
return pg_fetch_row($result);
|
305
|
305
|
}
|
306
|
306
|
|
307
|
|
- public function insert_id($result) {
|
|
307
|
+ public function insertId($result) {
|
308
|
308
|
list($id) = pg_fetch_row($result);
|
309
|
309
|
return (int)$id;
|
310
|
310
|
}
|
311
|
311
|
|
312
|
|
- public function affected_rows($result) {
|
|
312
|
+ public function affectedRows($result) {
|
313
|
313
|
return pg_affected_rows($result);
|
314
|
314
|
}
|
315
|
315
|
|
|
@@ -317,7 +317,7 @@ class PostgreSQL implements DatabaseInterface {
|
317
|
317
|
return pg_free_result($result);
|
318
|
318
|
}
|
319
|
319
|
|
320
|
|
- public function fetch_fields($result) {
|
|
320
|
+ public function fetchFields($result) {
|
321
|
321
|
$keys = array();
|
322
|
322
|
for($i=0;$i<pg_num_fields($result);$i++) {
|
323
|
323
|
$field = array();
|
|
@@ -328,7 +328,7 @@ class PostgreSQL implements DatabaseInterface {
|
328
|
328
|
return $keys;
|
329
|
329
|
}
|
330
|
330
|
|
331
|
|
- public function add_limit_to_sql($sql,$limit,$offset) {
|
|
331
|
+ public function addLimitToSql($sql,$limit,$offset) {
|
332
|
332
|
return "$sql LIMIT $limit OFFSET $offset";
|
333
|
333
|
}
|
334
|
334
|
|
|
@@ -336,11 +336,11 @@ class PostgreSQL implements DatabaseInterface {
|
336
|
336
|
return addcslashes($string,'%_');
|
337
|
337
|
}
|
338
|
338
|
|
339
|
|
- public function is_binary_type($field) {
|
|
339
|
+ public function isBinaryType($field) {
|
340
|
340
|
return $field->type == 'bytea';
|
341
|
341
|
}
|
342
|
342
|
|
343
|
|
- public function base64_encode($string) {
|
|
343
|
+ public function base64Encode($string) {
|
344
|
344
|
return base64_encode(hex2bin(substr($string,2)));
|
345
|
345
|
}
|
346
|
346
|
|
|
@@ -433,7 +433,7 @@ class SQLServer implements DatabaseInterface {
|
433
|
433
|
return isset($this->queries[$name])?$this->queries[$name]:false;
|
434
|
434
|
}
|
435
|
435
|
|
436
|
|
- public function connectDatabase($hostname,$username,$password,$database,$port,$socket,$charset) {
|
|
436
|
+ public function connect($hostname,$username,$password,$database,$port,$socket,$charset) {
|
437
|
437
|
$connectionInfo = array();
|
438
|
438
|
if ($port) $hostname.=','.$port;
|
439
|
439
|
if ($username) $connectionInfo['UID']=$username;
|
|
@@ -490,25 +490,25 @@ class SQLServer implements DatabaseInterface {
|
490
|
490
|
return sqlsrv_query($db,$sql,$args)?:null;
|
491
|
491
|
}
|
492
|
492
|
|
493
|
|
- public function fetch_assoc($result) {
|
|
493
|
+ public function fetchAssoc($result) {
|
494
|
494
|
$values = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC);
|
495
|
495
|
if ($values) $values = array_map(function($v){ return is_null($v)?null:(string)$v; },$values);
|
496
|
496
|
return $values;
|
497
|
497
|
}
|
498
|
498
|
|
499
|
|
- public function fetch_row($result) {
|
|
499
|
+ public function fetchRow($result) {
|
500
|
500
|
$values = sqlsrv_fetch_array($result, SQLSRV_FETCH_NUMERIC);
|
501
|
501
|
if ($values) $values = array_map(function($v){ return is_null($v)?null:(string)$v; },$values);
|
502
|
502
|
return $values;
|
503
|
503
|
}
|
504
|
504
|
|
505
|
|
- public function insert_id($result) {
|
|
505
|
+ public function insertId($result) {
|
506
|
506
|
sqlsrv_next_result($result);
|
507
|
507
|
sqlsrv_fetch($result);
|
508
|
508
|
return (int)sqlsrv_get_field($result, 0);
|
509
|
509
|
}
|
510
|
510
|
|
511
|
|
- public function affected_rows($result) {
|
|
511
|
+ public function affectedRows($result) {
|
512
|
512
|
return sqlsrv_rows_affected($result);
|
513
|
513
|
}
|
514
|
514
|
|
|
@@ -516,7 +516,7 @@ class SQLServer implements DatabaseInterface {
|
516
|
516
|
return sqlsrv_free_stmt($result);
|
517
|
517
|
}
|
518
|
518
|
|
519
|
|
- public function fetch_fields($result) {
|
|
519
|
+ public function fetchFields($result) {
|
520
|
520
|
//var_dump(sqlsrv_field_metadata($result));
|
521
|
521
|
return array_map(function($a){
|
522
|
522
|
$p = array();
|
|
@@ -527,7 +527,7 @@ class SQLServer implements DatabaseInterface {
|
527
|
527
|
},sqlsrv_field_metadata($result));
|
528
|
528
|
}
|
529
|
529
|
|
530
|
|
- public function add_limit_to_sql($sql,$limit,$offset) {
|
|
530
|
+ public function addLimitToSql($sql,$limit,$offset) {
|
531
|
531
|
return "$sql OFFSET $offset ROWS FETCH NEXT $limit ROWS ONLY";
|
532
|
532
|
}
|
533
|
533
|
|
|
@@ -535,11 +535,11 @@ class SQLServer implements DatabaseInterface {
|
535
|
535
|
return str_replace(array('%','_'),array('[%]','[_]'),$string);
|
536
|
536
|
}
|
537
|
537
|
|
538
|
|
- public function is_binary_type($field) {
|
|
538
|
+ public function isBinaryType($field) {
|
539
|
539
|
return ($field->type>=-4 && $field->type<=-2);
|
540
|
540
|
}
|
541
|
541
|
|
542
|
|
- public function base64_encode($string) {
|
|
542
|
+ public function base64Encode($string) {
|
543
|
543
|
return base64_encode($string);
|
544
|
544
|
}
|
545
|
545
|
|
|
@@ -677,7 +677,7 @@ class PHP_CRUD_API {
|
677
|
677
|
$table_list = array();
|
678
|
678
|
foreach ($table_array as $table) {
|
679
|
679
|
if ($result = $this->db->query($this->db->getSql('reflect_table'),array($table,$database))) {
|
680
|
|
- while ($row = $this->db->fetch_row($result)) $table_list[] = $row[0];
|
|
680
|
+ while ($row = $this->db->fetchRow($result)) $table_list[] = $row[0];
|
681
|
681
|
$this->db->close($result);
|
682
|
682
|
if ($action!='list') break;
|
683
|
683
|
}
|
|
@@ -740,7 +740,7 @@ class PHP_CRUD_API {
|
740
|
740
|
$count = 0;
|
741
|
741
|
$field = false;
|
742
|
742
|
if ($result = $this->db->query($this->db->getSql('reflect_pk'),array($tables[0],$database))) {
|
743
|
|
- while ($row = $this->db->fetch_row($result)) {
|
|
743
|
+ while ($row = $this->db->fetchRow($result)) {
|
744
|
744
|
$count++;
|
745
|
745
|
$field = $row[0];
|
746
|
746
|
}
|
|
@@ -815,10 +815,10 @@ class PHP_CRUD_API {
|
815
|
815
|
$filters[$table]['or'][] = array($key[1],'=',$key[0]);
|
816
|
816
|
$this->addWhereFromFilters($filters[$table],$sql,$params);
|
817
|
817
|
if ($result = $this->db->query($sql,$params)) {
|
818
|
|
- $object = $this->db->fetch_assoc($result);
|
|
818
|
+ $object = $this->db->fetchAssoc($result);
|
819
|
819
|
foreach ($fields[$table] as $field) {
|
820
|
|
- if ($this->db->is_binary_type($field) && $object[$field->name]) {
|
821
|
|
- $object[$field->name] = $this->db->base64_encode($object[$field->name]);
|
|
820
|
+ if ($this->db->isBinaryType($field) && $object[$field->name]) {
|
|
821
|
+ $object[$field->name] = $this->db->base64Encode($object[$field->name]);
|
822
|
822
|
}
|
823
|
823
|
}
|
824
|
824
|
$this->db->close($result);
|
|
@@ -835,7 +835,7 @@ class PHP_CRUD_API {
|
835
|
835
|
array_unshift($params, $tables[0]);
|
836
|
836
|
$result = $this->db->query('INSERT INTO "!" ("'.$keys.'") VALUES ('.$values.')',$params);
|
837
|
837
|
if (!$result) return null;
|
838
|
|
- return $this->db->insert_id($result);
|
|
838
|
+ return $this->db->insertId($result);
|
839
|
839
|
}
|
840
|
840
|
|
841
|
841
|
protected function updateObject($key,$input,$filters,$tables) {
|
|
@@ -856,7 +856,7 @@ class PHP_CRUD_API {
|
856
|
856
|
$filters[$table]['or'][] = array($key[1],'=',$key[0]);
|
857
|
857
|
$this->addWhereFromFilters($filters[$table],$sql,$params);
|
858
|
858
|
$result = $this->db->query($sql,$params);
|
859
|
|
- return $this->db->affected_rows($result);
|
|
859
|
+ return $this->db->affectedRows($result);
|
860
|
860
|
}
|
861
|
861
|
|
862
|
862
|
protected function deleteObject($key,$filters,$tables) {
|
|
@@ -868,7 +868,7 @@ class PHP_CRUD_API {
|
868
|
868
|
$filters[$table]['or'][] = array($key[1],'=',$key[0]);
|
869
|
869
|
$this->addWhereFromFilters($filters[$table],$sql,$params);
|
870
|
870
|
$result = $this->db->query($sql,$params);
|
871
|
|
- return $this->db->affected_rows($result);
|
|
871
|
+ return $this->db->affectedRows($result);
|
872
|
872
|
}
|
873
|
873
|
|
874
|
874
|
protected function findRelations($tables,$database) {
|
|
@@ -881,19 +881,19 @@ class PHP_CRUD_API {
|
881
|
881
|
$tableset[] = $table0;
|
882
|
882
|
|
883
|
883
|
$result = $this->db->query($this->db->getSql('reflect_belongs_to'),array($table0,$tables,$database,$database));
|
884
|
|
- while ($row = $this->db->fetch_row($result)) {
|
|
884
|
+ while ($row = $this->db->fetchRow($result)) {
|
885
|
885
|
$collect[$row[0]][$row[1]]=array();
|
886
|
886
|
$select[$row[2]][$row[3]]=array($row[0],$row[1]);
|
887
|
887
|
if (!in_array($row[0],$tableset)) $tableset[] = $row[0];
|
888
|
888
|
}
|
889
|
889
|
$result = $this->db->query($this->db->getSql('reflect_has_many'),array($tables,$table0,$database,$database));
|
890
|
|
- while ($row = $this->db->fetch_row($result)) {
|
|
890
|
+ while ($row = $this->db->fetchRow($result)) {
|
891
|
891
|
$collect[$row[2]][$row[3]]=array();
|
892
|
892
|
$select[$row[0]][$row[1]]=array($row[2],$row[3]);
|
893
|
893
|
if (!in_array($row[2],$tableset)) $tableset[] = $row[2];
|
894
|
894
|
}
|
895
|
895
|
$result = $this->db->query($this->db->getSql('reflect_habtm'),array($database,$database,$database,$database,$table0,$tables));
|
896
|
|
- while ($row = $this->db->fetch_row($result)) {
|
|
896
|
+ while ($row = $this->db->fetchRow($result)) {
|
897
|
897
|
$collect[$row[2]][$row[3]]=array();
|
898
|
898
|
$select[$row[0]][$row[1]]=array($row[2],$row[3]);
|
899
|
899
|
$collect[$row[4]][$row[5]]=array();
|
|
@@ -950,7 +950,7 @@ class PHP_CRUD_API {
|
950
|
950
|
protected function findTableFields($table,$database) {
|
951
|
951
|
$fields = array();
|
952
|
952
|
$result = $this->db->query('SELECT * FROM "!" WHERE 1=2;',array($table));
|
953
|
|
- foreach ($this->db->fetch_fields($result) as $field) {
|
|
953
|
+ foreach ($this->db->fetchFields($result) as $field) {
|
954
|
954
|
$fields[$field->name] = $field;
|
955
|
955
|
}
|
956
|
956
|
return $fields;
|
|
@@ -967,7 +967,7 @@ class PHP_CRUD_API {
|
967
|
967
|
|
968
|
968
|
protected function convertBinary(&$input,$keys) {
|
969
|
969
|
foreach ($keys as $key=>$field) {
|
970
|
|
- if (isset($input->$key) && $input->$key && $this->db->is_binary_type($field)) {
|
|
970
|
+ if (isset($input->$key) && $input->$key && $this->db->isBinaryType($field)) {
|
971
|
971
|
$data = $input->$key;
|
972
|
972
|
$data = str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT);
|
973
|
973
|
$input->$key = (object)array('type'=>'base64','data'=>$data);
|
|
@@ -1061,7 +1061,7 @@ class PHP_CRUD_API {
|
1061
|
1061
|
$this->addWhereFromFilters($filters[$table],$sql,$params);
|
1062
|
1062
|
}
|
1063
|
1063
|
if ($result = $this->db->query($sql,$params)) {
|
1064
|
|
- while ($pages = $this->db->fetch_row($result)) {
|
|
1064
|
+ while ($pages = $this->db->fetchRow($result)) {
|
1065
|
1065
|
$count = $pages[0];
|
1066
|
1066
|
}
|
1067
|
1067
|
}
|
|
@@ -1080,21 +1080,21 @@ class PHP_CRUD_API {
|
1080
|
1080
|
$params[] = $order[1];
|
1081
|
1081
|
}
|
1082
|
1082
|
if (is_array($order) && is_array($page)) {
|
1083
|
|
- $sql = $this->db->add_limit_to_sql($sql,$page[1],$page[0]);
|
|
1083
|
+ $sql = $this->db->addLimitToSql($sql,$page[1],$page[0]);
|
1084
|
1084
|
}
|
1085
|
1085
|
if ($result = $this->db->query($sql,$params)) {
|
1086
|
1086
|
echo '"columns":';
|
1087
|
1087
|
$keys = array();
|
1088
|
1088
|
$base64 = array();
|
1089
|
1089
|
foreach ($fields[$table] as $field) {
|
1090
|
|
- $base64[] = $this->db->is_binary_type($field);
|
|
1090
|
+ $base64[] = $this->db->isBinaryType($field);
|
1091
|
1091
|
$keys[] = $field->name;
|
1092
|
1092
|
}
|
1093
|
1093
|
echo json_encode($keys);
|
1094
|
1094
|
$keys = array_flip($keys);
|
1095
|
1095
|
echo ',"records":[';
|
1096
|
1096
|
$first_row = true;
|
1097
|
|
- while ($row = $this->db->fetch_row($result)) {
|
|
1097
|
+ while ($row = $this->db->fetchRow($result)) {
|
1098
|
1098
|
if ($first_row) $first_row = false;
|
1099
|
1099
|
else echo ',';
|
1100
|
1100
|
if (isset($collect[$table])) {
|
|
@@ -1104,7 +1104,7 @@ class PHP_CRUD_API {
|
1104
|
1104
|
}
|
1105
|
1105
|
foreach ($base64 as $k=>$v) {
|
1106
|
1106
|
if ($v && $row[$k]) {
|
1107
|
|
- $row[$k] = $this->db->base64_encode($row[$k]);
|
|
1107
|
+ $row[$k] = $this->db->base64Encode($row[$k]);
|
1108
|
1108
|
}
|
1109
|
1109
|
}
|
1110
|
1110
|
echo json_encode($row);
|
|
@@ -1145,14 +1145,14 @@ class PHP_CRUD_API {
|
1145
|
1145
|
$keys = array();
|
1146
|
1146
|
$base64 = array();
|
1147
|
1147
|
foreach ($fields[$table] as $field) {
|
1148
|
|
- $base64[] = $this->db->is_binary_type($field);
|
|
1148
|
+ $base64[] = $this->db->isBinaryType($field);
|
1149
|
1149
|
$keys[] = $field->name;
|
1150
|
1150
|
}
|
1151
|
1151
|
echo json_encode($keys);
|
1152
|
1152
|
$keys = array_flip($keys);
|
1153
|
1153
|
echo ',"records":[';
|
1154
|
1154
|
$first_row = true;
|
1155
|
|
- while ($row = $this->db->fetch_row($result)) {
|
|
1155
|
+ while ($row = $this->db->fetchRow($result)) {
|
1156
|
1156
|
if ($first_row) $first_row = false;
|
1157
|
1157
|
else echo ',';
|
1158
|
1158
|
if (isset($collect[$table])) {
|
|
@@ -1162,7 +1162,7 @@ class PHP_CRUD_API {
|
1162
|
1162
|
}
|
1163
|
1163
|
foreach ($base64 as $k=>$v) {
|
1164
|
1164
|
if ($v && $row[$k]) {
|
1165
|
|
- $row[$k] = $this->db->base64_encode($row[$k]);
|
|
1165
|
+ $row[$k] = $this->db->base64Encode($row[$k]);
|
1166
|
1166
|
}
|
1167
|
1167
|
}
|
1168
|
1168
|
echo json_encode($row);
|
|
@@ -1276,7 +1276,7 @@ class PHP_CRUD_API {
|
1276
|
1276
|
if (!$charset) {
|
1277
|
1277
|
$charset = $db->getDefaultCharset();
|
1278
|
1278
|
}
|
1279
|
|
- $db->connectDatabase($hostname,$username,$password,$database,$port,$socket,$charset);
|
|
1279
|
+ $db->connect($hostname,$username,$password,$database,$port,$socket,$charset);
|
1280
|
1280
|
}
|
1281
|
1281
|
|
1282
|
1282
|
$this->db = $db;
|