forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.php
More file actions
81 lines (65 loc) · 1.86 KB
/
Copy pathDatabase.php
File metadata and controls
81 lines (65 loc) · 1.86 KB
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
<?php namespace CodeIgniter\Database;
use Zend\Escaper\Exception\InvalidArgumentException;
/**
* Database Connection Factory
*
* Creates and returns an instance of the appropriate DatabaseConnection
*
* @package CodeIgniter\Database
*/
class Database
{
/**
* Maintains an array of the instances of all connections
* that have been created. Helps to keep track of all open
* connections for performance monitoring, logging, etc.
*
* @var array
*/
protected $connections = [];
//--------------------------------------------------------------------
/**
* Parses the connection binds and returns an instance of
* the driver ready to go.
*
* @param array $params
* @param bool $useBuilder
*/
public function load(array $params = [], string $alias)
{
// No DB specified? Beat them senseless...
if (empty($params['DBDriver']))
{
throw new InvalidArgumentException('You have not selected a database type to connect to.');
}
$className = strpos($params['DBDriver'], '\\') === false
? '\CodeIgniter\Database\\'.$params['DBDriver'].'\\Connection'
: $params['DBDriver'].'\\Connection';
$class = new $className($params);
// Store the connection
$this->connections[$alias] = $class;
return $this->connections[$alias];
}
//--------------------------------------------------------------------
/**
* Creates a new Forge instance for the current database type.
*
* @param ConnectionInterface $db
*
* @return mixed
*/
public function loadForge(ConnectionInterface $db)
{
$className = strpos($db->DBDriver, '\\') === false
? '\CodeIgniter\Database\\'.$db->DBDriver.'\\Forge'
: $db->DBDriver.'\\Connection';
// Make sure a connection exists
if (! $db->connID)
{
$db->initialize();
}
$class = new $className($db);
return $class;
}
//--------------------------------------------------------------------
}