<commit_before><?php namespace Franzose\ClosureTable\Models;

use DB;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Database\Query\Builder;
use Franzose\ClosureTable\Contracts\ClosureTableInterface;

/**
 * Basic ClosureTable model. Performs actions on the relationships table.
 *
 * @property int ancestor Alias for the ancestor attribute name
 * @property int descendant Alias for the descendant attribute name
 * @property int depth Alias for the depth attribute name
 *
 * @package Franzose\ClosureTable
 */
class ClosureTable extends Eloquent implements ClosureTableInterface {

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'entities_closure';

    /**
     * The primary key for the model.
     *
     * @var string
     */
    protected $primaryKey = 'ctid';

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

    /**
     * Inserts new node into closure table.
     *
     * @param int $ancestorId
     * @param int $descendantId
     * @return void
     * @throws \InvalidArgumentException
     */
    public function insertNode($ancestorId, $descendantId)
    {
        if ( ! is_numeric($ancestorId) || ! is_numeric($descendantId))
        {
            throw new \InvalidArgumentException('`ancestorId` and `descendantId` arguments must be of type int.');
        }

        $t   = $this->table;
        $ak  = $this->getAncestorColumn();
        $dk  = $this->getDescendantColumn();
        $dpk = $this->getDepthColumn();

        DB::transaction(function() use($t, $ak, $dk, $dpk, $ancestorId, $descendantId){
            $rawTable = DB::getTablePrefix().$t;

            $query = "
                INSERT INTO {$t} ({$ak}, {$dk}, {$dpk})
                SELECT tbl.{$ak} as {$ak}, {$descendantId} as {$dk}, tbl.{$dpk}+1 as {$dpk}
                FROM {$rawTable} AS tbl
                WHERE tbl.{$dk} = {$ancestorId}
                UNION ALL
                SELECT {$descendantId}, {$descendantId}, 0
            ";

            $results = DB::statement($query);
        });
    }

    /**
     * Make a node a descendant of another ancestor or makes it a root node.
     *
     * @param int $ancestorId
     * @return void
     * @throws \InvalidArgumentException
     */
    public function moveNodeTo($ancestorId = null)
    {<commit_msg>Use $rawTable instead of $t in the raw query that inserts new nodes into closure_table. Removed DB::transaction call now that there's only one query within ClosureTable::insertNode.<commit_after><?php namespace Franzose\ClosureTable\Models;

use DB;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Database\Query\Builder;
use Franzose\ClosureTable\Contracts\ClosureTableInterface;

/**
 * Basic ClosureTable model. Performs actions on the relationships table.
 *
 * @property int ancestor Alias for the ancestor attribute name
 * @property int descendant Alias for the descendant attribute name
 * @property int depth Alias for the depth attribute name
 *
 * @package Franzose\ClosureTable
 */
class ClosureTable extends Eloquent implements ClosureTableInterface {

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'entities_closure';

    /**
     * The primary key for the model.
     *
     * @var string
     */
    protected $primaryKey = 'ctid';

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

    /**
     * Inserts new node into closure table.
     *
     * @param int $ancestorId
     * @param int $descendantId
     * @return void
     * @throws \InvalidArgumentException
     */
    public function insertNode($ancestorId, $descendantId)
    {
        if ( ! is_numeric($ancestorId) || ! is_numeric($descendantId))
        {
            throw new \InvalidArgumentException('`ancestorId` and `descendantId` arguments must be of type int.');
        }

        $t   = $this->table;
        $ak  = $this->getAncestorColumn();
        $dk  = $this->getDescendantColumn();
        $dpk = $this->getDepthColumn();
        $rawTable = DB::getTablePrefix().$t;

        $query = "
            INSERT INTO {$rawTable} ({$ak}, {$dk}, {$dpk})
            SELECT tbl.{$ak} as {$ak}, {$descendantId} as {$dk}, tbl.{$dpk}+1 as {$dpk}
            FROM {$rawTable} AS tbl
            WHERE tbl.{$dk} = {$ancestorId}
            UNION ALL
            SELECT {$descendantId}, {$descendantId}, 0
        ";

        $results = DB::statement($query);
    }

    /**
     * Make a node a descendant of another ancestor or makes it a root node.
     *
     * @param int $ancestorId
     * @return void
     * @throws \InvalidArgumentException
     */
    public function moveNodeTo($ancestorId = null)
    {
{'fraction_non_alphanumeric': 0.11551087402544112, 'fraction_numerical': 0.0008206811653672548, 'mean_word_length': 2.41147655703289, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6265000', 'n_tokens_mistral': 1470, 'n_tokens_neox': 1407, 'n_words': 416}