コード例 #1
0
    private static function connect_database($name, $id) {
        if(is_null($id) && isset(self::$configs[$name])){
            $info = self::$configs[$name];
        } elseif(isset(self::$configs[$name][$id]) && is_array(self::$configs[$name])) {
            $info = self::$configs[$name][$id];
        } else {
            throw new Database_Exception("No DB with name $name $id available!");
        }
        
        Logger::debug("Connecting to DATABASE [<b>$name</b>] dns: " . var_export($info,true));
        
        if(get_class($info) != 'Dreamblaze\\SqlS\\Database_Config'){
            throw new Database_Exception("Invalid config on $name $id");
        }
        
        $fqclass = self::load_adapter_class($info->protocol);

        try {
            $connection = new $fqclass($info);
            $connection->protocol = $info->protocol;

            if (isset($info->charset))
                $connection->set_encoding($info->charset);
            
            $connection->set_timezone();
        } catch (PDOException $e) {
            throw new Database_Exception(null,null,$e);
        }
        
        if(is_null($id)){
            self::$connections[$name] = $connection;
        } else {
            self::$connections[$name][$id] = $connection;
        }
    }
コード例 #2
0
    /**
     * Execute a raw SQL query on the database.
     *
     * @param string $sql Raw SQL string to execute.
     * @param array &$values Optional array of bind values
     * @return mixed A result set object
     */
    public function query($sql, $values=array()) {
        $this->last_query = $sql;
        Logger::debug($sql . ' => ' . var_export($values,true));
        
        try {
            if (!($sth = $this->connection->prepare($sql)))
                Logger::error('PDO Prepare ERROR!' . ' SQL:' . $sql);
        } catch (PDOException $e) {
            Logger::error(array($e->getMessage(), $e->getTraceAsString()));
        }

        $sth->setFetchMode(PDO::FETCH_ASSOC);
        
        try {
            if (!$sth->execute($values)){
                Logger::error(array('PDO Exec ERROR!', "SQL: " . $sql , "VALUES: " . var_export($values, true) ,'PDO: ' . $sth->errorInfo()));
                throw new Database_Exception("PDO Exec ERROR! (1)", $e->getCode() , $e);
            }

        } catch (PDOException $e) {
            Logger::error(array('PDO Exec ERROR!' , "SQL: " . $sql , "VALUES: " . var_export($values, true) , "PDO: " . $e->getMessage()));
            throw $e;
        }
        return $sth;
    }