Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propose fix for PostgresQL syntax error #109

Merged
merged 1 commit into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/api/TotalCircleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public function handle(NovaRequest $request)
$calculation = isset($request->options) ? json_decode($request->options, true)['sum'] ?? 1 : 1;
$request->validate(['model' => ['bail', 'required', 'min:1', 'string']]);
$model = $request->input('model');
$modelInstance = new $model;
$tableName = $modelInstance->getConnection()->getTablePrefix() . $modelInstance->getTable();
$xAxisColumn = $request->input('col_xaxis') ?? DB::raw($tableName.'.created_at');
$modelInstance = new $model;
$tableName = $modelInstance->getConnection()->getTablePrefix() . $modelInstance->getTable();
$xAxisColumn = $request->input('col_xaxis') ?? DB::raw($tableName.'.created_at');
$cacheKey = hash('md4', $model . (int)(bool)$request->input('expires'));
$dataSet = Cache::get($cacheKey);
if (!$dataSet) {
Expand All @@ -45,17 +45,17 @@ public function handle(NovaRequest $request)
$filter = $seriesData->filter;
$labelList[$seriesKey] = $seriesData->label;
if(empty($filter->value)&&isset($filter->operator)&&($filter->operator=='IS NULL' || $filter->operator=='IS NOT NULL')) {
$seriesSql .= ", SUM(CASE WHEN ".$filter->key." ".$filter->operator." then ".$calculation." else 0 end) as '".$labelList[$seriesKey]."'";
$seriesSql .= ", SUM(CASE WHEN ".$filter->key." ".$filter->operator." then ".$calculation." else 0 end) as \"".$labelList[$seriesKey]."\"";
} else if(empty($filter->value)){
$seriesSql .= ", SUM(CASE WHEN ";
$countFilter = count($filter);
foreach($filter as $keyFilter => $listFilter){
$seriesSql .= " ".$listFilter->key." ".($listFilter->operator ?? "=")." '".$listFilter->value."' ";
$seriesSql .= $countFilter-1 != $keyFilter ? " AND " : "";
}
$seriesSql .= "then ".$calculation." else 0 end) as '".$labelList[$seriesKey]."'";
$seriesSql .= "then ".$calculation." else 0 end) as \"".$labelList[$seriesKey]."\"";
} else {
$seriesSql .= ", SUM(CASE WHEN ".$filter->key." ".($filter->operator ?? "=")." '".$filter->value."' then ".$calculation." else 0 end) as '".$labelList[$seriesKey]."'";
$seriesSql .= ", SUM(CASE WHEN ".$filter->key." ".($filter->operator ?? "=")." '".$filter->value."' then ".$calculation." else 0 end) as \"".$labelList[$seriesKey]."\"";
}
}
}
Expand All @@ -66,7 +66,7 @@ public function handle(NovaRequest $request)
} else {
$query = $model::selectRaw('SUM('.$calculation.') counted'.$seriesSql);
}

if(is_numeric($advanceFilterSelected)){
$query->where($xAxisColumn, '>=', Carbon::now()->subDays($advanceFilterSelected));
}
Expand All @@ -82,7 +82,7 @@ public function handle(NovaRequest $request)
else if($dataForLast != '*') {
$query->where($xAxisColumn, '>=', Carbon::now()->firstOfMonth()->subMonth($dataForLast-1));
}

if(isset(json_decode($request->options, true)['queryFilter'])){
$queryFilter = json_decode($request->options, true)['queryFilter'];
foreach($queryFilter as $qF){
Expand Down Expand Up @@ -134,6 +134,6 @@ public function handle(NovaRequest $request)
'xAxis' => $xAxis,
'yAxis' => $yAxis
]
]);
]);
}
}
}
18 changes: 9 additions & 9 deletions src/api/TotalRecordsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ public function handle(NovaRequest $request)
$filter = $seriesData->filter;
$labelList[$seriesKey] = $seriesData->label;
if(empty($filter->value)&&isset($filter->operator)&&($filter->operator=='IS NULL' || $filter->operator=='IS NOT NULL')) {
$seriesSql .= ", SUM(CASE WHEN ".$filter->key." ".$filter->operator." then ".$calculation." else 0 end) as '".$labelList[$seriesKey]."'";
$seriesSql .= ", SUM(CASE WHEN ".$filter->key." ".$filter->operator." then ".$calculation." else 0 end) as \"".$labelList[$seriesKey]."\"";
} else if(empty($filter->value)){
$seriesSql .= ", SUM(CASE WHEN ";
$countFilter = count($filter);
foreach($filter as $keyFilter => $listFilter){
$seriesSql .= " ".$listFilter->key." ".($listFilter->operator ?? "=")." '".$listFilter->value."' ";
$seriesSql .= $countFilter-1 != $keyFilter ? " AND " : "";
}
$seriesSql .= "then ".$calculation." else 0 end) as '".$labelList[$seriesKey]."'";
$seriesSql .= "then ".$calculation." else 0 end) as \"".$labelList[$seriesKey]."\"";
} else {
$seriesSql .= ", SUM(CASE WHEN ".$filter->key." ".($filter->operator ?? "=")." '".$filter->value."' then ".$calculation." else 0 end) as '".$labelList[$seriesKey]."'";
$seriesSql .= ", SUM(CASE WHEN ".$filter->key." ".($filter->operator ?? "=")." '".$filter->value."' then ".$calculation." else 0 end) as \"".$labelList[$seriesKey]."\"";
}
}
}
Expand All @@ -75,7 +75,7 @@ public function handle(NovaRequest $request)
} else {
$query = $model::selectRaw('DATE('.$xAxisColumn.') AS cat, DATE('.$xAxisColumn.') AS catorder, sum('.$calculation.') counted'.$seriesSql);
}

if(is_numeric($advanceFilterSelected)){
$query->where($xAxisColumn, '>=', Carbon::now()->subDays($advanceFilterSelected));
}
Expand Down Expand Up @@ -110,7 +110,7 @@ public function handle(NovaRequest $request)
$query = $model::selectRaw('YEARWEEK('.$xAxisColumn.', '.$startWeek.') AS cat, YEARWEEK('.$xAxisColumn.', '.$startWeek.') AS catorder, sum('.$calculation.') counted'.$seriesSql);
}
}

if(is_numeric($advanceFilterSelected)){
$query->where($xAxisColumn, '>=', Carbon::now()->subDays($advanceFilterSelected));
}
Expand Down Expand Up @@ -172,7 +172,7 @@ public function handle(NovaRequest $request)
$query = $model::selectRaw('DATE_FORMAT('.$xAxisColumn.', "%b %Y") AS cat, DATE_FORMAT('.$xAxisColumn.', "%Y-%m") AS catorder, sum('.$calculation.') counted'.$seriesSql);
}
}

if(is_numeric($advanceFilterSelected)){
$query->where($xAxisColumn, '>=', Carbon::now()->subDays($advanceFilterSelected));
}
Expand All @@ -191,7 +191,7 @@ public function handle(NovaRequest $request)
$query->groupBy('catorder', 'cat')
->orderBy('catorder', 'asc');
}

if(isset(json_decode($request->options, true)['queryFilter'])){
$queryFilter = json_decode($request->options, true)['queryFilter'];
foreach($queryFilter as $qF){
Expand Down Expand Up @@ -264,9 +264,9 @@ public function handle(NovaRequest $request)
'xAxis' => $xAxis,
'yAxis' => $yAxis
]
]);
]);
}

private function counted($dataSet, $bgColor = "#111", $type = "bar", $label = "Total")
{
$yAxis = [
Expand Down