123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399 |
- <?php
- define('ERR_DB_RES','0');
- define('ERR_DB_ERR','1');
- require_once 'OExceptions.php';
- /**
- * Wrapper per le funzioni di libreria php mysqli
- *
- * @author gine
- * @original author: Micah Carrick
- * @version: 1.1.1
- * @licence: GPL 2
- *
- */
- class Database {
- /**
- * Ricordati di settarla a FALSE per tutti i programmi
- * in cui usi questa classe.
- */
- private $debug=FALSE;
-
- private static $m_pInstance = array();
- private $last_query; // last query executed.
- var $row_count; // last number of rows
- var $db_link; // current/last database link identifier
- private $auto_slashes; // the class will add/strip slashes when it can
- private $host, $user, $pw, $db, $port;
- /**
- * Costruttore della classe che si interfaccia con il dbms. Privata per via del Singleton.
- *
- * @param string $db
- */
- private function Database($db) {
- $dbConfPath=dirname(__FILE__).'/../config/';
-
- include($dbConfPath.'db.php');
- if ($db == "dns")
- include($dbConfPath.'smdns_db.php');
- elseif ($db == "db") {
- include($dbConfPath.'mysql_db.php');
- require_once dirname(__FILE__).'/../lib/terminal.php';
- $dbms_password=prompt_silent();
- } elseif ($db == "ot")
- include($dbConfPath.'ot_db.php');
- elseif($db == "std")
- include($dbConfPath.'config.php');
-
- $this->host = $dbms_server;
- $this->user = $dbms_user;
- $this->pw = $dbms_password;
- $this->db = $dbms_db;
- $this->port = $dbms_port;
- $this->auto_slashes = true;
-
- self::connect();
- }
-
- /**
- * Singleton multiplo del costruttore della classe Database. Ritorna un istanza della classe.
- * $db può essere: ot, dns, db, ftp. Default: ot
- *
- * @param string $db
- * @return Mysqli_link $m_pInstance
- */
- public static function getInstance($db='ot'){
- if (!isset(self::$m_pInstance[$db])){
- self::$m_pInstance[$db] = new Database($db);
- }
-
- return self::$m_pInstance[$db];
- }
-
- /**
- * Esegue la connect al dbms.
- * I parametri, se configurato il costruttore correttamente, sono opzionali. Ritorna un mysqli_link.
- *
- * Solleva un eccezione se ci sono problemi di connessione.
- *
- * @param url $host
- * @param string $user
- * @param string $pw
- * @param string $db
- * @param int $port
- * @throws DbErrException
- * @return Mysqli_link $db_link
- *
- */
- function connect($host='', $user='', $pw='', $db='', $port='') {
- if (!empty($host)) $this->host = $host;
- if (!empty($user)) $this->user = $user;
- if (!empty($pw)) $this->pw = $pw;
- if (!empty($pw)) $this->port = $port;
- $this->db_link = mysqli_init();
- mysqli_real_connect($this->db_link , $this->host, $this->user, $this->pw, $this->db, $this->port);
- if (!$this->db_link)
- $this->callException(_("Database connection problem"),E_DB_ERR);
- return $this->db_link;
- }
-
- private function setDebugErr($msg){
- $err['text']=$msg;
- $err['code']=0;
-
- if($this->debug){
- $err['text'].="\n".$this->last_query;
- $err['code']=mysqli_errno($this->db_link);
- }
-
- return $err;
- }
-
- private function callException($msg,$e_type){
- $err=$this->setDebugErr($msg);
- if($e_type == ERR_DB_RES)
- throw new DbResException($err['text'],$err['code']);
- elseif($e_type == ERR_DB_ERR)
- throw new DbErrException($err['text'],$err['code']);
- else {
- die(_("what shit!"));
- exit;
- }
- }
-
- /**
- * Selezioziona il database ritorna TRUE se selezionato correttamente.
- * Solleva una Exception in caso di fallimento.
- *
- * @param string $db
- * @throws DbErrException
- * @return boolean
- */
- function selectDb($db='') {
- if (!empty($db)) $this->db = $db;
- if (!mysqli_select_db($this->db_link,$this->db))
- $this->callException(_("Database selection problem"),E_DB_ERR);
- return true;
- }
-
- /**
- * Esegue una SELECT espressa con una SqlQuery.
- * Utile quando non si conosce in numero di righe o colonne che potrebbero tornare.
- * Ritorna un oggetto mysqli_result. Solleva una eccezione sul fallimento.
- *
- * @param SqlQuery $sql
- * @throws DbErrException
- * @return mixed
- */
- function select($sql) {
- $this->last_query = $sql;
- $r = mysqli_query($this->db_link,$sql);
- if (!$r)
- $this->callException(_("Query select error"),E_DB_ERR);
- $this->row_count = mysqli_num_rows($r);
- return $r;
- }
-
- /**
- * Esegue una SELECT il cui risultato deve essere 1 sola riga.
- * Ritorna la riga. Solleva un eccezzione DbResException altrimenti.
- *
- * @param SqlQuery $sql
- * @param [String cMsg] : customMessage
- * @throws DbResException
- * @return mixed
- */
- function selectOneRow($sql,$cMsg=NULL) {
- $msg=$sMsg=NULL;
-
- $r = self::select($sql);
- if ($this->row_count > 1)
- $sMsg=_(" more than one result.");
- elseif ($this->row_count < 1)
- $sMsg=_(" query returned less than one result.");
-
- if($sMsg!=NULL && $cMsg==NULL)
- $msg=_("Your query returned").$sMsg;
- elseif ($sMsg!=NULL && $cMsg != NULL)
- $msg=$cMsg;
-
- if($msg!=NULL)
- $this->callException($msg,ERR_DB_RES);
-
- return mysqli_fetch_row($r);
- }
- /**
- * Esegue una SELECT il cui risultato deve essere 1 sola cella.
- * Ritorna la cella. Solleva un eccezzione DbResException altrimenti.
- *
- * @param SqlQuery $sql
- * @throws DbResException
- * @return mixed
- */
- function selectFirstRowFirstCell($sql) {
- $r = $this->select($sql);
- if ($this->row_count > 1 or $this->row_count < 1)
- $this->callException(_("Your query returned more or less that one result."),ERR_DB_RES);
- $ret = mysqli_fetch_row($r);
- if ($this->auto_slashes)
- return stripslashes($ret[0]);
- else
- return $ret[0];
- }
- /**
- * Esegue una select il cui risultato deve essere un array composto da
- * una sola cella per riga. Solleva un eccezzione DbResException altrimenti.
- *
- * @param SqlQuery $sql
- * @throws DbResException
- * @return array
- */
- function selectArrayOneElem($sql) {
- $arr=array();
-
- $r= $this->select($sql);
- for ($i=0; $i<$this->row_count;$i++){
- $row=mysqli_fetch_row($r);
- if (isset($row[1]))
- $this->callException(_("Your query return more than 1 element for row."),ERR_DB_RES);
-
- $arr[$i]=$row[0];
- }
-
- return $arr;
- }
-
- /**
- * Esegue una select il cui risultato deve essere un array composto da
- * una $n celle per riga. Solleva un eccezzione DbResException altrimenti.
- * Se $n non è specificato non viene effettuato nessun controllo sul
- * numero di elementi per riga.
- *
- * @param SqlQuery $sql
- * @throws DbResException
- * @return array
- */
- function selectArrayNElem($sql,$n=NULL) {
- $arr=array();
-
- $r= $this->select($sql);
- for ($i=0; $i<$this->row_count;$i++){
- $row=mysqli_fetch_row($r);
- if($n!=NULL){
- if (isset($row[$n]))
- $this->callException(sprintf(_("Your query return more than %s element for row."),$n),ERR_RES);
- }
- $arr[$i]=$row;
- }
-
- return $arr;
- }
-
- /**
- * Torna il numero di righe affette dalla query.
- * Da utilizzare con INSERT DELETE e UPDATE
- *
- * @param SqlQuery $sql
- * @throws DbResException
- * @return int
- */
- function execQuery($sql) {
- $this->last_query = $sql;
- $r = mysqli_query($this->db_link,$sql);
- if (!$r)
- $this->callException(_("Query error"),ERR_DB_ERR);
-
- return $this->row_count = mysqli_affected_rows($this->db_link);
- }
- /**
- * Eseque una SqlQuery il cui risultato deve aver avuto effetto su 1 sola riga.
- * Ritorna TRUE o solleva una eccezione altrimenti.
- *
- * @param SqlQuery $sql
- * @param [String cMsg] : customMessage
- * @throws DbResException
- * @return boolean
- */
- function execQueryOneRow($sql,$cMsg=NULL) {
- $msg=$sMsg=NULL;
-
- self::execQuery($sql);
- if ($this->row_count == 0)
- $sMsg=_("No row affected");
- elseif($this->row_count > 1)
- $sMsg=_("More than 1 row are affected");
-
- if($sMsg!=NULL && $cMsg==NULL)
- $msg=$sMsg;
- elseif ($sMsg!=NULL && $cMsg != NULL)
- $msg=$cMsg;
-
- if($msg!=NULL)
- $this->callException($msg,ERR_DB_RES);
- }
- /**
- * Eseque una execQueryOneRow e ritorna l'ID della riga inserita.
- * Ottenuto tramite AUTOINCREMENT; comoda per le INSERT.
- *
- * @param SqlQuery $sql
- */
- function execQueryRetId($sql) {
- $this->execQueryOneRow($sql);
-
- return mysqli_insert_id($this->db_link);
- }
- /**
- * Esegue una SELECT con $sql per verificare la presenza di un "id", se non esiste torna FALSE.
- * Altrimenti torna l'ID cercato con la select.
- * Se ritornano più risultati solleva un DbResException.
- *
- * @param SelectSqlQuery $sql
- * @return mixed
- */
- function isThereAlready($sql){
- $id=FALSE;
-
- $res=self::select($sql);
- if($this->row_count == 1){
- $row=mysqli_fetch_row($res);
- $id=$row[0];
- } elseif($this->row_count > 1 ) {
- $this->callException(_("Are present more than 1 row"),ERR_DB_RES);
- }
-
- return $id;
- }
-
- function escapeString($string){
- return mysqli_real_escape_string($this->db_link,$string);
- }
-
- /**
- * Esegue un flush dei privilegi degli utenti mysql.
- * Serve per assicurare che le operazioni effettuate con altre funzioni,
- * come la creazione o la eliminazione di un utente, siano attive.
- */
- function flushPrivileges(){
- self::execQuery("FLUSH PRIVILEGES");
- }
- /**
- * Se $debug è TRUE stampa la query $sql altrimenti tramite reflection esegue il metodo $action sulla query.
- *
- * @param boolean $debug TRUE|FALSE
- * @param String $action Metodi della Classe Database
- * @param SqlQuery $sql
- */
- function sqlOrDebug($debug,$action,$sql){
- if (! $debug)
- self::$action($sql);
- else
- echo "DEBUG:OR: ".str_replace(array("\n","\r","\t","/\s\s+/"),"",$sql)."\n";
- }
- /**
- * Se $debug è FALSE esegue tramite reflection il metodo $action sulla query $sql, altrimenti stampa anche la query.
- *
- * @param boolean $debug TRUE|FALSE
- * @param String $action Metodi della Classe Database
- * @param SqlQuery $sql
- */
- function sqlAndDebug($debug,$action,$sql){
- if ($debug)
- echo "DEBUG:AND: ".str_replace(array("\n","\r","\t","/\s\s+/"),"",$sql)."\n";
-
- return self::$action($sql);
- }
-
- /**
- * Ritorna l'ultimo errore ottenuto durante le azioni precedenti sul database.
- */
- public function getLastError() {
- return $this->last_error;
- }
-
- /**
- * Ritorna l'ultima query effettuata
- */
- public function getLastQuery() {
- return $this->last_query;
- }
- }
- ?>
|