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']?:
'*').(isset($query[
'fields2'])&&!empty($query[
'fields2'])?
','.(is_array($query[
'fields2'])?implode(
',',$query[
'fields2']):$query[
'fields2']):
'')
123 .
' FROM ('.($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'][] =
'('.$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)
307 $type = str_replace(
'/*bin*/',
'', $type, $count);
320 $type .=
' DEFAULT 0';
323 $type .=
' character set utf8 collate utf8_bin';
336 function createTable($table, $struct, $drop_flag=
true, $notexists_flag=
false)
340 $this->
query(
'DROP TABLE IF EXISTS '.$table);
343 foreach ($struct as $name => $type)
345 $key = explode(
' ', $name);
346 $type = $this->_changeType($type);
349 $query[] = $name.
' '.$type.
' NOT NULL';
354 $query[] =
'PRIMARY KEY ('.$type.
')';
357 $query[] =
'KEY '.$key[1].
' ('.$type.
')';
360 $query[] =
'UNIQUE KEY '.$key[1].
' ('.$type.
')';
364 $this->
query(
'CREATE TABLE '.($notexists_flag?
'IF NOT EXISTS ':
'').$table.
' ('.implode(
',', $query).
365 ') ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci AVG_ROW_LENGTH=1048576 CHECKSUM=0 DELAY_KEY_WRITE=1 MIN_ROWS=1000;');
371 foreach ($struct as $name => $type)
373 $key = explode(
' ', $name);
374 $type = $this->_changeType($type);
377 $query[] =
'ADD '.$name.
' '.$type.
' NOT NULL';
382 $query[] =
'ADD PRIMARY KEY ('.$type.
')';
385 $query[] =
'ADD KEY '.$key[1].
' ('.$type.
')';
388 $query[] =
'ADD UNIQUE KEY '.$key[1].
' ('.$type.
')';
391 $query[] =
'ADD FULLTEXT '.$key[1].
' ('.$type.
')';
394 $query[] =
'CHANGE '.$key[1].
' '.$key[2].
' '.$type;
398 return $this->
query(
'ALTER TABLE '.$table.
' '.implode(
',', $query));
408 require_once(
'base/Ego_System.php');
410 $this->
query(
'repair table '.$table);
423 $result_handle = mysqli_query($this->_dbHandle,
'SHOW PROFILE CPU');
425 while ($r = mysqli_fetch_assoc($result_handle))
427 $result.= str_pad($r[
'Status'], 40).
"\t".str_pad($r[
'Duration'], 8,
'0').
"\t".$r[
'CPU_user'].
"\t".$r[
'CPU_system'].
"\n";
446 if (!$table)
return array();
447 $hdl = mysqli_query($this->_dbHandle,
"SHOW COLUMNS FROM ".$table);
449 while($record = mysqli_fetch_assoc($hdl))
452 'field' => $record[
'Field'],
453 'type' => $record[
'Type']
465 return mysqli_get_server_info($this->_dbHandle);
489 $this->
query(
"SHOW TABLES FROM `".$GLOBALS[
'egotec_conf'][
'db'][
'database'].
"` LIKE '$table'");
504 'fields' =>
'cols.column_name,cols2.data_type',
505 'table' =>
'information_schema.table_constraints cons,information_schema.key_column_usage cols,information_schema.columns cols2',
506 'where' =>
'cols.table_name=\''.$table.
'\' '.
507 'AND cols.table_schema=\
''.$GLOBALS[
'egotec_conf'][
'db'][
'database'].
'\''.
508 'AND cons.constraint_type=\'PRIMARY KEY\' '.
509 'AND cols.constraint_name=cons.constraint_name AND cols.table_schema=cons.table_schema AND cols.table_name=cons.table_name '.
510 'AND cols2.table_schema=cons.table_schema AND cols2.table_name=cons.table_name AND cols2.column_name=cols.column_name'
513 while ($db->nextRecord())
515 if (strpos($db->Record[
'data_type'],
'int')!==
false)
521 $primary[$db->Record[
'column_name']] = $type;
532 require_once(
'base/Ego_System.php');
534 $this->
query(
"OPTIMIZE TABLE $table");
543 $this->_currentRow = -1;
544 mysqli_data_seek($this->_queryHandle, 0);
550 $GLOBALS[
'prepare_execute_stm'][] =
'@'.$matches[1];
ego_sql_mysqli_prepare_callback($matches)
alterTable($table, $struct)
createTable($table, $struct, $drop_flag=true, $notexists_flag=false)
query($query, $appendCurrent=false)
connect($database='', $host='', $user='', $password='')
static $_staticMysqliHandle
static eternalCache($active, $clear=true)