From: Nicolas Boisselier Date: Wed, 29 Jun 2016 09:33:28 +0000 (+0100) Subject: table.create X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=1fec6d58d1df10de664b8f78b648399742743589;p=nb.git table.create --- diff --git a/lib/php/db/table.php b/lib/php/db/table.php index e0405c2b..e4abc4fc 100644 --- a/lib/php/db/table.php +++ b/lib/php/db/table.php @@ -121,15 +121,24 @@ Class Table extends nb { */ public function create() { - if (!self::p('db.type')) return $this->sql(); - // Type specific - global $_create_fct, $_create_fct_type; - $_create_fct_type = $this->db()->type('create',false); -#die('enum!'.$this->db()->type.' '.(string)$this->db()->type('create',false)); + // String replace function + $sql_replace_fct = $this->db()->type('sql.replace',false); + $sql_replace = function($sql) use ($sql_replace_fct) { + return $sql_replace_fct ? $sql_replace_fct($sql) : $sql; + }; + + if (!self::p('db.type')) return $sql_replace($this->sql()); + + if ($this->type == 'view') { + return $sql_replace('CREATE VIEW '.$this->sql_name().' AS SELECT'.preg_replace('/^CREATE\s+.*?\s+AS\s+.*?SELECT/i','',$this->sql())); + } - $_create_fct = function(&$field) { - global $_create_fct_type; if ($_create_fct_type and ($sql=$_create_fct_type($field))) return $sql; + // Specific function for fields if return something use it instead of default + $_create_fct = $this->db()->type('field.create',false); + $_create = function(&$field) use ($_create_fct) { + if ($_create_fct and ($sql=$_create_fct($field))) return $sql; + // Default return $field->sql_name().' '.$field->type .($field->null ? ' NULL' : ' NOT NULL') .($field->default !== null ? ' DEFAULT '.$field->quote($field->default,true) : '') @@ -138,17 +147,9 @@ Class Table extends nb { ; }; - if ($this->type == 'view') { - return 'CREATE VIEW '.$this->sql_name().' AS SELECT'.preg_replace('/^CREATE\s+.*?\s+AS\s+.*?SELECT/i','',$this->sql()); - $sql = $this->sql(); - if ( strpos('CREATE ',$sql) !==0 ) $sql = 'CREATE VIEW '.$this->sql_name().' AS '.$sql; - #$sql_replace = $this->db()->type('create',false); - return 'CREATE VIEW '.$this->sql_name().' AS '.$this->sql(); - } - $sql = 'CREATE '.strtoupper($this->type).' '.$this->sql_name() .' (' - .join(",",array_map(function($f){global $_create_fct;return $_create_fct($f);},array_values($this->fields()))) + .join(",",array_map(function($f) use ($_create) {return $_create($f);},array_values($this->fields()))) # done at the end for primary keys .')' ; @@ -159,14 +160,13 @@ Class Table extends nb { ; } - unset($_create_fct,$_create_fct_type); - return $sql.')'; + return $sql_replace($sql.')'); } /* * Function sql * - * return the sql to create the table + * return the sql used create the table * */ public function sql() { @@ -240,7 +240,7 @@ Class Table extends nb { 'type' => strtolower($row['type']), '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 + 'extra' => (isset($row['extra']) ? $row['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']; diff --git a/lib/php/db/types/sqlite.php b/lib/php/db/types/sqlite.php index 438bb3f0..ead4fc79 100644 --- a/lib/php/db/types/sqlite.php +++ b/lib/php/db/types/sqlite.php @@ -47,12 +47,16 @@ $DB_TYPES['sqlite'] = array ( '$r["key"] = $r["pk"] == "0" ? 0 : 1;', '$r["default"] = $r["dflt_value"];', '$r["null"] = $r["notnull"] == "0" ? 1 : 0;', - '$r["autoincrement"] = preg_match("/[\(,]".$r["name"]." [^\),]+ AUTOINCREMENT/",$r["this"]->sql()) ? 1 : 0;', + '$r["autoincrement"] = preg_match("/[\(,]\s*".$r["name"]." [^\),]+ AUTOINCREMENT/",$r["this"]->sql()) ? 1 : 0;', + '$r["extra"] = preg_match("/[\(,]\s*".$r["name"]." \S+ (COLLATE[^,)]+)/",$r["this"]->sql(),$m) ? trim($m[1]) : "";', + #'debug($m);' ))), ), -'create' => function(&$field) { +'field.create' => function(&$field) { if ($field->autoincrement) return $field->sql_name().' INTEGER PRIMARY KEY AUTOINCREMENT'; + + // Replace type $size = $field->size(); if (is_array($size)) $size = join(',',$size); $r = array(