Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
/*
4
 * This file is part of the symfony package.
5
 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6
 * (c) 2004-2006 Sean Kerr <sean@code-box.org>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
/**
13
 * sfDatabase is a base abstraction class that allows you to setup any type of
14
 * database connection via a configuration file.
15
 *
16
 * @package    symfony
17
 * @subpackage database
18
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
19
 * @author     Sean Kerr <sean@code-box.org>
20
 * @version    SVN: $Id: sfDatabase.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
21
 */
22
abstract class sfDatabase
23
{
24
  protected
25
    $parameterHolder = null,
26
    $connection      = null,
27
    $resource        = null;
28
 
29
  /**
30
   * Class constructor.
31
   *
32
   * @see initialize()
33
   */
34
  public function __construct($parameters = array())
35
  {
36
    $this->initialize($parameters);
37
  }
38
 
39
  /**
40
   * Initializes this sfDatabase object.
41
   *
42
   * @param array $parameters An associative array of initialization parameters
43
   *
44
   * @return bool true, if initialization completes successfully, otherwise false
45
   *
46
   * @throws <b>sfInitializationException</b> If an error occurs while initializing this sfDatabase object
47
   */
48
  public function initialize($parameters = array())
49
  {
50
    $this->parameterHolder = new sfParameterHolder();
51
    $this->parameterHolder->add($parameters);
52
  }
53
 
54
  /**
55
   * Connects to the database.
56
   *
57
   * @throws <b>sfDatabaseException</b> If a connection could not be created
58
   */
59
  abstract function connect();
60
 
61
  /**
62
   * Retrieves the database connection associated with this sfDatabase implementation.
63
   *
64
   * When this is executed on a Database implementation that isn't an
65
   * abstraction layer, a copy of the resource will be returned.
66
   *
67
   * @return mixed A database connection
68
   *
69
   * @throws <b>sfDatabaseException</b> If a connection could not be retrieved
70
   */
71
  public function getConnection()
72
  {
73
    if (null === $this->connection)
74
    {
75
      $this->connect();
76
    }
77
 
78
    return $this->connection;
79
  }
80
 
81
  /**
82
   * Retrieves a raw database resource associated with this sfDatabase implementation.
83
   *
84
   * @return mixed A database resource
85
   *
86
   * @throws <b>sfDatabaseException</b> If a resource could not be retrieved
87
   */
88
  public function getResource()
89
  {
90
    if (null === $this->resource)
91
    {
92
      $this->connect();
93
    }
94
 
95
    return $this->resource;
96
  }
97
 
98
  /**
99
   * Gets the parameter holder for this object.
100
   *
101
   * @return sfParameterHolder A sfParameterHolder instance
102
   */
103
  public function getParameterHolder()
104
  {
105
    return $this->parameterHolder;
106
  }
107
 
108
  /**
109
   * Gets the parameter associated with the given key.
110
   *
111
   * This is a shortcut for:
112
   *
113
   * <code>$this->getParameterHolder()->get()</code>
114
   *
115
   * @param string $name    The key name
116
   * @param string $default The default value
117
   *
118
   * @return string The value associated with the key
119
   *
120
   * @see sfParameterHolder
121
   */
122
  public function getParameter($name, $default = null)
123
  {
124
    return $this->parameterHolder->get($name, $default);
125
  }
126
 
127
  /**
128
   * Returns true if the given key exists in the parameter holder.
129
   *
130
   * This is a shortcut for:
131
   *
132
   * <code>$this->getParameterHolder()->has()</code>
133
   *
134
   * @param string $name The key name
135
   *
136
   * @return boolean true if the given key exists, false otherwise
137
   *
138
   * @see sfParameterHolder
139
   */
140
  public function hasParameter($name)
141
  {
142
    return $this->parameterHolder->has($name);
143
  }
144
 
145
  /**
146
   * Sets the value for the given key.
147
   *
148
   * This is a shortcut for:
149
   *
150
   * <code>$this->getParameterHolder()->set()</code>
151
   *
152
   * @param string $name  The key name
153
   * @param string $value The value
154
   *
155
   * @see sfParameterHolder
156
   */
157
  public function setParameter($name, $value)
158
  {
159
    $this->parameterHolder->set($name, $value);
160
  }
161
 
162
  /**
163
   * Executes the shutdown procedure.
164
   *
165
   * @return void
166
   *
167
   * @throws <b>sfDatabaseException</b> If an error occurs while shutting down this database
168
   */
169
  abstract function shutdown();
170
}