<?php
 
	/*
		untested
	*/
 
class database_odbc_Resdis {
 
	var $host = NULL;
	var $username = NULL;
	var $password = NULL;
	var $databaseName = NULL;
	var $link = NULL;
	var $queries = NULL;
	var $errors = NULL;
 
	function __construct ($host, $username, $password) {
//                require_once '../_configs/conexion.php';
                require_once $_SERVER['DOCUMENT_ROOT'].'/_configs/conecta.php';				
		$this->host = $host;
		$this->username = sha1($username);
		$this->password = sha1($password);
		$this->link = "";
		$this->queries = array ();
		$this->errors = array ();
 
		$this->link = odbc_connect($this->host, $username, $password);
	}
	function database_odbc($host, $username, $password) {
		$this->host = $host;
		$this->username = sha1($username);
		$this->password = sha1($password);
		$this->link = "";
		$this->queries = array ();
		$this->errors = array ();
 
		$this->link = odbc_connect($this->host, $username, $password);
	}
 
	function justquery($sql) {
		$this->queries[] = $sql;
		return odbc_exec($this->link, $sql);
	}
 
	function loadResult($sql) {
		if (!($cur = $this->justquery($sql))) {
			return null;
		}
		$ret = null;

		if ($row = odbc_fetch_row( $cur )) { 
                        //$ret = $row[1];
			$ret = odbc_result ($cur,1);
		}
		odbc_free_result( $cur );
		return $ret;
	}
 
	function loadFirstRow($sql) {
		if (!($cur = $this->justquery($sql))) {
			return null;
		}
		$ret = null;
		if ($row = odbc_fetch_object( $cur )) {
			$ret = $row;
		}
		odbc_free_result( $cur );
		return $ret;
	}
 
	function insertid() {
		//return mysql_insert_id( $this->link );
	}
 
	function query($sql, $key = "", $returns = true, $batch = false) {
		$sqls = $result = array ();
 
		switch ($batch) {
			default:
			case true:
				foreach ($sql as $index => $query) {
					$this->queries[] = $query;
					$answer = odbc_exec($this->link, $query);
 
					if (!$answer) {
						$this->errors[] = odbc_errormsg($this->link);
					}
					else {
						if ($returns != false) {
							if (odbc_num_rows($answer) > 0){
								while ($row = odbc_fetch_object($answer)) {
									if ($key != ""){
										$result[$index][$row->$key] = $row;
									}
									else {
										$result[$index][] = $row;
									}
								}
							} else {}
						} else {}
					}
				}
				break;
 
			case false:
				$this->queries[] = $sql;
				$answer = odbc_exec($this->link, $sql);
 
				if (!$answer) {
					$this->errors[] = odbc_errormsg($this->link);
					$result = false;
				}
				else {
					if ($returns != false) {
						if (odbc_num_rows($answer) > 0){
							while ($row = odbc_fetch_object($answer)) {
								if ($key != ""){
									$result[$row->$key] = $row;
								}
								else {
									$result[] = $row;
								}
							}
						}
						else {
							return odbc_errormsg($this->link);
						}
					}
					else {
						$result = true;
					}
				}
				break;
		}
 
		return $result;
	}
 
	function loadObject( $sql, &$object ) {
		if ($object != null) {
			if (!($cur = $this->justquery($sql))) {
				return false;
			} else {}
			if ($array = odbc_fetch_array( $cur )) {
				odbc_free_result( $cur );
				$this->bindArrayToObject( $array, $object);
				return true;
			}
			else {
				return false;
			}
		}
		else {
			if ($cur = $this->justquery($sql)) {
				if ($object = odbc_fetch_object( $cur )) {
					odbc_free_result( $cur );
					return true;
				}
				else {
					$object = null;
					return false;
				}
			}
			else {
				return false;
			}
		}
	}
	function loadObjectList( $sql, &$object ) {
		if ($object != null) {
			if (!($cur = $this->justquery($sql))) {
				return false;
			} else {
                        $idx=0;
                        while ($array = odbc_fetch_array($cur)){
				$this->bindArrayToObject( $array, $object[$idx]);
                                $idx++;
                        }
			odbc_free_result( $cur );
			return true;
                       }
		}
		else {
			if ($cur = $this->justquery($sql)) {
                           $idx= 0;
                           while ( $object[$idx] = odbc_fetch_object( $cur )) {
                                    $idx++;
				}
					odbc_free_result( $cur );
					return true;
			}
			else {
				return false;
			}
		}
	}
 
	function bindArrayToObject( $array, &$obj) {
		if (!is_array( $array ) || !is_object( $obj )) {
			return (false);
		}
 
		foreach (get_object_vars($obj) as $k => $v) {
			if( substr( $k, 0, 1 ) != '_' ) {
				$ak = $k;
				if (isset($array[$ak])) {
					$obj->$k = $array[$ak];
				}
			}
		}
 
		return true;
	}
 
	function formatCSVCell($data) {
		$useQuotes = false;
 
		$quotable = array (
				"\"" => "\"\"",
				"," => ",",
				"\n" => "\n"
			);
 
		foreach ($quotable as $char => $repl) {
			if (eregi($char, $data)) {
				$useQuotes = true;
			} else {}
		}
 
		if ($useQuotes == true) {
			foreach ($quotable as $char => $repl) {
				$data = str_replace($char, $repl, $data);
			}
 
			$data = "\"" . $data . "\"";
		}
		else {
 
		}
 
		return $data;
	}
}
?>
