123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- <?php
-
- namespace Luticate\Utils\DataAccess;
- use Luticate\Utils\Business\LuStringUtils;
-
- /**
- * Created by PhpStorm.
- * User: robin
- * Date: 6/10/16
- * Time: 11:05 AM
- */
- class PgSqlDataAccess extends AbstractDbDataAccess
- {
- public static $types = ["SERIAL" => "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 "mixed";
- }
- 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) {
- $type = strtolower($p["parameter_mode"]);
- if (empty($p["name"])) {
- $p["name"] = "arg" . count($sps[$type]);
- }
- $sps[$type][] = $p;
- }
- $out_count = count($sps['out']);
- if ($out_count == 0)
- {
- $sps['out'][] = array(
- "name" => empty($sp_name) ? "arg" . count($sps['out']) : $sp_name,
- "data_type" => $sp["data_type"],
- "parameter_mode" => "OUT"
- );
- $out_count = 1;
- }
- return $sps;
- }
- else
- return null;
- }
- }
|