public
Description: Some PHP libs
Homepage: http://mihasya.com
Clone URL: git://github.com/mihasya/mihasya_libs.git
mihasya_libs / mihasya_db.php
100644 159 lines (156 sloc) 5.642 kb
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
<?php
ini_set('display_errors', '1');
function db_insert($pdo, $table, $kvValues) {
    $keys = array_keys($kvValues);
    $values = $placeHolders = array(); //declare scope arrays
    $q='INSERT INTO `'.$table.'` (';
    foreach ($kvValues as $k=>$v) {
        if (is_array($v)) {
            $placeHolders[] = $v['func']; //plug the user supplied function instead of the ?
            $values = array_merge($values, $v['values']);
        } else {
            $placeHolders[] = '?';
            $values[] = $v;
        }
    }
    $q='INSERT INTO `'.$table.'` ('
    .implode(',', $keys).') VALUES ('
    .implode(',', $placeHolders).')';
    $stmt = $pdo->prepare($q);
    if (!$stmt) {
        $err = $pdo->errorInfo();
        throw new Exception('Query Failed at prepare! ' . $err[2], $err[1]);
    }
    $res = $stmt->execute($values); //execute the query, using the $values array to plug the placeholders
    if(!$res) {
        $err = $stmt->errorInfo();
        throw new Exception('Query Failed at execute! ' . $err[2], $err[1]);
    }
    return $pdo->lastInsertId(); //return what's useful; this will never be 0 on success
}
 
function db_update($pdo, $table, $where, $what) {
    $values = array(); //declare scope arrays
    $whatStr = $whereStr = ''; //declare scope string.
    foreach ($what as $k=>$v) {
        if (is_array($v)) {
            //there's a function involved
            $whatStr.=$k.'='.$v['func'].',';
            $values = array_merge($values, $v['values']);
        } else {
            //just a value
            $whatStr.=$k.'=?,';
            $values[] = $v;
        }
    }
    foreach ($where as $k=>$v) {
        if (is_array($v)) {
            //there's a function involved
            $whereStr.=$k.'='.$v['func'].' AND ';
            $values = array_merge($values, $v['values']);
        } else {
            //just a value
            $whereStr.=$k.'=? AND ';
            $values[] = $v;
        }
    }
    $whatStr = rtrim($whatStr, ',');
    $whereStr = rtrim($whereStr, ' AND ');
    $q = 'UPDATE `'.$table.'` SET '.$whatStr.' WHERE '.$whereStr;
    $stmt = $pdo->prepare($q);
    if (!$stmt) {
        $err = $pdo->errorInfo();
        throw new Exception('Query Failed at prepare! ' . $err[2], $err[1]);
    }
    $res = $stmt->execute($values); //execute the query, using the $values array to plug the placeholders
    if(!$res) {
        $err = $stmt->errorInfo();
        throw new Exception('Query Failed at execute! ' . $err[2], $err[1]);
    }
    return $stmt->rowCount(); //return what's useful; this will never be 0 on success
}
//TODO: OR's in WHERE? $where can be a string?
function db_get($pdo, $table, $where=null, $what=null, $postfix=null) {
    $whereStr = '';
    $whatStr ='';
    $values=array();
    if (!$what || $what=='*') {
        $whatStr = '*';
    } else {
        $whatStr = implode(',',$what);
    }
    if ($where) {
        $whereStr.=' WHERE ';
        if (is_numeric($where)) { //if an id has been supplied, use it.
            $whereStr.= $table.'_id=?';
            $values[] = $where;
        } else { //a key value array has been supplied for the lookup
            foreach ($where as $k=>$v) {
                if (is_array($v)) {
                    //there's a function involved
                    $whereStr.=$k.'='.$v['func'].' AND ';
                    $values = array_merge($values, $v['values']);
                } else {
                    //just a value
                    $whereStr.=$k.'=? AND ';
                    $values[] = $v;
                }
            }
        }
    }
    $whereStr = rtrim($whereStr, ' AND ');
    $q = 'SELECT '.$whatStr.' FROM '.$table.$whereStr;
    if ($postfix) {
        $q.=' '.$postfix;
    }
    $stmt = $pdo->prepare($q);
    if (!$stmt) {
        $err = $pdo->errorInfo();
        throw new Exception('Query Failed at prepare! ' . $err[2], $err[1]);
    }
    $res = $stmt->execute($values); //execute the query, using the $values array to plug the placeholders
    if(!$res) {
        $err = $stmt->errorInfo();
        throw new Exception('Query Failed at execute! ' . $err[2], $err[1]);
    }
    return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
//TODO: recheck this and test it.
function db_delete($pdo, $table, $where, $postfix=null) {
    $whereStr.=' WHERE ';
    $values = array();
    if (is_numeric($where)) { //if an id has been supplied, use it.
        $whereStr.= $table.'_id=?';
        $values[] = $where;
    } else { //a key value array has been supplied for the lookup
        foreach ($where as $k=>$v) {
            if (is_array($v)) {
                //there's a function involved
                $whereStr.=$k.'='.$v['func'].' AND ';
                $values = array_merge($values, $v['values']);
            } else {
                //just a value
                $whereStr.=$k.'=? AND ';
                $values[] = $v;
            }
        }
    }
    $whereStr = rtrim($whereStr, ' AND ');
    $q = 'DELETE FROM '.$table.' '.$whereStr;
    if ($postfix) {
        $q.=' '.$postfix;
    }
    $stmt = $pdo->prepare($q);
    if (!$stmt) {
        $err = $pdo->errorInfo();
        throw new Exception('Query Failed at prepare! ' . $err[2], $err[1]);
    }
    $res = $stmt->execute($values); //execute the query, using the $values array to plug the placeholders
    if(!$res) {
        $err = $stmt->errorInfo();
        throw new Exception('Query Failed at execute! ' . $err[2], $err[1]);
    }
    return $stmt->rowCount();
}
 
//TODO: write a function to interpret unique key errors using
//use "show index from tablename" and a local copy of the database for performance
?>