Changeset 639

Show
Ignore:
Timestamp:
11/13/07 01:38:24 (1 year ago)
Author:
laurentj
Message:

fixed bugs in jDb::getTools, mysqlDbTools and rewrote pgsqlDbTools + added new unit tests

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/build/manifests/testapp.mn

    r638 r639  
    2626cd testapp/install 
    2727  install.mysql.sql 
     28  install.pgsql.sql 
    2829 
    2930cd testapp/modules/testapp 
     
    120121  jdb.1_queries.html.php 
    121122  jdb.2_queries_with_pdo.html.php 
     123  jdb.pgsql.html.php 
    122124  jforms.html.php 
    123125  jforms.check_datas.html.php 
  • trunk/build/manifests/testapp.mn

    r638 r639  
    2626cd testapp/install 
    2727  install.mysql.sql 
     28  install.pgsql.sql 
    2829 
    2930cd testapp/modules/testapp 
     
    120121  jdb.1_queries.html.php 
    121122  jdb.2_queries_with_pdo.html.php 
     123  jdb.pgsql.html.php 
    122124  jforms.html.php 
    123125  jforms.check_datas.html.php 
  • trunk/lib/jelix-scripts/commands/createdao.cmd.php

    r576 r639  
    9292               case 'smallint': 
    9393               case 'year': 
    94                   if($prop->auto_increment ){ 
     94                  if($prop->autoIncrement ){ 
    9595                     $type='autoincrement'; 
    9696                  }else{ 
     
    101101               case 'mediumint': 
    102102               case 'bigint': 
    103                   if($prop->auto_increment ){ 
     103                  if($prop->autoIncrement ){ 
    104104                     $type='bigautoincrement'; 
    105105                  }else{ 
     
    136136                     $primarykeys.=$fieldname; 
    137137               } 
    138                if($prop->not_null && !$prop->auto_increment) 
     138               if($prop->notNull && !$prop->auto_increment) 
    139139                  $properties.=' required="true"'; 
    140140               $properties.='/>'; 
  • trunk/lib/jelix-scripts/commands/createdao.cmd.php

    r576 r639  
    9292               case 'smallint': 
    9393               case 'year': 
    94                   if($prop->auto_increment ){ 
     94                  if($prop->autoIncrement ){ 
    9595                     $type='autoincrement'; 
    9696                  }else{ 
     
    101101               case 'mediumint': 
    102102               case 'bigint': 
    103                   if($prop->auto_increment ){ 
     103                  if($prop->autoIncrement ){ 
    104104                     $type='bigautoincrement'; 
    105105                  }else{ 
     
    136136                     $primarykeys.=$fieldname; 
    137137               } 
    138                if($prop->not_null && !$prop->auto_increment) 
     138               if($prop->notNull && !$prop->auto_increment) 
    139139                  $properties.=' required="true"'; 
    140140               $properties.='/>'; 
  • trunk/lib/jelix/dao/jDaoParser.class.php

    r638 r639  
    396396        $this->_def = $def; 
    397397 
    398         $params = $def->getAttr($method, array('name', 'type', 'call','distinct', 'eventBefore', 'eventAfter')); 
     398        $params = $def->getAttr($method, array('name', 'type', 'call','distinct', 'eventbefore', 'eventafter')); 
    399399 
    400400        if ($params['name']===null){ 
     
    441441 
    442442        if ($this->type == 'update' || $this->type == 'delete') { 
    443             if ($params['eventBefore'] == 'true') 
     443            if ($params['eventbefore'] == 'true') 
    444444                $this->eventBeforeEnabled = true; 
    445             if ($params['eventAfter'] == 'true') 
     445            if ($params['eventafter'] == 'true') 
    446446                $this->eventAfterEnabled = true; 
    447447        } 
  • trunk/lib/jelix/dao/jDaoParser.class.php

    r638 r639  
    396396        $this->_def = $def; 
    397397 
    398         $params = $def->getAttr($method, array('name', 'type', 'call','distinct', 'eventBefore', 'eventAfter')); 
     398        $params = $def->getAttr($method, array('name', 'type', 'call','distinct', 'eventbefore', 'eventafter')); 
    399399 
    400400        if ($params['name']===null){ 
     
    441441 
    442442        if ($this->type == 'update' || $this->type == 'delete') { 
    443             if ($params['eventBefore'] == 'true') 
     443            if ($params['eventbefore'] == 'true') 
    444444                $this->eventBeforeEnabled = true; 
    445             if ($params['eventAfter'] == 'true') 
     445            if ($params['eventafter'] == 'true') 
    446446                $this->eventAfterEnabled = true; 
    447447        } 
  • trunk/lib/jelix/db/jDb.class.php

    r582 r639  
    9090        global $gJConfig; 
    9191#ifnot ENABLE_OPTIMIZED_SOURCE 
    92         if(!isset($gJConfig->_pluginsPathList_db[$profil['driver']]) 
    93             || !file_exists($gJConfig->_pluginsPathList_db[$profil['driver']]) ){ 
    94                 throw new jException('jelix~db.error.driver.notfound', $profil['driver']); 
     92        if(!isset($gJConfig->_pluginsPathList_db[$driver]) 
     93            || !file_exists($gJConfig->_pluginsPathList_db[$driver]) ){ 
     94                throw new jException('jelix~db.error.driver.notfound', $driver); 
    9595        } 
    9696#endif 
    97         require_once($gJConfig->_pluginsPathList_db[$profil['driver']].$profil['driver'].'.dbtools.php'); 
    98         $class = $profil['driver'].'DbTools'; 
     97        require_once($gJConfig->_pluginsPathList_db[$driver].$driver.'.dbtools.php'); 
     98        $class = $driver.'DbTools'; 
    9999 
    100100        //Création de l'objet 
  • trunk/lib/jelix/db/jDb.class.php

    r582 r639  
    9090        global $gJConfig; 
    9191#ifnot ENABLE_OPTIMIZED_SOURCE 
    92         if(!isset($gJConfig->_pluginsPathList_db[$profil['driver']]) 
    93             || !file_exists($gJConfig->_pluginsPathList_db[$profil['driver']]) ){ 
    94                 throw new jException('jelix~db.error.driver.notfound', $profil['driver']); 
     92        if(!isset($gJConfig->_pluginsPathList_db[$driver]) 
     93            || !file_exists($gJConfig->_pluginsPathList_db[$driver]) ){ 
     94                throw new jException('jelix~db.error.driver.notfound', $driver); 
    9595        } 
    9696#endif 
    97         require_once($gJConfig->_pluginsPathList_db[$profil['driver']].$profil['driver'].'.dbtools.php'); 
    98         $class = $profil['driver'].'DbTools'; 
     97        require_once($gJConfig->_pluginsPathList_db[$driver].$driver.'.dbtools.php'); 
     98        $class = $driver.'DbTools'; 
    9999 
    100100        //Création de l'objet 
  • trunk/lib/jelix/db/jDbTools.class.php

    r582 r639  
    2222 */ 
    2323 class jDbFieldProperties { 
     24    /** 
     25     * type of the field 
     26     * @var string 
     27     */ 
    2428    public $type; 
     29     
     30    /** 
     31     * field name 
     32     * @var string 
     33     */ 
    2534    public $name; 
    26     public $not_null=true; 
     35     
     36    /** 
     37     * says if the field can be null or not 
     38     * @var boolean 
     39     */ 
     40    public $notNull=true; 
     41     
     42    /** 
     43     * says if the field is the primary key 
     44     * @var boolean 
     45     */ 
    2746    public $primary=false; 
    28     public $auto_increment=false; 
     47     
     48    /** 
     49     * says if the field is auto incremented 
     50     * @var boolean 
     51     */ 
     52    public $autoIncrement=false; 
     53     
     54    /** 
     55     * default value 
     56     * @var string 
     57     */ 
     58    public $default=''; 
     59     
     60    /** 
     61     * says if there is a default value 
     62     * @var boolean 
     63     */ 
     64    public $hasDefault = false; 
     65 
     66 
     67    public $length = 0; 
    2968} 
    3069 
  • trunk/lib/jelix/db/jDbTools.class.php

    r582 r639  
    2222 */ 
    2323 class jDbFieldProperties { 
     24    /** 
     25     * type of the field 
     26     * @var string 
     27     */ 
    2428    public $type; 
     29     
     30    /** 
     31     * field name 
     32     * @var string 
     33     */ 
    2534    public $name; 
    26     public $not_null=true; 
     35     
     36    /** 
     37     * says if the field can be null or not 
     38     * @var boolean 
     39     */ 
     40    public $notNull=true; 
     41     
     42    /** 
     43     * says if the field is the primary key 
     44     * @var boolean 
     45     */ 
    2746    public $primary=false; 
    28     public $auto_increment=false; 
     47     
     48    /** 
     49     * says if the field is auto incremented 
     50     * @var boolean 
     51     */ 
     52    public $autoIncrement=false; 
     53     
     54    /** 
     55     * default value 
     56     * @var string 
     57     */ 
     58    public $default=''; 
     59     
     60    /** 
     61     * says if there is a default value 
     62     * @var boolean 
     63     */ 
     64    public $hasDefault = false; 
     65 
     66 
     67    public $length = 0; 
    2968} 
    3069 
  • trunk/lib/jelix/plugins/db/mysql/mysql.dbtools.php

    r582 r639  
    2222class mysqlDbTools extends jDbTools { 
    2323 
    24    /** 
    25    * retourne la liste des tables 
    26    * @return   array    $tab[] = $nomDeTable 
    27    */ 
    28    function _getTableList (){ 
    29       $results = array (); 
     24    /** 
     25    * retourne la liste des tables 
     26    * @return   array    $tab[] = $nomDeTable 
     27    */ 
     28    function _getTableList (){ 
     29        $results = array (); 
     30     
     31        $rs = $this->_connector->query ('SHOW TABLES FROM '.$this->_connector->profil['database']); 
     32        $col_name = 'Tables_in_'.$this->_connector->profil['database']; 
     33     
     34        while ($line = $rs->fetch ()){ 
     35            $results[] = $line->$col_name; 
     36        } 
     37     
     38        return $results; 
     39    } 
    3040 
    31       $rs = $this->_connector->query ('SHOW TABLES FROM '.$this->_connector->profil['database']); 
    32       $col_name = 'Tables_in_'.$this->_connector->profil['database']; 
    33  
    34       while ($line = $rs->fetch ()){ 
    35          $results[] = $line->$col_name; 
    36       } 
    37  
    38       return $results; 
    39    } 
    40  
    41    /** 
    42    * récupère la liste des champs pour une base donnée. 
    43    * @return   array    $tab[NomDuChamp] = obj avec prop (tye, length, lengthVar, notnull) 
    44    */ 
    45    function _getFieldList ($tableName){ 
    46       $results = array (); 
    47  
    48       $rs = $this->_connector->query ('SHOW FIELDS FROM ' . $tableName); 
    49  
    50       while ($result_line = $rs->fetch ()){ 
    51          $field = new jDbFieldProperties(); 
    52  
    53          $type = $result_line->Type; 
    54  
    55           /** 
    56           * récupéré depuis phpMyAdmin 
    57           */ 
    58           // set or enum types: slashes single quotes inside options 
    59           $type   = str_replace('BINARY', '', $type); 
    60           $type   = str_replace('ZEROFILL', '', $type); 
    61           $type   = str_replace('UNSIGNED', '', $type); 
    62           /* 
    63           if (eregi('^(set|enum)\((.+)\)$', $type, $tmp)){ 
    64               $type   = $tmp[1]; 
    65               $length = substr(ereg_replace('([^,])\'\'', '\\1\\\'', ',' . $tmp[2]), 1); 
    66           }else{ 
    67               $length = $type; 
    68               $type   = chop(eregi_replace('\\(.*\\)', '', $type)); 
    69               if (!empty($type)){ 
    70                   $length = eregi_replace("^$type\(", '', $length); 
    71                   $length = eregi_replace('\)$', '', trim($length)); 
    72               } 
    73               if ($length == $type){ 
    74                   $length = ''; 
    75               } 
    76           }*/ 
    77  
    78           preg_match('/^(\w+).*$/',$type,$m); 
    79  
    80  
    81           $field->type      = $m[1]; 
    82           $field->name = $result_line->Field; 
    83           //$p_result_line->length    = $length; 
    84           $field->not_null   = (trim ($result_line->Null) != 'YES'); 
    85           $field->primary  = (trim ($result_line->Key) == 'PRI'); 
    86           $field->auto_increment  = ($result_line->Extra == 'auto_increment'); 
    87           $results[$result_line->Field] = $field; 
    88       } 
    89       return $results; 
    90    } 
     41    /** 
     42    * récupère la liste des champs pour une base donnée. 
     43    * @return   array    $tab[NomDuChamp] = obj avec prop (tye, length, lengthVar, notnull) 
     44    */ 
     45    function _getFieldList ($tableName){ 
     46        $results = array (); 
     47     
     48        $rs = $this->_connector->query ('SHOW FIELDS FROM ' . $tableName); 
     49     
     50        while ($line = $rs->fetch ()){ 
     51            $field = new jDbFieldProperties(); 
     52            preg_match('/^(\w+).*$/',$line->Type,$m); 
     53            $field->type = $m[1]; 
     54            $field->name = $line->Field; 
     55            $field->notNull = ($line->Null == 'NO'); 
     56            $field->primary = ($line->Key == 'PRI'); 
     57            $field->hasDefault = ($line->Default != '' || !($line->Default == null && $field->notNull)); 
     58            $field->default = $line->Default; 
     59            $field->autoIncrement  = ($line->Extra == 'auto_increment'); 
     60            $results[$line->Field] = $field; 
     61        } 
     62        return $results; 
     63    } 
    9164} 
    9265?> 
  • trunk/lib/jelix/plugins/db/mysql/mysql.dbtools.php

    r582 r639  
    2222class mysqlDbTools extends jDbTools { 
    2323 
    24    /** 
    25    * retourne la liste des tables 
    26    * @return   array    $tab[] = $nomDeTable 
    27    */ 
    28    function _getTableList (){ 
    29       $results = array (); 
     24    /** 
     25    * retourne la liste des tables 
     26    * @return   array    $tab[] = $nomDeTable 
     27    */ 
     28    function _getTableList (){ 
     29        $results = array (); 
     30     
     31        $rs = $this->_connector->query ('SHOW TABLES FROM '.$this->_connector->profil['database']); 
     32        $col_name = 'Tables_in_'.$this->_connector->profil['database']; 
     33     
     34        while ($line = $rs->fetch ()){ 
     35            $results[] = $line->$col_name; 
     36        } 
     37     
     38        return $results; 
     39    } 
    3040 
    31       $rs = $this->_connector->query ('SHOW TABLES FROM '.$this->_connector->profil['database']); 
    32       $col_name = 'Tables_in_'.$this->_connector->profil['database']; 
    33  
    34       while ($line = $rs->fetch ()){ 
    35          $results[] = $line->$col_name; 
    36       } 
    37  
    38       return $results; 
    39    } 
    40  
    41    /** 
    42    * récupère la liste des champs pour une base donnée. 
    43    * @return   array    $tab[NomDuChamp] = obj avec prop (tye, length, lengthVar, notnull) 
    44    */ 
    45    function _getFieldList ($tableName){ 
    46       $results = array (); 
    47  
    48       $rs = $this->_connector->query ('SHOW FIELDS FROM ' . $tableName); 
    49  
    50       while ($result_line = $rs->fetch ()){ 
    51          $field = new jDbFieldProperties(); 
    52  
    53          $type = $result_line->Type; 
    54  
    55           /** 
    56           * récupéré depuis phpMyAdmin 
    57           */ 
    58           // set or enum types: slashes single quotes inside options 
    59           $type   = str_replace('BINARY', '', $type); 
    60           $type   = str_replace('ZEROFILL', '', $type); 
    61           $type   = str_replace('UNSIGNED', '', $type); 
    62           /* 
    63           if (eregi('^(set|enum)\((.+)\)$', $type, $tmp)){ 
    64               $type   = $tmp[1]; 
    65               $length = substr(ereg_replace('([^,])\'\'', '\\1\\\'', ',' . $tmp[2]), 1); 
    66           }else{ 
    67               $length = $type; 
    68               $type   = chop(eregi_replace('\\(.*\\)', '', $type)); 
    69               if (!empty($type)){ 
    70                   $length = eregi_replace("^$type\(", '', $length); 
    71                   $length = eregi_replace('\)$', '', trim($length)); 
    72               } 
    73               if ($length == $type){ 
    74                   $length = ''; 
    75               } 
    76           }*/ 
    77  
    78           preg_match('/^(\w+).*$/',$type,$m); 
    79  
    80  
    81           $field->type      = $m[1]; 
    82           $field->name = $result_line->Field; 
    83           //$p_result_line->length    = $length; 
    84           $field->not_null   = (trim ($result_line->Null) != 'YES'); 
    85           $field->primary  = (trim ($result_line->Key) == 'PRI'); 
    86           $field->auto_increment  = ($result_line->Extra == 'auto_increment'); 
    87           $results[$result_line->Field] = $field; 
    88       } 
    89       return $results; 
    90    } 
     41    /** 
     42    * récupère la liste des champs pour une base donnée. 
     43    * @return   array    $tab[NomDuChamp] = obj avec prop (tye, length, lengthVar, notnull) 
     44    */ 
     45    function _getFieldList ($tableName){ 
     46        $results = array (); 
     47     
     48        $rs = $this->_connector->query ('SHOW FIELDS FROM ' . $tableName); 
     49     
     50        while ($line = $rs->fetch ()){ 
     51            $field = new jDbFieldProperties(); 
     52            preg_match('/^(\w+).*$/',$line->Type,$m); 
     53            $field->type = $m[1]; 
     54            $field->name = $line->Field; 
     55            $field->notNull = ($line->Null == 'NO'); 
     56            $field->primary = ($line->Key == 'PRI'); 
     57            $field->hasDefault = ($line->Default != '' || !($line->Default == null && $field->notNull)); 
     58            $field->default = $line->Default; 
     59            $field->autoIncrement  = ($line->Extra == 'auto_increment'); 
     60            $results[$line->Field] = $field; 
     61        } 
     62        return $results; 
     63    } 
    9164} 
    9265?> 
  • trunk/lib/jelix/plugins/db/pgsql/pgsql.dbtools.php

    r631 r639  
    33* @package    jelix 
    44* @subpackage db_driver 
    5 * @author     Croes Gérald, Ferlet Patrice, Laurent Jouanneau 
     5* @author     Laurent Jouanneau 
    66* @contributor Laurent Jouanneau 
    77* @contributor Nicolas Jeudy (patch ticket #99) 
    8 * @copyright  2001-2005 CopixTeam, 2005-2006 Laurent Jouanneau 
    9 * This class was get originally from the Copix project (CopixDBToolsPostgreSQL, Copix 2.3dev20050901, http://www.copix.org) 
    10 * Few lines of code are still copyrighted 2001-2005 CopixTeam (LGPL licence). 
    11 * Initial authors of this Copix class are Gerald Croes, Ferlet Patrice  and Laurent Jouanneau, 
    12 * and this class was adapted for Jelix by Laurent Jouanneau 
    13 
     8* @copyright  2005-2007 Laurent Jouanneau 
    149* @link        http://www.jelix.org 
    1510* @licence  http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file 
     
    2924   protected function _getTableList (){ 
    3025      $results = array (); 
    31       $sql = "SELECT tablename FROM pg_tables WHERE tablename NOT LIKE 'pg_%' ORDER BY tablename"; 
     26      $sql = "SELECT tablename FROM pg_tables WHERE schemaname NOT IN ('pg_catalog', 'information_schema') ORDER BY tablename"; 
    3227      $rs = $this->_connector->query ($sql); 
    3328      while ($line = $rs->fetch()){ 
     
    4237    protected function _getFieldList ($tableName){ 
    4338        $results = array (); 
    44         $sql_get_fields = "SELECT 
    45         a.attname as Field, t.typname as type, a.attlen as length, a.atttypmod, 
    46         case when a.attnotnull  then 1 else 0 end as notnull, 
    47         a.atthasdef, 
    48         (SELECT adsrc FROM pg_attrdef adef WHERE a.attrelid=adef.adrelid AND a.attnum=adef.adnum) AS adsrc 
    49         FROM 
    50             pg_attribute a, 
    51             pg_class c, 
    52             pg_type t 
     39         
     40        // get table informations 
     41        $sql ='SELECT oid, relhaspkey, relhasindex FROM pg_class WHERE relname = \''.$tableName.'\''; 
     42        $rs = $this->_connector->query ($sql); 
     43        if (! ($table = $rs->fetch())) { 
     44            throw new Exception('dbtools, pgsql: unknow table'); 
     45        } 
     46 
     47        $pkeys = array(); 
     48        // get primary keys informations 
     49        if ($table->relhaspkey == 't') { 
     50            $sql = 'SELECT indkey FROM pg_index WHERE indrelid = '.$table->oid.' and indisprimary = true'; 
     51            $rs = $this->_connector->query ($sql); 
     52            $pkeys = preg_split("/[\s]+/", $rs->fetch()->indkey); 
     53        } 
     54 
     55        // get field informations 
     56        $sql_get_fields = "SELECT t.typname, a.attname, a.attnotnull, a.attnum, 
     57        a.atthasdef, d.adsrc 
     58        FROM pg_type t, pg_attribute a LEFT JOIN pg_attrdef d ON (d.adrelid=a.attrelid AND d.adnum=a.attnum) 
    5359        WHERE 
    54           c.relname = '{$tableName}' AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid 
     60          a.attnum > 0 AND a.attrelid = ".$table->oid." AND a.atttypid = t.oid 
    5561        ORDER BY a.attnum"; 
     62         
     63        $toReturn=array(); 
     64        $rs = $this->_connector->query ($sql_get_fields); 
     65        while ($line = $rs->fetch ()){ 
     66            $field = new jDbFieldProperties(); 
     67            $field->name = $line->attname; 
     68            $field->type = preg_replace('/(\D*)\d*/','\\1',$line->typname); 
     69            $field->notNull = ($line->attnotnull=='t'); 
     70            $field->hasDefault = ($line->atthasdef == 't'); 
     71            $field->default = $line->adsrc; 
    5672 
    57         $rs = $this->_connector->query ($sql_get_fields); 
    58         $toReturn=array(); 
    59         while ($result_line = $rs->fetch ()){ 
    60             $field = new jDbFieldProperties(); 
    61             if(preg_match('/nextval\(\'(.*?)\.'.$tableName.'_'.$result_line->field.'_seq\'::text\)/', 
    62             $result_line->adsrc)){ 
    63                 $field->auto_increment=true; 
     73            if(preg_match('/^nextval\(.*\)$/', $line->adsrc)){ 
     74                $field->autoIncrement=true; 
     75                $field->default = ''; 
    6476            } 
    65  
    66             $field->notnull = ($result_line->notnull==1)  ? true:false; 
    67             $field->type = preg_replace('/(\D*)\d*/','\\1',$result_line->type); 
    68             if($result_line->length<0) 
    69                 $field->length=null; 
    70             else 
    71                 $field->length=$result_line->length; 
    72             $field->name = $result_line->field; 
    73              $field->primary = ($result_line->atthasdef == 't'); 
    74  
    75             $toReturn[$result_line->field]=$field; 
     77             
     78            if(in_array($line->attnum, $pkeys)) 
     79                $field->primary = true; 
     80             
     81            $toReturn[$line->attname]=$field; 
    7682        } 
    7783 
  • trunk/lib/jelix/plugins/db/pgsql/pgsql.dbtools.php

    r631 r639  
    33* @package    jelix 
    44* @subpackage db_driver 
    5 * @author     Croes Gérald, Ferlet Patrice, Laurent Jouanneau 
     5* @author     Laurent Jouanneau 
    66* @contributor Laurent Jouanneau 
    77* @contributor Nicolas Jeudy (patch ticket #99) 
    8 * @copyright  2001-2005 CopixTeam, 2005-2006 Laurent Jouanneau 
    9 * This class was get originally from the Copix project (CopixDBToolsPostgreSQL, Copix 2.3dev20050901, http://www.copix.org) 
    10 * Few lines of code are still copyrighted 2001-2005 CopixTeam (LGPL licence). 
    11 * Initial authors of this Copix class are Gerald Croes, Ferlet Patrice  and Laurent Jouanneau, 
    12 * and this class was adapted for Jelix by Laurent Jouanneau 
    13 
     8* @copyright  2005-2007 Laurent Jouanneau 
    149* @link        http://www.jelix.org 
    1510* @licence  http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file 
     
    2924   protected function _getTableList (){ 
    3025      $results = array (); 
    31       $sql = "SELECT tablename FROM pg_tables WHERE tablename NOT LIKE 'pg_%' ORDER BY tablename"; 
     26      $sql = "SELECT tablename FROM pg_tables WHERE schemaname NOT IN ('pg_catalog', 'information_schema') ORDER BY tablename"; 
    3227      $rs = $this->_connector->query ($sql); 
    3328      while ($line = $rs->fetch()){ 
     
    4237    protected function _getFieldList ($tableName){ 
    4338        $results = array (); 
    44         $sql_get_fields = "SELECT 
    45         a.attname as Field, t.typname as type, a.attlen as length, a.atttypmod, 
    46         case when a.attnotnull  then 1 else 0 end as notnull, 
    47         a.atthasdef, 
    48         (SELECT adsrc FROM pg_attrdef adef WHERE a.attrelid=adef.adrelid AND a.attnum=adef.adnum) AS adsrc 
    49         FROM 
    50             pg_attribute a, 
    51             pg_class c, 
    52             pg_type t 
     39         
     40        // get table informations 
     41        $sql ='SELECT oid, relhaspkey, relhasindex FROM pg_class WHERE relname = \''.$tableName.'\''; 
     42        $rs = $this->_connector->query ($sql); 
     43        if (! ($table = $rs->fetch())) { 
     44            throw new Exception('dbtools, pgsql: unknow table'); 
     45        } 
     46 
     47        $pkeys = array(); 
     48        // get primary keys informations 
     49        if ($table->relhaspkey == 't') { 
     50            $sql = 'SELECT indkey FROM pg_index WHERE indrelid = '.$table->oid.' and indisprimary = true'; 
     51            $rs = $this->_connector->query ($sql); 
     52            $pkeys = preg_split("/[\s]+/", $rs->fetch()->indkey); 
     53        } 
     54 
     55        // get field informations 
     56        $sql_get_fields = "SELECT t.typname, a.attname, a.attnotnull, a.attnum, 
     57        a.atthasdef, d.adsrc 
     58        FROM pg_type t, pg_attribute a LEFT JOIN pg_attrdef d ON (d.adrelid=a.attrelid AND d.adnum=a.attnum) 
    5359        WHERE 
    54           c.relname = '{$tableName}' AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid 
     60          a.attnum > 0 AND a.attrelid = ".$table->oid." AND a.atttypid = t.oid 
    5561        ORDER BY a.attnum"; 
     62         
     63        $toReturn=array(); 
     64        $rs = $this->_connector->query ($sql_get_fields); 
     65        while ($line = $rs->fetch ()){ 
     66            $field = new jDbFieldProperties(); 
     67            $field->name = $line->attname; 
     68            $field->type = preg_replace('/(\D*)\d*/','\\1',$line->typname); 
     69            $field->notNull = ($line->attnotnull=='t'); 
     70            $field->hasDefault = ($line->atthasdef == 't'); 
     71            $field->default = $line->adsrc; 
    5672 
    57         $rs = $this->_connector->query ($sql_get_fields); 
    58         $toReturn=array(); 
    59         while ($result_line = $rs->fetch ()){ 
    60             $field = new jDbFieldProperties(); 
    61             if(preg_match('/nextval\(\'(.*?)\.'.$tableName.'_'.$result_line->field.'_seq\'::text\)/', 
    62             $result_line->adsrc)){ 
    63                 $field->auto_increment=true; 
     73            if(preg_match('/^nextval\(.*\)$/', $line->adsrc)){ 
     74                $field->autoIncrement=true; 
     75                $field->default = ''; 
    6476            } 
    65  
    66             $field->notnull = ($result_line->notnull==1)  ? true:false; 
    67             $field->type = preg_replace('/(\D*)\d*/','\\1',$result_line->type); 
    68             if($result_line->length<0) 
    69                 $field->length=null; 
    70             else 
    71                 $field->length=$result_line->length; 
    72             $field->name = $result_line->field; 
    73              $field->primary = ($result_line->atthasdef == 't'); 
    74  
    75             $toReturn[$result_line->field]=$field; 
     77             
     78            if(in_array($line->attnum, $pkeys)) 
     79                $field->primary = true; 
     80             
     81            $toReturn[$line->attname]=$field; 
    7682        } 
    7783 
  • trunk/lib/jelix/plugins/db/sqlite/sqlite.dbtools.php

    r482 r639  
    8484            $field->name = $result_line->name; 
    8585            //$p_result_line->length    = $length; 
    86             $field->not_null   = ($result_line->notnull != 1); 
     86            $field->notNull   = ($result_line->notnull != 1); 
    8787            $field->primary  = ($result_line->pk == 1); 
    8888 
    8989            if (preg_match('/^int/i', $field->type)) { 
    9090                if ($field->primary) { 
    91                     $field->auto_increment = true; 
     91                    $field->autoIncrement = true; 
    9292                } else { 
    9393                    $str = stristr($create_table, $field->name); 
    9494                    $array = explode(',', $str); 
    9595                    if (preg_match('/autoincrement/i', $array[0])) { 
    96                         $field->auto_increment = true; 
     96                        $field->autoIncrement = true; 
    9797                    } else { 
    98                         $field->auto_increment = false; 
     98                        $field->autoIncrement = false; 
    9999                    } 
    100100                } 
    101101            } else { 
    102                 $field->auto_increment = false; 
     102                $field->autoIncrement = false; 
    103103            } 
    104104            $results[$result_line->name] = $field; 
  • trunk/lib/jelix/plugins/db/sqlite/sqlite.dbtools.php

    r482 r639  
    8484            $field->name = $result_line->name; 
    8585            //$p_result_line->length    = $length; 
    86             $field->not_null   = ($result_line->notnull != 1); 
     86            $field->notNull   = ($result_line->notnull != 1); 
    8787            $field->primary  = ($result_line->pk == 1); 
    8888 
    8989            if (preg_match('/^int/i', $field->type)) { 
    9090                if ($field->primary) { 
    91                     $field->auto_increment = true; 
     91                    $field->autoIncrement = true; 
    9292                } else { 
    9393                    $str = stristr($create_table, $field->name); 
    9494                    $array = explode(',', $str); 
    9595                    if (preg_match('/autoincrement/i', $array[0])) { 
    96                         $field->auto_increment = true; 
     96                        $field->autoIncrement = true; 
    9797                    } else { 
    98                         $field->auto_increment = false; 
     98                        $field->autoIncrement = false; 
    9999                    } 
    100100                } 
    101101            } else { 
    102                 $field->auto_increment = false; 
     102                $field->autoIncrement = false; 
    103103            } 
    104104            $results[$result_line->name] = $field; 
  • trunk/testapp/install/install.mysql.sql

    r629 r639  
    146146`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , 
    147147`name` VARCHAR( 150 ) NOT NULL , 
    148 `price` FLOAT NOT NULL 
     148`price` FLOAT   default '0' 
    149149) TYPE = MYISAM ; 
    150150 
  • trunk/testapp/install/install.mysql.sql

    r629 r639  
    146146`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , 
    147147`name` VARCHAR( 150 ) NOT NULL , 
    148 `price` FLOAT NOT NULL 
     148`price` FLOAT   default '0' 
    149149) TYPE = MYISAM ; 
    150150 
  • trunk/testapp/modules/jelix_tests/tests/jdao.parser_update.html.php

    r638 r639  
    143143 
    144144        array('<?xml version="1.0"?> 
    145           <method name="foo" type="update" eventBefore="true"> 
     145          <method name="foo" type="update" eventbefore="true"> 
    146146            <values> 
    147147                <value property="subject" value="my text" /> 
     
    172172         
    173173        array('<?xml version="1.0"?> 
    174           <method name="foo" type="update" eventAfter="true"> 
     174          <method name="foo" type="update" eventafter="true"> 
    175175            <values> 
    176176                <value property="subject" value="my text" /> 
  • trunk/testapp/modules/jelix_tests/tests/jdao.parser_update.html.php

    r638 r639  
    143143 
    144144        array('<?xml version="1.0"?> 
    145           <method name="foo" type="update" eventBefore="true"> 
     145          <method name="foo" type="update" eventbefore="true"> 
    146146            <values> 
    147147                <value property="subject" value="my text" /> 
     
    172172         
    173173        array('<?xml version="1.0"?> 
    174           <method name="foo" type="update" eventAfter="true"> 
     174          <method name="foo" type="update" eventafter="true"> 
    175175            <values> 
    176176                <value property="subject" value="my text" /> 
  • trunk/testapp/modules/jelix_tests/tests/jdb.1_queries.html.php

    r468 r639  
    114114    } 
    115115 
     116    function testTools(){ 
     117 
     118        $tools = jDb::getTools($this->dbProfil); 
     119        $fields = $tools->getFieldList('products'); 
     120        $structure = '<array> 
     121    <object key="id" class="jDbFieldProperties"> 
     122        <string property="type" value="int" /> 
     123        <string property="name" value="id" /> 
     124        <boolean property="notNull" value="true" /> 
     125        <boolean property="primary" value="true" /> 
     126        <boolean property="autoIncrement" value="true" /> 
     127        <boolean property="hasDefault" value="false" /> 
     128        <null property="default" /> 
     129    </object> 
     130    <object key="name" class="jDbFieldProperties"> 
     131        <string property="type" value="varchar" /> 
     132        <string property="name" value="name" /> 
     133        <boolean property="notNull" value="true" /> 
     134        <boolean property="primary" value="false" /> 
     135        <boolean property="autoIncrement" value="false" />