function createTable($tablename, $datadef, $info) { $sql = "CREATE TABLE \"" . $this->prefix . "{$tablename}\" ("; $alter_sql = array(); foreach ($datadef as $name => $def) { $sql .= $this->fieldSQL($name, $def) . ","; // PostGres is stupid, you cant specify NOT NULL in the Create Table if (!isset($def[DB_INCREMENT]) || !$def[DB_INCREMENT]) { $alter_sql[] = 'ALTER TABLE "' . $this->prefix . $tablename . '" ALTER COLUMN "' . $name . '" SET NOT NULL'; $default = null; if (isset($def[DB_DEFAULT])) { $default = $def[DB_DEFAULT]; } else { switch ($def[DB_FIELD_TYPE]) { case DB_DEF_ID: case DB_DEF_INTEGER: case DB_DEF_TIMESTAMP: case DB_DEF_DECIMAL: case DB_DEF_BOOLEAN: $default = 0; break; default: $default = ''; break; } } $alter_sql[] = 'ALTER TABLE "' . $this->prefix . $tablename . '" ALTER COLUMN "' . $name . '" SET DEFAULT ' . "'" . $default . "'"; } } $sql = substr($sql, 0, -1) . ")"; pg_query($this->connection, $sql); foreach ($alter_sql as $sql) { #echo '//'.$sql.'<br />'; pg_query($this->connection, $sql); } if (isset($info[DB_TABLE_WORKFLOW]) && $info[DB_TABLE_WORKFLOW]) { // Initialize workflow tables: if (!defined("SYS_WORKFLOW")) { require_once BASE . "subsystems/workflow.php"; } pathos_workflow_installWorkflowTables($tablename, $datadef); } }
function createTable($tablename, $datadef, $info) { if (!is_array($info)) { $info = array(); } // Initialize for later use. $sql = "CREATE TABLE `" . $this->prefix . "{$tablename}` ("; $primary = array(); $unique = array(); $index = array(); foreach ($datadef as $name => $def) { if ($def != null) { $sql .= $this->fieldSQL($name, $def) . ","; if (isset($def[DB_PRIMARY]) && $def[DB_PRIMARY] == true) { $primary[] = $name; } if (isset($def[DB_INDEX]) && $def[DB_INDEX] > 0) { if ($def[DB_FIELD_TYPE] == DB_DEF_STRING) { $index[$name] = $def[DB_INDEX]; } else { $index[$name] = 0; } } if (isset($def[DB_UNIQUE])) { if (!isset($unique[$def[DB_UNIQUE]])) { $unique[$def[DB_UNIQUE]] = array(); } $unique[$def[DB_UNIQUE]][] = $name; } } } $sql = substr($sql, 0, -1); if (count($primary)) { $sql .= ", PRIMARY KEY(`" . implode("`,`", $primary) . "`)"; } foreach ($unique as $key => $value) { $sql .= ", UNIQUE `" . $key . "` ( `" . implode("`,`", $value) . "`)"; } foreach ($index as $key => $value) { $sql .= ", INDEX (`" . $key . "`" . ($value > 0 ? "(" . $value . ")" : "") . ")"; } $sql .= ")"; if (isset($info[DB_TABLE_COMMENT])) { $sql .= " COMMENT = '" . $info[DB_TABLE_COMMENT] . "'"; } @mysql_query($sql, $this->connection); $return = array($tablename => $this->tableExists($tablename) ? DATABASE_TABLE_INSTALLED : DATABASE_TABLE_FAILED); if (isset($info[DB_TABLE_WORKFLOW]) && $info[DB_TABLE_WORKFLOW]) { // Initialize workflow tables: if (!defined("SYS_WORKFLOW")) { require_once BASE . "subsystems/workflow.php"; } $wf = pathos_workflow_installWorkflowTables($tablename, $datadef); foreach ($wf as $key => $status) { $return[$key] = $status; } } return $return; }