1
|
|
|
<?php
|
2
|
|
|
namespace BMorais\Database;
|
3
|
|
|
|
4
|
|
|
/**
|
5
|
|
|
* CLASSE CRUD
|
6
|
|
|
* Classe abastrada para fazer ligação entre o banco e aplicação
|
7
|
|
|
*
|
8
|
|
|
* @author Bruno Morais <[email protected]>
|
9
|
|
|
* @copyright GPL © 2022, bmorais.com
|
10
|
|
|
* @package bmorais\database
|
11
|
|
|
* @subpackage class
|
12
|
|
|
* @access private
|
13
|
|
|
*/
|
14
|
|
|
|
15
|
|
|
abstract class Crud {
|
16
|
|
|
|
17
|
|
|
use DatalayerTrait;
|
|
|
|
|
18
|
|
|
|
19
|
|
|
/**
|
20
|
|
|
* @param string $fields
|
21
|
|
|
* @param string $add
|
22
|
|
|
* @param array|null $values
|
23
|
|
|
* @param bool $returnModel
|
24
|
|
|
* @param bool $debug
|
25
|
|
|
* @return array|false|void
|
26
|
|
|
*/
|
27
|
|
|
public function select(string $fields = "*", string $add = "", array $values = null, bool $returnModel = false, bool $debug = false)
|
28
|
|
|
{
|
29
|
|
|
if (strlen($add)>0) { $add = " " . $add; }
|
30
|
|
|
$sql = "SELECT {$fields} FROM {$this->tableName}{$add}";
|
31
|
|
|
if ($debug) { echo $sql; die(); }
|
|
|
|
|
32
|
|
|
if ($returnModel) {
|
33
|
|
|
return $this->selectDB($sql, $values, $this->classModel);
|
34
|
|
|
} else {
|
35
|
|
|
return $this->selectDB($sql, $values, null);
|
36
|
|
|
}
|
37
|
|
|
}
|
38
|
|
|
|
39
|
|
|
/**
|
40
|
|
|
* @param string $fields
|
41
|
|
|
* @param array|null $values
|
42
|
|
|
* @param $debug
|
43
|
|
|
* @return bool|void
|
44
|
|
|
*/
|
45
|
|
|
public function insert(string $fields, array $values = null, $debug = false)
|
46
|
|
|
{
|
47
|
|
|
$numparams = "";
|
48
|
|
|
foreach ($values as $item) { $numparams .= ",?"; }
|
49
|
|
|
$numparams = substr($numparams, 1);
|
50
|
|
|
$sql = "INSERT INTO {$this->tableName} ({$fields}) VALUES ({$numparams})";
|
51
|
|
|
if ($debug) { echo $sql; echo "<pre>"; print_r($values); echo "</pre>"; die(); }
|
|
|
|
|
52
|
|
|
return $this->insertDB($sql, $values);
|
53
|
|
|
}
|
54
|
|
|
|
55
|
|
|
/**
|
56
|
|
|
* @param string $fields
|
57
|
|
|
* @param array|null $values
|
58
|
|
|
* @param string|null $where
|
59
|
|
|
* @param bool $debug
|
60
|
|
|
* @return bool|void
|
61
|
|
|
*/
|
62
|
|
|
public function update(string $fields, array $values = null, string $where = null, bool $debug = false)
|
63
|
|
|
{
|
64
|
|
|
$fields_T = "";
|
65
|
|
|
$atributos = explode(",", $fields);
|
66
|
|
|
|
67
|
|
|
foreach ($atributos as $item) { $fields_T .= ", {$item} = ?"; }
|
68
|
|
|
$fields_T = substr($fields_T, 2);
|
69
|
|
|
$sql = "UPDATE {$this->tableName} SET {$fields_T}";
|
70
|
|
|
if (isset($where)) { $sql .= " WHERE $where"; }
|
71
|
|
|
if ($debug) { echo $sql; echo "<pre>"; print_r($values); echo "</pre>"; die(); }
|
|
|
|
|
72
|
|
|
return $this->updateDB($sql, $values);
|
73
|
|
|
}
|
74
|
|
|
|
75
|
|
|
/**
|
76
|
|
|
* @param array|null $values
|
77
|
|
|
* @param string|null $where
|
78
|
|
|
* @param bool $debug
|
79
|
|
|
* @return bool|void
|
80
|
|
|
*/
|
81
|
|
|
public function delete(array $values = null, string $where = null, bool $debug = false)
|
82
|
|
|
{
|
83
|
|
|
$sql = "DELETE FROM {$this->tableName}";
|
84
|
|
|
if (isset($where)) { $sql .= " WHERE $where"; }
|
85
|
|
|
if ($debug) { echo $sql; echo "<pre>"; print_r($values); echo "</pre>"; die(); }
|
|
|
|
|
86
|
|
|
return $this->deleteDB($sql, $values);
|
87
|
|
|
}
|
88
|
|
|
|
89
|
|
|
/**
|
90
|
|
|
* @return false|string
|
91
|
|
|
*/
|
92
|
|
|
public function lastInsertId()
|
93
|
|
|
{
|
94
|
|
|
return $this->lastId();
|
95
|
|
|
}
|
96
|
|
|
|
97
|
|
|
/**
|
98
|
|
|
* @return mixed
|
99
|
|
|
*/
|
100
|
|
|
public function getLogSQL(){
|
101
|
|
|
return $this->logSQL;
|
102
|
|
|
}
|
103
|
|
|
|
104
|
|
|
} |