préfixe de colonne

This commit is contained in:
Jephté Clain 2024-07-09 11:56:28 +04:00
parent dbb67bc3bc
commit 584b7883a7
2 changed files with 10 additions and 4 deletions

View File

@ -2,6 +2,7 @@
namespace nur\sery\db\_private; namespace nur\sery\db\_private;
use nur\sery\cl; use nur\sery\cl;
use nur\sery\str;
use nur\sery\ValueException; use nur\sery\ValueException;
trait Tselect { trait Tselect {
@ -32,6 +33,8 @@ trait Tselect {
if (self::consume('(.*?)\s*(?=$|\bfrom\b)', $tmpsql, $ms)) { if (self::consume('(.*?)\s*(?=$|\bfrom\b)', $tmpsql, $ms)) {
if ($ms[1]) $usercols[] = $ms[1]; if ($ms[1]) $usercols[] = $ms[1];
} }
$colPrefix = $query["col_prefix"] ?? null;
if ($colPrefix !== null) str::add_suffix($colPrefix, ".");
$tmpcols = cl::withn($query["cols"] ?? null); $tmpcols = cl::withn($query["cols"] ?? null);
$schema = $query["schema"] ?? null; $schema = $query["schema"] ?? null;
if ($tmpcols !== null) { if ($tmpcols !== null) {
@ -41,20 +44,22 @@ trait Tselect {
if ($key === $index) { if ($key === $index) {
$index++; $index++;
$cols[] = $col; $cols[] = $col;
$usercols[] = $col; $usercols[] = "${colPrefix}$col";
} else { } else {
$cols[] = $key; $cols[] = $key;
$usercols[] = "$col as $key"; $usercols[] = "${colPrefix}$col as $key";
} }
} }
} else { } else {
$cols = null; $cols = null;
if ($schema && is_array($schema) && !in_array("*", $usercols)) { if ($schema && is_array($schema) && !in_array("*", $usercols)) {
$cols = array_keys($schema); $cols = array_keys($schema);
$usercols = array_merge($usercols, $cols); foreach ($cols as $col) {
$usercols[] = "${colPrefix}$col";
} }
} }
if (!$usercols && !$cols) $usercols = ["*"]; }
if (!$usercols && !$cols) $usercols = ["${colPrefix}*"];
$sql[] = implode(", ", $usercols); $sql[] = implode(", ", $usercols);
## from ## from

View File

@ -6,6 +6,7 @@ class _select {
"prefix" => "?string", "prefix" => "?string",
"schema" => "?array", "schema" => "?array",
"cols" => "?array", "cols" => "?array",
"col_prefix" => "?string",
"from" => "?string", "from" => "?string",
"where" => "?array", "where" => "?array",
"order by" => "?array", "order by" => "?array",