Add sqlite support.

This commit is contained in:
kj 2022-08-04 05:30:22 -04:00
parent 08d92a2b81
commit 2411704662
3 changed files with 92 additions and 77 deletions

View File

@ -22,74 +22,74 @@ En la siguiente tabla se encuentra la lista de estados de los SGBD que he probad
+ *not supported* como no soportado por el SGBD. + *not supported* como no soportado por el SGBD.
+ *fixed* para aquello que no existe, pero la librería lo traduce a un equivalente. + *fixed* para aquello que no existe, pero la librería lo traduce a un equivalente.
|------------------+---------------+---------+------------| |------------------+---------------+---------------+------------|
| method | MySQL/MariaDB | sqlite3 | postgreSQL | | method | MySQL/MariaDB | sqlite3 | postgreSQL |
|------------------+---------------+---------+------------| |------------------+---------------+---------------+------------|
| db | ok | | | | db | ok | ok | |
|------------------+---------------+---------+------------| |------------------+---------------+---------------+------------|
| query | ok | | | | query | ok | ok | |
|------------------+---------------+---------+------------| |------------------+---------------+---------------+------------|
| resetQuery | ok | | | | resetQuery | ok | ok | |
|------------------+---------------+---------+------------| |------------------+---------------+---------------+------------|
| buildQuery | ok | | | | buildQuery | ok | ok | |
|------------------+---------------+---------+------------| |------------------+---------------+---------------+------------|
| getInstance | ok | | | | getInstance | ok | ok | |
|------------------+---------------+---------+------------| |------------------+---------------+---------------+------------|
| getVars | ok | | | | getVars | ok | ok | |
|------------------+---------------+---------+------------| |------------------+---------------+---------------+------------|
| className | ok | | | | className | ok | ok | |
|------------------+---------------+---------+------------| |------------------+---------------+---------------+------------|
| table | ok | | | | table | ok | ok | |
|------------------+---------------+---------+------------| |------------------+---------------+---------------+------------|
| update | ok | | | | update | ok | ok | |
|------------------+---------------+---------+------------| |------------------+---------------+---------------+------------|
| beginTransaction | ok | | | | beginTransaction | ok | ok | |
|------------------+---------------+---------+------------| |------------------+---------------+---------------+------------|
| rollBack | ok | | | | rollBack | ok | ok | |
|------------------+---------------+---------+------------| |------------------+---------------+---------------+------------|
| commit | ok | | | | commit | ok | ok | |
|------------------+---------------+---------+------------| |------------------+---------------+---------------+------------|
| add | ok | | | | add | ok | ok | |
|------------------+---------------+---------+------------| |------------------+---------------+---------------+------------|
| save | ok | | | | save | ok | ok | |
|------------------+---------------+---------+------------| |------------------+---------------+---------------+------------|
| delete | ok | | | | delete | ok | ok | |
|------------------+---------------+---------+------------| |------------------+---------------+---------------+------------|
| select | ok | | | | select | ok | ok | |
|------------------+---------------+---------+------------| |------------------+---------------+---------------+------------|
| from | ok | | | | from | ok | ok | |
|------------------+---------------+---------+------------| |------------------+---------------+---------------+------------|
| where | ok | | | | where | ok | ok | |
|------------------+---------------+---------+------------| |------------------+---------------+---------------+------------|
| where_in | ok | | | | where_in | ok | ok | |
|------------------+---------------+---------+------------| |------------------+---------------+---------------+------------|
| leftJoin | ok | | | | leftJoin | ok | ok | |
|------------------+---------------+---------+------------| |------------------+---------------+---------------+------------|
| rightJoin | ok | | | | rightJoin | ok | not supported | |
|------------------+---------------+---------+------------| |------------------+---------------+---------------+------------|
| innerJoin | ok | | | | innerJoin | ok | ok | |
|------------------+---------------+---------+------------| |------------------+---------------+---------------+------------|
| and | ok | | | | and | ok | ok | |
|------------------+---------------+---------+------------| |------------------+---------------+---------------+------------|
| or | ok | | | | or | ok | ok | |
|------------------+---------------+---------+------------| |------------------+---------------+---------------+------------|
| groupBy | ok | | | | groupBy | ok | ok | |
|------------------+---------------+---------+------------| |------------------+---------------+---------------+------------|
| limit | ok | | | | limit | ok | ok | |
|------------------+---------------+---------+------------| |------------------+---------------+---------------+------------|
| orderBy | ok | | | | orderBy | ok | ok | |
|------------------+---------------+---------+------------| |------------------+---------------+---------------+------------|
| count | ok | | | | count | ok | ok | |
|------------------+---------------+---------+------------| |------------------+---------------+---------------+------------|
| getById | ok | | | | getById | ok | ok | |
|------------------+---------------+---------+------------| |------------------+---------------+---------------+------------|
| search | ok | | | | search | ok | ok | |
|------------------+---------------+---------+------------| |------------------+---------------+---------------+------------|
| get | ok | | | | get | ok | ok | |
|------------------+---------------+---------+------------| |------------------+---------------+---------------+------------|
| getFirst | ok | | | | getFirst | ok | ok | |
|------------------+---------------+---------+------------| |------------------+---------------+---------------+------------|
| all | ok | | | | all | ok | ok | |
|------------------+---------------+---------+------------| |------------------+---------------+---------------+------------|
| setNull | ok | | | | setNull | ok | ok | |
|------------------+---------------+---------+------------| |------------------+---------------+---------------+------------|

View File

@ -3,8 +3,11 @@
* Database - DuckBrain * Database - DuckBrain
* *
* Clase diseñada para crear y devolver una única instancia PDO (database). * Clase diseñada para crear y devolver una única instancia PDO (database).
* Depende de manera forzada de que estén definidas las constantes: * Hace uso de las siguientes constantes:
* DB_TYPE, DB_NAME, DB_HOST, DB_USER. DB_PASS * DB_TYPE, DB_NAME, DB_HOST, DB_USER, DB_PASS
*
* Si DB_TYPE es sqlite, usará DB_NAME como el nombre del archivo sqlite.
* Además DB_USER y DB_PASS, no será necesariop que estén definidos.
* *
* @author KJ * @author KJ
* @website https://kj2.me * @website https://kj2.me
@ -29,7 +32,14 @@ class Database extends PDO {
*/ */
static public function getInstance() : PDO { static public function getInstance() : PDO {
if (is_null(self::$db)) { if (is_null(self::$db)) {
if (DB_TYPE == 'sqlite') {
$dsn = DB_TYPE .':'. DB_NAME;
!defined('DB_USER') && define('DB_USER', '');
!defined('DB_PASS') && define('DB_PASS', '');
} else
$dsn = DB_TYPE.':dbname='.DB_NAME.';host='.DB_HOST; $dsn = DB_TYPE.':dbname='.DB_NAME.';host='.DB_HOST;
try { try {
self::$db = new PDO($dsn, DB_USER, DB_PASS); self::$db = new PDO($dsn, DB_USER, DB_PASS);
} catch (PDOException $e) { } catch (PDOException $e) {
@ -38,6 +48,7 @@ class Database extends PDO {
'Error at connect to database: ' . $e->getMessage() 'Error at connect to database: ' . $e->getMessage()
); );
} }
self::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); self::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
self::$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); self::$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
} }

View File

@ -733,9 +733,13 @@ class Model {
static::$queryVars[':search'] = $search; static::$queryVars[':search'] = $search;
$where = []; $where = [];
foreach($in as $row) { if (DB_TYPE == 'sqlite')
foreach($in as $row)
$where[] = "$row LIKE '%' || :search || '%'";
else
foreach($in as $row)
$where[] = "$row LIKE CONCAT('%', :search, '%')"; $where[] = "$row LIKE CONCAT('%', :search, '%')";
}
if (static::$querySelect['where']=='') if (static::$querySelect['where']=='')
static::$querySelect['where'] = join(' OR ', $where); static::$querySelect['where'] = join(' OR ', $where);