Skip to content

Commit 816a911

Browse files
committed
DB Connect can accept a database instance without breaking. Tested and documented that fact.
1 parent 2a98227 commit 816a911

3 files changed

Lines changed: 68 additions & 6 deletions

File tree

system/Database/Config.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ class Config extends BaseConfig
7474
*/
7575
public static function connect($group = null, bool $getShared = true)
7676
{
77+
// If a DB connection is passed in, just pass it back
78+
if ($group instanceof BaseConnection)
79+
{
80+
return $group;
81+
}
82+
7783
if (is_array($group))
7884
{
7985
$config = $group;

tests/system/Database/Live/ConnectTest.php

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
<?php namespace CodeIgniter\Database\Live;
2-
3-
;
1+
<?php namespace CodeIgniter\Database\Live;;
42

3+
use CodeIgniter\Config\Config;
54
use CodeIgniter\Test\CIDatabaseTestCase;
65
use Config\Database;
76

@@ -20,8 +19,8 @@ protected function setUp()
2019
$this->group1 = $config->default;
2120
$this->group2 = $config->default;
2221

23-
$this->group1['strictOn'] = false;
24-
$this->group2['strictOn'] = true;
22+
$this->group1['DBDriver'] = 'MySQLi';
23+
$this->group2['DBDriver'] = 'Postgre';
2524
}
2625

2726
public function testConnectWithMultipleCustomGroups()
@@ -39,6 +38,32 @@ public function testConnectWithMultipleCustomGroups()
3938
$this->assertEquals(3, count($instances));
4039
}
4140

42-
//--------------------------------------------------------------------
41+
public function testConnectReturnsProvidedConnection()
42+
{
43+
// This will be the tests database
44+
$db = Database::connect();
45+
$this->assertInstanceOf(\CodeIgniter\Database\SQLite3\Connection::class, $db);
46+
47+
// Get an instance of the system's default db so we have something to test with.
48+
$db1 = Database::connect($this->group1);
49+
$this->assertEquals('MySQLi', $this->getPrivateProperty($db1, 'DBDriver'));
50+
51+
// If a connection is passed into connect, it should simply be returned to us...
52+
$db2 = Database::connect($db1);
53+
$this->assertSame($db1, $db2);
54+
}
4355

56+
public function testConnectWorksWithGroupName()
57+
{
58+
$db = Database::connect('tests');
59+
$this->assertInstanceOf(\CodeIgniter\Database\SQLite3\Connection::class, $db);
60+
61+
$config = config('Database');
62+
$config->default['DBDriver'] = 'MySQLi';
63+
Config::injectMock('Database', $config);
64+
65+
$db1 = Database::connect('default');
66+
$this->assertNotInstanceOf(\CodeIgniter\Database\SQLite3\Connection::class, $db1);
67+
$this->assertEquals('MySQLi', $this->getPrivateProperty($db1, 'DBDriver'));
68+
}
4469
}

user_guide_src/source/database/connecting.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,37 @@ group names you are connecting to.
6161

6262
| $db->setDatabase($database2_name);
6363
64+
Connecting with Custom Settings
65+
===============================
66+
67+
You can pass in an array of database settings instead of a group name to get
68+
a connection that uses your custom settings. The array passed in must be
69+
the same format as the groups are defined in the configuration file::
70+
71+
$custom = [
72+
'DSN' => '',
73+
'hostname' => 'localhost',
74+
'username' => '',
75+
'password' => '',
76+
'database' => '',
77+
'DBDriver' => 'MySQLi',
78+
'DBPrefix' => '',
79+
'pConnect' => false,
80+
'DBDebug' => (ENVIRONMENT !== 'production'),
81+
'cacheOn' => false,
82+
'cacheDir' => '',
83+
'charset' => 'utf8',
84+
'DBCollat' => 'utf8_general_ci',
85+
'swapPre' => '',
86+
'encrypt' => false,
87+
'compress' => false,
88+
'strictOn' => false,
89+
'failover' => [],
90+
'port' => 3306,
91+
];
92+
$db = \Config\Database::connect($custom);
93+
94+
6495
Reconnecting / Keeping the Connection Alive
6596
===========================================
6697

0 commit comments

Comments
 (0)