|
@@ -8,6 +8,7 @@ use Illuminate\Database\Query\Builder;
|
8
|
8
|
use Luticate\Utils\Business\LuArrayUtils;
|
9
|
9
|
use Luticate\Utils\Business\LuStringUtils;
|
10
|
10
|
use Luticate\Utils\Dbo\LuDbo;
|
|
11
|
+use Luticate\Utils\Dbo\LuPaginatedDbo;
|
11
|
12
|
|
12
|
13
|
abstract class LuDataAccess {
|
13
|
14
|
|
|
@@ -72,6 +73,9 @@ abstract class LuDataAccess {
|
72
|
73
|
if (is_callable($query)) {
|
73
|
74
|
$query = $query(static::getTableAs());
|
74
|
75
|
}
|
|
76
|
+ else if (is_null($query)) {
|
|
77
|
+ return static::getTableAs();
|
|
78
|
+ }
|
75
|
79
|
return $query;
|
76
|
80
|
}
|
77
|
81
|
|
|
@@ -89,6 +93,16 @@ abstract class LuDataAccess {
|
89
|
93
|
$json = LuArrayUtils::camelCaseToSnake($json);
|
90
|
94
|
return $json;
|
91
|
95
|
}
|
|
96
|
+
|
|
97
|
+ public static function getData($query)
|
|
98
|
+ {
|
|
99
|
+ $query = static::resolveQuery($query);
|
|
100
|
+ $sql = $query->toSql();
|
|
101
|
+ $string = static::getConnection()->select("WITH query AS (${sql}) SELECT COALESCE(json_agg(data), '[]'::json) AS data FROM query data", $query->getBindings())[0]->data;
|
|
102
|
+
|
|
103
|
+ $data = LuStringUtils::convertJsonString($string);
|
|
104
|
+ return $data;
|
|
105
|
+ }
|
92
|
106
|
|
93
|
107
|
/**
|
94
|
108
|
* @param $query Builder|callable
|
|
@@ -96,12 +110,7 @@ abstract class LuDataAccess {
|
96
|
110
|
*/
|
97
|
111
|
public static function getMultiple($query)
|
98
|
112
|
{
|
99
|
|
- $query = static::resolveQuery($query);
|
100
|
|
- $string = $query->aggregate("json_agg", [static::$_table_as]);
|
101
|
|
- if (is_null($string)) {
|
102
|
|
- return [];
|
103
|
|
- }
|
104
|
|
- $data = LuStringUtils::convertJsonString($string);
|
|
113
|
+ $data = static::getData($query);
|
105
|
114
|
|
106
|
115
|
return LuDbo::deserializeValue($data, static::$_dboClass . "[]");
|
107
|
116
|
}
|
|
@@ -112,18 +121,24 @@ abstract class LuDataAccess {
|
112
|
121
|
*/
|
113
|
122
|
public static function getSingle($query)
|
114
|
123
|
{
|
115
|
|
- $query = static::resolveQuery($query);
|
116
|
|
- $string = $query->take(1)->aggregate("json_agg", [static::$_table_as]);
|
117
|
|
- if (is_null($string)) {
|
118
|
|
- return null;
|
119
|
|
- }
|
120
|
|
- $data = LuStringUtils::convertJsonString($string);
|
|
124
|
+ $data = static::getData($query);
|
|
125
|
+
|
121
|
126
|
if (count($data) < 1) {
|
122
|
127
|
return null;
|
123
|
128
|
}
|
124
|
129
|
|
125
|
130
|
return LuDbo::deserializeValue($data[0], static::$_dboClass);
|
126
|
131
|
}
|
|
132
|
+
|
|
133
|
+ public static function getMultiplePaginated($query, $page, $perPage)
|
|
134
|
+ {
|
|
135
|
+ $query = static::resolveQuery($query);
|
|
136
|
+ $sql = $query->toSql();
|
|
137
|
+ $count = static::getConnection()->select("WITH query AS (${sql}) SELECT count(data) AS data FROM query data", $query->getBindings())[0]->data;
|
|
138
|
+ $query->take($perPage)->offset($page * $perPage);
|
|
139
|
+ $data = self::getMultiple($query);
|
|
140
|
+ return new LuPaginatedDbo($count, $data);
|
|
141
|
+ }
|
127
|
142
|
|
128
|
143
|
/**
|
129
|
144
|
* @param $id
|