return $this->unvar($db_type[$key]);
}
- public function type($key=null,$die=false,$type=null) {
- if (empty($type)) $type = $this->type;
- if (!empty($key) and $key == 'type') return $type;
-
- return $this->conf_type($key,$die);
- }
+# NB 12.12.17 public function type($key=null,$die=false,$type=null) {
+# NB 12.12.17 if (empty($type)) $type = $this->type;
+# NB 12.12.17 if (!empty($key) and $key == 'type') return $type;
+# NB 12.12.17
+# NB 12.12.17 return $this->conf_type($key,$die);
+# NB 12.12.17 }
public function table_exist($table) {
# NB 02.11.16: TODO NEVEW USED
return $this->db()->sql_name($value === null ? $this->name : $value);
}
+ private function _post2sql($post) {
+ $keys = [];
+ $keys_values = [];
+ $fields = [];
+ $fields_values = [];
+ foreach ($this->fields() as $name => $field) {
+
+ if ($field->extras) continue;
+
+ $value = $post[$this->field_preff.$name];
+
+ if ($field->key) {
+ $keys[] = $name;
+
+ if (!isset($post[$this->key_preff.$name])) {
+ if ($field->key) $this->bye("Missing `$name`!");
+ continue;
+ }
+
+ if (isset($post[$this->key_preff.$name])) {
+ $keys_values[] = $post[$this->key_preff.$name];
+
+ } else {
+ $keys_values[] = $value;
+ }
+
+ } else {
+
+ $fields[] = $name;
+ $fields_values[] = $value;
+
+ }
+ }
+
+ return [
+ 'keys' => $keys,
+ 'keys_values' => $keys_values,
+ 'fields' => $fields,
+ 'fields_values' => $fields_values,
+ 'all_keys' => array_combine($keys,$keys_values),
+ 'all_fields' => array_combine($fields,$fields_values),
+ ];
+ }
+
public function replace($hvalues,&$info=[]) {
+ # NB 12.12.17: When REPLACE is not supported where call a personalized function
+ $fct = $this->db()->conf_type('replace_insert');
+ if (!empty($fct)) {
+ $info['sql_fct'] = $fct;
+ return $this->insert($hvalues,$info);
+ }
return $this->insert($hvalues,$info,'REPLACE');
}
$insert_word.' INTO '. $this->sql_name() . ' (' . join(',',array_values($sql_names)).')'
.' VALUES (' . join(',',ar_map('":$a"',array_keys($fields))) . ')'
;
+
+ if (!empty($info['sql_fct'])) {
+ $fct = $info['sql_fct'];
+ $fct($sql,$this);
+ }
$info['sql'] = $sql;
if (!($query = $this->db()->conn->prepare($sql))) {
}
# NB 07.12.17: Add LIMIT 1. If a table as not primary key when want to delete only on record
- $sql = 'DELETE FROM ' . $this->sql_name() . $where . ($this->db()->type == 'pgsql' ? '' : ' LIMIT 1');
+# NB 12.12.17 $sql = 'DELETE FROM ' . $this->sql_name() . $where . ($this->db()->type == 'pgsql' ? '' : ' LIMIT 1');
+ $sql = 'DELETE FROM ' . $this->sql_name() . $where . ($this->db()->conf_type('delete_no_limit') ? '' : ' LIMIT 1');
$info['sql'] = $sql;
if (self::p('debug')) {
<?php
$DB_TYPES['pgsql'] = array (
+'delete_no_limit' => true,
+'replace_insert' => function(&$sql,&$table) {
+ $keys = $table->fields_keys($others);
+ #if ($table->p('debug')) {
+ $sql .= ' ON CONFLICT ('.join(',',array_keys($keys)).') DO UPDATE ';
+ $sql .= ' SET ' . join(',',$table->ar_map('"$a=:$a"',array_keys($others)));
+ #$info['debug'] = [$keys,$others];
+},
'extra_where' => 'denorm',
'regexp' => '~',
# NB 04.12.17: Overwrite db->quote because of bugs with INSERT INTO
#header('Location: '.$this->table->base.'/');
$this->page($info);
+ } elseif ($action == 'replace' and $this->perm >= self::WRITE) {
+ if (!$this->table->replace($_POST,$info)) $this->error('replace: '.print_r($info,true));
+ header('Location: '.$this->table->base.'/');
+ $this->page($info);
+
} elseif ($action == 'rm' and $this->perm >= self::DELETE) {
if (!$this->table->delete($_POST,$info)) $this->error('rm: '.print_r($info,true));
header('Location: '.$this->table->base.'/');