Location: PHPKode > projects > phpAWE > phpAWE.0.4.beta.demo-page/awe/classes/TaweSqlQuery.php
<?php

class TaweSqlQuery {
    protected $debug_query;

    function TddSqlQuery() {
    }

    // function only for test puposes
    // suport active language selection, but not visibility fields
    function exec($query) {

        $query = $this->queryLanguageReplace($query);

        $sqlResult = mysql_query($query);

        $result = $this->formatResult();
        $this->debug_query = $query;

        return $result;
    }

    function select($fields, $from, $where="", $orderby="", $order="", $limit="") {

        global $aweSqlDefaultOrder;

        global $db_database;
        global $aweTablesHeader;

        $visibility = $this->setVisibility();

        if ($visibility) {
            if (is_array($where)) $where = array_merge($where, $visibility);
            else
                if ($where) $where = array_merge($visibility, array($where));
                else $where = $this->$visibility;
        }

        if ($fields == "") $fields = "*";
        $query  = "SELECT ".valueOrList(", ",$fields);
        $query .= " FROM ".strtolower(valueOrList(", ",$from,$db_database.".".$aweTablesHeader));
        if ($where != "") $query .= " WHERE ".valueOrList(" AND ",$where);
        if ($orderby != "") $query .= " ORDER BY ".valueOrList(", ",$orderby)." ".$order;
        if ($limit != "") $query .= " LIMIT ".$limit;

        $query = $this->queryLanguageReplace($query);
        $sqlResult = mysql_query($query);
        $result = $this->formatResult($sqlResult);

        $this->debug_query = $query;

        return $result;
    }

    function update($table, $fields, $values, $where, $limit="") {

        global $db_database;
        global $aweTablesHeader;

        if (is_array($fields) && is_array($values)) {
            $repeat = min(sizeof($fields), sizeof($values));
            for ($i=0; $i<$repeat; $i++) {
                $setString[$i] .= $fields[$i]." = ".$values[$i];
            }
        }
        else if (is_array($fields) && is_array($values)) return false; // fields and values must be the same type
        else if (isset($fields) && isset($values)) $setString = $fields." = ".$values; else return false;

        $query  = "UPDATE ".$db_database.".".$aweTablesHeader.$table;
        $query .= " SET ".valueOrList(", ",$setString);
        $query .= " WHERE ".valueOrList(" AND ",$where);

        if ($limit != "") $query .= " LIMIT ".$limit;

        $query = $this->queryLanguageReplace($query);

        $result = mysql_query($query);

        $this->debug_query = $query;

        return $result;

    }

    function insert($table, $fields, $values) {

        global $db_database;
        global $aweTablesHeader;

        $fields = valueOrList(", ", $fields, "`", "`");

        $values = valueOrList(", ", $values, "'", "'");

        $query = "INSERT INTO ".$db_database.".".$aweTablesHeader.$table;
        $query .= " ( ".$fields." ) ";
        $query .= "VALUES ( ".$values." )";

        $query = $this->queryLanguageReplace($query);

        $result = mysql_query($query);

        $this->debug_query = $query;

        return $result;
    }

    function remove($table,$where,$limit=0) {

        global $db_database;
        global $aweTablesHeader;

        $query = "DELETE FROM ".$db_database.".".$aweTablesHeader.$table;
        $query .= " WHERE ".valueOrList(" AND ",$where);
        if ($limit) $query .= " LIMIT ".$limit;

        $result = mysql_query($query);

        $this->debug_query = $query;

        return $result;

    }

    function getMax($table, $field, $where=0) {
        global $db_database;
        global $aweTablesHeader;


        $query = "SELECT MAX( ".$field." ) AS max FROM ".$db_database.".".$aweTablesHeader.$table;
        if ($where) $query.=" WHERE ".valueOrList(" AND ",$where);

        $query = $this->queryLanguageReplace($query);
        $result = mysql_query($query);
        $result = mysql_fetch_assoc($result);

        $this->debug_query = $query;

        return $result[max];
    }



    function showColumns($table) {

        global $activeLanguage;

        $query = "SHOW COLUMNS IN ".$table;

        $sqlResult = mysql_query($query);

        $c = 0;
        while ($row =@ mysql_fetch_array($sqlResult)) {

            while( list($key, $val) = each($row))
            $result[$c][str_replace("_".$activeLanguage."_","",$key)] = $val;
            $c++;
        }

        return $result;
    }

    public function debugShowQuery() {
        echo $this->debug_query;
    }

    function __construct() {
        global $db_address;
        global $db_username;
        global $db_password;
        global $db_database;
        global $aweSqlCharset;
        global $aweSqlCollation;

        mysql_connect($db_address,$db_username,$db_password)or die(mysql_error());

        $charset = sessionGet($aweSqlCharset);
        $collation = sessionGet($aweSqlCollation);

        @mysql_query("SET NAMES ".$charset);
        @mysql_query("SET collation_connection = ".$collation);
        @mysql_query("SET CHARACTER SET ".$charset);
        @mysql_query("set character_set_connection=".$charset);
        @mysql_query("set character_set_client=".$charset);
        @mysql_query("set character_set_database=".$charset);
        @mysql_query("set character_set_results=".$charset);
        @mysql_query("set character_set_server=".$charset);
        @mysql_query("set collation_database=".$collation);
        @mysql_query("set collation_server=".$collation);
        mysql_select_db($db_database)or die(mysql_error());
    }

    protected function queryLanguageReplace($query) {

        global $aweSqlLangTmp;
        global $aweSqlLangSufix;
        global $activeLanguage;

        $query = str_replace($aweSqlLangTmp, sprintf($aweSqlLangSufix, $activeLanguage),$query);
        return $query;
    }

    protected function formatResult($sqlResult) {

        global $aweSqlLangSufix;
        global $activeLanguage;


        $c = 0;

        while ($row =@ mysql_fetch_array($sqlResult)) {
            while( list($key, $val) = each($row))
            $result[$c][str_replace(sprintf($aweSqlLangSufix, $activeLanguage),"",$key)] = $val;
            $c++;
        }
        return $result;
    }

    protected function setVisibility() {
        global $aweSqlHiddenField;
        global $aweSqlSystemField;

        $list = array();
        if (sessionGet($aweSqlHiddenField)==1) array_push($list, sqlText(equals, $aweSqlHiddenField, 0));
        if (sessionGet($aweSqlSystemField)==1) array_push($list, sqlText(equals, $aweSqlSystemField, 0));
        if (sizeof($list)) return $list;
        else return null;
    }


}
Return current item: phpAWE