Hoa central
Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | Static Protected Attributes | Private Member Functions | Static Private Attributes | List of all members
Hoa\Database\Dal Class Reference
Inheritance diagram for Hoa\Database\Dal:

Public Member Functions

 getParameters ()
 
 close ()
 
 beginTransaction ()
 
 commit ()
 
 rollBack ()
 
 lastInsertId ($name=null)
 
 prepare ($statement, Array $options=[])
 
 quote ($string=null, $type=-1)
 
 query ($statement)
 
 errorCode ()
 
 errorInfo ()
 
 getAvailableDrivers ()
 
 setAttributes (Array $attributes)
 
 setAttribute ($attribute, $value)
 
 getAttributes ()
 
 getAttribute ($attribute)
 
 getId ()
 

Static Public Member Functions

static initializeParameters (Array $parameters=[])
 
static getInstance ($id, $dalName=null, $dsn=null, $username=null, $password=null, Array $driverOptions=[])
 
static getLastInstance ()
 

Public Attributes

const DBA = 'Dba'
 
const DBX = 'Dbx'
 
const ODBC = 'Odbc'
 
const PDO = 'Pdo'
 

Protected Member Functions

 setDal (IDal\Wrapper $dal)
 
 getDal ()
 

Protected Attributes

 $__id = null
 
 $_layer = null
 

Static Protected Attributes

static $_parameters = null
 

Private Member Functions

 __construct ($dalName, $dsn, $username, $password, Array $driverOptions=[])
 

Static Private Attributes

static $_instance = []
 
static $_id = null
 

Detailed Description

Class .

The higher class of the Database Abstract Layer. It wrappes all DAL.

Definition at line 49 of file Dal.php.

Constructor & Destructor Documentation

Hoa\Database\Dal::__construct (   $dalName,
  $dsn,
  $username,
  $password,
Array  $driverOptions = [] 
)
private

Create a DAL instance, representing a connection to a database. The constructor is private to make a multiton.

Parameters
string$dalNameThe database abstract layer name.
string$dsnThe DSN of database.
string$usernameThe username to connect to database.
string$passwordThe password to connect to database.
array$driverOptionsThe driver options.
Returns
void
Exceptions

Definition at line 128 of file Dal.php.

134  {
135  // Please see https://bugs.php.net/55154.
136  if (0 !== preg_match('#^sqlite:(.+)$#i', $dsn, $matches)) {
137  $dsn = 'sqlite:' . resolve($matches[1]);
138  }
139 
140  $id = $this->__id = self::$_id;
141  $event = 'hoa://Event/Database/' . $id;
142 
143  Core\Event::register($event . ':opened', $this);
144  Core\Event::register($event . ':closed', $this);
145 
146  $this->setDal(dnew(
147  '\Hoa\Database\Layer\\' . $dalName,
148  [$dsn, $username, $password, $driverOptions]
149  ));
150 
151  Core\Event::notify(
152  $event . ':opened',
153  $this,
154  new Core\Event\Bucket([
155  'id' => $id,
156  'dsn' => $dsn,
157  'username' => $username,
158  'driverOptions' => $driverOptions
159  ])
160  );
161 
162  return;
163  }
setDal(IDal\Wrapper $dal)
Definition: Dal.php:332

Here is the call graph for this function:

Member Function Documentation

Hoa\Database\Dal::beginTransaction ( )

Initiate a transaction.

Returns
bool
Exceptions

Definition at line 356 of file Dal.php.

357  {
358  return $this->getDal()->beginTransaction();
359  }

Here is the call graph for this function:

Hoa\Database\Dal::close ( )

Close connection to the database.

Returns
bool

Definition at line 305 of file Dal.php.

306  {
307  $id = $this->getId();
308  $event = 'hoa://Event/Database/' . $id;
309 
310  $this->_layer = null;
311  self::$_id = null;
312  unset(self::$_instance[$id]);
313 
314  Core\Event::notify(
315  $event . ':closed',
316  $this,
317  new Core\Event\Bucket(['id' => $id])
318  );
319 
320  Core\Event::unregister($event . ':opened');
321  Core\Event::unregister($event . ':closed');
322 
323  return true;
324  }

Here is the call graph for this function:

Hoa\Database\Dal::commit ( )

Commit a transaction.

Returns
bool
Exceptions

Definition at line 367 of file Dal.php.

368  {
369  return $this->getDal()->commit();
370  }

Here is the call graph for this function:

Hoa\Database\Dal::errorCode ( )

Fetch the SQLSTATE associated with the last operation on the database handle.

Returns
string
Exceptions

Definition at line 459 of file Dal.php.

460  {
461  return $this->getDal()->errorCode();
462  }

Here is the call graph for this function:

Hoa\Database\Dal::errorInfo ( )

Fetch extends error information associated with the last operation on the database handle.

Returns
array
Exceptions

Definition at line 471 of file Dal.php.

472  {
473  return $this->getDal()->errorInfo();
474  }

Here is the call graph for this function:

Hoa\Database\Dal::getAttribute (   $attribute)

Retrieve a database connection attribute.

Parameters
string$attributeAttribute name.
Returns
mixed
Exceptions

Definition at line 530 of file Dal.php.

531  {
532  return $this->getDal()->getAttribute($attribute);
533  }

Here is the call graph for this function:

Hoa\Database\Dal::getAttributes ( )

Retrieve all database connection attributes.

Returns
array
Exceptions

Definition at line 518 of file Dal.php.

519  {
520  return $this->getDal()->getAttributes();
521  }

Here is the call graph for this function:

Hoa\Database\Dal::getAvailableDrivers ( )

Return an array of available drivers.

Returns
array
Exceptions

Definition at line 482 of file Dal.php.

483  {
484  return $this->getDal()->getAvailableDrivers();
485  }

Here is the call graph for this function:

Hoa\Database\Dal::getDal ( )
protected

Get the database abstract layer instance.

Returns

Definition at line 345 of file Dal.php.

346  {
347  return $this->_layer;
348  }

Here is the caller graph for this function:

Hoa\Database\Dal::getId ( )

Get current ID.

Returns
string

Definition at line 540 of file Dal.php.

541  {
542  return $this->__id;
543  }

Here is the caller graph for this function:

static Hoa\Database\Dal::getInstance (   $id,
  $dalName = null,
  $dsn = null,
  $username = null,
  $password = null,
Array  $driverOptions = [] 
)
static

Make a multiton on the $id.

Parameters
string$idThe instance ID.
string$dalNameThe database abstract layer name.
string$dsnThe DSN of database.
string$usernameThe username to connect to database.
string$passwordThe password to connect to database.
array$driverOptionsThe driver options.
Returns
Exceptions

Definition at line 206 of file Dal.php.

213  {
214  if (null === self::$_parameters) {
215  self::initializeParameters();
216  }
217 
218  self::$_id = $id;
219 
220  if (isset(self::$_instance[$id])) {
221  return self::$_instance[$id];
222  }
223 
224  if (null === $dalName &&
225  null === $dsn &&
226  null === $username &&
227  null === $password &&
228  empty($driverOptions)) {
229  $list = self::$_parameters->unlinearizeBranche('connection.list');
230 
231  if (!isset($list[$id])) {
232  throw new Exception(
233  'Connection ID %s does not exist in the connection list.',
234  0,
235  $id
236  );
237  }
238 
239  $handle = $list[$id];
240  $dalName = @$handle['dal'] ?: 'Undefined';
241  $dsn = @$handle['dsn'] ?: '';
242  $username = @$handle['username'] ?: '';
243  $password = @$handle['password'] ?: '';
244  $driverOptions = @$handle['options'] ?: [];
245  }
246 
247  return self::$_instance[$id] = new self(
248  $dalName,
249  $dsn,
250  $username,
251  $password,
252  $driverOptions
253  );
254  }
static Hoa\Database\Dal::getLastInstance ( )
static

Get the last instance of a DAL, i.e. the last used singleton. If no instance was set but if the connection.autoload parameter is set, then we auto-connect (autoload) a connection.

Returns
Exceptions

Definition at line 264 of file Dal.php.

265  {
266  if (null === self::$_parameters) {
267  self::initializeParameters();
268  }
269 
270  if (null === self::$_id) {
271  $autoload = self::$_parameters->getFormattedParameter(
272  'connection.autoload'
273  );
274 
275  if (null !== $autoload) {
276  self::getInstance($autoload);
277  }
278  }
279 
280  if (null === self::$_id) {
281  throw new Exception(
282  'No instance was set, cannot return the last instance.',
283  1
284  );
285  }
286 
287  return self::$_instance[self::$_id];
288  }
static getInstance()
Definition: Core.php:193

Here is the call graph for this function:

Hoa\Database\Dal::getParameters ( )

Get parameters.

Returns

Implements Hoa\Core\Parameter\Parameterizable.

Definition at line 295 of file Dal.php.

296  {
297  return self::$_parameters;
298  }
static Hoa\Database\Dal::initializeParameters ( Array  $parameters = [])
static

Initialize parameters.

Parameters
array$parametersParameters.
Returns
void

Example: 'connection.list.default.dal' => Dal::PDO, 'connection.list.default.dsn' => 'sqlite:hoa://Data/Variable/Database/Foo.sqlite', 'connection.list.default.username' => '', 'connection.list.default.password' => '', 'connection.list.default.options' => null,

Definition at line 171 of file Dal.php.

172  {
173  self::$_parameters = new Core\Parameter(
174  __CLASS__,
175  [],
176  [
186  'connection.autoload' => null // or connection ID, e.g. 'default'.
187  ]
188  );
189  self::$_parameters->setParameters($parameters);
190 
191  return;
192  }
Hoa\Database\Dal::lastInsertId (   $name = null)

Return the ID of the last inserted row or sequence value.

Parameters
string$nameName of sequence object (needed for some driver).
Returns
string
Exceptions

Definition at line 391 of file Dal.php.

392  {
393  if (null === $name) {
394  return $this->getDal()->lastInsertId();
395  }
396 
397  return $this->getDal()->lastInsertId($name);
398  }

Here is the call graph for this function:

Hoa\Database\Dal::prepare (   $statement,
Array  $options = [] 
)

Prepare a statement for execution and returns a statement object.

Parameters
string$statementThis must be a valid SQL statement for the target database server.
array$optionsOptions to set attributes values for the layer statement.
Returns
Exceptions

Definition at line 410 of file Dal.php.

411  {
412  return new DalStatement(
413  $this->getDal()->prepare(
414  $statement, $options
415  )
416  );
417  }
prepare($statement, Array $options=[])
Definition: Dal.php:410

Here is the call graph for this function:

Hoa\Database\Dal::query (   $statement)

Execute an SQL statement, returning a result set as a object.

Parameters
string$statementThe SQL statement to prepare and execute.
Returns
Exceptions

Definition at line 445 of file Dal.php.

446  {
447  return new DalStatement(
448  $this->getDal()->query($statement)
449  );
450  }
query($statement)
Definition: Dal.php:445

Here is the call graph for this function:

Hoa\Database\Dal::quote (   $string = null,
  $type = -1 
)

Quote a string for use in a query.

Parameters
string$stringThe string to be quoted.
int$typeProvide a data type hint for drivers that have alternate quoting styles.
Returns
string
Exceptions

Definition at line 428 of file Dal.php.

429  {
430  if ($type < 0) {
431  return $this->getDal()->quote($string);
432  }
433 
434  return $this->getDal()->quote($string, $type);
435  }

Here is the call graph for this function:

Hoa\Database\Dal::rollBack ( )

Roll back a transaction.

Returns
bool
Exceptions

Definition at line 378 of file Dal.php.

379  {
380  return $this->getDal()->rollBack();
381  }

Here is the call graph for this function:

Hoa\Database\Dal::setAttribute (   $attribute,
  $value 
)

Set a specific attribute.

Parameters
mixed$attributeAttribute name.
mixed$valueAttribute value.
Returns
mixed
Exceptions

Definition at line 507 of file Dal.php.

508  {
509  return $this->getDal()->setAtribute($attribute, $value);
510  }

Here is the call graph for this function:

Hoa\Database\Dal::setAttributes ( Array  $attributes)

Set attributes.

Parameters
array$attributesAttributes values.
Returns
array
Exceptions

Definition at line 494 of file Dal.php.

495  {
496  return $this->getDal()->setAttributes($attributes);
497  }

Here is the call graph for this function:

Hoa\Database\Dal::setDal ( IDal\Wrapper  $dal)
protected

Set database abstract layer instance.

Parameters
\Hoa\Database\IDal\Wrapper$dalThe DAL instance.
Returns

Definition at line 332 of file Dal.php.

333  {
334  $old = $this->_layer;
335  $this->_layer = $dal;
336 
337  return $old;
338  }

Here is the caller graph for this function:

Member Data Documentation

Hoa\Database\Dal::$__id = null
protected

Definition at line 98 of file Dal.php.

Hoa\Database\Dal::$_id = null
staticprivate

Definition at line 91 of file Dal.php.

Hoa\Database\Dal::$_instance = []
staticprivate

Definition at line 84 of file Dal.php.

Hoa\Database\Dal::$_layer = null
protected

Definition at line 105 of file Dal.php.

Hoa\Database\Dal::$_parameters = null
staticprotected

Definition at line 112 of file Dal.php.

const Hoa\Database\Dal::DBA = 'Dba'

Abstract layer: DBA.

string

Definition at line 56 of file Dal.php.

const Hoa\Database\Dal::DBX = 'Dbx'

Abstract layer: DBX.

string

Definition at line 63 of file Dal.php.

const Hoa\Database\Dal::ODBC = 'Odbc'

Abstract layer: Odbc.

string

Definition at line 70 of file Dal.php.

const Hoa\Database\Dal::PDO = 'Pdo'

Abstract layer: PDO.

string

Definition at line 77 of file Dal.php.


The documentation for this class was generated from the following file: