"integer", "BIGSERIAL" => "integer", "abstime" => "datetime", "aclitem" => "", "any" => "", "anyarray" => "", "anyelement" => "", "anyenum" => "", "anynonarray" => "", "anyrange" => "", "bigint" => "integer", "bit" => "", "bit varying" => "", "boolean" => "boolean", "box" => "", "bytea" => "string", "char" => "string", "character" => "string", "character varying" => "string", "cid" => "", "cidr" => "", "circle" => "", "cstring" => "", "date" => "datetime", "daterange" => "", "decimal" => "float", "double precision" => "float", "event_trigger" => "", "fdw_handler" => "", "gtsvector" => "", "inet" => "", "information_schema.cardinal_number" => "", "information_schema.character_data" => "", "information_schema.sql_identifier" => "", "information_schema.time_stamp" => "", "information_schema.yes_or_no" => "", "int2vector" => "", "int4range" => "", "int8range" => "", "integer" => "integer", "internal" => "", "interval" => "", "json" => "", "jsonb" => "", "language_handler" => "", "line" => "", "lseg" => "", "macaddr" => "", "money" => "string", "name" => "", "numeric" => "float", "numrange" => "", "oid" => "", "oidvector" => "", "opaque" => "", "path" => "", "pg_lsn" => "", "pg_node_tree" => "", "point" => "", "polygon" => "", "real" => "float", "record" => "", "refcursor" => "", "regclass" => "", "regconfig" => "", "regdictionary" => "", "regoper" => "", "regoperator" => "", "regproc" => "", "regprocedure" => "", "regtype" => "", "reltime" => "", "smallint" => "integer", "smallserial" => "integer", "smgr" => "", "text" => "string", "tid" => "", "timestamp without time zone" => "datetime", "timestamp with time zone" => "datetime", "time without time zone" => "datetime", "time with time zone" => "datetime", "tinterval" => "", "trigger" => "", "tsquery" => "", "tsrange" => "", "tstzrange" => "", "tsvector" => "", "txid_snapshot" => "", "unknown" => "", "uuid" => "", "void" => "", "xid" => "", "xml" => ""]; /** * @param string $sqlType * @return string */ public function sqlTypeToPhpType($sqlType) { $isArray = false; if (LuStringUtils::endsWith($sqlType, "[]")) { $isArray = true; $sqlType = substr($sqlType, 0, strlen($sqlType) - 2); } if (!isset(static::$types) || static::$types[$sqlType] == "") { return "string"; } return static::$types[$sqlType] . ($isArray ? "[]" : ""); } /** * @param \PDO $pdo * @return array|null */ public function getTablesFull($pdo) { $tables_names = $this->getTables($pdo); if (is_null($tables_names)) { return null; } $tables = []; foreach ($tables_names as $table_name) { $columns = $this->getColumns($pdo, $table_name); if (is_null($columns)) { return null; } $tables[$table_name] = $columns; } return $tables; } /** * @param $pdo \PDO * @return string[]|null */ public function getTables($pdo) { $tablesQuery = $pdo->prepare("SELECT table_name AS name FROM information_schema.tables WHERE table_type = 'BASE TABLE' AND table_schema NOT IN ('pg_catalog', 'information_schema')"); if ($tablesQuery->execute(array())) { $tables = $tablesQuery->fetchAll(); $tables = array_map(function($table) { return $table["name"]; }, $tables); return $tables; } else return null; } /** * @param $pdo \PDO * @param $table_name string * @return array|null */ public function getColumns($pdo, $table_name) { $columnsQuery = $pdo->prepare("SELECT column_name as name, data_type as data_type, is_nullable as nullable FROM information_schema.columns WHERE table_name = :table_name"); if ($columnsQuery->execute(array(":table_name" => $table_name))) { $columns = $columnsQuery->fetchAll(); return $columns; } else return null; } /** * @param \PDO $pdo * @return array|null */ public function getStoredProceduresFull($pdo) { $sps = $this->getStoredProcedures($pdo); if (is_null($sps)) { return null; } foreach ($sps as $sp_name => $sp) { $args = $this->getStoredProceduresArguments($pdo, $sp); if (is_null($args)) { return null; } $sps[$sp_name]["args"] = $args; } return $sps; } /** * @param $pdo \PDO * @return array|null */ public function getStoredProcedures($pdo) { $spQuery = $pdo->prepare("SELECT r.routine_name AS sp_name, r.data_type AS data_type, proc.proretset AS proretset, proc.prosrc AS prosrc FROM information_schema.routines r LEFT JOIN pg_catalog.pg_proc proc ON proc.proname = r.routine_name WHERE r.specific_schema='public'"); if ($spQuery->execute()) { $sps = $spQuery->fetchAll(); $data = []; foreach ($sps as $sp) { $data[$sp["sp_name"]] = ["sp" => $sp]; } return $data; } else return null; } /** * @param $pdo \PDO * @param $sp string * @return array|null */ public function getStoredProceduresArguments($pdo, $sp) { $sp_name = $sp["sp"]["sp_name"]; $spQuery = $pdo->prepare("SELECT parameters.parameter_name as name, parameters.data_type, parameters.parameter_mode FROM information_schema.routines JOIN information_schema.parameters ON routines.specific_name=parameters.specific_name WHERE routines.specific_schema='public' AND routines.routine_name = :sp_name ORDER BY parameters.ordinal_position;"); if ($spQuery->execute(array("sp_name" => $sp_name))) { $sp_ = $spQuery->fetchAll(); $sps = array("in" => array(), "out" => array()); foreach ($sp_ as $p) $sps[strtolower($p["parameter_mode"])][] = $p; $out_count = count($sps['out']); if ($out_count == 0) { $sps['out'][] = array( "name" => $sp_name, "data_type" => $sp["data_type"], "parameter_mode" => "OUT" ); $out_count = 1; } return $sps; } else return null; } }