This repository has been archived by the owner on Apr 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
DbHelper.php
82 lines (74 loc) · 2.58 KB
/
DbHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
// vim: fenc=utf-8:ft=php:ai:si:ts=4:sw=4:et:
/**
* Hashmark_DbHelper
*
* @filesource
* @copyright Copyright (c) 2008-2011 David Smith
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* @package Hashmark
* @subpackage Base
* @version $Id$
*/
/**
* Factory for database adapters, prepared statements, etc.
*
* @package Hashmark
* @subpackage Base
*/
class Hashmark_DbHelper extends Hashmark_Module
{
/**
* Factory for Zend adapter wrappers.
*
* @param string $name 'Mysqli' or 'Pdo'.
* @param Array $config Constructor argument
* @return Zend_Db_Adapter_* New instance.
* return
*/
public function getAdapter($name, $config)
{
require_once HASHMARK_ROOT_DIR . "/DbAdapter/{$name}.php";
$className = "Hashmark_DbAdapter_{$name}";
return new $className($config);
}
/**
* Configure new database adapter that will open a connection on-demand.
*
* - Mainly for the Cron and unit test classes.
*
* @param string $profileName
* @return Zend_Db_Adapter_* New instance.
* @throws Exception If profile or adapter name is unrecognized.
*/
public function openDb($profileName)
{
$allowedAdapters = array('Mysqli', 'Pdo');
if (defined('HASHMARK_TEST_MODE')) {
$profileName = 'unittest';
}
if (!isset($this->_baseConfig['profile'][$profileName])) {
throw new Exception("Database profile '{$profileName}' is not available.");
}
if (!in_array($this->_baseConfig['profile'][$profileName]['adapter'], $allowedAdapters)) {
throw new Exception("Database adapter '{$this->_baseConfig['profile'][$profileName]['adapter']}' is not allowed.");
}
return $this->getAdapter($this->_baseConfig['profile'][$profileName]['adapter'],
$this->_baseConfig['profile'][$profileName]['params']);
}
/**
* Reuse an existing connection in a new adapter.
*
* @param mixed $link Database connection object/resource.
* @param string $adapterName Ex. 'Mysqli'
* @return Zend_Db_Adapter_* New instance.
* @throws Exception If adapter name is unrecognized.
*/
public function reuseDb($link, $adapterName)
{
$config = array('host' => '', 'username' => '', 'password' => '', 'dbname' => '');
$dbAdapter = $this->getAdapter($adapterName, $config);
$dbAdapter->setConnection($link);
return $dbAdapter;
}
}