Migration
$table = $schema->createTable('foobar');
$table->addColumn('id', Types::BIGINT, ['autoincrement' => true]);
$table->addColumn('baz', Types::BOOLEAN, ['default' => null, 'notnull' => false]);
$table->setPrimaryKey(['id']);
Entity
/**
* @method ?bool getBaz()
* @method void setBaz(?bool $baz)
*/
class Foobar extends Entity {
/** @var ?bool */
protected $baz;
public function __construct() {
$this->addType('baz', 'boolean');
}
}
Mapper
class FoobarMapper extends QBMapper {
public const TABLE_NAME = 'foobar';
public function __construct(IDBConnection $db) {
parent::__construct($db, self::TABLE_NAME);
}
public function create(?bool $baz = null): Foobar {
$foobar = new Foobar();
$foobar->setBaz($baz);
return $this->insert($foobar);
}
}
Description
When the FoobarMapper::create
method is called with the $baz
parameter set to null
, PostgreSQL throws an exception:
OC\DB\Exceptions\DbalException: An exception occurred while executing a query: SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near ")"
LINE 1: INSERT INTO "oc_appname_foobar" () VALUES()
This is because when no columns are passed at all, PostgreSQL INSERT
queries need to use DEFAULT VALUES
, e.g. INSERT INTO "oc_appname_foobar" DEFAULT VALUES
. If at least one column is passed, there is no exception.
Also, INSERT INTO "oc_appname_foobar" () VALUES()
is totally valid in MySQL, so no issues there.
So currently I am not sure what to do. Is it possible to use the IQueryBuilder
to run completely self-written SQL queries, but only when on PostgreSQL? So basically I need to be able to detect which database is used and then run a manual query.