Exemplo n.º 1
0
    public static function load_current_schema()
    {
        $schema_oid = ERDiagram::get_object_oid('schema');
        $sql = 'SELECT "table"."oid" AS "tableOID", "table"."relname" AS "tableName", COALESCE("pg_catalog"."obj_description"("table"."oid", \'pg_class\'), \'\') AS "tableComment", NOT "table"."relhasoids" AS "tableWithoutOIDS",
					"column"."attnum" AS "columnNum", "column"."attname" AS "columnName", "column"."attnotnull" AS "columnNotNull", COALESCE("columnDef"."adsrc", \'\') AS "columnDefaultDef", COALESCE("pg_catalog"."col_description"("column"."attrelid", "column"."attnum"), \'\') AS "columnComment", UPPER("pg_catalog"."format_type"("column"."atttypid", "column"."atttypmod")) as "columnType"
					FROM "pg_catalog"."pg_class" AS "table"
					LEFT JOIN "pg_catalog"."pg_attribute" AS "column" ON "table"."oid" = "column"."attrelid" AND "column"."attnum" > 0 AND NOT "column"."attisdropped"
					LEFT JOIN "pg_catalog"."pg_attrdef" AS "columnDef" ON "column"."attrelid" = "columnDef"."adrelid" AND "column"."attnum" = "columnDef"."adnum"
					WHERE "relkind"=\'r\' AND "relnamespace" = ' . $schema_oid . '
					ORDER BY "table"."oid", "column"."attnum"';
        $rs = ERDiagram::$logic_driver->selectSet($sql);
        $tables = array();
        $indexed_tables = array();
        $indexed_columns = array();
        $table = null;
        $column = null;
        if (is_object($rs)) {
            while (!$rs->EOF) {
                if (is_null($table) || $table->name != $rs->fields['tableName']) {
                    $table = new stdClass();
                    $table->name = $rs->fields['tableName'];
                    $table->withoutOIDS = $rs->fields['tableWithoutOIDS'] == 't';
                    $table->comment = $rs->fields['tableComment'];
                    $table->columns = array();
                    $table->uniqueKeys = array();
                    $table->foreignKeys = array();
                    $tables[] = $table;
                    $indexed_tables[(int) $rs->fields['tableOID']] = $table;
                }
                if (!is_null($rs->fields['columnNum'])) {
                    $column = new stdClass();
                    $column->name = $rs->fields['columnName'];
                    $column->comment = $rs->fields['columnComment'];
                    $column->defaultDef = $rs->fields['columnDefaultDef'];
                    $column->notNull = $rs->fields['columnNotNull'] == 't';
                    $column->array = preg_match('/\\[\\]/', $rs->fields['columnType']) === 1;
                    $column->length = preg_match('/\\(([0-9, ]*)\\)/', $rs->fields['columnType'], $matches) ? $matches[1] : '';
                    $column->type = preg_replace('/\\([0-9, ]*\\)/', '', preg_replace('/\\[\\]/', '', $rs->fields["columnType"]));
                    $column->primaryKey = false;
                    if (preg_match('/^nextval/', strtolower($column->defaultDef)) && $column->notNull && ($column->type == "INTEGER" || $column->type == "BIGINT")) {
                        $column->defaultDef = '';
                        $column->type = $column->type == 'INTEGER' ? 'SERIAL' : 'BIGSERIAL';
                    }
                    $table->columns[] = $column;
                    $indexed_columns[(int) $rs->fields['tableOID']][(int) $rs->fields['columnNum']] = $column;
                }
                $rs->MoveNext();
            }
        }
        $sql = 'SELECT "conname", "contype", "condeferrable", "condeferred", "conrelid",
				"confrelid", "confupdtype", "confdeltype", "confmatchtype", "conkey", "confkey",
				COALESCE("pg_catalog"."obj_description"("oid", \'pg_constraint\'), \'\') AS "comment"
				FROM "pg_catalog"."pg_constraint"
				WHERE "contype" IN (\'f\', \'p\', \'u\') AND connamespace = ' . $schema_oid;
        $rs = ERDiagram::$logic_driver->selectSet($sql);
        if (is_object($rs)) {
            while (!$rs->EOF) {
                $table_oid = (int) $rs->fields['conrelid'];
                $conkey = explode(',', str_replace(array('{', '}'), '', $rs->fields['conkey']));
                $constraint = new stdClass();
                $constraint->name = $rs->fields['conname'];
                $constraint->comment = $rs->fields['comment'];
                $constraint->columns = array();
                switch ($rs->fields["contype"]) {
                    case 'f':
                        $confkey = explode(',', str_replace(array('{', '}'), '', $rs->fields['confkey']));
                        $ftableOID = (int) $rs->fields['confrelid'];
                        foreach ($conkey as $i => $key) {
                            $constraint->columns[] = array('localColumn' => $indexed_columns[$table_oid][(int) $key]->name, 'foreignColumn' => $indexed_columns[$ftableOID][(int) $confkey[$i]]->name);
                        }
                        $constraint->deleteAction = $rs->fields['confdeltype'];
                        $constraint->updateAction = $rs->fields['confupdtype'];
                        $constraint->deferred = $rs->fields['condeferred'];
                        $constraint->deferrable = $rs->fields['condeferrable'];
                        $constraint->matchFull = $rs->fields['confmatchtype'] == 'f';
                        $constraint->referencedTable = $indexed_tables[$ftableOID]->name;
                        $indexed_tables[$table_oid]->foreignKeys[] = $constraint;
                        break;
                    case 'p':
                        foreach ($conkey as $key) {
                            $indexed_columns[$table_oid][(int) $key]->primaryKey = true;
                        }
                        break;
                    case 'u':
                        $constraint->columns = array();
                        foreach ($conkey as $key) {
                            $constraint->columns[] = $indexed_columns[$table_oid][(int) $key]->name;
                        }
                        $indexed_tables[$table_oid]->uniqueKeys[] = $constraint;
                        break;
                }
                $rs->MoveNext();
            }
        }
        return array('tables' => $tables);
    }
Exemplo n.º 2
0
 function ajax_load_schema_structure()
 {
     $this->connect();
     $schema = ERDiagram::load_current_schema();
     $this->send_ajax_response($schema);
 }