Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email')->unique(); $table->string('password'); $table->rememberToken(); $table->timestamps(); });
CREATE TABLE "users" ( "id" INTEGER NOT NULL PRIMARY KEY, "name" VARCHAR(255) NOT NULL, "email" VARCHAR(255) NOT NULL, "password" VARCHAR(255) NOT NULL, "remember_token" VARCHAR(100), "created_at" TIMESTAMP, "updated_at" TIMESTAMP ); ALTER TABLE "users" ADD CONSTRAINT "users_email_unique" UNIQUE ("email"); CREATE SEQUENCE "seq_users"; CREATE OR ALTER TRIGGER "tr_users_bi" FOR "users" ACTIVE BEFORE INSERT AS BEGIN IF (NEW."id" IS NULL) THEN NEW."id" = NEXT VALUE FOR "seq_users"; END
Schema::create('users', function (Blueprint $table) { $table->useIdentity(); // only Firebird 3.0 $table->increments('id'); $table->string('name'); $table->string('email')->unique(); $table->string('password'); $table->rememberToken(); $table->timestamps(); });
CREATE TABLE "users" ( "id" INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, "name" VARCHAR(255) NOT NULL, "email" VARCHAR(255) NOT NULL, "password" VARCHAR(255) NOT NULL, "remember_token" VARCHAR(100), "created_at" TIMESTAMP, "updated_at" TIMESTAMP ); ALTER TABLE "users" ADD CONSTRAINT "users_email_unique" UNIQUE ("email");
/** * Compile an insert and get ID statement into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param array $values * @param string $sequence * @return string */ public function compileInsertGetId(Builder $query, $values, $sequence) { if (is_null($sequence)) { $sequence = 'id'; } return $this->compileInsert($query, $values) . ' returning ' . $this->wrap($sequence); }
/** * Fix PDO driver bug for 'INSERT ... RETURNING' * See https://bugs.php.net/bug.php?id=72931 * Reproduced in Firebird 3.0 only * Remove when the bug is fixed! * * @param \Illuminate\Database\Query\Builder $query * @param array $values * @param string $sequence * @param string $sql */ private function fixInsertReturningBug(Builder $query, $values, $sequence, $sql) { /* * Since the PDO Firebird driver bug because of which is not executed * sql query 'INSERT ... RETURNING', then we wrap the statement in * the block and execute it. PDO may not recognize the colon (:) within * a block properly, so we will not use it. The only way I found * buyout perform a query via EXECUTE STATEMENT. */ if (!is_array(reset($values))) { $values = [$values]; } $table = $this->wrapTable($query->from); $columns = array_map([$this, 'wrap'], array_keys(reset($values))); $columnsWithTypeOf = []; foreach ($columns as $column) { $columnsWithTypeOf[] = " {$column} TYPE OF COLUMN {$table}.{$column} = ?"; } $ret_column = $this->wrap($sequence); $columns_str = $this->columnize(array_keys(reset($values))); $new_sql = "EXECUTE BLOCK (\n"; $new_sql .= implode(",\n", $columnsWithTypeOf); $new_sql .= ")\n"; $new_sql .= "RETURNS ({$ret_column} TYPE OF COLUMN {$table}.{$ret_column})\n"; $new_sql .= "AS\n"; $new_sql .= " DECLARE STMT VARCHAR(8191);\n"; $new_sql .= "BEGIN\n"; $new_sql .= " STMT = '{$sql}';\n"; $new_sql .= " EXECUTE STATEMENT (STMT) ({$columns_str})\n"; if (!$query->getConnection()->getPdo()->inTransaction()) { // For some unknown reason, there is a ROLLBACK. Probably due to the COMMIT RETAINING. $new_sql .= " WITH AUTONOMOUS TRANSACTION\n"; } $new_sql .= " INTO {$ret_column};\n"; $new_sql .= " SUSPEND;\n"; $new_sql .= "END"; return $new_sql; } /** * Compile an insert and get ID statement into SQL. * * @param \Illuminate\Database\Query\Builder $query * @param array $values * @param string $sequence * @return string */ public function compileInsertGetId(Builder $query, $values, $sequence) { $sql = parent::compileInsertGetId($query, $values, $sequence); // Fix PDO driver bug for 'INSERT ... RETURNING' // See https://bugs.php.net/bug.php?id=72931 $sql = $this->fixInsertReturningBug($query, $values, $sequence, $sql); return $sql; }
CommentI absolutely do not like this method. I hope that in the future the bug will be fixed and this temporary solution can be removed. |
use Firebird\Eloquent\Model; class Customer extends Model { /** * * @var string */ protected $table = 'CUSTOMER'; /** * * @var string */ protected $primaryKey = 'CUSTOMER_ID'; /** * Indicates if the model should be timestamped. * @var bool */ public $timestamps = false; /** * * @var string */ protected $sequence = 'GEN_CUSTOMER_ID'; }
<?php use Firebird\Schema\Blueprint; use Firebird\Schema\SequenceBlueprint; use Illuminate\Database\Migrations\Migration; class CreateUsersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::createSequence('seq_users_id'); Schema::create('users', function (Blueprint $table) { $table->integer('id')->primary(); $table->string('name'); $table->string('email')->unique(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); Schema::sequence('seq_users_id', function (SequenceBlueprint $sequence) { $sequence->increment(5); $sequence->restart(10); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropSequence('seq_users_id'); Schema::drop('users'); } }
CREATE SEQUENCE "seq_users_id"; CREATE TABLE "users" ( "id" INTEGER NOT NULL, "name" VARCHAR(255) NOT NULL, "email" VARCHAR(255) NOT NULL, "password" VARCHAR(255) NOT NULL, "remember_token" VARCHAR(100), "created_at" TIMESTAMP, "updated_at" TIMESTAMP ); ALTER TABLE "users" ADD PRIMARY KEY ("id"); ALTER TABLE "users" ADD CONSTRAINT "users_email_unique" UNIQUE ("email"); ALTER SEQUENCE "seq_users_id" RESTART WITH 10 INCREMENT BY 5;
DROP SEQUENCE "seq_users_id"; DROP TABLE "users";
'connections' => [ 'firebird' => [ 'driver' => 'firebird', 'host' => env('DB_HOST', 'localhost'), 'port' => env('DB_PORT', '3050'), 'database' => env('DB_DATABASE', 'examples'), 'username' => env('DB_USERNAME', 'BOSS'), 'password' => env('DB_PASSWORD', 'qw897tr'), 'role' => 'RDB$ADMIN', 'charset' => env('DB_CHARSET', 'UTF8'), 'engine_version' => '3.0.0', ], ],
"repositories": [ { "type": "package", "package": { "version": "dev-master", "name": "sim1984/laravel-firebird", "source": { "url": "https://github.com/sim1984/laravel-firebird", "type": "git", "reference": "master" }, "autoload": { "classmap": [""] } } } ],
"sim1984/laravel-firebird": "dev-master"
Source: https://habr.com/ru/post/312874/
All Articles