A super easy PHP Framework for web development!
https://github.com/exacti/phacil-framework
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
91 lines
2.4 KiB
91 lines
2.4 KiB
5 years ago
|
<?php
|
||
3 years ago
|
/*
|
||
|
* Copyright © 2021 ExacTI Technology Solutions. All rights reserved.
|
||
|
* GPLv3 General License.
|
||
|
* https://exacti.com.br
|
||
|
* Phacil PHP Framework - https://github.com/exacti/phacil-framework
|
||
|
*/
|
||
|
|
||
|
namespace Phacil\Framework\Databases;
|
||
|
|
||
|
use Exception;
|
||
|
use \SQLite3;
|
||
|
use stdClass;
|
||
5 years ago
|
|
||
|
final class Sqlite3_db {
|
||
3 years ago
|
/**
|
||
|
*
|
||
|
* @var SQLite3
|
||
|
*/
|
||
5 years ago
|
private $connection;
|
||
|
|
||
|
public function __construct($hostname, $username = null, $password = null, $database, $port = '3306', $charset = 'utf8mb4')
|
||
|
{
|
||
3 years ago
|
$this->connection = new \SQLite3($hostname.$database, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE, $password);
|
||
5 years ago
|
|
||
|
if (!$this->connection) {
|
||
|
throw new \Exception('Error: ' . $this->connection->lastErrorMsg() . '<br />Error No: ' . $this->connection->lastErrorCode());
|
||
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
/**
|
||
|
* @param string $sql
|
||
|
* @return true|stdClass
|
||
|
* @throws Exception
|
||
|
*/
|
||
5 years ago
|
public function query($sql){
|
||
|
//$query = $this->connection->query($sql);
|
||
|
|
||
|
if ($stm = $this->connection->prepare($sql)) {
|
||
|
|
||
|
$query = $stm->execute();
|
||
|
|
||
|
if (!$query instanceof \SQLite3Result || $query->numColumns() == 0)
|
||
|
return true;
|
||
|
|
||
|
|
||
|
$data = [];
|
||
|
while ($row = $query->fetchArray(SQLITE3_ASSOC)) {
|
||
|
$data[] = $row;
|
||
|
}
|
||
|
$result = new \stdClass();
|
||
|
$result->num_rows = (!empty($data)) ? count($data) : 0;
|
||
|
$result->row = isset($data[0]) ? $data[0] : array();
|
||
|
$result->rows = $data;
|
||
|
$query->finalize();
|
||
|
return $result;
|
||
|
|
||
|
} else {
|
||
|
throw new \Exception('Error: ' . $this->connection->lastErrorMsg() . '<br />Error No: ' . $this->connection->lastErrorCode() . '<br />' . $sql);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
3 years ago
|
/**
|
||
|
* @param string $value
|
||
|
* @return string
|
||
|
*/
|
||
5 years ago
|
public function escape($value) {
|
||
|
return $this->connection->escapeString($value);
|
||
|
}
|
||
|
|
||
3 years ago
|
/** @return int */
|
||
5 years ago
|
public function countAffected() {
|
||
|
return $this->connection->changes();
|
||
|
}
|
||
3 years ago
|
|
||
|
/** @return int */
|
||
5 years ago
|
public function getLastId() {
|
||
|
return $this->connection->lastInsertRowID();
|
||
|
}
|
||
|
|
||
3 years ago
|
/** @return bool */
|
||
5 years ago
|
public function isConnected() {
|
||
|
return ($this->connection) ? true : false;
|
||
|
}
|
||
|
|
||
3 years ago
|
/** @return void */
|
||
5 years ago
|
public function __destruct() {
|
||
|
$this->connection->close();
|
||
|
}
|
||
|
}
|