}
- public function dump() {
+ public function dump($db_type='') {
return $this->sql(true);
}
- public function sql($insert=null) {
+ public function sql($insert=null,$db_type='') {
if ($insert === null) $insert = self::p('insert');
+ if (empty($db_type)) $db_type = self::p('db_type');
# Params
$this->pset('orderby',null);
echo ''
#."-- Database : ".$this->name."\n"
."-- Date : ".strftime('%F %T')."\n"
- ."-- Pdo : ".$this->pdo_info()."\n"
+# NB 07.01.18 ."-- Pdo : ".$this->pdo_info()."\n"
;
# Change db type if needed
$type_from = $type_to = '';
- if (self::p('db_type')) {
- echo "-- Type : ".self::p('db_type')."\n";
+ if ($db_type) {
+ echo "-- Type orig: ".$this->type."\n";
+ echo "-- Type : ".$db_type."\n";
$type_from = $this->type;
- $type_to = self::p('db_type');
+ $type_to = $db_type;
$this->type = $type_to;
+ } else {
+ echo "-- Type : ".$this->type."\n";
}
# Specific function
#bye($this->db()->type);
if ($type_from == 'mysql') $views = []; # Mysql store sql create view in mysql format wich only works with mysql
+ echo "\n-- SQL\n";
foreach (array_merge($tables,$views) as $t) {
# DROP / CREATE
public function __construct($attr=[]) {
if (is_scalar($attr)) $attr = ['name'=>$attr];
foreach ($attr as $k => $v) { $this->$k = $v; }
+ }
+
+ public static function default2str($default) {
if ($this->default == "''") $this->default = '';
-# NB 12.12.17 if (strtoupper($this->default) == "NULL") $this->default = null;
- #if (strtoupper($this->default) == "NOW()") $this->default = null;
- if (strtoupper($this->default) == "NOW()") $this->default = $this->date_microtime();
- if (strtoupper($this->default) == "CURRENT_TIMESTAMP)") $this->default = $this->date_microtime();
- if (strtoupper($this->default) == "NOW()") $this->default = date("Y-m-d H:i:s.u");
- if (strtoupper($this->default) == "CURRENT_TIMESTAMP)") $this->default = date("Y-m-d H:i:s.u");
- if (strtoupper($this->default) == "CURRENT_DATE)") $this->default = date("Y-m-d");
+# NB 12.12.17 if (strtoupper($default) == "NULL") $default = null;
+ #if (strtoupper($default) == "NOW()") $default = null;
+# NB 08.01.18 if (strtoupper($default) == "NOW()") $default = $date_microtime();
+# NB 08.01.18 if (strtoupper($default) == "CURRENT_TIMESTAMP") $default = $date_microtime();
+ if (strtoupper($default) == "NOW()") $default = date("Y-m-d H:i:s.u");
+ if (strtoupper($default) == "CURRENT_TIMESTAMP") $default = date("Y-m-d H:i:s.u");
+ if (strtoupper($default) == "CURRENT_DATE") $default = date("Y-m-d");
+ if (preg_match('/^(null|.*\(.*)?$/',strtolower($default))) $default = '';
+ return $default;
}
public function autoincrement() {
* return the sql to create the table build internaly
*
*/
- public function create($from_sql=true) {
+ public function create($from_engine=true) {
// String replace function
- $sql_replace_fct = $this->db()->conf_type('table.sql.create',false);
+ $sql_replace_fct = $this->db()->conf_type('table.sql.create');
$db_sql_replace_fct = empty($this->db()->sql_replace) ? '' : $this->db()->sql_replace;
$sql_replace = function($sql) use ($sql_replace_fct,$db_sql_replace_fct) {
if ($db_sql_replace_fct) $sql = $db_sql_replace_fct($sql);
return $sql_replace_fct ? $sql_replace_fct($sql,$this) : $sql;
};
- if ($from_sql) return $sql_replace($this->sql());
+ if ($from_engine) return $sql_replace($this->sql());
if ($this->type() == 'view') {
return $sql_replace('CREATE VIEW '.$this->sql_name().' AS '.preg_replace('/^CREATE\s+.*?\s+AS\s+.*?SELECT/i','SELECT',$this->sql()));
foreach ($this->fields() as $name => $field) {
- if ($add and !preg_match('/^(null)?$/',strtolower($field->default))) {
- $row[$name] = $field->default;
+# NB 08.01.18 if ($add and !preg_match('/^(null|.*\(.*)?$/',strtolower($field->default))) {
+# NB 08.01.18 $row[$name] = $field->default;
+ if ($add) {
+ $row[$name] = $field->default2str($field->default);
} elseif(!isset($row[$name])) {
$row[$name] = '';
if (!empty($opt['buttons'])) $buttons = $opt['buttons'];
$r = '<form class="menu" method="get" action="?">'.NB_EOL;
+ #$r = '<form class="menu" method="get" action="'.preg_replace('/\?.*$/','',$_SERVER["REQUEST_URI"]).'">'.NB_EOL;
+
# See: http://html5doctor.com/html5-forms-input-types/
#$r .= '<input id="skill" type="range" min="1" max="100" value="0" />';
#$r .= '<input id="startdate" name="startdate" min="2012-01-01" max="2013-01-01" type="date" />';
$sql = str_replace('\\n',"\n",$sql);
return $sql;
},
+
'connect' => function($Db) {
if (!empty($Db->attach)) {
}
},
+
'disconnect' => function($Db) {
foreach ($Db->attach as $name => $file) {
$Db->conn->exec("DETACH DATABASE $name");
}
},
+
+'table.sql.create' => function($sql) {
+ $r = [
+ '/::\w+(\b|\s)/' => '$1', # pgsql cast;
+ '/"([\w_]+)"\(/' => '$1(', # pgsql function quoted;
+ "/'([\w_]+\(.*?\))'/" => '$1', # pgsql function quoted;
+ "/now\(\)/i" => 'CURRENT_TIMESTAMP', # pgsql function quoted;
+ ];
+ $sql= preg_replace(array_keys($r),array_values($r),$sql);
+ return $sql;
+},
);?>
$this->table->html_edit($values,
# NB 14.12.17 $this->table->base . '/' . ($add ? 'insert' : 'update' . '/' . urlencode($this->params['args'])) . '/'
- $this->table->base . '/' . ($add ? 'insert' : 'update') . '/'
+ $this->table->base . '/' . ($add ? 'insert' : 'update') # NB 07.01.18 . '/'
,$add);
}
} elseif ($action == 'databases') {
$this->page($this->db,'databases');
- } elseif ($action == 'dump') {
+ } elseif ($action == 'csv') {
header('Content-type: text/plain');
- $this->db->dump();
+ $this->db->dump2csv();
exit;
- #echo serialize($this->db->tables());
- $this->page(array_values($this->db()->conf));
- } elseif ($action == 'csv') {
+ } elseif ($action == 'dump') {
header('Content-type: text/plain');
- $this->db->dump2csv();
+ $this->db->dump($this->params['action']);
exit;
} elseif ($action == 'schema') {
header('Content-type: text/plain');
- $this->db->sql();
+ $this->db->sql(null,$this->params['action']);
exit;
}
form.querySelectorAll("form.menu select.tables").forEach(function(e) {
e.removeAttribute('name');
e.onchange = function() {
- this.form.setAttribute('action',window._dbq['db.base']+'/'+this.value+'.html');
+ this.form.setAttribute('action',window._dbq['db.base']+'/'+this.value
+ + ( window._dbq['param.format'] ? '.'+window._dbq['param.format'] : '')
+ );
return this.form.onsubmit();
}
});