Skip to content

2x Feature request #1

@mrdamien

Description

@mrdamien

Allow method chaining with column()

Example:

$fp = fopen('php://input', 'r');
$update->table('foo')
    ->where('id =', 4)
    ->column('bar', $fp, \PDO::PARAM_LOB)
    ->columns([
        'fizz' => 'buzz'
    ])
    ->perform();
// src/Clause/ModifyColumns.php
@@ -15,10 +15,11 @@ trait ModifyColumns
     protected $columns;
 
     public function column(string $column, ...$value)
     {
         $this->columns->hold($column, ...$value);
+        return $this;
     }
 
     public function columns(array $columns)
     {
         foreach ($columns as $key => $val) {

Handle NULL in conditionals

Currently sending NULL to a WHERE like so:
$select->columns('*')->from('table')->where('cond =', null);
Generates SQL SELECT * FROM table WHERE cond = :__1__
But we want SELECT * FROM table WHERE cond IS NULL

// src/Clause/Component/Conditions.php
@@ -52,10 +52,25 @@ class Conditions extends Component
         $this->list[$key] .= $expr;
     }
 
     protected function append(string $andor, string $expr, array $bindInline) : void
     {
+        if (! empty($bindInline) && $bindInline === [null]) {
+            $nospace = preg_replace('/\s/', '', $expr);
+            if ($nospace[-1] === '=') {
+                $trims = "\t= ";
+                $cond = ' IS';
+                if ($nospace[-2] === '!') {
+                    $cond .= ' NOT';
+                    $trims .= '!';
+                }
+                $cond  .= ' NULL';
+                $expr = rtrim($expr, $trims) . $cond;
+            }
+            $bindInline = [];
+        }
+
         if (! empty($bindInline)) {
             $expr .= $this->bind->inline(...$bindInline);
         }
 
         if (empty($this->list)) {
    public function testAutoNull()
    {
        $select = $this->newQuery()->from('t1')
            ->where('t1.c3 =', NULL)
            ->columns('t1.c1', 't1.c2', 't1.c3');

        $actual = $select->getStatement();

        $expect = '
            SELECT
                t1.c1,
                t1.c2,
                t1.c3
            FROM
                t1
            WHERE
                t1.c3 IS NULL
        ';
        $this->assertSameSql($expect, $actual);

        $select = $this->newQuery()->from('t1')
                ->where('t1.c3 !=', NULL)
                ->columns('t1.c1', 't1.c2', 't1.c3');

        $actual = $select->getStatement();

        $expect = '
            SELECT
                t1.c1,
                t1.c2,
                t1.c3
            FROM
                t1
            WHERE
                t1.c3 IS NOT NULL
        ';
        $this->assertSameSql($expect, $actual);
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions