some optimization for IPPF forms
[openemr.git] / library / DBC_database.php
blob9b2550261e05ae96b9a69ab32d93f3aae05d489a
1 <?php
2 /**
3 * DBC DUTCH SYSTEM
4 * DATABASE FUNCTIONS - A Simple but effective framework over database operations
5 *
6 * Cristian Navalici lemonsoftware [ @ ] gmail [ . ] com
7 * @version 2.0 May 2008
9 */
11 // if TRUE, before every sql statement will run SET NAMES utf8
12 define('SETNAMES', TRUE);
14 //-----------------------------------------------------------------------------
15 /**
16 * DBC SQL SELECT FUNCTION
18 * @param string $what - what to select
19 * @param string $from - the table to retrieve the results from
20 * @param array the where clause
21 * @param string the order clause
22 * @param string the join clause
23 * @param int debug flag - if 1, just echo the query, don't execute anything
24 * @return resource returns the mysql resource or FALSE
26 function dsql_select($what, $from, $where = NULL, $order = NULL, $join = NULL, $debug = 0) {
28 // make the query string
29 $q = "SELECT $what FROM $from ";
31 if ( $join ) {
32 $q .= " JOIN $join ";
35 if ( $where ) {
36 $wherestr = _dsql_where($where);
37 $q .= "WHERE $wherestr ";
39 if ( $order ) $q .= "ORDER BY $order ";
41 if ( $debug ) echo $q . '<br />';
42 else return _dsql_query($q);
46 //-----------------------------------------------------------------------------
47 /**
48 * DBC SQL START TRANSACTION
50 * @param none
51 * @return bool
53 function dsql_starttrans() {
55 $q = 'START TRANSACTION';
56 return _dsql_query($q);
59 //-----------------------------------------------------------------------------
60 /**
61 * DBC SQL COMMIT TRANSACTION
63 * @param none
64 * @return bool
66 function dsql_commit() {
68 $q = 'COMMIT';
69 return _dsql_query($q);
72 //-----------------------------------------------------------------------------
73 /**
74 * DBC SQL UPDATE FUNCTION
76 * @param string the table to retrieve the results from
77 * @param array an associative array of update values
78 * @param array the where clause
79 * @param int debug flag - if 1, just echo the query, don't execute anything
80 * @return bool
82 function dsql_update($table, $set, $where, $debug = 0) {
84 $setstr = _dsql_set($set);
85 $wherestr = _dsql_where($where);
87 // make the query string
88 $q = "UPDATE $table SET $setstr WHERE $wherestr";
90 if ( $debug ) echo $q . '<br />';
91 else return _dsql_query($q);
95 //-----------------------------------------------------------------------------
96 /**
97 * DBC SQL INSERT FUNCTION
99 * @param string $table - the table to insert the values
100 * @param array $flds - an array with table fields
101 * @param array $val - an array with inserted values
102 * @param int debug flag - if 1, just echo the query, don't execute anything
103 * @return bool
105 function dsql_insert($table, $flds, $val, $debug = 0) {
107 $fields = _dsql_fields($flds);
108 $values = _dsql_ivalues($val);
110 // make the query string
111 $q = "INSERT INTO $table $fields $values";
113 if ( $debug ) echo $q . '<br />';
114 else return _dsql_query($q);
118 //-----------------------------------------------------------------------------
120 * DBC SQL INSERT ON DUPLICATE FUNCTIONS
122 * @param string $table - the table to insert the values
123 * @param array $flds - an array with table fields
124 * @param array $val - an array with inserted values
125 * @param int debug flag - if 1, just echo the query, don't execute anything
126 * @return bool
128 function dsql_insert_duplicate($table, $flds, $val, $set = 0, $debug = 0) {
130 $fields = _dsql_fields($flds);
131 $values = _dsql_ivalues($val);
132 $update = _dsql_set($set);
134 // make the query string
135 $q = "INSERT INTO $table $fields $values ON DUPLICATE KEY UPDATE $update";
137 if ( $debug ) echo $q . '<br />';
138 else return _dsql_query($q);
141 //-----------------------------------------------------------------------------
143 * DBC SQL LAST INSERTED ID FUNCTION
145 * @param none
146 * @return int
148 function dsql_lastid() {
149 if ( isset($GLOBALS['dbh']) ) {
150 // if we call from webinterface
151 $r = mysql_insert_id($GLOBALS['dbh']) or die(mysql_error());
152 } else {
153 // if we call from CLI
154 $r = mysql_insert_id() or die(mysql_error());
157 return $r;
160 //-----------------------------------------------------------------------------
162 // PRIVATE FUNCTIONS BELOW
163 //-----------------------------------------------------------------------------
166 //-----------------------------------------------------------------------------
168 * DBC SQL QUERY FUNCTION
170 * @param string $str - the query string
171 * @return resource|bool
173 function _dsql_query($str) {
175 if ( isset($GLOBALS['dbh']) ) {
176 // if we call from webinterface
177 if ( SETNAMES ) mysql_query("SET NAMES utf8", $GLOBALS['dbh']);
178 $r = mysql_query($str, $GLOBALS['dbh']) or die(mysql_error());
179 } else {
180 // if we call from CLI
181 $r = mysql_query($str) or die(mysql_error());
184 return $r;
188 //-----------------------------------------------------------------------------
190 * PREPARE THE SET STRING
192 * @param array the array to compile
193 * @return string
195 function _dsql_set($setarr) {
196 $setstr = '';
198 foreach ( $setarr as $sk => $sv) {
199 $setstr .= " {$sk} = {$sv},";
201 $setstr = substr(trim($setstr), 0, -1); // cut the last ,
203 return $setstr;
206 //-----------------------------------------------------------------------------
208 * PREPARE THE WHERE STRING
210 * @param array the array to compile
211 * @return string
213 function _dsql_where($wherearr) {
214 if ( !$wherearr ) return NULL;
215 $wherestr = '';
217 foreach ( $wherearr as $wk => $wv) {
218 // for the same key we could have an array as arg = multiple values (otherwise keys are not unique)
219 if ( is_array($wv) ) {
221 foreach ( $wv as $v ) {
222 // if we have value starting with <=; != or => then we don't use = for them
223 $first = substr(trim($v), 0, 1);
224 if ( $first != '>' && $first != '<' && $first != '!' ) $sign = '=';
225 else $sign = '';
227 $wherestr .= " {$wk} $sign {$v} ";
228 } // foreach
230 } else {
232 // if we have value starting with <=; != or => then we don't use = for them
233 $first = substr(trim($wv), 0, 1);
234 if ( $first != '>' && $first != '<' && $first != '!' ) $sign = '=';
235 else $sign = '';
238 $wherestr .= " {$wk} $sign {$wv} ";
240 } // foreach
242 return $wherestr;
245 //-----------------------------------------------------------------------------
247 * PREPARE THE FIELDS STRING
249 * @param array the array to compile
250 * @return string
252 function _dsql_fields($fiearr) {
253 if ( !is_array($fiearr) || !$fiearr ) return '';
255 $fiestr = '(';
256 foreach ( $fiearr as $fk => $fv) {
257 $fiestr .= " {$fv}, ";
259 $fiestr = substr(trim($fiestr), 0, -1); // cut the last ,
260 $fiestr .= ')';
262 return $fiestr;
265 //-----------------------------------------------------------------------------
267 * PREPARE THE VALUES STRING
269 * used by insert
271 * @param array the array to compile
272 * @return string
274 function _dsql_ivalues($values) {
276 $valstr = 'VALUES (';
277 foreach ( $values as $vk => $vv) {
278 $valstr .= ( is_int($vv) ) ? "{$vv}, " : " '{$vv}', ";
280 $valstr = substr(trim($valstr), 0, -1); // cut the last ,
281 $valstr .= ')';
283 return $valstr;
286 //-----------------------------------------------------------------------------