forked from sabbirahmed395/sqlHelper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlHelper.php
79 lines (67 loc) · 2.05 KB
/
sqlHelper.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
<?php
class sqlHelper {
public static function bind(array $array = [], $dilimiter = "AND") {
if(count($array) === 0) {
throw new Exception("Please passes an array, that contain column name as key and value as value");
}
$keys = array_keys($array);
$clause = "`".$keys[0]."`='".$array[$keys[0]]."'";
if(count($array)>1) {
for($i=1;$i<=count($array)-1;$i++) {
$clause .= " ".$dilimiter." `".$keys[$i]."`='".$array[$keys[$i]]."'";
}
}
return $clause;
}
public static function serialize(array $array, $type) {
if(count($array) === 0) {
throw new Exception("Please passes an array, that contain column name as key and value as value");
}
if($type==="value") {
$keys = array_keys($array);
$clause = "`".$array[$keys[0]]."`";
if(count($array) > 1) {
for($i=1;$i<=count($array)-1;$i++) {
$clause .= ", `".$array[$keys[$i]]."`";
}
}
}
if($type==="key") {
$keys = array_keys($array);
$clause = "`".$keys[0]."`";
if(count($keys) > 1) {
for($i=1;$i<=count($keys)-1;$i++) {
$clause .= ", `".$keys[$i]."`";
}
}
}
return $clause;
}
public static function select($table, array $data, $where=[], $limit=1) {
if(is_array($where) === FALSE) {
$limit = $where;
$where=[];
}
$clause = "SELECT ".sqlHelper::serialize($data, 'value')." FROM `".$table."`";
if(count($where)>0){
$clause .= " WHERE ".sqlHelper::bind($where);
}
if($limit !== "*") {
$clause .= " LIMIT ".$limit;
}
return $clause;
}
public static function update($table, array $data, array $where) {
return "UPDATE `".$table."` SET ".sqlHelper::bind($data, ',')." WHERE ".sqlHelper::bind($where);
}
public static function insert($table, array $data) {
return "INSERT INTO `".$table."` (".sqlHelper::serialize($data, 'key').") VALUES (".sqlHelper::serialize($data, 'value').")";
}
public static function delete($table, array $where, $limit=1) {
$clause = "DELETE FROM `".$table."` WHERE ".sqlHelper::bind($where);
if($limit !== "*") {
$clause .= " LIMIT ".$limit;
}
return $clause;
}
}