1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the Ocrend Framewok 3 package. |
||
5 | * |
||
6 | * (c) Ocrend Software <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace Ocrend\Kernel\Database\Drivers\Sqlite; |
||
13 | |||
14 | use Ocrend\Kernel\Database\Driver; |
||
15 | |||
16 | /** |
||
17 | * Driver de conexión con Sqlite utilizando PDO |
||
18 | * |
||
19 | * @author Brayan Narváez <[email protected]> |
||
20 | */ |
||
21 | class Sqlite extends \PDO implements Driver { |
||
22 | |||
23 | /** |
||
24 | * Constructor de la clase |
||
25 | */ |
||
26 | public function __construct() { |
||
27 | global $config; |
||
28 | |||
29 | # Configuración |
||
30 | $sqlite3 = $config['database']['drivers']['sqlite3']; |
||
31 | |||
32 | # Establecer conexión, si la base de datos no existe, es creada |
||
33 | parent::__construct('sqlite:' . str_replace('___ROOT___',___ROOT___,$sqlite3['file'])); |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * Escapa caracteres para evitar sql injection |
||
38 | * |
||
39 | * @param string $param : Parámetro |
||
40 | * |
||
41 | * @return string escapado |
||
42 | */ |
||
43 | public function scape($param) : string { |
||
44 | if (null === $param) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
45 | return ''; |
||
46 | } |
||
47 | if (is_numeric($param) and $param <= 2147483647) { |
||
48 | if (explode('.', $param)[0] != $param) { |
||
49 | return (string) $param; |
||
50 | } |
||
51 | return (string) $param; |
||
52 | } |
||
53 | return (string) trim(str_replace(['\\', "\x00", '\n', '\r', "'", '"', "\x1a"], ['\\\\', '\\0', '\\n', '\\r', "\'", '\"', '\\Z'], $param)); |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * Selecciona elementos de una tabla y devuelve un objeto |
||
58 | * |
||
59 | * @param string $fields: Campos |
||
60 | * @param string $table: Tabla |
||
61 | * @param null|string $inners: Inners |
||
62 | * @param null|string $where : Condiciones |
||
63 | * @param null|int $limit: Límite de resultados |
||
64 | * @param string $extra: Instrucciones extras |
||
65 | * |
||
66 | * @return bool|stdClass |
||
0 ignored issues
–
show
|
|||
67 | */ |
||
68 | public function select(string $fields, string $table, $inners = null, $where = null, $limit = null, string $extra = '') { |
||
69 | $result = $this->query("SELECT $fields FROM $table $inners " |
||
70 | . (null != $where ? "WHERE $where" : '') |
||
0 ignored issues
–
show
|
|||
71 | . " $extra " |
||
72 | . (null !== $limit ? "LIMIT $limit" : '') |
||
73 | ); |
||
74 | |||
75 | if(false != $result && sizeof($result)) { |
||
76 | $matriz = (array) $result->fetchAll(PDO::FETCH_ASSOC); |
||
0 ignored issues
–
show
|
|||
77 | $result->closeCursor(); |
||
78 | |||
79 | return $matriz; |
||
0 ignored issues
–
show
|
|||
80 | } |
||
81 | |||
82 | return false; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Actualiza elementos de una tabla en la base de datos según una condición |
||
87 | * |
||
88 | * @param string $table: Tabla a actualizar |
||
89 | * @param array $e: Arreglo asociativo de elementos, con la estrctura 'campo_en_la_tabla' => 'valor_a_insertar_en_ese_campo', |
||
90 | * todos los elementos del arreglo $e, serán sanados por el método sin necesidad de hacerlo manualmente al crear el arreglo |
||
91 | * @param null|string $where: Condición que indica quienes serán modificados |
||
92 | * @param null|string $limite: Límite de elementos modificados, por defecto los modifica a todos |
||
93 | * |
||
94 | * @throws \RuntimeException si el arreglo está vacío |
||
95 | * @return int con la cantidad de tablas afectadas |
||
96 | */ |
||
97 | public function update(string $table, array $e, $where = null, $limit = null) : int { |
||
98 | if (sizeof($e) == 0) { |
||
99 | throw new \RuntimeException('El arreglo pasado por $this->db->update(\'' . $table . '\'...) está vacío.'); |
||
100 | } |
||
101 | |||
102 | $query = "UPDATE $table SET "; |
||
103 | foreach ($e as $campo => $valor) { |
||
104 | $query .= $campo . '=\'' . $this->scape($valor) . '\','; |
||
105 | } |
||
106 | $query[strlen($query) - 1] = ' '; |
||
107 | |||
108 | return $this->exec($query |
||
109 | . (null != $where ? "WHERE $where" : '') |
||
110 | . (null !== $limit ? "LIMIT $limit" : '') |
||
111 | ); |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Inserta una serie de elementos a una tabla en la base de datos |
||
116 | * |
||
117 | * @param string $table: Tabla a la cual se le va a insertar elementos |
||
118 | * @param array $e: Arreglo asociativo de elementos, con la estrctura 'campo_en_la_tabla' => 'valor_a_insertar_en_ese_campo', |
||
119 | * todos los elementos del arreglo $e, serán sanados por el método sin necesidad de hacerlo manualmente al crear el arreglo |
||
120 | * |
||
121 | * @throws \RuntimeException si el arreglo está vacío |
||
122 | * |
||
123 | * @return int con el PRIMARY AUTO_INCREMENT de el último elemento insertado |
||
124 | */ |
||
125 | public function insert(string $table, array $e) : int { |
||
126 | if (sizeof($e) == 0) { |
||
127 | throw new \RuntimeException('El arreglo pasado por $this->db->insert(\'' . $table . '\',...) está vacío.'); |
||
128 | } |
||
129 | |||
130 | $query = "INSERT INTO $table ("; |
||
131 | $values = ''; |
||
132 | foreach ($e as $campo => $v) { |
||
133 | $query .= $campo . ','; |
||
134 | $values .= '\'' . $this->scape($v) . '\','; |
||
135 | } |
||
136 | $query[strlen($query) - 1] = ')'; |
||
137 | $values[strlen($values) - 1] = ')'; |
||
138 | $query .= ' VALUES (' . $values . ';'; |
||
139 | |||
140 | $this->exec($query); |
||
141 | |||
142 | return $this->lastInsertId(); |
||
0 ignored issues
–
show
|
|||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Elimina elementos de una tabla y devuelve la cantidad de filas afectadas |
||
147 | * |
||
148 | * @param string $table: Tabla a la cual se le quiere remover un elemento |
||
149 | * @param null|string $where: Condición de borrado que define quien/quienes son dichos elementos |
||
150 | * @param null|string $limit: Por defecto se limita a borrar un solo elemento que cumpla el $where |
||
151 | * |
||
152 | * @return int cantidad de filas afectadas |
||
153 | */ |
||
154 | public function delete(string $table, $where = null, $limit = null) : int { |
||
155 | return $this->exec("DELETE FROM $table " . (null != $where ? "WHERE $where" : ' ') . (null !== $limit ? "LIMIT $limit" : '')); |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * __destruct() |
||
160 | */ |
||
161 | public function __destruct() {} |
||
162 | } |