Skip to content

Version 3: Major improvments #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 53 additions & 34 deletions AbstractDB.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
<?php

declare(strict_types=1);

namespace MaplePHP\Query;

use MaplePHP\Query\Exceptions\ConnectException;
use MaplePHP\Query\Interfaces\ConnectInterface;
use MaplePHP\Query\Utility\Attr;
use MaplePHP\Query\Interfaces\AttrInterface;
use MaplePHP\Query\Interfaces\MigrateInterface;
use MaplePHP\Query\Interfaces\DBInterface;
use MaplePHP\Query\Exceptions\DBValidationException;
use MaplePHP\Query\Exceptions\DBQueryException;
use MaplePHP\Query\Exceptions\ResultException;

/**
* @psalm-taint-source
Expand All @@ -32,7 +34,7 @@ abstract class AbstractDB implements DBInterface
protected $whereProtocol = [];
protected $fkData;
protected $joinedTables;

protected ?string $pluck = null;
protected string $connKey = "default";

/**
Expand Down Expand Up @@ -85,36 +87,51 @@ abstract protected function dropView(): self;
*/
abstract protected function showView(): self;

public function setConnKey(?string $key) {

/**
* Set connection
* @param string|null $key
* @return void
*/
public function setConnKey(?string $key): void
{
$this->connKey = is_null($key) ? "default" : $key;
}

public function connInst() {

/**
* @throws ConnectException
*/
public function connInst(): Connect
{
return Connect::getInstance($this->connKey);
}

/**
* Access Mysql DB connection
* @return \mysqli
* @return ConnectInterface
* @throws ConnectException
*/
public function connect()
public function connect(): ConnectInterface
{
return $this->connInst()->DB();
}

/**
* Get current instance Table name with prefix attached
* @param bool $withAlias
* @return string
* @throws ConnectException
*/
public function getTable(bool $withAlias = false): string
{
$alias = ($withAlias && !is_null($this->alias)) ? " {$this->alias}" : "";
$alias = ($withAlias && !is_null($this->alias)) ? " $this->alias" : "";
return $this->connInst()->getHandler()->getPrefix() . $this->table . $alias;
}

/**
* Get current instance Columns
* @return array
* @throws DBValidationException
*/
public function getColumns(): array
{
Expand All @@ -137,7 +154,6 @@ protected function getAttr(array|string|int|float $value): AttrInterface
return new Attr($value);
}


/**
* Will reset Where input
* @return void
Expand Down Expand Up @@ -191,17 +207,18 @@ protected function joinTypes(string $val): string
}

/**
* Sperate Alias
* @param string|array $data
* Separate Alias
* @param string|array $data
* @return array
* @throws ResultException
*/
final protected function sperateAlias(string|array $data): array
final protected function separateAlias(string|array $data): array
{
$alias = null;
$table = $data;
if (is_array($data)) {
if (count($data) !== 2) {
throw new DBQueryException("If you specify Table as array then it should look " .
throw new ResultException("If you specify Table as array then it should look " .
"like this [TABLE_NAME, ALIAS]", 1);
}
$alias = array_pop($data);
Expand All @@ -211,15 +228,16 @@ final protected function sperateAlias(string|array $data): array
}

/**
* Propegate where data structure
* @param string|AttrInterface $key
* Propagate where data structure
* @param string|AttrInterface $key
* @param string|int|float|AttrInterface $val
* @param array|null &$data static value
* @throws DBValidationException
*/
final protected function setWhereData(string|AttrInterface $key, string|int|float|AttrInterface $val, ?array &$data): void
{
if (is_null($data)) {
$data = array();
$data = [];
}
$key = (string)$this->prep($key, false);
$val = $this->prep($val);
Expand All @@ -228,12 +246,12 @@ final protected function setWhereData(string|AttrInterface $key, string|int|floa
throw new DBValidationException($this->mig->getMessage(), 1);
}

//$data[$this->whereIndex][$this->whereAnd][$this->compare][$key][] = $val;
$data[$this->whereIndex][$this->whereAnd][$key][] = [
"not" => $this->whereNot,
"operator" => $this->compare,
"value" => $val
];
$this->whereNot = null;

$this->whereProtocol[$key][] = $val;
$this->resetWhere();
Expand All @@ -251,35 +269,34 @@ final protected function whereArrToStr(array $array): string
foreach ($array as $key => $arr) {
foreach ($arr as $col => $a) {
if (is_array($a)) {
foreach ($a as $int => $row) {
foreach ($a as $row) {
if ($count > 0) {
$out .= "{$key} ";
$out .= "$key ";
}
if ($row['not'] === true) {
$out .= "NOT ";
}
$out .= "{$col} {$row['operator']} {$row['value']} ";
$out .= "$col {$row['operator']} {$row['value']} ";
$count++;
}

} else {
$out .= "{$key} {$a} ";
$out .= ($count) > 0 ? "$key $a " : $a;
$count++;
}
}
}

return $out;
}

/**
* Get the Main FK data protocol
* @return array
*/
final protected function getMainFKData(): array
{
if (is_null($this->fkData)) {
$this->fkData = array();
$this->fkData = [];
foreach ($this->mig->getMig()->getData() as $col => $row) {
if (isset($row['fk'])) {
foreach ($row['fk'] as $a) {
Expand Down Expand Up @@ -315,7 +332,7 @@ final protected function prep(mixed $val, bool $enclose = true): AttrInterface
*/
final protected function prepArr(array $arr, bool $enclose = true): array
{
$new = array();
$new = [];
foreach ($arr as $pKey => $pVal) {
$key = (string)$this->prep($pKey, false);
$new[$key] = (string)$this->prep($pVal, $enclose);
Expand All @@ -324,12 +341,12 @@ final protected function prepArr(array $arr, bool $enclose = true): array
}

/**
* Use vsprintf to mysql prep/protect input in string. Prep string values needs to be eclosed manually
* Use vsprintf to mysql prep/protect input in string. Prep string values needs to be enclosed manually
* @param string $str SQL string example: (id = %d AND permalink = '%s')
* @param array $arr Mysql prep values
* @return string
*/
final protected function sprint(string $str, array $arr = array()): string
final protected function sprint(string $str, array $arr = []): string
{
return vsprintf($str, $this->prepArr($arr, false));
}
Expand All @@ -351,7 +368,8 @@ final protected function camelLoop(array $camelCaseArr, array $valArr, callable
}

/**
* Will extract camle case to array
* MOVE TO DTO ARR
* Will extract camelcase to array
* @param string $value string value with possible camel cases
* @return array
*/
Expand All @@ -369,7 +387,7 @@ final protected function extractCamelCase(string $value): array
*/
final protected function buildJoinFromMig(MigrateInterface $mig, string $type): array
{
$joinArr = array();
$joinArr = [];
$prefix = $this->connInst()->getHandler()->getPrefix();
$main = $this->getMainFKData();
$data = $mig->getData();
Expand Down Expand Up @@ -403,6 +421,7 @@ final protected function buildJoinFromMig(MigrateInterface $mig, string $type):
/**
* Build on YB to col sql string part
* @return string|null
* @throws ConnectException
*/
protected function getAllQueryTables(): ?string
{
Expand All @@ -420,7 +439,7 @@ protected function getAllQueryTables(): ?string
* @param string|null $method
* @param array $args
* @return array|object|bool|string
* @throws DBQueryException
* @throws ResultException|ConnectException
*/
final protected function query(string|self $sql, ?string $method = null, array $args = []): array|object|bool|string
{
Expand All @@ -430,8 +449,8 @@ final protected function query(string|self $sql, ?string $method = null, array $
if (method_exists($query, $method)) {
return call_user_func_array([$query, $method], $args);
}
throw new DBQueryException("Method \"$method\" does not exists!", 1);
throw new ResultException("Method \"$method\" does not exists!", 1);
}
return $query;
}
}
}
1 change: 1 addition & 0 deletions AbstractMigrate.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace MaplePHP\Query;
Expand Down
Loading