[ -z "$DARWIN" ] && return 0
[ -x /opt/local/bin/mysqld_safe5 ] && alias mac_mysqlstart='sudo /opt/local/bin/mysqld_safe5 &'
-[ -x /opt/local/bin/mysqladmin5 ] && alias mac_mysqlstop='sudo /opt/local/bin/mysqladmin5 -p shutdown'
+[ -x /opt/local/bin/mysqladmin5 ] && alias mac_mysqlstop='sudo /opt/local/bin/mysqladmin5 -u root -p shutdown'
[ -x /Applications/iTunes.app/Contents/MacOS/iTunes ] && alias iTunes=/Applications/iTunes.app/Contents/MacOS/iTunes
[ -x /Applications/VLC.app/Contents/MacOS/VLC ] && alias vlc=/Applications/VLC.app/Contents/MacOS/VLC
class Db extends nb {
# PDO Connection
+ public static $encoding = 'utf-8';
public $conn;
public $pdo;
# NB 07.04.16 public $pdo_error = array( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); # See: http://php.net/manual/en/pdo.error-handling.php
if (empty($this->conn)) return false;
# Type queries
- if ($sql = $this->type('post_connect')) {
+ if ($sql = $this->type('exec')) {
if (is_scalar($sql)) $sql = array($sql);
foreach ($sql as $s) {
$this->conn->exec($s);
}
function quote($v) {
+ static $fct = false; if ($fct === false) $fct = $this->type('quote');
+ if ($fct) return $fct($v);
return $this->conn->quote($v);
}
return $return;
}
- public function type($key=null,$die=false) {
+ public function type($key=null,$die=false,$type=null) {
global $DB_TYPES;
+ if (!$type) $type = $this->type;
static $require = array();
- if (empty($require[$this->type])) {
- #if (empty($this->type)) return;
- if (empty($this->type)) self::bye("Type is required");
- require_once(dirname(__FILE__).'/db/types/'.$this->type.'.php');
- $require[$this->type] = 1;
+ if (empty($require[$type])) {
+ #if (empty($type)) return;
+ if (empty($type)) self::bye("Type is required");
+ require_once(dirname(__FILE__).'/db/types/'.$type.'.php');
+ $require[$type] = 1;
}
- if ($key === null) return $DB_TYPES[$this->type];
+ if ($key === null) return $DB_TYPES[$type];
- if (empty($DB_TYPES[$this->type][$key])) {
- if ($die) self::bye("db.type(): Unknow key `$key` for type `".$this->type."`");#.' - '.print_r($DB_TYPES,true));
+ if (empty($DB_TYPES[$type][$key])) {
+ if ($die) self::bye("db.type(): Unknow key `$key` for type `".$type."`");#.' - '.print_r($DB_TYPES,true));
return;
}
- return $DB_TYPES[$this->type][$key];
+ return $DB_TYPES[$type][$key];
}
public function tables() {
}
public function dump() {
- $this->sql(true);
+ return $this->sql(true);
}
public function pdo_info() {
- return preg_replace_callback('/(\w+)=[^;]*;?/',function($m){
+ return preg_replace_callback('/(\w+)=([^;]*)(;?)/',function($m){
return preg_match('/^(host|dbname|name)/',$m[1]) ? $m[0] : '';
},$this->pdo);
}
if ($insert === null) $insert = self::p('insert');
$this->pset('orderby',null);
$this->pset('format','sql');
- $r = array();
- $tables = array();
- foreach (explode(',',self::p('table')) as $t) {
- $tables[$t] = 1;
+ # Tables param filter
+ $tables = $this->tables();
+ if (self::p('table')) {
+ $new = array();
+ foreach (explode(',',self::p('table')) as $t) {
+ $new[$t] = $tables[$t];
+ }
+ $tables = $new;
}
#if (NB_EOL == "\n")
+ # Change db type
+ foreach ($tables as $t) { $t->fields(); }
+ if (self::p('db.type')) {
+ $this->type = self::p('db.type');
+ }
+
echo ''
#."-- Database : ".$this->name."\n"
."-- Date : ".strftime('%F %T')."\n"
#."-- Host : ".$this->host."\n"
;
-# NB 03.04.16 if ($insert) {
-# NB 03.04.16 if ($sql = $this->type('post_connect')) {
-# NB 03.04.16 if (is_scalar($sql)) $sql = array($sql);
-# NB 03.04.16 foreach ($sql as $s) {
-# NB 03.04.16 echo "$sql;\n";
-# NB 03.04.16 }
-# NB 03.04.16 }
-# NB 03.04.16 }
-
- foreach ($this->tables() as $t) {
- if ($tables and empty($tables[$t->name])) continue;
+ if ($sql = $this->type('exec')) {
+ foreach ((is_array($sql) ? $sql : array($sql)) as $sql) {
+ echo rtrim($sql,';').";\n";
+ }
+ }
+
+ foreach ($tables as $t) {
+ if (!empty($tables) and empty($tables[$t->name])) continue;
if ($insert) {
echo "\n-- Table: ".$t->name."\n";
- #echo 'DROP TABLE IF EXISTS '.$t->sql_name().';'.NB_EOL;
- echo 'DROP '.strtoupper($t->type).' IF EXISTS '.$t->sql_name().';'.NB_EOL;
}
- echo rtrim($t->sql(),';').';'.NB_EOL;
+ #echo 'DROP TABLE IF EXISTS '.$t->sql_name().';'.NB_EOL;
+ echo 'DROP '.strtoupper($t->type).' IF EXISTS '.$t->sql_name().';'.NB_EOL;
+ echo rtrim($t->create(),';').';'.NB_EOL;
if ($insert and $t->type == 'table') $t->rows();
#break;
}
- return $r;
+
+ return true;
}
public function status() {
class field {
private $table;
public $name;
- #public $type = 'text';
public $type;
public $null = true;
- public $key = false;
- #public $value;
+ public $key = 0;
+ public $uniq = 0;
public $default;
public $autoincrement;
public $extra;
'type',
'null',
'key',
+ 'uniq',
'default',
'autoincrement',
'extra',
return preg_match('/signed|(snall )?int|float|decimal|numeric|number|currency/i',$this->type) ? true : false;
}
+ public function unsigned() {
+ return ( stripos($this->type,'unsigned') !== false ? true : false );
+ }
+
public function html_type() {
# See: http://www.w3schools.com/html/html_form_input_types.asp
if (strpos($this->name,'password') !== false) return 'password';
}
public function size() {
+
+ # Enum type
+ if (preg_match("/enum\('(.*?)'\)/i",$this->type,$m)) {
+ $m = explode("\t",preg_replace("/' *, *'/","\t",$m[1]));
+ $m = array_map(function($v){return strlen($v);},$m);
+ rsort($m,SORT_NUMERIC);
+ return $m[0];
+ }
+
if (! preg_match('/\((\d+)(?:,(\d+)\))?/',$this->type,$m) ) return null;
array_shift($m);
#debug($m);
if ($value === null) return 'NULL';
if ($force_quote or !$this->numeric()) {
- if (($db = $this->db()) === null) return "'".preg_replace("/'/","\\'",$value)."'";
+ if (!($db = $this->db())) return "'".preg_replace("/'/","\\'",$value)."'";
return $db->quote($value);
} else {
return $db;
}
+ /*
+ * Function create
+ *
+ * return the sql to create the table build internaly
+ *
+ */
+ public function create() {
+
+ // Type specific
+ global $_create_fct, $_create_fct_type;
+ $_create_fct_type = $this->db()->type('create',false,self::p('db.type'));
+
+ $_create_fct = function(&$field) {
+ global $_create_fct_type; if ($_create_fct_type and ($sql=$_create_fct_type($field))) return $sql;
+
+ return $field->sql_name().' '.$field->type
+ .($field->null ? ' NULL' : ' NOT NULL')
+ .($field->default !== null ? ' DEFAULT '.$field->quote($field->default,true) : '')
+ .($field->key ? ' PRIMARY KEY' : '')
+ .($field->uniq ? ' UNIQUE' : '')
+ ;
+ };
+
+ $sql = 'CREATE TABLE '.$this->sql_name()
+ .' ('
+ .join(",",array_map(function($f){global $_create_fct;return $_create_fct($f);},array_values($this->fields())))
+ # done at the end for primary keys .')'
+ ;
+
+ // Handle multiple primary keys syntaxe
+ if (substr_count(strtoupper($sql),'PRIMARY KEY')>1) {
+ $sql = str_ireplace(' PRIMARY KEY','',$sql)
+ .', PRIMARY KEY ('.join(',',array_map(function($f){return $f->sql_name();},$this->fields_keys())).')'
+ ;
+ }
+
+ unset($_create_fct,$_create_fct_type);
+ return $sql.')';
+ }
+
/*
* Function sql
*
$field = array(
'name' => $row['name'],
'type' => strtolower($row['type']),
- 'default' => (isset($row['default']) ? $row['default'] : null),
'key' => (preg_match('/^(f.*|no|0)?\s*$/i',$row['key']) ? 0 : 1),
'null' => (preg_match('/^(f.*|no|0)?\s*$/i',$row['null']) ? 0 : 1),
'extra' => null, # !!! nothing todo with array $extra, this info from the sql server
'autoincrement' => (isset($row['autoincrement']) ? $row['autoincrement'] : 0),
);
+ if (isset($row['uniq'])) $field['uniq'] = $row['uniq'];
+ if (isset($row['default'])) $field['default'] = $row['default'];
$this->fields[$field['name']] = new Field($field);
+ $this->fields[$field['name']]->size = $this->fields[$field['name']]->size();
}
- if (empty($this->fields)) bye("Table `".$this->name."` does not exists into database ".$this->db()->name."!");
+ if (empty($this->fields)) bye("Table `".$this->name."` does not exists");
} # < $this->fields
foreach ($row as $k=>$v) {
if (!isset($this->extras[$k])) $row[$k] = out::format($v);
}
+
+ /*
+ */
+ } elseif(0) {
+ //iconv(mb_detect_encoding($text, mb_detect_order(), true), "UTF-8", $text);
+ foreach ($row as $k=>$v) {
+ #if (!isset($this->extras[$k])) $row[$k] = mb_convert_encoding($v, "UTF-8");
+ #if (!isset($this->extras[$k])) $row[$k] = preg_replace('/[\r\f]/','',$v);
+ }
+
}
if ($extras and !empty($this->row_parse_post)) {
'quote_name' => '`',
#'select_count' => array('SQL_CALC_FOUND_ROWS','SELECT FOUND_ROWS()'),
-'post_connect' => 'SET NAMES utf8',
+'exec' => 'SET NAMES '.str_replace('utf-8','utf8',strtolower(Db::$encoding)),
'extra_where' => 'having',
'localFile' => array (getenv('HOME').'/.my.cnf','^(?:user(?:name)?=(?P<user>\\S+)|password=(?P<password>\\S+))'),
'fct' => create_function('&$r',join('',array(
'$r["autoincrement"] = $r["extra"] == "auto_increment" ? 1 : 0;',
'$r["name"] = $r["field"];',
+ #'debug($r["key"]);',
+ '$r["uniq"] = $r["key"] == "UNI" ? 1 : 0;',
+ '$r["key"] = $r["key"] == "PRI" ? 1 : 0;',
))),
),
<?php
$DB_TYPES['sqlite'] = array (
+'exec' => 'PRAGMA encoding = "'.strtoupper(Db::$encoding).'"',
'use_path' => true,
'extra_where' => 'denorm',
'quote_name' => '`',
+'quote' => function(&$v) { return "'".str_replace("'","''",$v)."'"; },
'table.sql' => 'SELECT sql FROM sqlite_master WHERE name=\'<NAME>\'',
))),
),
+'create' => function(&$field) {
+ if ($field->autoincrement) return $field->sql_name().' INTEGER PRIMARY KEY AUTOINCREMENT';
+ $field->size = $field->size();
+ $r = array(
+ '/^(.*?) (unsigned)( .*?)?$/i' => '\2 \1\3',
+ '/enum\(.*?\)/i' => 'VARCHAR'.($field->size ? "($field->size)" : '')
+ );
+ $field->type = preg_replace(array_keys($r),array_values($r),$field->type);
+},
+
);?>
else self::bye("Unknow action: `$action`");
#else $rows = $class->$name;
if (!$rows) return false;
+ if ($rows === true) return true;
#bye($rows);
if (is_scalar($rows)) {
}
#echo join($o['sep'],array_values($row));
- echo str_replace("\n",'|',join($o['sep'],$values));
+ echo str_replace("\n",'\\n',join($o['sep'],$values));
}
function out_csv_head(&$row,$o) {