-
-
Notifications
You must be signed in to change notification settings - Fork 38
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
Grammar for VALUES/ROWs #51
Comments
Hi @rudiedirkx, |
What Laravel version are you using? |
Laravel 10.6.0 I used to do this in SQLite (I don't remember why), but its syntax is different (no |
I'm using this in rdx/laravel-aggregate-relationships:MultiColumnHasMany.php currently. Usage: // protected $fillable = ['a_id', 'b_id'];
// ...
function similar_records() {
return $this->multiColumnHasMany(self::class, ['a_id' => 'a_id', 'b_id' => 'b_id']);
}
|
Do you know about the improved database expressions in Laravel 10? laravel/framework#44784 They are great for your use case: <?php
namespace App\Expressions;
use Illuminate\Contracts\Database\Query\Expression;
use Illuminate\Database\Grammar;
use Illuminate\Database\Query\Grammars\MySqlGrammar;
class Values implements Expression
{
public function __construct(
protected array $rows
) {
//
}
public function getValue(Grammar $grammar): string
{
switch (true) {
case $grammar instanceof MySqlGrammar:
$rows = [];
foreach ($this->rows as $row) {
$values = array_map(
fn ($value) => $grammar->escape($value),
$row
);
$rows[] = 'ROW(' . implode(', ', $values) . ')';
}
return 'VALUES ' . implode(', ', $rows);
// TODO
}
}
}
DB::table('ids')
->withExpression(
'ids',
(new Values([[1, 2], [3, 4]]))->getValue(DB::connection()->getQueryGrammar())
)->get(), If DB::table('ids')
->withExpression(
'ids',
new Values([[1, 2], [3, 4]])
)->get(), The PR's author also collects these classes in a package where you could propose a |
But but but this package already has CTE Grammar. VALUES/ROW seem very CTE grammar to me. |
IMO, this is a very niche use case and so I don't see it as part of the package. |
I use this package to load a bunch of specific records by multi column combinations instead of a query:
But I had to type the
VALUES
andROW
s myself. VALUES might be standard SQL, butROW()
is db specific. MySQL wants it, SQLite doesn't, that's all I know.Seems like a job for grammar? Maybe including the VALUES, or maybe not, or maybe even per ROW.
(I don't even know how to call grammar from my custom relationship, but I could probably find out 😄)
The text was updated successfully, but these errors were encountered: