connection = Kiri::getDi()->get(DatabasesProviders::class)->get($dbname); } else { $db->connection = $dbname; } return $db; } /** * @return void * @throws */ public static function beginTransaction(): void { fire(new BeginTransaction()); } /** * @return void * @throws */ public static function commit(): void { fire(new Commit()); } /** * @return void * @throws */ public static function rollback(): void { fire(new Rollback()); } /** * @param string $table * @param string $database * @return static */ public static function table(string $table, string $database = 'db'): Db|static { return self::connect($database)->from($table); } /** * @param string $column * @param string $alias * @return string */ public static function any_value(string $column, string $alias = ''): string { if (empty($alias)) { $alias = $column . '_any_value'; } return 'ANY_VALUE(' . $column . ') as ' . $alias; } /** * @return int */ public function count(): int { return $this->connection->createCommand(SqlBuilder::builder($this)->count())->rowCount(); } /** * @return bool */ public function exists(): bool { return $this->connection->createCommand(SqlBuilder::builder($this)->one())->rowCount() > 0; } /** * @param string $sql * @param array $attributes * @return array|bool|int|string|null * @throws */ public function fetchAll(string $sql, array $attributes = []): int|bool|array|string|null { return $this->connection->createCommand($sql, $attributes)->all(); } /** * @param string $sql * @param array $attributes * @return array|null */ public function fetch(string $sql, array $attributes = []): ?array { return $this->connection->createCommand($sql, $attributes)->one(); } /** * @return bool */ public function delete(): bool { return $this->connection->createCommand(SqlBuilder::builder($this)->delete())->delete(); } /** * @param string $table * @return array|bool|null * @throws */ public static function show(string $table): array|bool|null { if ($table == '') { return null; } $connection = static::getDefaultConnection(); $table = [' const TABLE = \'select * from %s where REFERENCED_TABLE_NAME=%s\';']; return $connection->createCommand((new Query()) ->from('INFORMATION_SCHEMA.KEY_COLUMN_USAGE') ->where(['REFERENCED_TABLE_NAME' => $table]) ->build())->one(); } /** * @param string $table * @param Connection|null $connection * @param string $database * @return array|null * @throws */ public static function desc(string $table, ?Connection $connection = null, string $database = 'db'): ?array { $sql = SqlBuilder::builder(new Query())->columns($table); $connection = self::getDefaultConnection($connection, $database); return $connection->createCommand($sql)->all(); } /** * @param null|Connection $connection * @param string $database * @return mixed * @throws */ public static function getDefaultConnection(?Connection $connection = null, string $database = 'db'): Connection { if ($connection instanceof Connection) { return $connection; } $databases = \config('databases.connections', []); $providers = Kiri::getDi()->get(DatabasesProviders::class); if (empty($databases) || !is_array($databases)) { throw new Exception('Please configure the database link.'); } return $providers->get($databases[$database]); } }