]> git.nbdom.net Git - nb.git/commitdiff
table.create
authorNicolas Boisselier <nicolas.boisselier@semantico.com>
Wed, 29 Jun 2016 09:33:28 +0000 (10:33 +0100)
committerNicolas Boisselier <nicolas.boisselier@semantico.com>
Wed, 29 Jun 2016 09:33:28 +0000 (10:33 +0100)
lib/php/db/table.php
lib/php/db/types/sqlite.php

index e0405c2bece5c2ad5f99c21ce555e05622f1ae52..e4abc4fc0a38db6160efdc1ee7ec4ae4e835535a 100644 (file)
@@ -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'];
index 438bb3f0f8829dafdafee041c7f2d024439a3f55..ead4fc7939481678e0b93ca65ce2b1173d565b40 100644 (file)
@@ -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(