소스 검색

pgsql dataaccess

develop
Robin Thoni 8 년 전
부모
커밋
dcfea8e015
5개의 변경된 파일323개의 추가작업 그리고 14개의 파일을 삭제
  1. 31
    0
      src/Utils/DataAccess/AbstractDbDataAccess.php
  2. 8
    0
      src/Utils/DataAccess/LuDataAccess.php
  3. 264
    0
      src/Utils/DataAccess/PgSqlDataAccess.php
  4. 1
    1
      src/Utils/Dbo/LuDbo.php
  5. 19
    13
      tests/DatabaseTest.php

+ 31
- 0
src/Utils/DataAccess/AbstractDbDataAccess.php 파일 보기

@@ -0,0 +1,31 @@
1
+<?php
2
+
3
+/**
4
+ * Created by PhpStorm.
5
+ * User: robin
6
+ * Date: 6/10/16
7
+ * Time: 1:12 PM
8
+ */
9
+
10
+namespace Luticate\Utils\DataAccess;
11
+
12
+abstract class AbstractDbDataAccess
13
+{
14
+    /**
15
+     * @param $sqlType string
16
+     * @return string
17
+     */
18
+    abstract public function sqlTypeToPhpType($sqlType);
19
+
20
+    /**
21
+     * @param $pdo \PDO
22
+     * @return array
23
+     */
24
+    abstract public function getTablesFull($pdo);
25
+
26
+    /**
27
+     * @param $pdo \PDO
28
+     * @return array
29
+     */
30
+    abstract public function getStoredProceduresFull($pdo);
31
+}

+ 8
- 0
src/Utils/DataAccess/LuDataAccess.php 파일 보기

@@ -22,6 +22,14 @@ abstract class LuDataAccess {
22 22
         return static::getConnection()->transaction($function);
23 23
     }
24 24
 
25
+    /**
26
+     * @return \PDO
27
+     */
28
+    public static function getPdo()
29
+    {
30
+        return Capsule::connection(static::$_connection)->getPdo();
31
+    }
32
+
25 33
     /**
26 34
      * @return Connection
27 35
      */

+ 264
- 0
src/Utils/DataAccess/PgSqlDataAccess.php 파일 보기

@@ -0,0 +1,264 @@
1
+<?php
2
+
3
+namespace Luticate\Utils\DataAccess;
4
+use Luticate\Utils\Business\LuStringUtils;
5
+
6
+/**
7
+ * Created by PhpStorm.
8
+ * User: robin
9
+ * Date: 6/10/16
10
+ * Time: 11:05 AM
11
+ */
12
+class PgSqlDataAccess extends AbstractDbDataAccess
13
+{
14
+    public static $types = ["SERIAL" => "integer",
15
+        "BIGSERIAL" => "integer",
16
+        "abstime" => "datetime",
17
+        "aclitem" => "",
18
+        "any" => "",
19
+        "anyarray" => "",
20
+        "anyelement" => "",
21
+        "anyenum" => "",
22
+        "anynonarray" => "",
23
+        "anyrange" => "",
24
+        "bigint" => "integer",
25
+        "bit" => "",
26
+        "bit varying" => "",
27
+        "boolean" => "boolean",
28
+        "box" => "",
29
+        "bytea" => "string",
30
+        "char" => "string",
31
+        "character" => "string",
32
+        "character varying" => "string",
33
+        "cid" => "",
34
+        "cidr" => "",
35
+        "circle" => "",
36
+        "cstring" => "",
37
+        "date" => "datetime",
38
+        "daterange" => "",
39
+        "decimal" => "float",
40
+        "double precision" => "float",
41
+        "event_trigger" => "",
42
+        "fdw_handler" => "",
43
+        "gtsvector" => "",
44
+        "inet" => "",
45
+        "information_schema.cardinal_number" => "",
46
+        "information_schema.character_data" => "",
47
+        "information_schema.sql_identifier" => "",
48
+        "information_schema.time_stamp" => "",
49
+        "information_schema.yes_or_no" => "",
50
+        "int2vector" => "",
51
+        "int4range" => "",
52
+        "int8range" => "",
53
+        "integer" => "integer",
54
+        "internal" => "",
55
+        "interval" => "",
56
+        "json" => "",
57
+        "jsonb" => "",
58
+        "language_handler" => "",
59
+        "line" => "",
60
+        "lseg" => "",
61
+        "macaddr" => "",
62
+        "money" => "string",
63
+        "name" => "",
64
+        "numeric" => "float",
65
+        "numrange" => "",
66
+        "oid" => "",
67
+        "oidvector" => "",
68
+        "opaque" => "",
69
+        "path" => "",
70
+        "pg_lsn" => "",
71
+        "pg_node_tree" => "",
72
+        "point" => "",
73
+        "polygon" => "",
74
+        "real" => "float",
75
+        "record" => "",
76
+        "refcursor" => "",
77
+        "regclass" => "",
78
+        "regconfig" => "",
79
+        "regdictionary" => "",
80
+        "regoper" => "",
81
+        "regoperator" => "",
82
+        "regproc" => "",
83
+        "regprocedure" => "",
84
+        "regtype" => "",
85
+        "reltime" => "",
86
+        "smallint" => "integer",
87
+        "smallserial" => "integer",
88
+        "smgr" => "",
89
+        "text" => "string",
90
+        "tid" => "",
91
+        "timestamp without time zone" => "datetime",
92
+        "timestamp with time zone" => "datetime",
93
+        "time without time zone" => "datetime",
94
+        "time with time zone" => "datetime",
95
+        "tinterval" => "",
96
+        "trigger" => "",
97
+        "tsquery" => "",
98
+        "tsrange" => "",
99
+        "tstzrange" => "",
100
+        "tsvector" => "",
101
+        "txid_snapshot" => "",
102
+        "unknown" => "",
103
+        "uuid" => "",
104
+        "void" => "",
105
+        "xid" => "",
106
+        "xml" => ""];
107
+
108
+    /**
109
+     * @param string $sqlType
110
+     * @return string
111
+     */
112
+    public function sqlTypeToPhpType($sqlType)
113
+    {
114
+        $isArray = false;
115
+        if (LuStringUtils::endsWith($sqlType, "[]")) {
116
+            $isArray = true;
117
+            $sqlType = substr($sqlType, 0, strlen($sqlType) - 2);
118
+        }
119
+        if (!isset(static::$types) || static::$types[$sqlType] == "") {
120
+            return "string";
121
+        }
122
+        return static::$types[$sqlType] . ($isArray ? "[]" : "");
123
+    }
124
+
125
+    /**
126
+     * @param \PDO $pdo
127
+     * @return array|null
128
+     */
129
+    public function getTablesFull($pdo)
130
+    {
131
+        $tables_names = $this->getTables($pdo);
132
+        if (is_null($tables_names)) {
133
+            return null;
134
+        }
135
+
136
+        $tables = [];
137
+        foreach ($tables_names as $table_name) {
138
+            $columns = $this->getColumns($pdo, $table_name);
139
+            if (is_null($columns)) {
140
+                return null;
141
+            }
142
+            $tables[$table_name] = $columns;
143
+        }
144
+
145
+        return $tables;
146
+    }
147
+
148
+    /**
149
+     * @param $pdo \PDO
150
+     * @return string[]|null
151
+     */
152
+    public function getTables($pdo)
153
+    {
154
+        $tablesQuery = $pdo->prepare("SELECT table_name AS name
155
+FROM information_schema.tables
156
+WHERE table_type = 'BASE TABLE' AND table_schema NOT IN ('pg_catalog', 'information_schema')");
157
+        if ($tablesQuery->execute(array())) {
158
+            $tables = $tablesQuery->fetchAll();
159
+            $tables = array_map(function($table)
160
+            {
161
+                return $table["name"];
162
+            }, $tables);
163
+            return $tables;
164
+        }
165
+        else
166
+            return null;
167
+    }
168
+
169
+    /**
170
+     * @param $pdo \PDO
171
+     * @param $table_name string
172
+     * @return array|null
173
+     */
174
+    public function getColumns($pdo, $table_name)
175
+    {
176
+        $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");
177
+        if ($columnsQuery->execute(array(":table_name" => $table_name)))
178
+        {
179
+            $columns = $columnsQuery->fetchAll();
180
+            return $columns;
181
+        }
182
+        else
183
+            return null;
184
+    }
185
+
186
+    /**
187
+     * @param \PDO $pdo
188
+     * @return array|null
189
+     */
190
+    public function getStoredProceduresFull($pdo)
191
+    {
192
+        $sps = $this->getStoredProcedures($pdo);
193
+        if (is_null($sps)) {
194
+            return null;
195
+        }
196
+
197
+        foreach ($sps as $sp_name => $sp) {
198
+            $args = $this->getStoredProceduresArguments($pdo, $sp_name);
199
+            if (is_null($args)) {
200
+                return null;
201
+            }
202
+            $sps[$sp_name]["args"] = $args;
203
+        }
204
+        return $sps;
205
+    }
206
+
207
+    /**
208
+     * @param $pdo \PDO
209
+     * @return array|null
210
+     */
211
+    public function getStoredProcedures($pdo)
212
+    {
213
+        $spQuery = $pdo->prepare("SELECT r.routine_name AS sp_name, r.data_type AS data_type, proc.proretset AS proretset, proc.prosrc AS prosrc
214
+FROM information_schema.routines r
215
+LEFT JOIN pg_catalog.pg_proc proc ON proc.proname = r.routine_name
216
+WHERE r.specific_schema='public'");
217
+        if ($spQuery->execute())
218
+        {
219
+            $sps = $spQuery->fetchAll();
220
+            $data = [];
221
+            foreach ($sps as $sp) {
222
+                $data[$sp["name"]] = ["sp" => $sp];
223
+            }
224
+            return $data;
225
+        }
226
+        else
227
+            return null;
228
+    }
229
+
230
+    /**
231
+     * @param $pdo \PDO
232
+     * @param $sp string
233
+     * @return array|null
234
+     */
235
+    public function getStoredProceduresArguments($pdo, $sp)
236
+    {
237
+        $sp_name = $sp["sp_name"];
238
+        $spQuery = $pdo->prepare("SELECT parameters.parameter_name as name, parameters.data_type, parameters.parameter_mode
239
+FROM information_schema.routines
240
+JOIN information_schema.parameters ON routines.specific_name=parameters.specific_name
241
+WHERE routines.specific_schema='public' AND routines.routine_name = :sp_name
242
+ORDER BY parameters.ordinal_position;");
243
+        if ($spQuery->execute(array("sp_name" => $sp_name)))
244
+        {
245
+            $sp_ = $spQuery->fetchAll();
246
+            $sps = array("in" => array(), "out" => array());
247
+            foreach ($sp_ as $p)
248
+                $sps[strtolower($p["parameter_mode"])][] = $p;
249
+            $out_count = count($sps['out']);
250
+            if ($out_count == 0)
251
+            {
252
+                $sps['out'][] = array(
253
+                    "name" => $sp_name,
254
+                    "data_type" => $sp["data_type"],
255
+                    "parameter_mode" => "OUT"
256
+                );
257
+                $out_count = 1;
258
+            }
259
+            return $sps;
260
+        }
261
+        else
262
+            return null;
263
+    }
264
+}

+ 1
- 1
src/Utils/Dbo/LuDbo.php 파일 보기

@@ -85,7 +85,7 @@ abstract class LuDbo implements \JsonSerializable {
85 85
             $dbo->checkConstraints($constraints);
86 86
             return $dbo->getInt();
87 87
         }
88
-        else if ($type == "float") {
88
+        else if ($type == "float" || $type == "double") {
89 89
             $dbo = LuFloatDbo::jsonDeserialize($value);
90 90
             $dbo->checkConstraints($constraints);
91 91
             return $dbo->getFloat();

+ 19
- 13
tests/DatabaseTest.php 파일 보기

@@ -2,6 +2,7 @@
2 2
 use Luticate\Utils\Controller\LuticateApplication;
3 3
 use Illuminate\Database\Capsule\Manager as Capsule;
4 4
 use Luticate\Utils\DataAccess\LuDataAccess;
5
+use Luticate\Utils\DataAccess\PgSqlDataAccess;
5 6
 use Luticate\Utils\Dbo\LuDbo;
6 7
 use Luticate\Utils\Dbo\LuDboDeserializeException;
7 8
 
@@ -103,7 +104,7 @@ class TestTableDboArray extends LuDbo
103 104
         if (!is_array($json)) {
104 105
             throw new LuDboDeserializeException("Invalid array value");
105 106
         }
106
-        $dbo = new self();
107
+        $dbo = new static();
107 108
         $array = [];
108 109
         foreach ($json as $data) {
109 110
             $array[] = TestTableDbo::jsonDeserialize($data);
@@ -134,18 +135,18 @@ class DatabaseTest extends \PHPUnit_Framework_TestCase
134 135
 {
135 136
     public function testSetup()
136 137
     {
137
-//        $config = ['databases' => [
138
-//            [
139
-//                'name'      => 'mydb',
140
-//                'driver'    => 'pgsql',
141
-//                'host'      => '172.17.0.1',
142
-//                'database'  => 'luticate2',
143
-//                'username'  => 'dev',
144
-//                'password'  => 'dev'
145
-//            ]
146
-//        ]];
147
-//        $app = new LuticateApplication($config);
148
-//        $app->setupDatabases();
138
+        $config = ['databases' => [
139
+            [
140
+                'name'      => 'mydb',
141
+                'driver'    => 'pgsql',
142
+                'host'      => '172.17.0.1',
143
+                'database'  => 'luticate2',
144
+                'username'  => 'dev',
145
+                'password'  => 'dev'
146
+            ]
147
+        ]];
148
+        $app = new LuticateApplication($config);
149
+        $app->setupDatabases();
149 150
         
150 151
 //        $dbo = new TestTableDbo();
151 152
 //        $dbo->setId(11);
@@ -164,5 +165,10 @@ class DatabaseTest extends \PHPUnit_Framework_TestCase
164 165
 //        {
165 166
 //            return $q->where("some_text", "like", "lol%");
166 167
 //        }, ['Id']));
168
+
169
+
170
+        $pdo = TestTableDataAccess::getPdo();
171
+        $pgsql = new PgSqlDataAccess();
172
+        var_dump($pgsql->getTablesFull($pdo));
167 173
     }
168 174
 }

Loading…
취소
저장