0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

LaravelAdvent Calendar 2024

Day 17

結局laravelのtruncate()の実装箇所ってどこ?実行されているSQLって何?

Posted at

概要

laravelのモデルクラス::truncate();の実装箇所と実行SQLをざっくり探ってみる。

内容

  1. 個々のモデルクラスを見る

  2. 個々のモデルクラスの継承元のvendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.phpをみる

  3. 下記のような記載があるこの記載のおかげでクエリビルダーが使えている

    vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php
    /**
     * The Eloquent query builder class to use for the model.
     *
     * @var class-string<\Illuminate\Database\Eloquent\Builder<*>>
     */
    protected static string $builder = Builder::class;
    
  4. 上記の記載をヒントにvendor/laravel/framework/src/Illuminate/Database/Query/Builder.phpを見る

  5. 下記の処理が動作している事がわかる

    vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php
    /**
     * Run a truncate statement on the table.
     *
     * @return void
     */
    public function truncate()
    {
        $this->applyBeforeQueryCallbacks();
    
        foreach ($this->grammar->compileTruncate($this) as $sql => $bindings) {
            $this->connection->statement($sql, $bindings);
        }
    }
    
  6. 余談だがdelete()は同クラスの近くに定義されていた

    vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php
    /**
     * Delete records from the database.
     *
     * @param  mixed  $id
     * @return int
     */
    public function delete($id = null)
    {
        // If an ID is passed to the method, we will set the where clause to check the
        // ID to let developers to simply and quickly remove a single row from this
        // database without manually specifying the "where" clauses on the query.
        if (! is_null($id)) {
            $this->where($this->from.'.id', '=', $id);
        }
    
        $this->applyBeforeQueryCallbacks();
    
        return $this->connection->delete(
            $this->grammar->compileDelete($this), $this->cleanBindings(
                $this->grammar->prepareBindingsForDelete($this->bindings)
            )
        );
    }
    
  7. どうやらSQL文はこちらで与えられているらしいvendor/laravel/framework/src/Illuminate/Database/Query/Grammars/Grammar.php

  8. compileTruncate()は下記、SQLのTRUNCATE TABLE句を生成して実行している模様

    vendor/laravel/framework/src/Illuminate/Database/Query/Grammars/Grammar.php
    /**
     * Compile a truncate table statement into SQL.
     *
     * @param  \Illuminate\Database\Query\Builder  $query
     * @return array
     */
    public function compileTruncate(Builder $query)
    {
        return ['truncate table '.$this->wrapTable($query->from) => []];
    }
    
  9. MySQLのTRUNCATE TABLE句に関しては下記を参照

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?