Open-Source PHP Framework - Designed for rapid development of performance-oriented scalable applications

/sql/sybase

[return to app]
1
<?php
/**
 * Converts the procedural Sybase PHP class to object-oriented
 */
class sybase {
    /**
     * Database resource ID
     * @var object
     */
    public $resource;

    /**
     * The database method used to sanitize user input strings
     */
    const ESCAPE_STRING = 'escapeString';

    /**
     * Creates a string sanitation method as it is missing in the Sybase PHP extension
     *
     * @param string $var
     * @return string
     */
    public function escapeString($var) {
        return str_replace("'", "\\'", $var);
    }

    /**
     * Connect to Sybase
     *
     * @param string $host
     * @param string $username
     * @param string $passwd
     * @param string $dbname Optional
     * @param boolean $persistent Optional default is true
     * @param string $charset Optional
     * @param string $appname Optional
     * @param boolean $new Optional
     */
    public function __construct($host, $username, $password, $dbname = null, $persistent = true,
                                $charset = null, $appname = null, $new = null) {
        try {
            $connect = 'sybase_' . ($persistent ? 'pconnect' : 'connect');
            $this->resource = $connect($host, $username, $password, $charset, $appname, $new);
            if (!$this->resource) {
                throw new Exception('Cannot connect to Sybase');
            }
        } catch (Exception $e) {
            $this->error = $e->getMessage();
        }
        if ($dbname) {
            sybase_select_db($dbname, $this->resource);
        }
    }

    /**
     * Catch-all method allowing you to call any Sybase function via camel-cap object-oriented syntax.
     *
     * From your models you could access:
     *
     * $this->db->fetchAssoc()
     * $this->db->result()
     * $this->db->unbufferredQuery()
     * $this->db->close()
     * etc.
     *
     * @param string $name
     * @param mixed $val
     */
    public function __call($name, $val) {
        $name = strtolower('sybase_' . preg_replace('/[A-Z]/', '_$0', $name));
        if (function_exists($name)) {
            return call_user_func_array($name, $val);
        }
    }
}