Skip to content

Commit

Permalink
Add phpdoc, minor improvements. (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed May 7, 2023
1 parent af2d3ee commit 9d9b7ed
Showing 1 changed file with 74 additions and 52 deletions.
126 changes: 74 additions & 52 deletions src/DbSchemaManager.php
Expand Up @@ -5,7 +5,6 @@
namespace Yiisoft\Log\Target\Db;

use Throwable;
use Yiisoft\Db\Command\CommandInterface;
use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidArgumentException;
Expand All @@ -14,97 +13,107 @@
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Schema\SchemaInterface;

/**
* Manages the log table schema in the database.
*/
final class DbSchemaManager
{
private CommandInterface $command;
private string $driverName = '';
private SchemaInterface $schema;

public function __construct(private ConnectionInterface $db)
{
$this->command = $db->createCommand();
$this->driverName = $db->getDriverName();
$this->schema = $db->getSchema();
}

/**
* Ensures that the log table exists in the database.
*
* @param string $table The name of the log table. Defaults to '{{%yii_log}}'.
*
* @throws Exception
* @throws InvalidArgumentException
* @throws InvalidConfigException
* @throws NotSupportedException
* @throws Throwable
*/
public function ensureTable(string $table = '{{%log}}'): void
public function ensureTable(string $table = '{{%yii_log}}'): void
{
$tableRawName = $this->schema->getRawTableName($table);
$driverName = $this->db->getDriverName();
$schema = $this->db->getSchema();
$tableRawName = $schema->getRawTableName($table);

if ($this->hasTable($table)) {
return;
}

// `log_Time` Default value custom for all dbms
$defaultValue = match ($this->driverName) {
$defaultValue = match ($driverName) {
'mysql' => new Expression('CURRENT_TIMESTAMP(6)'),
'sqlite' => new Expression("(STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW', 'UTC'))"),
default => new Expression('CURRENT_TIMESTAMP'),
};

// `log_Time` Type custom for all dbms
$logTimeType = match ($this->driverName) {
'sqlsrv' => $this->schema->createColumn('DATETIME2(6)')->defaultValue($defaultValue),
default => $this->schema->createColumn(SchemaInterface::TYPE_TIMESTAMP, 6)->defaultValue($defaultValue),
$logTimeType = match ($driverName) {
'sqlsrv' => $schema->createColumn('DATETIME2(6)')->defaultValue($defaultValue),
default => $schema->createColumn(SchemaInterface::TYPE_TIMESTAMP, 6)->defaultValue($defaultValue),
};

// `id` AutoIncrement custom for all dbms
$id = match ($this->driverName) {
'mysql' => $this->schema->createColumn(SchemaInterface::TYPE_BIGINT)->notNull()->append('AUTO_INCREMENT'),
'pgsql' => $this->schema->createColumn('BIGSERIAL')->notNull(),
'sqlsrv' => $this->schema->createColumn(SchemaInterface::TYPE_BIGINT)->notNull()->append('IDENTITY'),
default => $this->schema->createColumn(SchemaInterface::TYPE_INTEGER)->notNull(),
$id = match ($driverName) {
'mysql' => $schema->createColumn(SchemaInterface::TYPE_BIGINT)->notNull()->append('AUTO_INCREMENT'),
'pgsql' => $schema->createColumn('BIGSERIAL')->notNull(),
'sqlsrv' => $schema->createColumn(SchemaInterface::TYPE_BIGINT)->notNull()->append('IDENTITY'),
default => $schema->createColumn(SchemaInterface::TYPE_INTEGER)->notNull(),
};

// create table
$this->command->createTable(
$this->db->createCommand()->createTable(
$table,
[
'id' => $id,
'level' => $this->schema->createColumn(SchemaInterface::TYPE_STRING, 16),
'category' => $this->schema->createColumn(SchemaInterface::TYPE_STRING),
'level' => $schema->createColumn(SchemaInterface::TYPE_STRING, 16),
'category' => $schema->createColumn(SchemaInterface::TYPE_STRING),
'log_time' => $logTimeType,
'message' => $this->schema->createColumn(SchemaInterface::TYPE_TEXT),
'message' => $schema->createColumn(SchemaInterface::TYPE_TEXT),
"CONSTRAINT [[PK_$tableRawName]] PRIMARY KEY ([[id]])",
],
)->execute();

if ($this->driverName === 'oci') {
if ($driverName === 'oci') {
$this->addSequenceAndTrigger($tableRawName);
}

$this->command->createIndex($table, "IDX_{$tableRawName}-category", 'category')->execute();
$this->command->createIndex($table, "IDX_{$tableRawName}-level", 'level')->execute();
$this->command->createIndex($table, "IDX_{$tableRawName}-time", 'log_time')->execute();
$this->db->createCommand()->createIndex($table, "IDX_{$tableRawName}-category", 'category')->execute();
$this->db->createCommand()->createIndex($table, "IDX_{$tableRawName}-level", 'level')->execute();
$this->db->createCommand()->createIndex($table, "IDX_{$tableRawName}-time", 'log_time')->execute();
}

/**
* Ensures that the log table does not exist in the database.
*
* @param string $table The name of the log table. Defaults to '{{%yii_log}}'.
*
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*/
public function ensureNoTable(string $table = '{{%log}}'): void
public function ensureNoTable(string $table = '{{%yii_log}}'): void
{
$tableRawName = $this->schema->getRawTableName($table);
$schema = $this->db->getSchema();
$tableRawName = $schema->getRawTableName($table);

// drop table
if ($this->db->getTableSchema($table, true) !== null) {

Check warning on line 104 in src/DbSchemaManager.php

View workflow job for this annotation

GitHub Actions / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ $schema = $this->db->getSchema(); $tableRawName = $schema->getRawTableName($table); // drop table - if ($this->db->getTableSchema($table, true) !== null) { + if ($this->db->getTableSchema($table, false) !== null) { $this->db->createCommand()->dropTable($tableRawName)->execute(); // drop sequence oracle if ($this->db->getDriverName() === 'oci') {
$this->command->dropTable($tableRawName)->execute();
$this->db->createCommand()->dropTable($tableRawName)->execute();

// drop sequence oracle
if ($this->driverName === 'oci') {
$this->command->setSql(
<<<SQL
DROP SEQUENCE {{{$tableRawName}_SEQ}}
SQL,
)->execute();
if ($this->db->getDriverName() === 'oci') {
$this->db
->createCommand()
->setSql(
<<<SQL
DROP SEQUENCE {{{$tableRawName}_SEQ}}
SQL,
)
->execute();
}
}
}
Expand All @@ -116,26 +125,39 @@ public function ensureNoTable(string $table = '{{%log}}'): void
private function addSequenceAndTrigger(string $tableRawName): void
{
// create sequence oracle
$this->command->setSql(
<<<SQL
CREATE SEQUENCE {{{$tableRawName}_SEQ}}
START WITH 1
INCREMENT BY 1
NOMAXVALUE
SQL,
)->execute();
$this->db
->createCommand()
->setSql(
<<<SQL
CREATE SEQUENCE {{{$tableRawName}_SEQ}}
START WITH 1
INCREMENT BY 1
NOMAXVALUE
SQL,
)
->execute();

// create trigger oracle
$this->command->setSql(
<<<SQL
CREATE TRIGGER {{{$tableRawName}_TRG}} BEFORE INSERT ON {{{$tableRawName}}} FOR EACH ROW BEGIN <<COLUMN_SEQUENCES>> BEGIN
IF INSERTING AND :NEW."id" IS NULL THEN SELECT {{{$tableRawName}_SEQ}}.NEXTVAL INTO :NEW."id" FROM SYS.DUAL; END IF;
END COLUMN_SEQUENCES;
END;
SQL,
)->execute();
$this->db
->createCommand()
->setSql(
<<<SQL
CREATE TRIGGER {{{$tableRawName}_TRG}} BEFORE INSERT ON {{{$tableRawName}}} FOR EACH ROW BEGIN <<COLUMN_SEQUENCES>> BEGIN
IF INSERTING AND :NEW."id" IS NULL THEN SELECT {{{$tableRawName}_SEQ}}.NEXTVAL INTO :NEW."id" FROM SYS.DUAL; END IF;
END COLUMN_SEQUENCES;
END;
SQL,
)
->execute();
}

/**
* Checks if the given table exists in the database.
*
* @param string $table The name of the table to check.
*
* @return bool Whether the table exists or not.
*/
private function hasTable(string $table): bool
{
return $this->db->getTableSchema($table, true) !== null;
Expand Down

0 comments on commit 9d9b7ed

Please sign in to comment.