|
@@ -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
|
}
|