diff --git a/Command.php b/Command.php index 84b0d4e..d4d7ed6 100644 --- a/Command.php +++ b/Command.php @@ -90,7 +90,7 @@ class Command extends Component * @return mixed * @throws */ - public function rowCount(): mixed + public function rowCount(): int { return $this->search('rowCount'); } diff --git a/Db.php b/Db.php index dcf5e35..73c9cc2 100644 --- a/Db.php +++ b/Db.php @@ -87,10 +87,7 @@ class Db extends QueryTrait implements ISqlBuilder */ public static function table(string $table, string $database = 'db'): Db|static { - $connection = new Db(); - $connection->connection = current(\config('databases.connections')); - $connection->from($table); - return $connection; + return self::connect($database)->from($table); } @@ -108,31 +105,12 @@ class Db extends QueryTrait implements ISqlBuilder } - /** - * @return array|bool - * @throws - */ - public function get(): array|bool - { - return $this->connection->createCommand(SqlBuilder::builder($this)->all())->all(); - } - - /** - * @return array|bool|null - * @throws - */ - public function first(): array|bool|null - { - return $this->connection->createCommand(SqlBuilder::builder($this)->all())->one(); - } - /** * @return int */ public function count(): int { - $count = $this->connection->createCommand(SqlBuilder::builder($this)->count())->one(); - return current($count); + return $this->connection->createCommand(SqlBuilder::builder($this)->count())->rowCount(); } /** @@ -140,8 +118,7 @@ class Db extends QueryTrait implements ISqlBuilder */ public function exists(): bool { - $fetchColumn = $this->connection->createCommand(SqlBuilder::builder($this)->one())->fetchColumn(); - return !empty($fetchColumn); + return $this->connection->createCommand(SqlBuilder::builder($this)->one())->rowCount() > 0; } /** @@ -150,7 +127,7 @@ class Db extends QueryTrait implements ISqlBuilder * @return array|bool|int|string|null * @throws */ - public function query(string $sql, array $attributes = []): int|bool|array|string|null + public function fetchAll(string $sql, array $attributes = []): int|bool|array|string|null { return $this->connection->createCommand($sql, $attributes)->all(); } @@ -160,7 +137,7 @@ class Db extends QueryTrait implements ISqlBuilder * @param array $attributes * @return array|null */ - public function one(string $sql, array $attributes = []): ?array + public function fetch(string $sql, array $attributes = []): ?array { return $this->connection->createCommand($sql, $attributes)->one(); } @@ -188,7 +165,6 @@ class Db extends QueryTrait implements ISqlBuilder $table = [' const TABLE = \'select * from %s where REFERENCED_TABLE_NAME=%s\';']; return $connection->createCommand((new Query()) - ->select('*') ->from('INFORMATION_SCHEMA.KEY_COLUMN_USAGE') ->where(['REFERENCED_TABLE_NAME' => $table]) ->build())->one();