<?

//DEFINE("DB_HOST", "localhost");
//DEFINE("DB_NAME", "jaws");
//DEFINE("DB_USER", "root");
//DEFINE("DB_PASS", "");


class mysql_db{

var $dbLink = null;

      function mysql_db(){}

      function connect($host,$only_db,$user,$password){
               return mysql_select_db($only_db, $this->dbLink = mysql_connect($host,$user,$password));
      }

      function runSQL($sql){
               return $this->query($sql);
      }

      function query($sql, $strong_result = false){
        $sql = trim($sql);
               switch (1) {
                  case eregi(" IN \{SELECT", $sql):
                       switch (1) {
                          case eregi("^SELECT", $sql):
                               preg_match_all("/({[^}]*)/", $sql, $matches, PREG_SET_ORDER); //	find matching sql

                               $sql_in = str_replace("{", "", strrchr($matches[0][0], "{"));
                               if(NULL == ($in = $this->set_IN($sql_in)))  return NULL;//~ ;

                               $sql = str_replace($sql_in, $in, $sql);
                               $sql = str_replace("{(", "(", $sql);
                               $sql = str_replace(")}", ")", $sql);

                               if(NULL == $this->query($sql)) return NULL;
                            break;
                       }
                  default:// normal MySQL syntax
                          // analyse syntax of query
                          switch (1) {
                             case eregi("^SELECT",$sql):
                                  if("" == ($result_identifier = mysql_query($sql)))return NULL;//~ no result
                                  $mysql_error = mysql_error();
                                  if("" != $mysql_error) $this->print_in_HTML("ERROR", $mysql_error);
                                  if("" != $mysql_error)return NULL;//~ error occured
                                  switch (1) {
                                     case eregi("COUNT\([^,]{1,}FROM",$sql):
                                     case eregi("MAX\(",  $sql):
                                     case eregi("MIN\(",  $sql):
                                       return mysql_result($result_identifier,0);//~ result for select COUNT, MAX, MIN
                                       break;
                                     case eregi("LIMIT 0\,1$",$sql):
                                       switch (1) {
                                          case eregi("^.+,.+FROM.+",$sql):
                                            return mysql_fetch_object($result_identifier);//~ result an object
                                            break;
                                          default:
                                            if(0 == mysql_num_rows($result_identifier))return NULL;//~
                                            return mysql_result($result_identifier,0);//~
                                       }
                                       break;
                                     default:
                                       // get result values to array of objects
                                       $i=0;
                                       $result_array = null;
                                       while ($result = mysql_fetch_array($result_identifier, MYSQL_ASSOC))
                                              {
                                               $result_array[$i] = $result;
                                               $i++;
                                              }
                                       if(!$strong_result) {
                                           return (count($result_array) == 1) ? $result_array[0] : $result_array;
                                       } else {
                                           return $result_array;
                                       }
                                  }
                               break;
                             default:
                               return mysql_unbuffered_query($sql);
                          }
               }
      }

      function set_IN($sql) {
               $in  = "(";
               if (!($res = mysql_query($sql))) return NULL;
               while ($val = mysql_fetch_array($res, MYSQL_NUM))$in .= $val[0].",";
               $in = ereg_replace(",$", ")", $in);
               mysql_free_result($res);
               return $in;
      }


      function getLastId() {
               return mysql_insert_id($this->dbLink);
      }

      function connectClose() {
               return mysql_close($this->dbLink);
      }


      function print_in_HTML($title, $string) {
               echo " <br>---- $title<br><br> <pre>".htmlentities($string)."</pre> ";
               return TRUE;         
      }
}
?>
