Robin Thoni 8 лет назад
Родитель
Сommit
10c1a2ca14
2 измененных файлов: 142 добавлений и 12 удалений
  1. 91
    0
      src/Utils/DataAccess/LuStoredProcedure.php
  2. 51
    12
      tests/DatabaseTest.php

+ 91
- 0
src/Utils/DataAccess/LuStoredProcedure.php Просмотреть файл

@@ -9,13 +9,17 @@
9 9
 namespace Luticate\Utils\DataAccess;
10 10
 
11 11
 use Illuminate\Database\Connection;
12
+use Luticate\Utils\Business\LuStringUtils;
12 13
 use Luticate\Utils\Dbo\LuDbo;
13 14
 use Illuminate\Database\Capsule\Manager as Capsule;
15
+use Luticate\Utils\Dbo\LuMultipleDbo;
14 16
 
15 17
 abstract class LuStoredProcedure
16 18
 {
17 19
     protected static $_connection = "mydb";
20
+    protected static $_storedProcedure = "mydb_table";
18 21
     protected static $_dboClass = LuDbo::class;
22
+    protected static $_dboArrayClass = LuDbo::class;
19 23
 
20 24
     /**
21 25
      * @return Connection
@@ -24,4 +28,91 @@ abstract class LuStoredProcedure
24 28
     {
25 29
         return Capsule::connection(static::$_connection);
26 30
     }
31
+
32
+    public static function getArgsArray($args)
33
+    {
34
+        $argsArray = [];
35
+        for ($i = 0; $i < count($args); ++$i) {
36
+            $argsArray[":_arg" . $i] = $args[$i];
37
+        }
38
+        return $argsArray;
39
+    }
40
+
41
+    public static function getSqlSelectQuery($args)
42
+    {
43
+        $storedProcedure = static::$_storedProcedure;
44
+        $argsString = implode(", ", array_map(function($i)
45
+        {
46
+            return ":_arg" . $i;
47
+        }, range(0, count($args) - 1)));
48
+        $query = "SELECT COALESCE(json_agg(data), '[]'::json) as data FROM ${storedProcedure}(${argsString}) data";
49
+        return $query;
50
+    }
51
+
52
+    /**
53
+     * @param $args array
54
+     * @return LuDbo[]
55
+     */
56
+    public static function getMultiple($args)
57
+    {
58
+        $argsArray = static::getArgsArray($args);
59
+        $query = static::getSqlSelectQuery($args);
60
+        $values = static::getConnection()->select($query, $argsArray);
61
+
62
+        $value = $values[0]->data;
63
+        $json = LuStringUtils::convertJsonString($value);
64
+        $dboValues = call_user_func([static::$_dboArrayClass, 'jsonDeserialize'], $json)->getArray();
65
+        return $dboValues;
66
+    }
67
+
68
+    /**
69
+     * @param $args array
70
+     * @return LuDbo|null
71
+     */
72
+    public static function getSingle($args)
73
+    {
74
+        $argsArray = static::getArgsArray($args);
75
+        $query = static::getSqlSelectQuery($args);
76
+        $values = static::getConnection()->select($query, $argsArray);
77
+
78
+        $value = $values[0]->data;
79
+        $json = LuStringUtils::convertJsonString($value);
80
+        if (count($json) < 1 || is_null($json[0])) {
81
+            return null;
82
+        }
83
+        $dboValue = call_user_func([static::$_dboArrayClass, 'jsonDeserialize'], $json)->getArray()[0];
84
+        return $dboValue;
85
+    }
86
+
87
+    /**
88
+     * @param $args array
89
+     * @param $page int The page number, 0 based
90
+     * @param $perPage int The number of items per page
91
+     * @return LuMultipleDbo
92
+     */
93
+    public static function getMultiplePaginated($args, $page, $perPage)
94
+    {
95
+        $argsArray = static::getArgsArray($args);
96
+        $argsArray[":_page"] = $page;
97
+        $argsArray[":_perPage"] = $perPage;
98
+
99
+        $storedProcedure = static::$_storedProcedure;
100
+        $argsString = implode(", ", array_map(function($i)
101
+        {
102
+            return ":_arg" . $i;
103
+        }, range(0, count($args) - 1)));
104
+
105
+        $query = "SELECT (SELECT count(*) FROM ${storedProcedure}(${argsString})) as count,
106
+          (SELECT COALESCE(json_agg(data), '[]'::json) FROM (SELECT data FROM ${storedProcedure}(${argsString}) data OFFSET (:_page::int * :_perPage::int) LIMIT :_perPage) data) as data";
107
+        $values = static::getConnection()->select($query, $argsArray);
108
+
109
+        $value = $values[0];
110
+        if (is_null($value->data))
111
+        {
112
+            $value->data = '[]';
113
+        }
114
+        $json = LuStringUtils::convertJsonString($value->data);
115
+        $dboValues = call_user_func([static::$_dboArrayClass, 'jsonDeserialize'], $json)->getArray();
116
+        return new LuMultipleDbo($value->count, $dboValues);
117
+    }
27 118
 }

+ 51
- 12
tests/DatabaseTest.php Просмотреть файл

@@ -4,9 +4,12 @@ use Illuminate\Database\Query\Builder;
4 4
 use Luticate\Utils\Controller\LuticateApplication;
5 5
 use Illuminate\Database\Capsule\Manager as Capsule;
6 6
 use Luticate\Utils\DataAccess\LuDataAccess;
7
+use Luticate\Utils\DataAccess\LuStoredProcedure;
7 8
 use Luticate\Utils\DataAccess\PgSqlDataAccess;
8 9
 use Luticate\Utils\Dbo\LuDbo;
9 10
 use Luticate\Utils\Dbo\LuDboDeserializeException;
11
+use Luticate\Utils\Dbo\LuIntDbo;
12
+use Luticate\Utils\Dbo\LuIntDboArray;
10 13
 
11 14
 /**
12 15
  * Created by PhpStorm.
@@ -147,6 +150,36 @@ class TestTableDboArray extends LuDbo
147 150
 
148 151
 }
149 152
 
153
+class SpTest extends LuStoredProcedure {
154
+
155
+    protected static $_connection = "mydb";
156
+    protected static $_storedProcedure = "sp_test";
157
+    protected static $_dboClass = LuIntDbo::class;
158
+    protected static $_dboArrayClass = LuIntDboArray::class;
159
+
160
+    public static function execute($an_int) {
161
+        return self::getSingle([$an_int]);
162
+    }
163
+
164
+}
165
+
166
+class SpTest2 extends LuStoredProcedure {
167
+
168
+    protected static $_connection = "mydb";
169
+    protected static $_storedProcedure = "sp_test2";
170
+    protected static $_dboClass = LuIntDbo::class;
171
+    protected static $_dboArrayClass = LuIntDboArray::class;
172
+
173
+    public static function execute($an_int) {
174
+        return self::getMultiple([$an_int]);
175
+    }
176
+
177
+    public static function executePaginated($an_int, $page, $perPage) {
178
+        return self::getMultiplePaginated([$an_int], $page, $perPage);
179
+    }
180
+
181
+}
182
+
150 183
 class TestTableDataAccess extends LuDataAccess
151 184
 {
152 185
     protected static $_connection = "mydb";
@@ -159,18 +192,24 @@ class DatabaseTest extends \PHPUnit_Framework_TestCase
159 192
 {
160 193
     public function testSetup()
161 194
     {
162
-//        $config = ['databases' => [
163
-//            [
164
-//                'name'      => 'mydb',
165
-//                'driver'    => 'pgsql',
166
-//                'host'      => '172.17.0.1',
167
-//                'database'  => 'luticate2',
168
-//                'username'  => 'dev',
169
-//                'password'  => 'dev'
170
-//            ]
171
-//        ]];
172
-//        $app = new LuticateApplication($config);
173
-//        $app->setupDatabases();
195
+        $config = ['databases' => [
196
+            [
197
+                'name'      => 'mydb',
198
+                'driver'    => 'pgsql',
199
+                'host'      => '172.17.0.1',
200
+                'database'  => 'luticate2',
201
+                'username'  => 'dev',
202
+                'password'  => 'dev'
203
+            ]
204
+        ]];
205
+        $app = new LuticateApplication($config);
206
+        $app->setupDatabases();
207
+
208
+        var_dump(SpTest::execute(42));
209
+        var_dump(SpTest::execute(4));
210
+        var_dump(SpTest2::execute(42));
211
+        var_dump(SpTest2::executePaginated(42, 0, 2));
212
+        var_dump(SpTest2::executePaginated(42, 1, 2));
174 213
 //
175 214
 //        $dbo = new TestTableDbo();
176 215
 //        $dbo->setId(11);

Загрузка…
Отмена
Сохранить