Skip to content

Create obj that will not be used stored

Fernando Correa de Oliveira edited this page Apr 6, 2019 · 2 revisions

SQLite doesn't have the RETURNING clause on SELECT so we have to run a SELECT after a .^create to know the new object's data, like this:

use Red;
model Bla { has Int $.bla is column }

my $*RED-DB = database "SQLite";

Bla.^create-table: :if-not-exists;

my $*RED-DEBUG = True;

say Bla.^create(:bla(42));
SQL : INSERT INTO bla(
   bla
)
VALUES(
   ?
)
BIND: [42]
SQL : SELECT
   bla.bla 
FROM
   bla
WHERE
   _rowid_ = last_insert_rowid()
LIMIT 1
BIND: []
Bla.new(bla => 42)

But if the new object isn't being stored/used we don't need to get its data:

use Red;
model Bla { has Int $.bla is column }

my $*RED-DB = database "SQLite";

Bla.^create-table: :if-not-exists;

my $*RED-DEBUG = True;

Bla.^create(:bla(42));
SQL : INSERT INTO bla(
   bla
)
VALUES(
   ?
)
BIND: [42]
Clone this wiki locally