class SqlQuery
{
/** @var access attributes query */
public $Fields = array("col", "table", "where", "groupby", "having", "orderby", "value", 'data',"limit");
/** @var array of attributes query */
public $Sql = array();
/** @var type of sql query */
public $typeQuery = "select";
/**
* define type of SQL query
* @param string $typeQuery - select, insert, update, delete
*/
function SqlQuery($typeQuery)
{
$this->typeQuery = $typeQuery;
}
function Add($method, $param)
{
if (in_array(strtolower($method), $this->Fields)) {
switch ($method) {
/*case 'col' :
if ( $param != '*' ) $this->Sql[$method][] = '`'.$param.'`';;
break;*/
case 'value' :
if (is_string($param))
$this->Sql[$method][] = '\'' . $param . '\'';
else
$this->Sql[$method][] = intval($param);
break;
case 'data' :
$key = key($param);
$value = current($param);
$this->Sql['col'][] = '`' . $key . '`';
if (is_string($value))
$this->Sql['value'][] = '\'' . $value . '\'';
else
$this->Sql['value'][] = intval($value);
break;
case 'where' :
if (count($param) == 2) {
$key = '`' . key($param) . '`';
$value = current($param);
(is_string($value)) ? $value = '\'' . $value . '\'' :
$value = intval($value);
$tmp = $key . '=' . $value;
next($param);
$key = '`' . key($param) . '`';
$value = current($param);
(is_string($value)) ? $value = '\'' . $value . '\'' :
$value = intval($value);
$tmp .= " AND " . $key . '=' . $value;
$this->Sql['where'][] = $tmp;
} else {
$key = '`' . key($param) . '`';
$value = current($param);
(is_string($value)) ? $value = '\'' . $value . '\'' :
$value = intval($value);
$this->Sql['where'][] = $key . '=' . $value;
}
break;
case 'limit':
$this->Sql['limit'] = $param;
break;
default :
$this->Sql[$method][] = $param;
break;
}
}
}
/**
* multi condition where
* @param string $column col in table
* @param array $values value array
* @param boolean $possitive if FALSE => NOT
*/
function addWhereMulti($column, $values, $possitive = TRUE)
{
foreach ($values as $value)
$where[] = "$column = '$value'";
$sqlWhere = implode($where, " OR ");
if (!$possitive)
$sqlWhere = "NOT (" . $sqlWhere . ") OR $column IS NULL";
;
$this->addWhere($sqlWhere);
}
/**
* make sql query
* @return string sql query
*/
function toSql()
{
if (!($this->Sql["table"])) {
// genError("", 1);
return FALSE;
}
$query = FALSE;
switch ($this->typeQuery) {
// SELECT
case ("select") :
if (!($this->Sql["col"]))
break;
$query = "SELECT " . implode($this->Sql["col"], ", ") . " FROM " . implode($this->Sql["table"], ", ");
if (isset ($this->Sql["where"]))
$query .= " WHERE (" . implode($this->Sql["where"], ") AND (") . ")";
if (isset ($this->Sql["groupby"]))
$query .= " GROUP BY " . implode($this->Sql["groupby"], ", ");
if (isset ($this->Sql["having"]))
$query .= " HAVING " . implode($this->Sql["having"], " AND ");
if (isset ($this->Sql["orderby"]))
$query .= " ORDER BY " . implode($this->Sql["orderby"], ", ");
if (isset ($this->Sql["limit"])) {
$query .= " LIMIT " . $this->Sql["limit"];
}
break;
// INSERT
case ("insert") :
if (empty($this->Sql["value"]))
break;
$query = "INSERT INTO " . implode($this->Sql["table"], ", ");
// ak su definovane aj stplce tabulky
if (isset ($this->Sql["col"])) {
if (count($this->Sql["col"]) != count($this->Sql["value"]))
die('INSERT: fileds and values numbers are not equal!
' .
'Fileds:' . print_r($this->Sql["col"]) . '
' .
'Values:' . print_r($this->Sql["value"]) . '
'
);
//genError ("INSERT: Pocet stlpcov sa nerovna poctu hodnot.", 1);
$query .= " (" . implode($this->Sql["col"], ", ") . ")";
}
$query .= " VALUES (" . implode($this->Sql["value"], ", ") . ")";
break;
// UPDATE
case ("update") :
if (empty($this->Sql["col"]) || empty($this->Sql["value"]))
break;
$query = "UPDATE " . implode($this->Sql["table"], ", ");
// porovnanie poctu stlpcov a poctu hodnot
if (count($this->Sql["col"]) != count($this->Sql["value"]))
die('update error!');
//genError ("INSERT: Pocet stlpcov sa nerovna poctu hodnot.", 1);
// priradenie hodnot stlpcom
for ($i = 0; $i < count($this->Sql["col"]); $i++) {
$temp[] = $this->Sql["col"][$i] . " = " . $this->Sql["value"][$i];
}
$query .= " SET " . implode($temp, ", ");
if (isset ($this->Sql["where"]))
$query .= " WHERE " . implode($this->Sql["where"], ") AND (");
break;
// DELETE
case ("delete") :
$query = "DELETE FROM " . implode($this->Sql["table"], ", ");
if (isset ($this->Sql["where"]))
$query .= " WHERE (" . implode($this->Sql["where"], ") AND (") . ")";
break;
default:
die ("Invalid type of query - $this->typeQuery.");
return FALSE;
}
return $query;
}
} // end of class
?>
class DataBase {
var $link;
var $res;
var $debuglevel = 0; //0 - don't show errors, 1 - show mysq_error, 2 - show queries
var $calc;
function DataBase($host,$user,$pass,$db,$port='')
{
$this->calc = 0;
( $this->link = mysql_pconnect($host.( ($port!='')?':'.$port:'' ),$user,$pass ) ) || $this->translate_error();
( mysql_select_db($db,$this->link) ) || $this->translate_error();
//( mysql_query("SET NAMES cp1251",$this->link) ) || $this->translate_error();
}
//cunstructs the query from a template and executes it
function query($query_key, $vars) {
global $query;
$query_str = $query[$query_key];
if( !empty($vars) ) {
foreach ($vars as $var => $value) {
//checking if addslashes is needed
if(!get_magic_quotes_gpc() ) $value = addslashes($value);
$query_str = str_replace('{'.$var.'}', $value, $query_str);
}
//clears any unset indexes
//$query_str = preg_replace('/\{\w+\}/', '', $query_str);
$query_str = preg_replace('/\'\{\w+\}\'/', '\'\'', $query_str);
$query_str = preg_replace('/\{\w+\}/', '\'\'', $query_str);
//print query if debug
if($this->debuglevel >= 2) print_r(''.$query_key.':
'.$query_str.'
');
}
( $this->res = mysql_query($query_str,$this->link) ) ||
($this->translate_error());
//echo $query_str;
$this->calc++;
return $this->res;
}
//launches a direct SQL query to the DB
function sql_query($query_str) {
( $this->res = mysql_query($query_str,$this->link) ) || ($this->translate_error());
//print query if debug
if($this->debuglevel >= 2) print_r('
'.$query_str.'
');
return $this->res;
}
//get number of rows returned
function num_rows()
{
if($this->res) {
return mysql_num_rows($this->res);
}
return -1;
}
//get number of rows affected by last update, delete query
function affected() {
if($this->res) {
return mysql_affected_rows($this->link);
}
return -1;
}
//Fetch a result row
function fetch($mode = MYSQL_ASSOC)
{
return mysql_fetch_array($this->res,$mode);
}
function fetch_item($item, $mode = MYSQL_ASSOC) {
$fetch = mysql_fetch_array($this->res,$mode);
return $fetch[$item];
}
function reset()
{
mysql_data_seek($this->res,0);
}
//print error message
function translate_error()
{
if($this->debuglevel >= 1) echo 'MySQL error : '.mysql_error($this->link).'
';
}
//returns an array of all fetched rows
function fetched_array($mode = MYSQL_ASSOC) {
$arr_fetched = array();
while( $result = mysql_fetch_array($this->res,$mode) ) {
$arr_fetched[] = $result;
}
if( $this->num_rows() >0 )
return $arr_fetched;
else
return false;
}
//returns an array of "row" ids
function fetched_ids($mode = MYSQL_BOTH) {
$res = array();
$arr_fetched = $this->fetched_array($mode);
if( !$arr_fetched ) return false;
foreach($arr_fetched as $row) {
$res[] = current($row);
}
return $res;
}
function fetched_ids2($mode = MYSQL_BOTH) {
$res = array();
$arr_fetched = $this->fetched_array($mode);
if( !$arr_fetched ) return false;
foreach($arr_fetched as $row) {
$res[intval($row['id'])] = $row;
}
return $res;
}
//Get the ID generated from the previous INSERT operation
function getLastID() {
return mysql_insert_id($this->link);
}
function field_name($index)
{
return mysql_field_name($this->link,$index);
}
}
?>