<?php
   require_once(V3_ROOT . '/etc/v3_config.inc');

##
#    v3_db: singletonish (actually, more like what python geeks
#          call "borg": all instances share the same state)
#          database handler; one db connection is shared between
#          instances, but error-reporting can be set per-instance.
##
   class v3_db
   {
      protected static $_db_conn   = null; # borgy mysql connection ref
      protected static $_db_select = null; # borgy db select ref
      protected $_show_errors      = true; # instance-specific

      ##
      #  protected _die()
      #  @param $message  pass $message to die, if $this->_show_errors is true.
      ##
      protected function _die($message)
      {
         die(
         $this->_show_errors
            ? sprintf(
                  "%s! (%d: %s)\n",
                  $message,
                  mysql_errno(self::$_db_conn),
                  mysql_error(self::$_db_conn)
            )
            : 'IO Error!'
         );
      }


      function __construct($show_errors=true)
      {
         $this->_show_errors = $show_errors;

         if(self::$_db_conn === null) {
            self::$_db_select = self::$_db_conn = false;

            self::$_db_conn = @mysql_connect(V3_DB_HOST, V3_DB_USER, V3_DB_PASSWORD)
               or $this->_die('Connect failed');

            self::$_db_select = @mysql_select_db(V3_DB_NAME, self::$_db_conn)
               or $this->_die('Select failed');
         }
      }

      protected function _unslash_row($row)
      {
         foreach($row as $id => $value)
            $row[$id] = stripslashes($value);

         return $row;
      }

      public function escape($string)
      {
         $str = @mysql_real_escape_string($string, self::$_db_conn)
            or $this->_die('Escape failed');

         return $str;
      }

      public function query($query_string)
      {
         $res = @mysql_query($query_string, self::$_db_conn)
            or $this->_die('Query failed');

         return $res;
      }

      public function fetch_array($result)
      {
         $row = @mysql_fetch_array($result, MYSQL_NUM);

         return $row ? $this->_unslash_row($row) : false;
      }

      public function fetch_assoc($result)
      {
         $row = @mysql_fetch_assoc($result);

         return $row ? $this->_unslash_row($row) : false;
      }

      public function num_rows($result)
      {
         $num = @mysql_num_rows($result)
            or $this->_die('num_rows failed');

         return $num;
      }

      public function fetch_all_rows($sql, $index=false)
      {
         $out = array();
         $res = $this->query($sql);

         while($row = $this->fetch_assoc($res)) {
            if($index !== false)
               $out[$row[$index]] = $row;
            else
               $out[]            = $row;
         }

         return $out;
      }

      public function seek($result, $to)
      {
         @mysql_data_seek($result, $to)
            or $this->_die('Seek failed');
      }
   }
?>