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.
		
		
		
		
			
				
					157 lines
				
				4.4 KiB
			
		
		
			
		
	
	
					157 lines
				
				4.4 KiB
			| 
								 
											6 years ago
										 
									 | 
							
								<?php
							 | 
						||
| 
								 
											4 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;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								use Phacil\Framework\Databases\Api\DriverInterface;
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								use \SQLite3 as nativeSQLite3;
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								class SQLite3 implements DriverInterface {
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								    const DB_TYPE = 'SQLite3';
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    const DB_TYPE_ID = 5;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    /**
							 | 
						||
| 
								 | 
							
								     * 
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								     * @var nativeSQLite3
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								     */
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								    private $connection;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    /**
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								     * {@inheritdoc}
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								     */
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								    public function __construct($hostname, $username = null, $password = null, $database, $port = '3306', $charset = 'utf8mb4')
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								        $this->connection = new nativeSQLite3($hostname.$database, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE, $password);
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								        if (!$this->connection) {
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								            throw new \Phacil\Framework\Exception('Error: ' . $this->connection->lastErrorMsg()  . '<br />Error No: ' . $this->connection->lastErrorCode());
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								        }
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    /**
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								     * 
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								     * @inheritdoc
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								     */
							 | 
						||
| 
								 
											6 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;
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								            /** @var \Phacil\Framework\Databases\Object\ResultInterface */
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								            $result = \Phacil\Framework\Registry::getInstance()->create(\Phacil\Framework\Databases\Object\ResultInterface::class, [$data]);
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								            $result->setNumRows((!empty($data)) ? count($data) : 0);
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								            $query->finalize();
							 | 
						||
| 
								 | 
							
								            return $result;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        } else {
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								            throw new \Phacil\Framework\Exception('Error: ' . $this->connection->lastErrorMsg()  . '<br />Error No: ' . $this->connection->lastErrorCode() . '<br />' . $sql);
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    /**
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								     * {@inheritdoc}
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								     */
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								    public function escape($value) {
							 | 
						||
| 
								 | 
							
								        return $this->connection->escapeString($value);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    /** {@inheritdoc} */
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								    public function countAffected() {
							 | 
						||
| 
								 | 
							
								        return $this->connection->changes();
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								    /** {@inheritdoc} */
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								    public function getLastId() {
							 | 
						||
| 
								 | 
							
								        return $this->connection->lastInsertRowID();
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    /** {@inheritdoc} */
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								    public function isConnected() {
							 | 
						||
| 
								 | 
							
								        return ($this->connection) ? true : false;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    /**
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								     * {@inheritdoc}
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								     */
							 | 
						||
| 
								 | 
							
								    public function execute($sql, array $params = [])
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        if (!empty($params)) {
							 | 
						||
| 
								 | 
							
								            // Bind parameters if there are any
							 | 
						||
| 
								 | 
							
								            foreach ($params as $placeholder => &$param) {
							 | 
						||
| 
								 | 
							
								                $sql = str_replace($placeholder, ':' . $placeholder, $sql);
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								            $stmt = $this->connection->prepare($sql);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								            if ($stmt === false) {
							 | 
						||
| 
								 | 
							
								                throw new \Phacil\Framework\Exception('Error preparing query: ' . $this->connection->lastErrorMsg());
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								            foreach ($params as $placeholder => &$param) {
							 | 
						||
| 
								 | 
							
								                $stmt->bindValue(':' . $placeholder, $param);
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								            $result = $stmt->execute();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								            if ($result === false) {
							 | 
						||
| 
								 | 
							
								                throw new \Phacil\Framework\Exception('Error executing query: ' . $this->connection->lastErrorMsg());
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								            // Processar resultados se for um SELECT
							 | 
						||
| 
								 | 
							
								            if ($result instanceof \SQLite3Result) {
							 | 
						||
| 
								 | 
							
								                $data = [];
							 | 
						||
| 
								 | 
							
								                while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
							 | 
						||
| 
								 | 
							
								                    $data[] = $row;
							 | 
						||
| 
								 | 
							
								                }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								                /** @var \Phacil\Framework\Databases\Object\ResultInterface */
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								                $resultObj = \Phacil\Framework\Registry::getInstance()->create(\Phacil\Framework\Databases\Object\ResultInterface::class, [$data]);
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								                $resultObj->setNumRows(count($data));
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								                $result->finalize();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								                return $resultObj;
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								            // Se não for um SELECT, apenas retornar verdadeiro
							 | 
						||
| 
								 | 
							
								            return true;
							 | 
						||
| 
								 | 
							
								        } else {
							 | 
						||
| 
								 | 
							
								            // Se não há parâmetros, executar diretamente sem consulta preparada
							 | 
						||
| 
								 | 
							
								            return $this->query($sql);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								    /**
							 | 
						||
| 
								 | 
							
									 * {@inheritdoc}
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 | 
							
									public function getDBType() { 
							 | 
						||
| 
								 | 
							
										return self::DB_TYPE;
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/**
							 | 
						||
| 
								 | 
							
									 * {@inheritdoc}
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 | 
							
									public function getDBTypeId() {
							 | 
						||
| 
								 | 
							
										return self::DB_TYPE_ID;
							 | 
						||
| 
								 | 
							
									 }
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								}
							 |