Skip to content

Commit b55b1e7

Browse files
committed
Add handler support
1 parent 2142ef3 commit b55b1e7

17 files changed

+397
-103
lines changed

AbstractDB.php

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
namespace MaplePHP\Query;
55

6+
use MaplePHP\Query\Exceptions\ConnectException;
67
use MaplePHP\Query\Utility\Attr;
7-
use MaplePHP\Query\Handlers\MySqliHandler;
88
use MaplePHP\Query\Interfaces\AttrInterface;
99
use MaplePHP\Query\Interfaces\MigrateInterface;
1010
use MaplePHP\Query\Interfaces\DBInterface;
@@ -284,7 +284,8 @@ final protected function getMainFKData(): array
284284

285285
/**
286286
* Mysql Prep/protect string
287-
* @param mixed $val
287+
* @param mixed $val
288+
* @param bool $enclose
288289
* @return AttrInterface
289290
*/
290291
final protected function prep(mixed $val, bool $enclose = true): AttrInterface
@@ -347,15 +348,15 @@ final protected function camelLoop(array $camelCaseArr, array $valArr, callable
347348
*/
348349
final protected function extractCamelCase(string $value): array
349350
{
350-
$arr = preg_split('#([A-Z][^A-Z]*)#', $value, 0, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
351-
return $arr;
351+
return preg_split('#([A-Z][^A-Z]*)#', $value, 0, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
352352
}
353353

354354
/**
355355
* Build join data from Migrate data
356-
* @param MigrateInterface $mig
357-
* @param string $type Join type (INNER, LEFT, ...)
356+
* @param MigrateInterface $mig
357+
* @param string $type Join type (INNER, LEFT, ...)
358358
* @return array
359+
* @throws ConnectException
359360
*/
360361
final protected function buildJoinFromMig(MigrateInterface $mig, string $type): array
361362
{
@@ -370,16 +371,16 @@ final protected function buildJoinFromMig(MigrateInterface $mig, string $type):
370371
if (isset($row['fk'])) {
371372
foreach ($row['fk'] as $a) {
372373
if ($a['table'] === (string)$this->table) {
373-
$joinArr[] = "{$type} JOIN " . $prefix . $migTable . " " . $migTable .
374-
" ON (" . $migTable . ".{$col} = {$a['table']}.{$a['column']})";
374+
$joinArr[] = "$type JOIN " . $prefix . $migTable . " " . $migTable .
375+
" ON (" . $migTable . ".$col = {$a['table']}.{$a['column']})";
375376
}
376377
}
377378
} else {
378379
foreach ($main as $c => $a) {
379380
foreach ($a as $t => $d) {
380381
if (in_array($col, $d)) {
381-
$joinArr[] = "{$type} JOIN " . $prefix . $migTable . " " . $migTable .
382-
" ON ({$t}.{$col} = {$this->alias}.{$c})";
382+
$joinArr[] = "$type JOIN " . $prefix . $migTable . " " . $migTable .
383+
" ON ($t.$col = $this->alias.$c)";
383384
}
384385
}
385386
}
@@ -407,19 +408,21 @@ protected function getAllQueryTables(): ?string
407408

408409
/**
409410
* Query result
410-
* @param string|self $sql
411-
* @param string|null $method
412-
* @param array $args
413-
* @return array|object|bool
411+
* @param string|self $sql
412+
* @param string|null $method
413+
* @param array $args
414+
* @return array|object|bool|string
415+
* @throws DBQueryException
414416
*/
415-
final protected function query(string|self $sql, ?string $method = null, array $args = []): array|object|bool
417+
final protected function query(string|self $sql, ?string $method = null, array $args = []): array|object|bool|string
416418
{
417419
$query = new Query($sql);
420+
$query->setPluck($this->pluck);
418421
if (!is_null($method)) {
419422
if (method_exists($query, $method)) {
420423
return call_user_func_array([$query, $method], $args);
421424
}
422-
throw new DBQueryException("Method \"{$method}\" does not exists!", 1);
425+
throw new DBQueryException("Method \"$method\" does not exists!", 1);
423426
}
424427
return $query;
425428
}

Connect.php

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55

66
use MaplePHP\Query\Exceptions\ConnectException;
77
use MaplePHP\Query\Interfaces\AttrInterface;
8+
use MaplePHP\Query\Interfaces\MigrateInterface;
89
use MaplePHP\Query\Utility\Attr;
910
use mysqli;
1011

12+
/**
13+
* @method static select(string $string, string|array|MigrateInterface $array)
14+
* @method static table(string $string)
15+
*/
1116
class Connect
1217
{
13-
1418
private $handler;
1519
private static array $inst;
1620
private $db;
@@ -27,12 +31,24 @@ private function __construct($handler)
2731
private function __clone() {
2832
}
2933

34+
/**
35+
* Access query builder intance
36+
* @param string $name
37+
* @param array $arguments
38+
* @return mixed
39+
*/
3040
public static function __callStatic(string $name, array $arguments)
3141
{
3242
$inst = new DB();
3343
return $inst::$name(...$arguments);
3444
}
3545

46+
/**
47+
* Set connection handler
48+
* @param $handler
49+
* @param string|null $key
50+
* @return self
51+
*/
3652
public static function setHandler($handler, ?string $key = null): self
3753
{
3854
$key = self::getKey($key);
@@ -42,6 +58,12 @@ public static function setHandler($handler, ?string $key = null): self
4258
return self::$inst[$key];
4359
}
4460

61+
/**
62+
* Get default instance or secondary instances with key
63+
* @param string|null $key
64+
* @return self
65+
* @throws ConnectException
66+
*/
4567
public static function getInstance(?string $key = null): self
4668
{
4769
$key = self::getKey($key);
@@ -52,18 +74,32 @@ public static function getInstance(?string $key = null): self
5274
return self::$inst[$key];
5375
}
5476

55-
private static function hasInstance(?string $key = null): bool
77+
/**
78+
* Check if default instance or secondary instances exist for key
79+
* @param string|null $key
80+
* @return bool
81+
*/
82+
public static function hasInstance(?string $key = null): bool
5683
{
5784
$key = self::getKey($key);
5885
return (isset(self::$inst[$key]) && (self::$inst[$key] instanceof self));
5986
}
6087

88+
/**
89+
* Get the possible connection key
90+
* @param string|null $key
91+
* @return string
92+
*/
6193
private static function getKey(?string $key = null): string
6294
{
6395
$key = (is_null($key)) ? "default" : $key;
6496
return $key;
6597
}
6698

99+
/**
100+
* Access the connection handler
101+
* @return mixed
102+
*/
67103
function getHandler() {
68104
return $this->handler;
69105
}
@@ -85,6 +121,15 @@ public function getPrefix(): string
85121
return $this->handler->getPrefix();
86122
}
87123

124+
/**
125+
* Check if a connections is open
126+
* @return bool
127+
*/
128+
public function hasConnection(): bool
129+
{
130+
return $this->handler->hasConnection();
131+
}
132+
88133
/**
89134
* Connect to database
90135
* @return void

Create.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,7 @@ public function fkExists(string $table, string $col)
912912
{
913913
$table = Connect::getInstance()->prep($table);
914914
$col = Connect::getInstance()->prep($col);
915-
$dbName = Connect::getInstance()->getDBName();
915+
$dbName = Connect::getInstance()->getHandler()->getDBName();
916916
$result = Connect::getInstance()->query("SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, CONSTRAINT_NAME FROM " .
917917
"INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE REFERENCED_TABLE_SCHEMA = '{$dbName}' AND " .
918918
"TABLE_NAME = '{$table}' AND COLUMN_NAME = '{$col}'");

0 commit comments

Comments
 (0)