*/
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) : '')
;
};
- 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 .')'
;
;
}
- 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() {
'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'];
'$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(