Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4: */
3
 
4
// i think this class should go somewhere in a common PEAR-place,
5
// but since it is not very fancy to crowd the PEAR-namespace too much
6
// i dont know where to put it yet :-(
7
 
8
//
9
// +----------------------------------------------------------------------+
10
// | PHP Version 4                                                        |
11
// +----------------------------------------------------------------------+
12
// | Copyright (c) 1997-2003 The PHP Group                                |
13
// +----------------------------------------------------------------------+
14
// | This source file is subject to version 2.02 of the PHP license,      |
15
// | that is bundled with this package in the file LICENSE, and is        |
16
// | available at through the world-wide-web at                           |
17
// | http://www.php.net/license/2_02.txt.                                 |
18
// | If you did not receive a copy of the PHP license and are unable to   |
19
// | obtain it through the world-wide-web, please send a note to          |
20
// | license@php.net so we can mail you a copy immediately.               |
21
// +----------------------------------------------------------------------+
22
// | Authors: Wolfram Kriesing <wolfram@kriesing.de>                      |
23
// +----------------------------------------------------------------------+
24
//
25
//  $Id: OptionsDB.php,v 1.8.2.4 2009/03/12 17:19:52 dufuz Exp $
26
 
27
require_once 'Tree/Common.php';
28
 
29
/**
30
 *   this class additionally retreives a DB connection and saves it
31
 *   in the property "dbh"
32
 *
33
 *   @package  Tree
34
 *   @access   public
35
 *   @author   Wolfram Kriesing <wolfram@kriesing.de>
36
 *
37
 */
38
class Tree_OptionsDB extends Tree_Common
39
{
40
    /**
41
     *   @var    object
42
     */
43
    var $dbh;
44
 
45
 
46
    // {{{ Tree_OptionsDB()
47
 
48
    /**
49
     *   this constructor sets the options, since i normally need this and
50
     *   in case the constructor doesnt need to do anymore i already have
51
     * it done :-)
52
     *
53
     *   @version    02/01/08
54
     *   @access     public
55
     *   @author     Wolfram Kriesing <wolfram@kriesing.de>
56
     *   @param      boolean true if loggedIn
57
     */
58
    function Tree_OptionsDB($dsn , $options = array())
59
    {
60
        $res = $this->_connectDB($dsn);
61
        if (PEAR::isError($res)) {
62
            return $res;
63
        }
64
 
65
        $this->dbh->setFetchmode(DB_FETCHMODE_ASSOC);
66
        // do options afterwards since it overrules
67
        $this->Tree_Options($options);
68
    }
69
 
70
    // }}}
71
    // {{{ _connectDB()
72
 
73
    /**
74
     * Connect to database by using the given DSN string
75
     *
76
     * @author  copied from PEAR::Auth, Martin Jansen, slightly modified
77
     * @access private
78
     * @param  string DSN string
79
     * @return mixed  Object on error, otherwise bool
80
     */
81
    function _connectDB($dsn)
82
    {
83
        // only include the db if one really wants to connect
84
        require_once 'DB.php';
85
 
86
        if (is_string($dsn) || is_array($dsn)) {
87
            // put the dsn parameters in an array
88
            // DB would be confused with an additional URL-queries,
89
            //like ?table=... so we do it before connecting to the DB
90
            if (is_string($dsn)) {
91
                $dsn = DB::parseDSN($dsn);
92
            }
93
            $this->dbh = DB::Connect($dsn);
94
        } else {
95
            if (strtolower(get_parent_class($dsn)) == 'db_common') {
96
                $this->dbh = $dsn;
97
            } else {
98
                if (is_object($dsn) && DB::isError($dsn)) {
99
                    return new DB_Error($dsn->code, PEAR_ERROR_DIE);
100
                }
101
 
102
                return new PEAR_Error(
103
                                'The given dsn was not valid in file '.
104
                                __FILE__ . " at line " . __LINE__,
105
                                41,
106
                                PEAR_ERROR_RETURN,
107
                                null,
108
                                null
109
                                );
110
 
111
            }
112
        }
113
 
114
        if (DB::isError($this->dbh)) {
115
            return new DB_Error($this->dbh->code, PEAR_ERROR_DIE);
116
        }
117
 
118
        return true;
119
    }
120
 
121
    // }}}
122
}