<?php
   //......................................
   public function Execute($query,$data=false)
   {
      $q2 = UTF8decode($query);
      preprocess_sql2000($q2);
      return parent::Execute($q2, UTF8decode($data) );
   }
   //......................................
   public function PageExecute($query,$pgsz,$pg,$data=false)
   {
      $q2 = UTF8decode($query);
      preprocess_sql2000($q2);
      return parent::PageExecute($query, $pgsz, $pg, UTF8decode($data) );
   }
   //......................................
   public function MetaPrimaryKeys($tablename)
   {
      $query="sp_pkeys @table_name='$tablename' ";
      if (($rs=parent::Execute($query))===false)
         return false;
         
      $ar1=array();
      while ($o1=$rs->FetchNextObject()) {
         $ar1[]=$o1->COLUMN_NAME;
      }
      $rs->Close();

      return $ar1;
   }
   //......................................
   function get_idx_array($tablename, $extdata)
   {
      $keys=array_keys($extdata);

      if (($rs=$this->Execute("sp_helpindex @objname='$tablename' "))===false) {
         echo $this->ErrorMsg();
         return false;
      }
      while ($o1=$rs->FetchNextObject()) {
         $ar1=explode(',', $o1->INDEX_DESCRIPTION);
         array_walk($ar1, 'f_index_proc');
         if (!in_array('UNIQUE', $ar1))
            continue;

         $ar1=explode(',', $o1->INDEX_KEYS);
         array_walk($ar1, 'f_index_proc');

         $fi=true;
         foreach ($ar1 as $f1) {
            if (!in_array($f1,$keys)) {
               $fi=false;
               break;
            }
         }
         if ($fi) {
            return $ar1;
         }
      }
      $rs->Close();

      return false;
   }
   //......................................
   public function MetaColumns($tablename)
   {
      $query="EXEC sp_columns @table_name = '$tablename' ";
      if (($rs=parent::Execute($query))===false)
         return false;

      $ar1=array();
      while ($o1=$rs->FetchNextObject()) {
         $ar2=array();

         $ar2['name']=$o1->COLUMN_NAME;
         $ar2['max_length']=$o1->PRECISION;

         $tna=explode(' ',$o1->TYPE_NAME);
         $ar2['type']=$tna[0];

         $ar1[$o1->COLUMN_NAME]=(object)$ar2;
      }
      $rs->Close();

      return $ar1;
   }
   //......................................
   public function ReplaceEx($t1, $data, $prim=false)
   {
      if ($prim===false)
         $prim=$this->get_idx_array($t1, $data);
         
      if ($prim===false) {
         // Still false
         return 0;
      }
      return ReplaceEx($this, $t1, $data, $prim);
   }
   //......................................
   public function ReplaceEx2($t1, $data, $prim=false)
   {
      if ($prim===false)
         $prim=$this->get_idx_array($t1, $data);
         
      if ($prim===false) {
         // Still false
         return 0;
      }
      return ReplaceEx2($this, $t1, $data, $prim);
   }
   //......................................
?>