25 return $this->cfg->getInstanceCfg;
45 public function query($sql, array $bind=array()) {
49 $stmt->execute($bind);
100 $cols = array_map(array($this,
'quoteIdentifier'), array_keys($prm[
'values']));
101 $vals =
count($cols) > 0? array_fill(0,
count($cols),
'?') : array();
103 $sql =
'INSERT INTO '.$this->quoteIdentifier($prm[
'table']);
104 $sql.=
' ('.implode(
',',$cols).
') VALUES ('.implode(
',',$vals).
')';
106 $stmt = $this->
query($sql, array_values($prm[
'values']));
109 throw new nException(
'db_abstract - insert: The table or the values is missing.');
126 $cols = array_map(array($this,
'quoteIdentifier'), array_keys($prm[
'values']));
127 $vals = array_fill(0,
count($cols),
'?');
129 $sql =
'REPLACE INTO '.$this->quoteIdentifier($prm[
'table']);
130 $sql.=
' ('.implode(
',',$cols).
') VALUES ('.implode(
',',$vals).
')';
132 $stmt = $this->
query($sql, array_values($prm[
'values']));
135 throw new nException(
'db_abstract - replace: The table or the values is missing.');
158 foreach($prm[
'values'] as $col=>$val)
161 $sql =
'UPDATE '.$this->quoteIdentifier($prm[
'table']);
162 $sql.=
' SET '.implode(
',',$set);
163 $sql.= $this->
makeWhere($prm[
'where'], $prm[
'whereOp']);
164 $stmt = $this->
query($sql, array_values($prm[
'values']));
165 return $stmt->rowCount();
167 throw new nException(
'db_abstract - update : The table or the values is missing.');
180 public function delete(array $prm) {
188 $sql =
'DELETE FROM '.$this->quoteIdentifier($prm[
'table'])
189 .$this->makeWhere($prm[
'where'], $prm[
'whereOp']);
191 $stmt = $this->
query($sql);
192 $nb = $stmt->rowCount();
199 throw new nException(
'db_abstract - delete : The table is missing.');
210 public function makeWhere($where, $whereOp=
'AND', $incWhere=
true) {
212 if (!empty($where)) {
214 $query = $where->toString();
215 else if (is_array($where)) {
217 $where = array_filter($where);
220 foreach($where as $k=>$v) {
223 $query =
'('.implode(
' '.$whereOp.
' ', $tmp).
')';
226 $query = ($incWhere && $query?
' WHERE ' : null).$query;
237 return $this->cfg->fetchMode;
246 $this->cfg->fetchMode = (int) $mode;
293 $table =
db::get(
'table', $prm[
'table']);
295 if (is_array($prm[
'fields'])) {
296 $f = implode(
',', array_map(array($this,
'quoteIdentifier'), $prm[
'fields']));
298 if (strpos($prm[
'fields'], $this->cfg->quoteIdentifier) ===
false) {
299 $f = implode(
',', array_map(array($this,
'quoteIdentifier'), explode(
',', $prm[
'fields'])));
304 if (!empty($prm[
'i18nFields'])) {
307 $primary = $i18nTable->getPrimary();
308 $prm[
'join'][] = array(
309 'table'=>$i18nTable->getRawName(),
310 'on'=>$tableName.
'.'.$table->getIdent().
'='.$i18nTableName.
'.'.$primary[0].
311 ' AND '.$i18nTableName.
'.'.$primary[1].
'="'.
request::get(
'lang').
'"' 313 if (is_array($prm[
'i18nFields'])) {
314 array_walk($prm[
'i18nFields'], array($this,
'quoteIdentifier'));
315 $f.=
','.$i18nTableName.
'.'.implode(
','.$i18nTableName.
'.', $prm[
'fields']);
316 }
else if ($prm[
'i18nFields']) {
317 foreach(explode(
',', $prm[
'i18nFields']) as $t) {
318 $f.=
','.$i18nTableName.
'.'.$t;
323 $query =
'SELECT '.$f.
' FROM '.$tableName;
325 if ($prm[
'moreTables']) {
326 if (is_array($prm[
'moreTables'])) {
327 foreach($prm[
'moreTables'] as $k=>$v) {
328 if (is_numeric($k)) {
329 $query.=
', '.$this->quoteIdentifier($v);
331 $query.=
', '.$this->quoteIdentifier($v).
' '.$this->
quoteIdentifier($k);
335 $query.=
', '.$prm[
'moreTables'];
340 if (is_array($prm[
'join'])) {
342 foreach($prm[
'join'] as &$v) {
343 $v = array_merge(array(
'dir'=>
'left',
'on'=>1,
'alias'=>
''), $v);
345 if (!empty($v[
'alias'])) {
346 $alias =
' AS '.$this->quoteIdentifier($v[
'alias']);
347 if ($v[
'table'] != $table->getRawName())
348 $tblAlias[$v[
'table']] = $v[
'alias'];
350 $join[] = strtoupper($v[
'dir']).
' JOIN '.$this->
quoteIdentifier($v[
'table']).$alias.
' ON '.$v[
'on'];
352 $query.=
' '.implode(
' ', $join).
' ';
355 $query.= $this->
makeWhere($prm[
'where'], $prm[
'whereOp']);
357 if (!empty($prm[
'group']))
358 $query.=
' GROUP BY '.$prm[
'group'];
360 if (!empty($prm[
'having']))
361 $query.=
' HAVING '.$prm[
'having'];
363 if (!empty($prm[
'order']))
364 $query.=
' ORDER BY '.$prm[
'order'];
366 if (!empty($prm[
'nb'])) {
367 if (empty($prm[
'start']))
369 $query.=
' LIMIT '.$prm[
'start'].
','.$prm[
'nb'];
372 if ($prm[
'bindData'] && !empty($prm[
'bind']) && is_array($prm[
'bind'])) {
373 $tmp = explode(
'?', $query,
count($prm[
'bind'])+1);
374 array_splice($prm[
'bind'],
count($tmp));
377 while($tmp2 = array_shift($tmp)) {
378 $query.= $tmp2.array_shift($prm[
'bind']);
382 if ($prm[
'groupAfter'])
383 $query =
'SELECT * FROM ('.$query.
') AS res GROUP BY '.$prm[
'groupAfter'];
387 throw new nException(
'db_abstract - selectQuery : The table is missing.');
400 foreach($tblAlias as $tbl=>$alias) {
401 $search = array_merge($search, array(
403 ' '.$this->quoteIdentifier($tbl).
'.',
404 '('.$this->quoteIdentifier($tbl).
'.' 406 $replace = array_merge($replace, array(
408 ' '.$this->quoteIdentifier($alias).
'.',
409 '('.$this->quoteIdentifier($alias).
'.' 412 return str_replace($search, $replace, $query);
423 if (is_array($prm)) {
429 $fetchMode = $prm[
'forceFetchMode'] ? $prm[
'forceFetchMode'] : $this->cfg->fetchMode;
431 $stmt = $this->
query($prm);
432 $fetchMode = $this->cfg->fetchMode;
435 if ($fetchMode == PDO::FETCH_CLASS) {
436 $tmp = $stmt->fetchAll(PDO::FETCH_ASSOC);
441 'table'=>$prm[
'table'],
442 'props'=>array_keys($tmp[0])
444 foreach($tmp as $t) {
451 return $stmt->fetchAll($fetchMode);
463 $subQuery = $this->
selectQuery(array_merge($prm, array(
'bindData'=>
true)));
464 $stmt = $this->
query(
'SELECT COUNT(*) AS count FROM ('.$subQuery.
') AS subquerycount');
465 $tmp = $stmt->fetch(PDO::FETCH_ASSOC);
466 $count = $tmp[
'count'];
468 $stmt->closeCursor();
488 $prm[
'forceFetchMode'] = PDO::FETCH_ASSOC;
489 return $this->
select($prm);
503 $prm[
'forceFetchMode'] = PDO::FETCH_NUM;
504 return $this->
select($prm);
518 return array_pop($this->
select($prm));
528 if (strpos($ident,
'(') !==
false)
530 $tmpSpace = explode(
' ', $ident);
531 $tmp = explode(
'.', $tmpSpace[0]);
532 if (
count($tmp) == 1 && $tmp[0] ==
'*')
534 else if (
count($tmp) == 2 && $tmp[1] ==
'*')
535 return $this->cfg->quoteIdentifier.$tmp[0].$this->cfg->quoteIdentifier.
'.*';
536 $tmpSpace[0] = $this->cfg->quoteIdentifier
537 .implode($this->cfg->quoteIdentifier.
'.'.$this->cfg->quoteIdentifier, $tmp)
538 .$this->cfg->quoteIdentifier;
539 return implode(
' ', $tmpSpace);
549 return $this->cfg->quoteValue.addcslashes($value, $this->cfg->quoteValue).$this->cfg->quoteValue;
558 $this->
query(
'OPTIMIZE TABLE '.$table);
571 $tmp = array_fill(0, 3,
'');
573 if (array_key_exists(
'start', $prm))
574 $tmp[0] = $prm[
'start'];
576 if (array_key_exists(
'contains', $prm))
577 $tmp[1] = $prm[
'contains'];
579 if (array_key_exists(
'end', $prm))
580 $tmp[2]= $prm[
'end'];
582 $regex =
'/^'.implode(
'(.*)', $tmp).
'$/';
584 return array_merge(array_filter($this->
getTables(),
585 create_function(
'$val',
'return preg_match("'.$regex.
'", $val);')));
596 'end'=>$table.db::getCfg(
'i18n')
598 if (
count($tmp) == 1)
643 abstract public function getTables($unPrefix =
true);
659 abstract public function fields($table);
664 abstract protected function _connect();
679 abstract public function prepare($sql);
696 abstract protected function _commit();
getWhere(array $prm=array())
static getInstance(array $cfg=array())
getTables($unPrefix=true)
query($sql, array $bind=array())
static initTab(array &$vars, array $init)
getTablesWith(array $prm)
makeWhere($where, $whereOp='AND', $incWhere=true)
static log($sql=null, $bind=null)
static get($type, $table, array $prm=array())
tableAlias($query, array $tblAlias)
static get($className, array $cfg=array())