/*
Parent of insert() function
@param string $query Query string
@return \Core\Orm\RowGateway | bool
333611
protected function _insert($query, $params)
    {
        $db = $this->db;
        $sth = $db->prepare($query);

        foreach ($params as $key => $val) {
            if (is_int($val)) {
                $sth->bindValue("$key", $val, $db::PARAM_INT);
            } else {
                $sth->bindValue("$key", $val);
            }
        }

        $response = $sth->execute();

        if ($response) {

            $class = $this->_rowClass;
            $row = new $class($this, $db->lastInsertId());

            return $row;
        }
        return false;
    }
825572
protected function setKeysForSaveQuery(Builder $query)
    {
        if (isset($this->attributes[$this->getKeyName()])) {
            return parent::setKeysForSaveQuery($query);
        }

        $query->where($this->foreignKey, $this->getOriginal(
            $this->foreignKey, $this->getAttribute($this->foreignKey)
        ));

        return $query->where($this->relatedKey, $this->getOriginal(
            $this->relatedKey, $this->getAttribute($this->relatedKey)
        ));
    }
1196759
public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
    {
        if ($parentQuery->getQuery()->from === $query->getQuery()->from) {
            return $this->getRelationExistenceQueryForSelfRelation($query, $parentQuery, $columns);
        }

        return $query->select($columns)->whereColumn(
            $this->getQualifiedForeignKey(),
            '=',
            $query->getModel()->getTable() . '.' . $this->ownerKey
        );
    }
32410
public function bindExecuteLastInsertId($query, $parameters = [])
    {
        $statement = $this->link->prepare($query);

        foreach ($parameters as $index => $paramProperties) {
            if (is_object($paramProperties)) {
                $paramProperties = (array) $paramProperties;
            }

            if (is_array($paramProperties)) {
                $statement->bindValue(
                    (int) $index + 1,
                    $paramProperties['value'],
                    $paramProperties['param']
                );
            } else {
                $statement->bindValue((int) $index + 1, $paramProperties);
            }
        }

        $statement->execute();

        return $this->link->lastInsertId();
    }
1023285
public function getRelationExistenceQueryForSelfRelation(Builder $query, Builder $parentQuery, $columns = ['*'])
    {
        $query->from($query->getModel()->getTable().' as '.$hash = $this->getRelationCountHash());

        $query->getModel()->setTable($hash);

        return $query->select($columns)->whereColumn(
            $this->getQualifiedParentKeyName(), '=', $hash.'.'.$this->getForeignKeyName()
        );
    }
974813
public function withQuery($query)
    {
        if (!is_string($query) && !method_exists($query, '__toString')) {
            throw new \InvalidArgumentException('Query string must be a string');
        }
        $query = (string) $query;
        if (strpos($query, '?') === 0) {
            $query = substr($query, 1);
        }
        $query = $this->filterQueryAndFragment($query);

        $new = clone $this;
        $new->query = $query;

        return $new;
    }
635752
protected function performInsert(QueryBuilder $query)
    {
        // Disparar evento de documento sendo criado
        if ($this->fireModelEvent('creating') === false) {
            return false;
        }

        // Verificar para atualizar os timestamps
        if ($this->usesTimestamps()) {
            $this->updateTimestamps();
        }

        // Inserir documento e capturar ID
        $id = $query->insertGetId($this->attributes);
        $this->setAttribute('_id', $id);

        // Marcar como model já carregou o documento
        $this->exists = true;

        // Disparar evento de documento criado
        $this->fireModelEvent('created', false);

        return true;
    }
1101520
public function execute($query)
    {
        $context = $this->getCommandContext();

        $context->query = $query;

        if ($this->getCommandChain()->run('before.execute', $context) !== false) {
            $context->result = $this->_adapter->execute(to_str($context->query));
            $this->getCommandChain()->run('after.execute', $context);
        }

        return $context->result;
    }
609955
public function scopeStillActive($query)
    {
        $query->where(function ($query) {
            return $query->where('start_at', '<=', Carbon::now())->orWhereNull('start_at');
        })
        ->where(function ($query) {
            return $query->where('end_at', '>=', Carbon::now())->orWhereNull('end_at');
        })
        ->where(function ($query) {
            return $query->where('parent_id', 0)->orWhereNull('parent_id');
        });

        return $query;
    }
1042898
public static function render(Query\Insert $query)
    {
        return Compiler::withDb($query->getDb(), function () use ($query) {

            $table = $query->getTable();
            $select = $query->getSelect();

            return Compiler::expression(array(
                'INSERT',
                $query->getType(),
                Compiler::word('INTO', $table !== null ? Aliased::render($table) : null),
                Columns::render($query->getColumns()),
                Compiler::word('VALUES', Values::combine($query->getValues())),
                Compiler::word('SET', Set::combine($query->getSet())),
                $select !== null ? Select::render($select) : null,
            ));
        });
    }
444124
public function getInsertQuery()
    {
        $sql = 'INSERT INTO ' . $this->getTable();
        $sql .= "\n" . $this->getColumns();
        $sql .= "\nVALUES " . $this->getValues();

        return $sql;
    }
270392
public function insert($query, array $data){        
        $this->conn->prepare($query)->execute($data);
        
        return $this->conn->lastInsertId();
    }
182814
public function insertWithLastInsertId($query, &$lastInsertedId = null)
    {
        $done = $this->exec($query);
        if ($done) {
            $lastInsertedId = $this->lastInsertRowID();
        }
        return $done;
    }
292676
public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
    {
        if ($parentQuery->getQuery()->from == $query->getQuery()->from) {
            return $this->getRelationExistenceQueryForSelfRelation($query, $parentQuery, $columns);
        }

        $this->performJoin($query);

        return $query->select($columns)->whereColumn(
            $this->getQualifiedForeignKeyName(), '=', $this->getQualifiedSecondOwnerKeyName()
        );
    }
97169
public function getRelationExistenceQueryForSelfRelation(Builder $query, Builder $parentQuery, $columns = ['*'])
    {
        $query->from($query->getModel()->getTable().' as '.$hash = $this->getRelationCountHash());

        $query->join($this->throughParent->getTable(), $this->getQualifiedParentKeyName(), '=', $hash.'.'.$this->secondKey);

        if ($this->throughParentSoftDeletes()) {
            $query->whereNull($this->throughParent->getQualifiedDeletedAtColumn());
        }

        $query->getModel()->setTable($hash);

        return $query->select($columns)->whereColumn(
            $parentQuery->getQuery()->from.'.'.$this->localKey, '=', $this->getQualifiedFirstKeyName()
        );
    }
116044
public static function exec($query)
    {
        $ret = false;

        $res = self::query($query);

        if ($res) {
            $ret = self::connection()->sql_affected_rows();
            self::free($res);
        }

        return $ret;
    }
774264
public function insert($query, $params = array())
    {
        $this->_testQueryStarts($query, 'insert');
        return $this->_executeQuery($query, $params, true);
    }