/** * * Constructor. * * If there is an error on instantiation, $this->error will be * populated with the PEAR_Error. * * @access public * * @param object &$db A PEAR DB/MDB2 object. * * @param string $table The table name to connect to in the database. * * @param mixed $create The automatic table creation mode to pursue: * - boolean false to not attempt creation * - 'safe' to create the table only if it does not exist * - 'drop' to drop any existing table with the same name and re-create it * - 'verify' to check whether the table exists, whether all the columns * exist, whether the columns have the right type, and whether the indexes * exist and have the right type * - 'alter' does the same as 'safe' if the table does not exist; if it * exists, a verification for columns existence, the column types, the * indexes existence, and the indexes types will be performed and the * table schema will be modified if needed * * @return object DB_Table * */ function DB_Table(&$db, $table, $create = false) { // is the first argument a DB/MDB2 object? $this->backend = null; if (is_subclass_of($db, 'db_common')) { $this->backend = 'db'; } elseif (is_subclass_of($db, 'mdb2_driver_common')) { $this->backend = 'mdb2'; } if (is_null($this->backend)) { $this->error =& DB_Table::throwError(DB_TABLE_ERR_NOT_DB_OBJECT); return; } // array with column definition may not be empty if (!isset($this->col) || is_null($this->col) || is_array($this->col) && count($this->col) === 0) { $this->error =& DB_Table::throwError(DB_TABLE_ERR_NO_COLS); return; } // set the class properties $this->db =& $db; $this->table = $table; // is the RDBMS supported? list($phptype, $dbsyntax) = DB_Table::getPHPTypeAndDBSyntax($db); if (!DB_Table::supported($phptype, $dbsyntax)) { $this->error =& DB_Table::throwError(DB_TABLE_ERR_PHPTYPE, "({$db->phptype})"); return; } // load MDB2_Extended module if ($this->backend == 'mdb2') { $this->db->loadModule('Extended', null, false); } // should we attempt table creation? if ($create) { if ($this->backend == 'mdb2') { $this->db->loadModule('Manager'); } // check whether the chosen mode is supported list($phptype, ) = DB_Table::getPHPTypeAndDBSyntax($this->db); $mode_supported = DB_Table::modeSupported($create, $phptype); if (PEAR::isError($mode_supported)) { return $mode_supported; } if (!$mode_supported) { return $this->throwError(DB_TABLE_ERR_CREATE_PHPTYPE, "('{$create}', '{$phptype}')"); } include_once 'DB/Table/Manager.php'; switch ($create) { case 'alter': $result = $this->alter(); break; case 'drop': case 'safe': $result = $this->create($create); break; case 'verify': $result = $this->verify(); break; } if (PEAR::isError($result)) { // problem creating/altering/verifing the table $this->error =& $result; return; } } }
/** * Constructor. * * The constructor returns a DB_Table object that wraps an * instance $db DB or MDB2, and that binds to a specific database * table named $table. It can optionally create the database table * or verify that its schema matches that declared in the $col and * $idx parameters, depending on the value of the $create parameter. * * If there is an error on instantiation, $this->error will be * populated with the PEAR_Error. * * @param object &$db A PEAR DB/MDB2 object. * * @param string $table The table name to connect to in the database. * * @param mixed $create The automatic table creation mode to pursue: * - boolean false to not attempt creation * - 'safe' to create the table only if it does not exist * - 'drop' to drop any existing table with the same name and re-create it * - 'verify' to check whether the table exists, whether all the columns * exist, whether the columns have the right type, and whether the indexes * exist and have the right type * - 'alter' does the same as 'safe' if the table does not exist; if it * exists, a verification for columns existence, the column types, the * indexes existence, and the indexes types will be performed and the * table schema will be modified if needed * * @return object DB_Table * @access public */ function DB_Table(&$db, $table, $create = false) { // is the first argument a DB/MDB2 object? $this->backend = null; if (is_subclass_of($db, 'db_common')) { $this->backend = 'db'; } elseif (is_subclass_of($db, 'mdb2_driver_common')) { $this->backend = 'mdb2'; } if (is_null($this->backend)) { $this->error =& DB_Table::throwError(DB_TABLE_ERR_NOT_DB_OBJECT); return; } // set the class properties $this->db =& $db; $this->table = $table; // Identify the class for error handling by parent class $this->_primary_subclass = 'DB_TABLE'; // is the RDBMS supported? $phptype = $db->phptype; $dbsyntax = $db->dbsyntax; if (!DB_Table::supported($phptype, $dbsyntax)) { $this->error =& DB_Table::throwError(DB_TABLE_ERR_PHPTYPE, "({$db->phptype})"); return; } // load MDB2_Extended module if ($this->backend == 'mdb2') { $this->db->loadModule('Extended', null, false); } // should we attempt table creation? if ($create) { if ($this->backend == 'mdb2') { $this->db->loadModule('Manager'); } // check whether the chosen mode is supported $mode_supported = DB_Table::modeSupported($create, $phptype); if (PEAR::isError($mode_supported)) { $this->error =& $mode_supported; return; } if (!$mode_supported) { $this->error =& $this->throwError(DB_TABLE_ERR_CREATE_PHPTYPE, "('{$create}', '{$phptype}')"); return; } include_once 'DB/Table/Manager.php'; switch ($create) { case 'alter': $result = $this->alter(); break; case 'drop': case 'safe': $result = $this->create($create); break; case 'verify': $result = $this->verify(); break; } if (PEAR::isError($result)) { // problem creating/altering/verifing the table $this->error =& $result; return; } } }