Skip to content

Commit

Permalink
correcao
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruno Morais authored and Bruno Morais committed Nov 28, 2023
1 parent 3764641 commit 2103d0a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 31 deletions.
2 changes: 1 addition & 1 deletion example/ExampleDao.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class ExampleDao extends CrudBuilder {

public function __construct()
{
$this->setTable("EXAMPLE");
$this->setTableName("EXAMPLE");
$this->setClassModel("exampleModel");
}

Expand Down
12 changes: 6 additions & 6 deletions src/Crud.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function select(string $fields = '*', string $add = '', array $values = n
$add = ' ' . $add;
}

$sql = "SELECT {$fields} FROM {$this->getTable()}";
$sql = "SELECT {$fields} FROM {$this->getTableName()}";

if (!empty($this->getTableAlias()))
$sql .= " AS {$this->getTableAlias()}";
Expand Down Expand Up @@ -64,7 +64,7 @@ public function insert(string $fields, array $values = null, bool $debug = false
$numparams .= ',?';
}
$numparams = substr($numparams, 1);
$sql = "INSERT INTO {$this->getTable()} ({$fields}) VALUES ({$numparams})";
$sql = "INSERT INTO {$this->getTableName()} ({$fields}) VALUES ({$numparams})";
if ($debug) {
echo $sql.'<pre>'.print_r($values).'</pre>';
return;
Expand Down Expand Up @@ -100,7 +100,7 @@ public function insertObject(object $object)
public function insertArray(array $params): bool
{
if (!empty($params)) {
$query = "INSERT INTO {$this->getTable()}";
$query = "INSERT INTO {$this->getTableName()}";
$values = [];
$dataColumns = array_keys($params);
if (isset($dataColumns[0])) {
Expand Down Expand Up @@ -143,7 +143,7 @@ public function update(string $fields, array $values = null, string $where = nul
$fields_T .= ", {$item} = ?";
}
$fields_T = substr($fields_T, 2);
$sql = "UPDATE {$this->getTable()} SET {$fields_T}";
$sql = "UPDATE {$this->getTableName()} SET {$fields_T}";
if (isset($where)) {
$sql .= " WHERE $where";
}
Expand Down Expand Up @@ -185,7 +185,7 @@ public function updateObject(object $object, string $where): ?bool
public function updateArray(array $params, string $where): bool
{
if (!empty($params)) {
$query = "UPDATE {$this->getTable()} SET";
$query = "UPDATE {$this->getTableName()} SET";
$values = [];

foreach ($params as $index => $column) {
Expand Down Expand Up @@ -218,7 +218,7 @@ public function updateArray(array $params, string $where): bool

public function delete(array $values = null, string $where = null, bool $debug = false)
{
$sql = "DELETE FROM {$this->getTable()}";
$sql = "DELETE FROM {$this->getTableName()}";
if (!empty($where)) {
$sql .= " WHERE $where";
}
Expand Down
20 changes: 9 additions & 11 deletions src/CrudBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
*/
abstract class CrudBuilder extends Crud
{
use DatalayerTrait;

/**
* @var array
*/
Expand Down Expand Up @@ -48,7 +46,7 @@ abstract class CrudBuilder extends Crud
public function selectBuilder(string $fields = "*", array $paramns = []): self
{
try {
$query = "SELECT {$fields} FROM {$this->getTable()}";
$query = "SELECT {$fields} FROM {$this->getTableName()}";
if (!empty($this->getTableAlias()))
$query .= " AS {$this->getTableAlias()}";
$this->add($query, "main", $paramns);
Expand All @@ -71,7 +69,7 @@ protected function insertBuilder(string $fields, array $paramns): self
$numparams .= ',?';
}
$numparams = substr($numparams, 1);
$query = "INSERT INTO {$this->getTable()} ({$fields}) VALUES ({$numparams})";
$query = "INSERT INTO {$this->getTableName()} ({$fields}) VALUES ({$numparams})";
$this->add($query, "main", $paramns);
return $this;
} catch (\PDOException $e) {
Expand All @@ -94,7 +92,7 @@ protected function updateBuilder(string $fields, array $paramns): self
$fields_T .= ", {$item} = ?";
}
$fields_T = substr($fields_T, 2);
$query = "UPDATE {{$this->getTable()}} SET {$fields_T}";
$query = "UPDATE {{$this->getTableName()}} SET {$fields_T}";
$this->add($query, "main", $paramns);
return $this;
} catch (\PDOException $e) {
Expand All @@ -110,7 +108,7 @@ protected function updateBuilder(string $fields, array $paramns): self
protected function deleteBuilder(): self
{
try {
$query = "DELETE FROM {$this->getTable()}";
$query = "DELETE FROM {$this->getTableName()}";
$this->add($query, "main");
return $this;
} catch (\PDOException $e) {
Expand Down Expand Up @@ -359,15 +357,15 @@ protected function executeQuery(): self
foreach ($this->sqlPartsSelect as $key => $part){
if (is_array($part)) {
foreach ($part as $item){
$this->query .= $item;
$this->setQuery($this->getQuery().$item);
}
} else {
$this->query .= $part;
$this->setQuery($this->getQuery().$part);
}

}

$this->executeSQL($this->query, $this->params);
$this->executeSQL($this->getQuery(), $this->getParams());
return $this;
} catch (\PDOException $e) {
$this->setError($e);
Expand All @@ -380,7 +378,7 @@ protected function executeQuery(): self
protected function debug(): void
{
try {
echo $this->query . '<pre>' . print_r($this->params) . '</pre>';
echo $this->getQuery() . '<pre>' . print_r($this->getParams()) . '</pre>';
exit;
} catch (\PDOException $e) {
$this->setError($e);
Expand All @@ -405,7 +403,7 @@ private function add(string $query, string $type, array $params = []): void
}

if (!empty($params))
$this->setParameter($params);
$this->setParams($params);
} catch (\PDOException $e) {
$this->setError($e);
}
Expand Down
35 changes: 22 additions & 13 deletions src/DatalayerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ trait DatalayerTrait

/** @var array
*/
private array $resultArray = [];
private array $data = [];

/** @var string */
private $logSQL;
Expand Down Expand Up @@ -159,7 +159,7 @@ protected function getFields():string
* @param string $tableAlias
* @return CrudBuilder|Crud|DatalayerTrait
*/
protected function setTable(string $tableName, string $tableAlias = ""): self
protected function setTableName(string $tableName, string $tableAlias = ""): self
{
if (!empty($tableAlias))
$this->tableAlias = $tableAlias;
Expand All @@ -170,7 +170,7 @@ protected function setTable(string $tableName, string $tableAlias = ""): self
/**
* @return string
*/
protected function getTable(): string
protected function getTableName(): string
{
return $this->tableName ?? "";
}
Expand Down Expand Up @@ -214,7 +214,7 @@ protected function getPrepare(): PDOStatement
* @param $params
* @return self
*/
protected function setParameter($params): self
protected function setParams($params): self
{
$this->params = array_merge($this->params, $params);
return $this;
Expand All @@ -224,19 +224,28 @@ protected function setParameter($params): self
* @param $params
* @return array
*/
protected function getParameter(): array
protected function getParams(): array
{
return $this->params;
}

protected function getResult(): array
protected function getData(): array
{
return $this->resultArray;
return $this->data;
}

protected function setResult(array $array): self
protected function setData(array $array): self
{
$this->resultArray = $array;
$this->data = $array;
return $this;
}
public function getQuery(): string
{
return $this->query;
}
public function setQuery(string $query): self
{
$this->query = $query;
return $this;
}

Expand Down Expand Up @@ -279,7 +288,7 @@ protected function fetchArrayAssoc(PDOStatement $prepare = null): ?array
try {
$prepare = empty($prepare) ? $this->getPrepare() : $prepare;
$dados = $prepare->fetchAll(PDO::FETCH_ASSOC);
$this->setResult($dados);
$this->setData($dados);
return $dados;
} catch (PDOException $e) {
$this->setError($e);
Expand All @@ -295,7 +304,7 @@ protected function fetchArrayObj(PDOStatement $prepare = null): ?array
try {
$prepare = empty($prepare) ? $this->getPrepare() : $prepare;
$dados = $prepare->fetchAll(PDO::FETCH_OBJ);
$this->setResult($dados);
$this->setData($dados);
return $dados;
} catch (PDOException $e) {
$this->setError($e);
Expand All @@ -313,7 +322,7 @@ protected function fetchArrayClass(PDOStatement $prepare = null, string $classMo
$prepare = empty($prepare) ? $this->getPrepare() : $prepare;
$classModel = empty($classModel) ? $this->getClassModel() : $classModel;
$dados = $prepare->fetchAll(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, CONFIG_DATA_LAYER["directory_models"] . $classModel);
$this->setResult($dados);
$this->setData($dados);
return $dados;
} catch (PDOException $e) {
$this->setError($e);
Expand Down Expand Up @@ -475,7 +484,7 @@ protected function getSQL(): ?string
* @param PDOException $e
* @return void
*/
private function setError(PDOException $e)
protected function setError(PDOException $e)
{
$this->error = $e;
throw new PDOException("{$e->getMessage()}<br/><b>SQL:</b> {$this->getSQL()}");
Expand Down

0 comments on commit 2103d0a

Please sign in to comment.