dg / dibi

Dibi - smart database abstraction layer
https://dibiphp.com
Other
487 stars 136 forks source link

Select only one column with Fluent #281

Closed BigOHenry closed 6 years ago

BigOHenry commented 6 years ago

Description

How to select one column with Dibi Fluent? When i use this code:

$this->db
->select("column")
->from("table");

I always got this kind of sql: SELECT 'column' FROM table; The result is string column.

I have to use this:

$this->db
->select("column as column")
->from("table");
dg commented 6 years ago

I am unable to reproduce it.

try {
    $this->db->select("column")->from("table")->fetchSingle();
} catch (Exception $e) {
}

echo dibi::$sql;
// prints  SELECT [column] FROM [table] LIMIT 1
BigOHenry commented 6 years ago

I finally found case when its happening:

$this->db
            ->select("column")
            ->from("table")
            ->where("value = ?", 4);

In where is a column with name value (keyword), then it will create this sql:

SELECT 'column' FROM table WHERE value = 4;

dg commented 6 years ago

Still I am unable to reproduce it:

try {
    $this->db->select("column")->from("table")->where("value = ?", 4)->fetchSingle();
} catch (Exception $e) {
}

echo dibi::$sql;
// prints  SELECT [column] FROM [table] WHERE value = 4 LIMIT 1
BigOHenry commented 6 years ago

Intereting. Did you try it on Postgre?

dg commented 6 years ago

It is tested with nearly all databases.

Can you paste here your complete code?

milo commented 6 years ago

@BigOHenry Tested with PostgreSQL 9.6 and 10 and the translation is correct.

$db
    ->select("column")
    ->from("table")
    ->where("value = ?", 4)
    ->test();
SELECT "column"
FROM "table"
WHERE value = 4

You have a next difference in output. Missing quotes around table.

Don't you pass Fluent somewhere else to %ex or %sql modifiers? If so, you should use %SQL, not escaping variant. A serialized Fluent is already sanitized.