2017-08-04 16:28:16 +02:00
< ? php
/**
* Classe per gestire la connessione al database .
*
* @ since 2.3
*/
class Database extends Util\Singleton
{
2017-08-31 11:32:49 +02:00
/** @var string Host del database */
2017-08-04 16:28:16 +02:00
protected $host ;
2017-08-31 11:32:49 +02:00
/** @var int Porta di accesso del database */
2017-08-04 16:28:16 +02:00
protected $port ;
2017-08-31 11:32:49 +02:00
/** @var string Username di accesso */
2017-08-04 16:28:16 +02:00
protected $username ;
2017-08-31 11:32:49 +02:00
/** @var string Password di accesso */
2017-08-04 16:28:16 +02:00
protected $password ;
2017-08-31 11:32:49 +02:00
/** @var string Nome del database */
2017-08-04 16:28:16 +02:00
protected $database_name ;
2017-08-31 11:32:49 +02:00
/** @var string Charset della comunicazione */
2017-08-04 16:28:16 +02:00
protected $charset ;
2017-08-31 11:32:49 +02:00
/** @var array Opzioni riguardanti la comunicazione (PDO) */
2017-08-04 16:28:16 +02:00
protected $option = [];
2017-08-31 11:32:49 +02:00
/** @var DebugBar\DataCollector\PDO\TraceablePDO Classe PDO tracciabile */
2017-08-04 16:28:16 +02:00
protected $pdo ;
2017-08-31 11:32:49 +02:00
/** @var bool Stato di installazione del database */
2017-08-04 16:28:16 +02:00
protected $is_installed ;
2017-08-31 11:32:49 +02:00
/** @var string Versione corrente di MySQL */
2017-08-04 16:28:16 +02:00
protected $mysql_version ;
/**
* Costruisce la nuova connessione al database .
2018-02-20 14:56:13 +01:00
* Ispirato dal framework open - source Medoo .
2017-08-04 16:28:16 +02:00
*
* @ param string | array $server
* @ param string $username
* @ param string $password
* @ param string $database_name
* @ param string $charset
* @ param array $option
*
* @ since 2.3
*
* @ return Database
*/
2017-08-31 11:32:49 +02:00
protected function __construct ( $server , $username , $password , $database_name , $charset = null , $option = [ \PDO :: ATTR_ERRMODE => \PDO :: ERRMODE_EXCEPTION ])
2017-08-04 16:28:16 +02:00
{
if ( is_array ( $server )) {
$host = $server [ 'host' ];
$port = ! empty ( $server [ 'port' ]) ? $server [ 'port' ] : null ;
} else {
$temp = explode ( ':' , $server );
$host = $temp [ 0 ];
$port = ! empty ( $temp [ 1 ]) ? $temp [ 1 ] : null ;
}
2018-05-11 14:48:46 +02:00
// Possibilità di specificare una porta per il servizio MySQL diversa dalla standard 3306
$port = ! empty ( App :: getConfig ()[ 'port' ]) ? App :: getConfig ()[ 'port' ] : $port ;
2017-08-04 16:28:16 +02:00
$this -> host = $host ;
if ( ! empty ( $port ) && is_int ( $port * 1 )) {
$this -> port = $port ;
}
$this -> username = $username ;
$this -> password = $password ;
$this -> database_name = $database_name ;
$this -> charset = $charset ;
$this -> option = $option ;
if ( ! empty ( $this -> host ) && ! empty ( $this -> database_name )) {
try {
2017-09-01 12:55:48 +02:00
$pdo = new PDO (
2017-08-04 16:28:16 +02:00
'mysql:host=' . $this -> host . ( ! empty ( $this -> port ) ? ';port=' . $this -> port : '' ) . ';dbname=' . $this -> database_name ,
$this -> username ,
$this -> password ,
$this -> option
2017-09-01 12:55:48 +02:00
);
2018-03-02 19:01:30 +01:00
if ( App :: getConfig ()[ 'debug' ]) {
2017-09-01 12:55:48 +02:00
$pdo = new \DebugBar\DataCollector\PDO\TraceablePDO ( $pdo );
}
$this -> pdo = $pdo ;
2017-08-31 11:32:49 +02:00
2017-09-01 18:13:25 +02:00
if ( empty ( $this -> charset ) && version_compare ( $this -> getMySQLVersion (), '5.5.3' ) >= 0 ) {
$this -> charset = 'utf8mb4' ;
2017-08-31 11:32:49 +02:00
}
2017-08-04 16:28:16 +02:00
2017-08-31 10:09:06 +02:00
// Fix per problemi di compatibilità delle password MySQL 4.1+ (da versione precedente)
2017-08-31 11:32:49 +02:00
$this -> pdo -> query ( 'SET SESSION old_passwords = 0' );
//$this->pdo->query('SET PASSWORD = PASSWORD('.$this->prepare($this->password).')');
2017-08-31 10:09:06 +02:00
2017-08-31 11:32:49 +02:00
// Impostazione del charset della comunicazione
if ( ! empty ( $this -> charset )) {
$this -> pdo -> query ( " SET NAMES ' " . $this -> charset . " ' " );
}
2017-08-31 10:09:06 +02:00
2017-08-31 11:32:49 +02:00
// Reset della modalità di esecuzione MySQL per la sessione corrente
$this -> pdo -> query ( " SET sql_mode = '' " );
2017-08-04 16:28:16 +02:00
} catch ( PDOException $e ) {
if ( $e -> getCode () == 1049 || $e -> getCode () == 1044 ) {
2017-09-04 12:02:29 +02:00
$e = new PDOException (( $e -> getCode () == 1049 ) ? tr ( 'Database non esistente!' ) : tr ( 'Credenziali di accesso invalide!' ));
2017-08-04 16:28:16 +02:00
}
2017-09-04 12:02:29 +02:00
$this -> signal ( $e , tr ( 'Errore durante la connessione al database' ), [ 'throw' => false , 'session' => false ]);
2017-08-04 16:28:16 +02:00
}
}
}
/**
* Restituisce la connessione attiva al database , creandola nel caso non esista .
*
* @ since 2.3
*
* @ return Database
*/
2018-03-30 21:27:57 +02:00
public static function getConnection ( $new = false , $data = [])
2017-08-04 16:28:16 +02:00
{
2018-03-02 19:01:30 +01:00
$class = get_called_class ();
2017-08-07 13:07:18 +02:00
if ( empty ( parent :: $instance [ $class ]) || ! parent :: $instance [ $class ] -> isConnected () || $new ) {
2018-03-02 19:01:30 +01:00
$config = App :: getConfig ();
2017-08-04 16:28:16 +02:00
2018-03-30 21:27:57 +02:00
// Sostituzione degli eventuali valori aggiuntivi
$config = array_merge ( $config , $data );
2018-03-02 19:01:30 +01:00
parent :: $instance [ $class ] = new self ( $config [ 'db_host' ], $config [ 'db_username' ], $config [ 'db_password' ], $config [ 'db_name' ]);
2017-08-04 16:28:16 +02:00
}
2017-08-07 13:07:18 +02:00
return parent :: $instance [ $class ];
2017-08-04 16:28:16 +02:00
}
public static function getInstance ()
{
return self :: getConnection ();
}
/**
* Restituisce l ' oggetto PDO artefice della connessione .
*
* @ since 2.3
*
2018-07-03 15:39:29 +02:00
* @ return \DebugBar\DataCollector\PDO\TraceablePDO | PDO
2017-08-04 16:28:16 +02:00
*/
public function getPDO ()
{
return $this -> pdo ;
}
/**
* Controlla se la connessione è valida e andata a buon fine .
*
* @ since 2.3
*
* @ return bool
*/
public function isConnected ()
{
return ! empty ( $this -> pdo );
}
/**
* Controlla se il database necessario al progetto esiste .
*
* @ since 2.3
*
* @ return bool
*/
public function isInstalled ()
{
if ( empty ( $this -> is_installed )) {
2018-07-03 15:05:27 +02:00
$this -> is_installed = $this -> tableExists ( 'zz_modules' );
2017-08-04 16:28:16 +02:00
}
return $this -> is_installed ;
}
/**
* Restituisce la versione del DBMS MySQL in utilizzo .
*
* @ since 2.3
*
2018-06-26 16:33:15 +02:00
* @ return string
2017-08-04 16:28:16 +02:00
*/
public function getMySQLVersion ()
{
if ( empty ( $this -> mysql_version ) && $this -> isConnected ()) {
$ver = $this -> fetchArray ( 'SELECT VERSION()' );
if ( ! empty ( $ver [ 0 ][ 'VERSION()' ])) {
$this -> mysql_version = explode ( '-' , $ver [ 0 ][ 'VERSION()' ])[ 0 ];
}
}
return $this -> mysql_version ;
}
/**
* Restituisce il nome del database a cui si è connessi .
*
* @ since 2.3
*
* @ return string
*/
public function getDatabaseName ()
{
return $this -> database_name ;
}
/**
* Esegue la query indicata , restituendo l ' identificativo della nuova entità se si tratta di una query di inserimento .
*
* @ since 2.0
*
* @ param string $query Query da eseguire
*
* @ return int
*/
public function query ( $query , $signal = null , $options = [])
{
try {
$this -> pdo -> query ( $query );
$id = $this -> lastInsertedID ();
if ( $id == 0 ) {
return 1 ;
} else {
return $id ;
}
} catch ( PDOException $e ) {
$signal = empty ( $signal ) ? $query : $signal ;
$this -> signal ( $e , $signal , $options );
}
}
/**
* Restituisce un ' array strutturato in base ai nomi degli attributi della selezione .
*
* @ since 2.0
*
* @ param string $query Query da eseguire
*
* @ return array
*/
2017-08-30 14:43:38 +02:00
public function fetchArray ( $query , $numeric = false , $options = [])
2017-08-04 16:28:16 +02:00
{
try {
$mode = empty ( $numeric ) ? PDO :: FETCH_ASSOC : PDO :: FETCH_NUM ;
$result = $this -> pdo -> query ( $query ) -> fetchAll ( $mode );
return $result ;
} catch ( PDOException $e ) {
2017-08-30 14:43:38 +02:00
$this -> signal ( $e , $query , $options );
2017-08-04 16:28:16 +02:00
}
}
/**
* Restituisce un ' array strutturato in base agli indici degli attributi della selezione .
*
* @ since 2.0
* @ deprecated 2.3
*
* @ param string $query Query da eseguire
*
* @ return array
*/
public function fetchRows ( $query )
{
return $this -> fetchArray ( $query , true );
}
/**
* Restituisce il primo elemento della selezione , strutturato in base ai nomi degli attributi .
*
* @ since 2.0
* @ deprecated 2.3
*
* @ param string $query Query da eseguire
*
* @ return array
*/
public function fetchRow ( $query )
{
2018-06-23 15:41:32 +02:00
return $this -> fetchOne ( $query );
}
/**
* Restituisce il primo elemento della selezione , strutturato in base ai nomi degli attributi .
* Attenzione : aggiunge il LIMIT relativo a fine della query .
*
* @ since 2.4
*
* @ param string $query Query da eseguire
*
* @ return array
*/
public function fetchOne ( $query )
{
if ( ! str_contains ( $query , 'LIMIT' )) {
$query .= ' LIMIT 1' ;
}
2017-08-04 16:28:16 +02:00
$result = $this -> fetchArray ( $query );
2018-06-23 15:41:32 +02:00
if ( isset ( $result [ 0 ])) {
2017-08-04 16:28:16 +02:00
return $result [ 0 ];
}
return $result ;
}
/**
* Restituisce il numero dei risultati della selezione .
*
* @ since 2.0
*
* @ param string $query Query da eseguire
*
2018-07-03 15:05:27 +02:00
* @ return int
2017-08-04 16:28:16 +02:00
*/
public function fetchNum ( $query )
{
2018-07-03 15:05:27 +02:00
$result = $this -> fetchArray ( 'SELECT COUNT(*) as `tot` FROM (' . $query . ') AS `count`' );
if ( ! empty ( $result )) {
return $result [ 0 ][ 'tot' ];
2017-08-04 16:28:16 +02:00
}
2018-07-03 15:05:27 +02:00
return 0 ;
}
public function tableExists ( $table )
{
$results = null ;
if ( $this -> isConnected ()) {
$results = $this -> fetchArray ( " SHOW TABLES LIKE ' " . $table . " ' " );
}
return ! empty ( $results );
2017-08-04 16:28:16 +02:00
}
/**
* Restituisce l 'identificativo dell' ultimo elemento inserito .
*
* @ since 2.0
* @ deprecated 2.3
*
* @ return int
*/
public function last_inserted_id ()
{
return $this -> lastInsertedID ();
}
/**
* Restituisce l 'identificativo dell' ultimo elemento inserito .
*
* @ since 2.3
*
* @ return int
*/
public function lastInsertedID ()
{
try {
return $this -> pdo -> lastInsertId ();
} catch ( PDOException $e ) {
2017-09-04 12:02:29 +02:00
$this -> signal ( $e , tr ( " Impossibile ottenere l'ultimo identificativo creato " ));
2017-08-04 16:28:16 +02:00
}
}
/**
* Prepara il parametro inserito per l ' inserimento in una query SQL .
* Attenzione : protezione di base contro SQL Injection .
*
* @ param string $parameter
*
* @ since 2.3
*
2018-06-26 16:33:15 +02:00
* @ return mixed
2017-08-04 16:28:16 +02:00
*/
public function prepare ( $parameter )
{
return $this -> pdo -> quote ( $parameter );
}
/**
* Prepara il campo per l ' inserimento in uno statement SQL .
*
* @ since 2.3
*
* @ param string $value
*
* @ return string
*/
protected function quote ( $string )
{
2017-08-24 10:39:32 +02:00
$char = '`' ;
2017-08-28 09:58:40 +02:00
return $char . str_replace ([ $char , '#' ], '' , $string ) . $char ;
2017-08-04 16:28:16 +02:00
}
/**
* Costruisce la query per l ' INSERT definito dagli argomenti .
*
* @ since 2.3
*
* @ param string $table
* @ param array $array
* @ param bool $return
*
* @ return string | array
*/
public function insert ( $table , $array , $return = false )
{
if ( ! is_string ( $table ) || ! is_array ( $array )) {
throw new UnexpectedValueException ();
}
2018-06-23 15:41:32 +02:00
if ( ! isset ( $array [ 0 ]) || ! is_array ( $array [ 0 ])) {
2017-08-04 16:28:16 +02:00
$array = [ $array ];
}
2017-09-04 17:18:54 +02:00
// Chiavi dei valori
2017-08-04 16:28:16 +02:00
$keys = [];
$temp = array_keys ( $array [ 0 ]);
foreach ( $temp as $value ) {
$keys [] = $this -> quote ( $value );
}
2017-09-04 17:18:54 +02:00
// Valori da inserire
2017-08-04 16:28:16 +02:00
$inserts = [];
foreach ( $array as $values ) {
foreach ( $values as $key => $value ) {
2017-08-28 09:58:40 +02:00
$values [ $key ] = $this -> prepareValue ( $key , $value );
2017-08-04 16:28:16 +02:00
}
$inserts [] = '(' . implode ( array_values ( $values ), ', ' ) . ')' ;
}
2017-09-04 17:18:54 +02:00
// Costruzione della query
2017-08-04 16:28:16 +02:00
$query = 'INSERT INTO ' . $this -> quote ( $table ) . ' (' . implode ( ',' , $keys ) . ') VALUES ' . implode ( $inserts , ', ' );
if ( ! empty ( $return )) {
return $query ;
} else {
return $this -> query ( $query );
}
}
/**
* Costruisce la query per l ' UPDATE definito dagli argomenti .
*
* @ since 2.3
*
* @ param string $table
* @ param array $array
* @ param array $conditions
* @ param bool $return
*
* @ return string | array
*/
public function update ( $table , $array , $conditions , $return = false )
{
if ( ! is_string ( $table ) || ! is_array ( $array ) || ! is_array ( $conditions )) {
throw new UnexpectedValueException ();
}
2017-09-04 17:18:54 +02:00
// Valori da aggiornare
2017-08-04 16:28:16 +02:00
$update = [];
foreach ( $array as $key => $value ) {
2017-08-28 09:58:40 +02:00
$update [] = $this -> quote ( $key ) . ' = ' . $this -> prepareValue ( $key , $value );
2017-08-04 16:28:16 +02:00
}
2017-09-04 17:18:54 +02:00
// Condizioni di aggiornamento
2017-08-04 16:28:16 +02:00
$where = [];
foreach ( $conditions as $key => $value ) {
2017-08-28 09:58:40 +02:00
$where [] = $this -> quote ( $key ) . ' = ' . $this -> prepareValue ( $key , $value );
2017-08-04 16:28:16 +02:00
}
2017-09-04 17:18:54 +02:00
// Costruzione della query
2017-08-04 16:28:16 +02:00
$query = 'UPDATE ' . $this -> quote ( $table ) . ' SET ' . implode ( $update , ', ' ) . ' WHERE ' . implode ( $where , ' AND ' );
if ( ! empty ( $return )) {
return $query ;
} else {
return $this -> query ( $query );
}
}
/**
* Costruisce la query per il SELECT definito dagli argomenti .
*
* @ since 2.3
*
* @ param string $table
* @ param array $array
* @ param array $conditions
* @ param array $order
* @ param string | array $limit
* @ param bool $return
*
* @ return string | array
*/
public function select ( $table , $array = [], $conditions = [], $order = [], $limit = null , $return = false )
{
if (
! is_string ( $table ) ||
2017-09-04 16:04:45 +02:00
( ! empty ( $order ) && ! is_string ( $order ) && ! is_array ( $order )) ||
2018-07-02 15:41:38 +02:00
( ! empty ( $limit ) && ! is_string ( $limit ) && ! is_integer ( $limit ) && ! is_array ( $limit ))
2017-08-04 16:28:16 +02:00
) {
throw new UnexpectedValueException ();
}
2017-09-04 17:18:54 +02:00
// Valori da ottenere
2017-08-04 16:28:16 +02:00
$select = [];
foreach (( array ) $array as $key => $value ) {
$select [] = $value . ( is_numeric ( $key ) ? '' : 'AS ' . $this -> quote ( $key ));
}
$select = ! empty ( $select ) ? $select : [ '*' ];
2017-09-04 17:18:54 +02:00
// Costruzione della query
2017-08-04 16:28:16 +02:00
$query = 'SELECT ' . implode ( ', ' , $select ) . ' FROM ' . $this -> quote ( $table );
2017-09-04 17:18:54 +02:00
// Condizioni di selezione
2017-08-04 16:28:16 +02:00
$where = $this -> whereStatement ( $conditions );
if ( ! empty ( $where )) {
$query .= ' WHERE ' . $where ;
}
2017-09-04 17:18:54 +02:00
// Impostazioni di ordinamento
2017-08-04 16:28:16 +02:00
if ( ! empty ( $order )) {
$list = [];
$allow = [ 'ASC' , 'DESC' ];
foreach (( array ) $order as $key => $value ) {
if ( is_numeric ( $key )) {
$key = $value ;
$value = $allow [ 0 ];
}
$value = in_array ( $value , $allow ) ? $value : $allow [ 0 ];
$list [] = $this -> quote ( $key ) . ' ' . $value ;
}
$query .= ' ORDER BY ' . implode ( ', ' , $list );
}
2017-09-04 17:18:54 +02:00
// Eventuali limiti
2017-08-04 16:28:16 +02:00
if ( ! empty ( $limit )) {
$query .= ' LIMIT ' . ( is_array ( $limit ) ? $limit [ 0 ] . ', ' . $limit [ 1 ] : $limit );
}
if ( ! empty ( $return )) {
return $query ;
} else {
return $this -> fetchArray ( $query );
}
}
2018-06-22 17:04:37 +02:00
/**
* Costruisce la query per il SELECT definito dagli argomenti ( LIMIT 1 ) .
*
* @ since 2.4 . 1
*
* @ param string $table
* @ param array $array
* @ param array $conditions
* @ param array $order
* @ param bool $return
*
* @ return string | array
*/
public function selectOne ( $table , $array = [], $conditions = [], $order = [], $return = false )
{
$limit = 1 ;
2018-07-02 15:41:38 +02:00
$result = $this -> select ( $table , $array , $conditions , $order , $limit , $return );
if ( isset ( $result [ 0 ])) {
return $result [ 0 ];
}
return $result ;
2018-06-22 17:04:37 +02:00
}
2017-08-31 11:32:49 +02:00
/**
* Sincronizza i valori indicati associati alle condizioni , rimuovendo le combinazioni aggiuntive e inserendo quelle non ancora presenti .
*
* @ since 2.3
*
* @ param string $table
* @ param array $conditions Condizioni di sincronizzazione
* @ param array $list Valori da sincronizzare
*/
2017-08-24 10:39:32 +02:00
public function sync ( $table , $conditions , $list )
{
if (
! is_string ( $table ) ||
! is_array ( $conditions ) ||
! is_array ( $list )
) {
throw new UnexpectedValueException ();
}
$field = key ( $list );
$sync = array_unique (( array ) current ( $list ));
if ( ! empty ( $field )) {
2017-09-04 16:04:45 +02:00
$results = array_column ( $this -> select ( $table , $field , $conditions ), $field );
2017-08-24 10:39:32 +02:00
$detachs = array_unique ( array_diff ( $results , $sync ));
$this -> detach ( $table , $conditions , [ $field => $detachs ]);
$this -> attach ( $table , $conditions , $list );
}
}
2017-08-31 11:32:49 +02:00
/**
* Inserisce le le combinazioni tra i valori indicati e le condizioni .
*
* @ since 2.3
*
* @ param string $table
* @ param array $conditions Condizioni
* @ param array $list Valori da aggiungere
*/
2017-08-24 10:39:32 +02:00
public function attach ( $table , $conditions , $list )
{
if (
! is_string ( $table ) ||
! is_array ( $conditions ) ||
! is_array ( $list )
) {
throw new UnexpectedValueException ();
}
$field = key ( $list );
$sync = array_unique (( array ) current ( $list ));
if ( ! empty ( $field )) {
2017-09-04 16:04:45 +02:00
$results = array_column ( $this -> select ( $table , $field , $conditions ), $field );
2017-08-24 10:39:32 +02:00
$inserts = array_unique ( array_diff ( $sync , $results ));
foreach ( $inserts as $insert ) {
$this -> insert ( $table , array_merge ( $conditions , [ $field => $insert ]));
}
}
}
2017-08-31 11:32:49 +02:00
/**
* Rimuove le le combinazioni tra i valori indicati e le condizioni .
*
* @ since 2.3
*
* @ param string $table
* @ param array $conditions Condizioni
* @ param array $list Valori da rimuovere
*/
2017-08-24 10:39:32 +02:00
public function detach ( $table , $conditions , $list )
{
if (
! is_string ( $table ) ||
! is_array ( $conditions ) ||
! is_array ( $list )
) {
throw new UnexpectedValueException ();
}
$field = key ( $list );
$sync = array_unique (( array ) current ( $list ));
if ( ! empty ( $field ) && ! empty ( $sync )) {
2017-09-04 16:04:45 +02:00
$conditions [ $field ] = $sync ;
2017-08-24 10:39:32 +02:00
2017-09-04 16:04:45 +02:00
$this -> query ( 'DELETE FROM ' . $this -> quote ( $table ) . ' WHERE ' . $this -> whereStatement ( $conditions ));
2017-08-24 10:39:32 +02:00
}
}
2017-08-04 16:28:16 +02:00
/**
* Predispone una variabile per il relativo inserimento all ' interno di uno statement SQL .
*
* @ since 2.3
*
* @ param string $value
*
* @ return string
*/
2017-08-28 09:58:40 +02:00
protected function prepareValue ( $field , $value )
2017-08-04 16:28:16 +02:00
{
$value = ( is_null ( $value )) ? 'NULL' : $value ;
$value = is_bool ( $value ) ? intval ( $value ) : $value ;
2017-08-28 09:58:40 +02:00
if ( ! starts_with ( $field , '#' )) {
if ( $value != 'NULL' ) {
$value = $this -> prepare ( $value );
}
2017-08-04 16:28:16 +02:00
}
return $value ;
}
/**
* Predispone il contenuto di un array come clausola WHERE .
*
* @ since 2.3
*
* @ param string | array $where
* @ param bool $and
*
* @ return string
*/
protected function whereStatement ( $where , $and = true )
{
$result = [];
2017-09-04 16:04:45 +02:00
foreach ( $where as $key => $value ) {
// Query personalizzata
if ( starts_with ( $key , '#' )) {
$result [] = $this -> prepareValue ( $key , $value );
} else {
// Ulteriori livelli di complessità
if ( is_array ( $value ) && in_array ( strtoupper ( $key ), [ 'AND' , 'OR' ])) {
2017-08-04 16:28:16 +02:00
$result [] = '(' . $this -> whereStatement ( $value , $key == 'AND' ) . ')' ;
2017-09-04 16:04:45 +02:00
}
// Condizione IN
elseif ( is_array ( $value )) {
if ( ! empty ( $value )) {
$in = [];
foreach ( $value as $v ) {
$in [] = $this -> prepareValue ( $key , $v );
}
$result [] = $this -> quote ( $key ) . ' IN (' . implode ( ',' , $in ) . ')' ;
}
}
// Condizione LIKE
elseif ( str_contains ( $value , '%' ) || str_contains ( $value , '_' )) {
2017-08-28 09:58:40 +02:00
$result [] = $this -> quote ( $key ) . ' LIKE ' . $this -> prepareValue ( $key , $value );
2017-09-04 16:04:45 +02:00
}
// Condizione BETWEEN
elseif ( str_contains ( $value , '|' )) {
2017-08-04 16:28:16 +02:00
$pieces = explode ( '|' , $value );
2017-08-28 09:58:40 +02:00
$result [] = $this -> quote ( $key ) . ' BETWEEN ' . $this -> prepareValue ( $key , $pieces [ 0 ]) . ' AND ' . $this -> prepareValue ( $key , $pieces [ 1 ]);
2017-09-04 16:04:45 +02:00
}
// Condizione di uguaglianza
else {
2017-08-28 09:58:40 +02:00
$result [] = $this -> quote ( $key ) . ' = ' . $this -> prepareValue ( $key , $value );
2017-08-04 16:28:16 +02:00
}
}
}
$cond = ! empty ( $and ) ? 'AND' : 'OR' ;
return implode ( ' ' . $cond . ' ' , $result );
}
/**
* Esegue le query interne ad un file . sql .
*
* @ since 2.0
*
* @ param string $filename Percorso per raggiungere il file delle query
* @ param string $delimiter Delimitatore delle query
*/
public function multiQuery ( $filename , $start = 0 )
{
$queries = readSQLFile ( $filename , ';' );
$end = count ( $queries );
for ( $i = $start ; $i < $end ; ++ $i ) {
try {
$this -> pdo -> query ( $queries [ $i ]);
} catch ( PDOException $e ) {
2017-08-28 12:15:30 +02:00
$this -> signal ( $e , $queries [ $i ], [
2017-08-04 16:28:16 +02:00
'throw' => false ,
]);
return $i ;
}
}
return true ;
}
/**
* Aggiunge informazioni alla struttura di base dell 'erroe o dell' eccezione intercettata .
*
* @ since 2.3
*/
protected function signal ( $e , $message , $options = [])
{
global $logger ;
$options = array_merge ([
'session' => true ,
'level' => \Monolog\Logger :: ERROR ,
'throw' => true ,
], $options );
if ( ! empty ( $options [ 'session' ])) {
2017-09-04 12:02:29 +02:00
$msg = tr ( " Si è verificato un'errore " ) . '.' ;
2017-08-04 16:28:16 +02:00
if ( Auth :: check ()) {
2017-09-04 12:02:29 +02:00
$msg .= ' ' . tr ( 'Se il problema persiste siete pregati di chiedere assistenza tramite la sezione Bug' ) . '. <a href="' . ROOTDIR . '/bug.php"><i class="fa fa-external-link"></i></a>' ;
2017-08-04 16:28:16 +02:00
}
$msg .= '<br><small>' . $e -> getMessage () . '</small>' ;
$_SESSION [ 'errors' ][] = $msg ;
}
$error = $e -> getMessage () . ' - ' . $message ;
if ( ! empty ( $options [ 'throw' ])) {
throw new PDOException ( $error );
} else {
$logger -> addRecord ( $options [ 'level' ], $error );
}
}
}