5 require_once(
'base/Ego_Sql_Abstract.php');
18 private $_queryHandle;
24 function connect($database=
'', $host=
'', $user=
'', $password=
'')
26 if (func_num_args()==0)
33 $this->_dbHandle = @mysqli_connect(
34 $GLOBALS[
'egotec_conf'][
'db'][
'host'],
35 $GLOBALS[
'egotec_conf'][
'db'][
'user'],
36 $GLOBALS[
'egotec_conf'][
'db'][
'password'],
37 $GLOBALS[
'egotec_conf'][
'db'][
'database'],
38 $GLOBALS[
'egotec_conf'][
'db'][
'port'],
39 $GLOBALS[
'egotec_conf'][
'db'][
'socket']
44 $this->_dbHandle = @mysqli_connect($host, $user, $password, $database)
47 if (!$this->_dbHandle)
52 $this->
query(
'SET NAMES utf8');
53 $this->
query(
'SET collation_connection=\'utf8_bin\'');
54 if ($GLOBALS[
'egotec_conf'][
'db'][
'profiling'])
56 $this->
query(
'SET PROFILING=1');
66 function query($query, $appendCurrent =
false)
69 $this->currentQuery .= $query;
71 $this->currentQuery = $query;
73 $this->_queryHandle = mysqli_query($this->_dbHandle, $query);
74 if (!$this->_queryHandle) {
77 $this->_currentRow = -1;
78 return $this->_queryHandle;
89 return $this->Record = $this->_queryHandle?mysqli_fetch_assoc($this->_queryHandle):
false;
99 return $this->_queryHandle?mysqli_num_rows($this->_queryHandle):0;
112 if ($query[
'bitand']) {
113 if (empty($query[
'where'])) {
114 $query[
'where'] =
'1=1';
116 foreach ($query[
'bitand'] as $bit) {
117 $query[
'where'] .=
' AND '.$bit[0].
'&'.$bit[1].
'='.$bit[2];
120 return 'SELECT '.($query[
'no_cache']?
'SQL_NO_CACHE ':
'')
121 .($query[
'distinct']?
'DISTINCT ':
'')
122 .($query[
'fields']?$query[
'fields']:
'*').($query[
'fields2']?
','.$query[
'fields2']:
'')
123 .
' FROM ('.($query[
'table']?$query[
'table']:$query[
'from'])
124 .($query[
'table2']?($query[
'table']?
',':
'').$query[
'table2']:
'').
')' 125 .($query[
'inner']?
' INNER JOIN '.(is_array($query[
'inner'])?
126 implode(
' INNER JOIN ', $query[
'inner']):$query[
'inner']):
'')
127 .($query[
'join']?
' LEFT JOIN '.(is_array($query[
'join'])?
128 implode(
' LEFT JOIN ', $query[
'join']):$query[
'join']):
'')
129 .($query[
'where']?
' WHERE '.$query[
'where']:
'')
130 .($query[
'group']?
' GROUP BY '.$query[
'group']:
'')
131 .($query[
'having']?
' HAVING '.$query[
'having']:
'')
132 .($query[
'order']?
' ORDER BY '.$query[
'order']:
'');
145 if ($query[
'field_as'])
147 foreach ($query[
'field_as'] as $k=>$v)
149 if ($query[
'field_as_bitand'][$k]) {
150 foreach ($query[
'field_as_bitand'][$k] as $bit) {
151 $v .=
' AND '.$bit[0].
'&'.$bit[1].
'='.$bit[2];
154 $query[
'fields2'] = ($query[
'fields2']?$query[
'fields2'].
',':
'').
'('.$v.
') AS '.$k;
156 unset($query[
'field_as']);
159 if ($query[
'proposed_order'])
161 $query[
'order'] =
'FIELD('.$query[
'proposed_order'][
'field'].
','.implode(
',', $query[
'proposed_order'][
'values']).
')';
166 foreach ($query[
'union'] as $union)
168 $union = array_merge($query, $union);
169 $union[
'where'] = $query[
'where']?
'('.$query[
'where'].
') AND ('.$union[
'where'].
')':$union[
'where'];
170 $union[
'join'] = $query[
'join']?array_merge_recursive($query[
'join'], $union[
'join']):$union[
'join'];
171 $unions[] =
'('.$this->_selectString($union).
')';
173 $s = implode(
' UNION ', $unions).($query[
'order']?
' order by'.preg_replace(
'/([, ])[^, .]+?\./',
'$1',
' '.$query[
'order']):
'');
179 $limit = explode(
',', $query[
'limit']);
180 $s.=
' LIMIT '.$limit[0].(sizeof($limit)>1?
','.$limit[1]:
'');
184 $GLOBALS[
'prepare_execute_stm'] = array();
185 $q =
'PREPARE STMT FROM "'.preg_replace_callback(
'#:([a-zA-Z][a-zA-Z0-9_]*)#ims',
'ego_sql_mysqli_prepare_callback', $s).
'"';
186 if (!empty($GLOBALS[
'prepare_execute_stm'])){
187 if (!mysqli_real_query($this->_dbHandle, $q))
191 foreach ($query[
'bind'] as $k=>$v)
193 $q =
'SET @'.$k.
'=\''.mysqli_real_escape_string($this->_dbHandle, $v).
'\'';
194 mysqli_real_query($this->_dbHandle, $q);
196 $s =
'EXECUTE STMT USING '.implode(
',', $GLOBALS[
'prepare_execute_stm']);
199 return $this->
query($s);
202 function delete($query)
204 assert($query[
'table']||$query[
'from']);
206 .($query[
'fields']?$query[
'fields'].
' ':
'')
208 .($query[
'table']?$query[
'table']:$query[
'from'])
209 .($query[
'inner']?
' INNER JOIN '.(is_array($query[
'inner'])?
210 implode(
' INNER JOIN ', $query[
'inner']):$query[
'inner']):
'')
211 .($query[
'join']?
' LEFT JOIN '.(is_array($query[
'join'])?
212 implode(
' LEFT JOIN ', $query[
'join']):$query[
'join']):
'')
213 .($query[
'where']?
' WHERE '.$query[
'where']:
'');
216 $GLOBALS[
'prepare_execute_stm'] = array();
217 $q =
'PREPARE STMT FROM "'.preg_replace_callback(
'#:([a-zA-Z][a-zA-Z0-9_]*)#ims',
'ego_sql_mysqli_prepare_callback', $s).
'"';
218 if (!mysqli_real_query($this->_dbHandle, $q))
222 foreach ($query[
'bind'] as $k=>$v)
224 $q =
'SET @'.$k.
'=\''.mysqli_real_escape_string($this->_dbHandle, $v).
'\'';
225 mysqli_real_query($this->_dbHandle, $q);
227 $s =
'EXECUTE STMT USING '.implode(
',', $GLOBALS[
'prepare_execute_stm']);
229 return $this->
query($s);
232 private function _getUpdateField($query)
234 if (isset($query[
'key']))
236 $query[
'set'] = array_combine($query[
'key'], $query[
'val']);
239 if (isset($query[
'set']))
241 foreach ($query[
'set'] as $k => $v)
244 $field[] =
'`'.$k.
'`=NULL';
246 $field[] =
'`'.$k.
'`=\''.str_replace(array(
'\\',
'\''), array(
'\\\\',
'\'\
''), $v).
'\'';
261 $field = $this->_getUpdateField($query);
262 if (isset($query[
'set_f']))
264 foreach ($query[
'set_f'] as $k => $v)
266 $field[] =
'`'.$k.
'`='.$v;
269 $s =
'UPDATE '.($query[
'ignore']?
'IGNORE ':
'').
271 ' SET '.implode(
',', $field).
272 ($query[
'where']?
' WHERE '.$query[
'where']:
'');
275 $GLOBALS[
'prepare_execute_stm'] = array();
276 $q =
'PREPARE STMT FROM "'.preg_replace_callback(
'#:([a-zA-Z][a-zA-Z0-9_]*)#ims',
'ego_sql_mysqli_prepare_callback', $s).
'"';
277 if (!mysqli_real_query($this->_dbHandle, $q))
281 foreach ($query[
'bind'] as $k=>$v)
283 $q =
'SET @'.$k.
'=\''.mysqli_real_escape_string($this->_dbHandle, $v).
'\'';
284 mysqli_real_query($this->_dbHandle, $q);
286 $s =
'EXECUTE STMT USING '.implode(
',', $GLOBALS[
'prepare_execute_stm']);
288 return $this->
query($s);
299 return $this->
query(($query[
'replace']?
'REPLACE':
'INSERT').
' INTO '.$query[
'table'].
' SET '.implode(
',', $this->_getUpdateField($query)));
305 private function _changeType($type)
318 $type.=
' DEFAULT 0';
333 function createTable($table, $struct, $drop_flag=
true, $notexists_flag=
false)
337 $this->
query(
'DROP TABLE IF EXISTS '.$table);
340 foreach ($struct as $name => $type)
342 $key = explode(
' ', $name);
345 $query[] = $name.
' '.$this->_changeType($type);
350 $query[] =
'PRIMARY KEY ('.$type.
')';
353 $query[] =
'KEY '.$key[1].
' ('.$type.
')';
356 $query[] =
'UNIQUE KEY '.$key[1].
' ('.$type.
')';
361 $query_string =
'CREATE TABLE '.($notexists_flag?
'IF NOT EXISTS ':
'').$table.
' ('.implode(
',', $query).
362 ') ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci AVG_ROW_LENGTH=1048576 CHECKSUM=0 DELAY_KEY_WRITE=1 MIN_ROWS=1000;';
363 $query_string = str_replace(
'/*bin*/',
' character set utf8 collate utf8_bin ', $query_string);
364 $this->
query($query_string);
370 foreach ($struct as $name => $type)
372 $key = explode(
' ', $name);
375 $query[] =
'ADD '.$name.
' '.$this->_changeType($type);
380 $query[] =
'ADD PRIMARY KEY ('.$type.
')';
383 $query[] =
'ADD KEY '.$key[1].
' ('.$type.
')';
386 $query[] =
'ADD UNIQUE KEY '.$key[1].
' ('.$type.
')';
389 $query[] =
'ADD FULLTEXT '.$key[1].
' ('.$type.
')';
392 $query[] =
'CHANGE '.$key[1].
' '.$key[2].
' '.$type;
397 return $this->
query(
'ALTER TABLE '.$table.
' '.implode(
',', $query));
407 require_once(
'base/Ego_System.php');
409 $this->
query(
'repair table '.$table);
422 $result_handle = mysqli_query($this->_dbHandle,
'SHOW PROFILE CPU');
424 while ($r = mysqli_fetch_assoc($result_handle))
426 $result.= str_pad($r[
'Status'], 40).
"\t".str_pad($r[
'Duration'], 8,
'0').
"\t".$r[
'CPU_user'].
"\t".$r[
'CPU_system'].
"\n";
445 if (!$table)
return array();
446 $hdl = mysqli_query($this->_dbHandle,
"SHOW COLUMNS FROM ".$table);
448 while($record = mysqli_fetch_assoc($hdl))
451 'field' => $record[
'Field'],
452 'type' => $record[
'Type']
464 return mysqli_get_server_info($this->_dbHandle);
488 $this->
query(
"SHOW TABLES FROM `".$GLOBALS[
'egotec_conf'][
'db'][
'database'].
"` LIKE '$table'");
503 'fields' =>
'cols.column_name,cols2.data_type',
504 'table' =>
'information_schema.table_constraints cons,information_schema.key_column_usage cols,information_schema.columns cols2',
505 'where' =>
'cols.table_name=\''.$table.
'\' '. 506 'AND cols.table_schema=\
''.$GLOBALS[
'egotec_conf'][
'db'][
'database'].
'\''.
507 'AND cons.constraint_type=\'PRIMARY KEY\' '.
508 'AND cols.constraint_name=cons.constraint_name AND cols.table_schema=cons.table_schema AND cols.table_name=cons.table_name '.
509 'AND cols2.table_schema=cons.table_schema AND cols2.table_name=cons.table_name AND cols2.column_name=cols.column_name' 512 while ($db->nextRecord())
514 if (strpos($db->Record[
'data_type'],
'int')!==
false)
520 $primary[$db->Record[
'column_name']] = $type;
531 require_once(
'base/Ego_System.php');
533 $this->
query(
"OPTIMIZE TABLE $table");
542 $this->_currentRow = -1;
543 mysqli_data_seek($this->_queryHandle, 0);
549 $GLOBALS[
'prepare_execute_stm'][] =
'@'.$matches[1];
connect($database='', $host='', $user='', $password='')
static eternalCache($active)
alterTable($table, $struct)
static $_staticMysqliHandle
ego_sql_mysqli_prepare_callback($matches)
createTable($table, $struct, $drop_flag=true, $notexists_flag=false)
query($query, $appendCurrent=false)