<?php

##################################################################################
#    PHP-RESIDENCE
#    Copyright (C) 2001-2009 by Marco Maria Francesco De Santis (marco@digitaldruid.net)
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    any later version accepted by Marco Maria Francesco De Santis, which
#    shall act as a proxy as defined in Section 14 of version 3 of the
#    license.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
##################################################################################

#Funzioni per usare il database SQLITE

ignore_user_abort(1);

# variabili per le differenze nella sintassi delle query
#global $ILIKE,$LIKE;
$ILIKE = "LIKE";
$LIKE = "GLOB";
$DATETIME = "text";
$MEDIUMTEXT = "text";


function connetti_db ($database,$host,$port,$user,$password,$estensione) {

if ($estensione == "SI") dl("sqlite.so");
if (defined("C_PERCORSO_A_DATI")) $numconnessione = sqlite_open(C_PERCORSO_A_DATI."db_".$database);
else $numconnessione = sqlite_open("./dati/db_".$database);
return $numconnessione;

} # fine function connetti_db


function disconnetti_db ($numconnessione) {

$risul = sqlite_close($numconnessione);
return $risul;

} # fine function disconnetti_db


function esegui_query_reale ($query,$silenzio = "") {
global $numconnessione;

if (str_replace(" GLOB '","",$query) != $query) {
$query .= " ";
$q_vett = explode(" GLOB '",$query);
for ($n = 1 ; $n < count($q_vett) ; $n++) {
if (substr(str_replace("''","",$q_vett[$n]),0,1) != "'") {
$arg = str_replace("''","^'^",$q_vett[$n]);
$arg = explode("' ",$arg);
$arg = str_replace("^'^","''",$arg[0]);
if (str_replace("''","",$arg) == str_replace("'","",str_replace("''","",$arg))) {
$query = str_replace(" GLOB '$arg' "," GLOB '".str_replace("%","*",str_replace("_","?",$arg))."' ",$query);
} # fine if (str_replace("''","",$arg) == str_replace("'","",str_replace("''","",$arg)))
} # fine if (substr(str_replace("''","",$q_vett[$n]),0,1) != "'")
} # fine for $n
} # fine if (str_replace(" GLOB '","",$query) != $query)

$risul = sqlite_query($numconnessione,$query);
if (!$risul and !$silenzio) {
global $PHPR_TAB_PRE;
echo "<br>ERROR in: ".str_replace(" ".$PHPR_TAB_PRE," ",$query)."<br>";
} # fine (!$risul and !$silenzio)
return $risul;

} # fine function esegui_query_reale


if ($PHPR_LOG != "SI") {

function esegui_query ($query,$silenzio = "",$nolog = "") {
$risul = esegui_query_reale($query,$silenzio);
return $risul;
} # fine function esegui_query

} # fine if ($PHPR_LOG != "SI")


else {
if (!function_exists("inserisci_log")) include("./includes/funzioni_log.php");

function esegui_query ($query,$silenzio = "",$nolog = "") {
$risul = esegui_query_reale($query,$silenzio);

if (!$nolog) inserisci_log($query);

return $risul;

} # fine function esegui_query

} # fine else if ($PHPR_LOG != "SI")


function risul_query ($query,$riga,$colonna,$tab="") {

sqlite_seek($query,$riga);
if ($tab) $colonna = "$tab.$colonna";
$risul = sqlite_column($query,$colonna);
return $risul;

} # fine function risul_query


function numlin_query ($query) {

$risul = sqlite_num_rows($query);
return $risul;

} # fine function numlin_query


function aggslashdb ($stringa) {

$risul = sqlite_escape_string($stringa);
return $risul;

} # fine function aggslashdb


function arraylin_query ($query,$num) {

sqlite_seek($query,$num);
$risul = sqlite_fetch_array($query);
return $risul;

} # fine function arraylin_query


function numcampi_query ($query) {

$risul = sqlite_num_fields($query);
return $risul;

} # fine function numcampi_query


function nomecampo_query ($query,$num) {

$risul = sqlite_field_name($query,$num);
return $risul;

} # fine function nomecampo_query


function tipocampo_query ($query,$num) {

$risul = "unknown";
return $risul;

} # fine function tipocampo_query


function dimcampo_query ($query,$num) {

$risul = "unknown";
return $risul;

} # fine function dimcampo_query


function lock_tabelle ($tabelle,$altre_tab_usate = "") {

$risul = esegui_query("begin transaction");
return $risul;

} # fine function lock_tabelle


function unlock_tabelle ($tabelle_lock,$azione = "") {

esegui_query("commit transaction");

} # fine function unlock_tabelle


function crea_indice ($tabella,$colonne,$nome) {

esegui_query("create index $nome on $tabella ($colonne)");

} # fine function crea_indice



?>