/** * Import an Eloquent * * @param Model $model * @param array $relations * @param int $batchSize * @param callable $callback * @internal param $type */ public function import(Model $model, $relations = [], $batchSize = 750, callable $callback = null) { $batch = 0; $asQueryLoggind = $model->getConnection()->logging(); $model->getConnection()->disableQueryLog(); while (true) { // Increase the batch number $batch += 1; // Load records from the database $records = $model->newInstance()->with($relations)->skip($batchSize * ($batch - 1))->take($batchSize)->get(); // Break out of the loop if we are out of records if (count($records) == 0) { break; } // Call the callback function to provide feedback on the import process if ($callback) { $callback($batch); } // Transform each record before sending it to Elasticsearch $data = []; foreach ($records as $record) { $data[] = ['index' => ['_id' => $record->getEsId()]]; $data[] = $record->transform(!empty($relations)); } // Bulk import the data to Elasticsearch $this->bulk($data); } if ($asQueryLoggind) { $model->getConnection()->enableQueryLog(); } }
/** * Set mock connection. * * @param \Illuminate\Database\Eloquent\Model $model */ protected function addMockConnection(Model $model) { $resolver = m::mock('\\Illuminate\\Database\\ConnectionResolverInterface'); $model->setConnectionResolver($resolver); $resolver->shouldReceive('connection')->andReturn(m::mock('\\Illuminate\\Database\\Connection')); $model->getConnection()->shouldReceive('getQueryGrammar')->andReturn(m::mock('\\Illuminate\\Database\\Query\\Grammars\\Grammar')); $model->getConnection()->shouldReceive('getPostProcessor')->andReturn(m::mock('\\Illuminate\\Database\\Query\\Processors\\Processor')); }
/** * {@inheritDoc} * @see \Illuminate\Database\Eloquent\ScopeInterface::apply() */ public function apply(Builder $builder, Model $model) { /** * @var $model Model */ $name = $model::getBootUseToTraitProperyName(); $value = $model::${$name}; $value = $model->getConnection()->getPdo()->quote($value); $builder->where($model->getQualifiedUseToFieldName(), new Expression($value)); $this->addWithTrashed($builder); }
/** * Create fields for type. * * @return void */ protected function schemaFields() { $table = $this->model->getTable(); $schema = $this->model->getConnection()->getSchemaBuilder(); $columns = collect($schema->getColumnListing($table)); $columns->each(function ($column) use($table, $schema) { if (!$this->skipField($column)) { $this->generateField($column, $schema->getColumnType($table, $column)); } }); }
/** * @param Closure $closure * @return mixed * @throws Exception */ protected function transaction(Closure $closure) { $connection = $this->model->getConnection(); $connection->beginTransaction(); try { $result = $closure(); $connection->commit(); return $result; } catch (Exception $e) { $connection->rollBack(); throw $e; } }
public function performTransaction($amount, $sender_id, $recipient_id) { $connection = Eloquent::getConnection(); try { $connection->getPdo()->beginTransaction(); $sender = Balance::where('character_id', $sender_id)->first(); $recipient = Balance::where('character_id', $recipient_id)->first(); $sender->update(['amount' => $sender->amount - $amount]); $recipient->update(['amount' => $recipient->amount + $amount]); $connection->getPdo()->commit(); } catch (\Exception $e) { $connection->getPdo()->rollback(); return false; } return true; }
protected function database(Model $model) { return $model->getConnection()->table($model->getTable()); }
/** * Load the properties from the database table. * * @param \Illuminate\Database\Eloquent\Model $model */ protected function getPropertiesFromTable($model) { $table = $model->getTable(); $schema = $model->getConnection()->getDoctrineSchemaManager($table); $schema->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string'); $columns = $schema->listTableColumns($table); if ($columns) { foreach ($columns as $column) { $name = $column->getName(); $type = $column->getType()->getName(); switch ($type) { case 'string': case 'text': case 'date': case 'time': case 'guid': $type = 'string'; break; case 'integer': case 'bigint': case 'smallint': $type = 'integer'; break; case 'decimal': case 'float': $type = 'float'; break; case 'boolean': $type = 'boolean'; break; case 'datetimetz': //String or DateTime, depending on $dates //String or DateTime, depending on $dates case 'datetime': $type = '\\Carbon\\Carbon'; break; default: $type = 'mixed'; break; } $this->setProperty($name, $type, true, true); } } }
private function getTableColumns(Model $model) { return $model->getConnection()->getSchemaBuilder()->getColumnListing($model->getTable()); }
/** * @param \Illuminate\Database\Eloquent\Model $model */ public function __construct(Model $model) { $this->model = $model; $this->db = $model->getConnection(); }
/** * Delete entity values. * * @throws \InvalidArgumentException */ public function delete() { if (!in_array(SoftDeletes::class, class_uses_recursive(get_class($this->entity)), true) || $this->entity->forceDeleting) { $instance = new Value(); $connection = $this->entity->getConnection(); $connection->table($instance->getTable())->where('entity_id', $this->entity->getKey())->whereIn('property_id', $this->properties->pluck('id'))->delete(); } }
/** * {@inheritdoc} */ protected function getConnection() { return $this->model->getConnection(); }
/** * Check if column is nullable. * * @param \Illuminate\Database\Eloquent\Model $model * @param string $column * @return bool */ public function getNullable($model, $column) { $schema = $model->getConnection()->getDoctrineSchemaManager($model->getTable()); $columns = $schema->listTableColumns($model->getTable()); return isset($columns[$column]) ? !$columns[$column]->getNotnull() : false; }
/** * Load the properties from the database table. * * @param \Illuminate\Database\Eloquent\Model $model */ protected function getPropertiesFromTable($model) { $table = $model->getConnection()->getTablePrefix() . $model->getTable(); $schema = $model->getConnection()->getDoctrineSchemaManager($table); $databasePlatform = $schema->getDatabasePlatform(); $databasePlatform->registerDoctrineTypeMapping('enum', 'string'); $platformName = $databasePlatform->getName(); $customTypes = $this->laravel['config']->get("ide-helper.custom_db_types.{$platformName}", array()); foreach ($customTypes as $yourTypeName => $doctrineTypeName) { $databasePlatform->registerDoctrineTypeMapping($yourTypeName, $doctrineTypeName); } $database = null; if (strpos($table, '.')) { list($database, $table) = explode('.', $table); } $columns = $schema->listTableColumns($table, $database); if ($columns) { foreach ($columns as $column) { $name = $column->getName(); if (in_array($name, $model->getDates())) { $type = 'datetime'; } else { $type = $column->getType()->getName(); } if (!($model->incrementing && $model->getKeyName() === $name) && $name !== $model::CREATED_AT && $name !== $model::UPDATED_AT) { if (!method_exists($model, 'getDeletedAtColumn') || method_exists($model, 'getDeletedAtColumn') && $name !== $model->getDeletedAtColumn()) { $this->setProperty($name, $type); } } } } }
/** * Load the properties from the database table. * * @param \Illuminate\Database\Eloquent\Model $model */ protected function getPropertiesFromTable($model) { $table = $model->getConnection()->getTablePrefix() . $model->getTable(); $schema = $model->getConnection()->getDoctrineSchemaManager($table); $schema->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string'); $columns = $schema->listTableColumns($table); if ($columns) { foreach ($columns as $column) { $name = $column->getName(); if (in_array($name, $model->getDates())) { $type = '\\Carbon\\Carbon'; } else { $type = $column->getType()->getName(); switch ($type) { case 'string': case 'text': case 'date': case 'time': case 'guid': case 'datetimetz': case 'datetime': $type = 'string'; break; case 'integer': case 'bigint': case 'smallint': $type = 'integer'; break; case 'decimal': case 'float': $type = 'float'; break; case 'boolean': $type = 'boolean'; break; default: $type = 'mixed'; break; } } $this->setProperty($name, $type, true, true); $this->setMethod(Str::camel("where_" . $name), '\\Illuminate\\Database\\Query\\Builder|\\' . get_class($model), array('$value')); } } }
/** * Load the properties from the database table. * * @param \Illuminate\Database\Eloquent\Model $model */ protected function getPropertiesFromTable($model) { $table = $model->getConnection()->getTablePrefix() . $model->getTable(); $schema = $model->getConnection()->getDoctrineSchemaManager($table); $databasePlatform = $schema->getDatabasePlatform(); $databasePlatform->registerDoctrineTypeMapping('enum', 'string'); $platformName = $databasePlatform->getName(); $customTypes = $this->laravel['config']->get("ide-helper.custom_db_types.{$platformName}", array()); foreach ($customTypes as $yourTypeName => $doctrineTypeName) { $databasePlatform->registerDoctrineTypeMapping($yourTypeName, $doctrineTypeName); } $database = null; if (strpos($table, '.')) { list($database, $table) = explode('.', $table); } $columns = $schema->listTableColumns($table, $database); if ($columns) { foreach ($columns as $column) { $name = $column->getName(); if (in_array($name, $model->getDates())) { $type = '\\Carbon\\Carbon'; } else { $type = $column->getType()->getName(); switch ($type) { case 'string': case 'text': case 'date': case 'time': case 'guid': case 'datetimetz': case 'datetime': $type = 'string'; break; case 'integer': case 'bigint': case 'smallint': $type = 'integer'; break; case 'decimal': case 'float': $type = 'float'; break; case 'boolean': $type = 'boolean'; break; default: $type = 'mixed'; break; } } $comment = $column->getComment(); $this->setProperty($name, $type, true, true, $comment); $this->setMethod(Str::camel("where_" . $name), '\\Illuminate\\Database\\Query\\Builder|\\' . get_class($model), array('$value')); } } }
/** * @param Model $model * @param array $expected * @return int */ private function countModels(Model $model, array $expected) { return $model->getConnection()->table($model->getTable())->where($expected)->count(); }