Skip to content

Instantly share code, notes, and snippets.

@ionware
Last active August 14, 2019 07:36
Show Gist options
  • Save ionware/802838bb6e64ad116d0b3c8502a23b62 to your computer and use it in GitHub Desktop.
Save ionware/802838bb6e64ad116d0b3c8502a23b62 to your computer and use it in GitHub Desktop.
Acquiring data from multiple database source or cluster (simplest arbitrary demonstration)
<?php
class Database {
/**
* The connection resource.
*
* @var PDO|null
*/
protected $connection = null;
public function __construct(string $dsn, string $username, string $password) {
$this->connection = new PDO(
$dsn,
$username,
$password,
$this->dbOptions()
);
}
/** statically create an instance of database
* class.
*
* @param string $dsn
* @param string $username
* @param string $password
* @return Database
*/
public static function connect(string $dsn, string $username, string $password)
{
return new self($dsn, $username, $password);
}
protected function action()
{
/*
* [IMPORTANT]
* Whatever query / action we'd like to perform
* goes in here.
* */
return $this->connection->query('select name from students');
}
public function get()
{
/*
* [IMPORTANT]
* Put whatever logic of getting data here.
* */
$result = $this->action()->fetch(PDO::FETCH_ASSOC);
$this->close();
return $result;
}
/**
* Closes the database connection
*/
protected function close()
{
$this->connection = null;
}
/**
* Options for PDO connections.
*
* @return array
*/
protected function dbOptions()
{
return [
PDO::ATTR_FECH
];
}
}
<?php
/*
* Just a quick hack, it is best to use composer autoloader.
* */
require_once './db.php';
$data_store = [
/*
* since the database name varies by just some unique values at the end
* we can generate automatically, I will just assume that is true.
* */
[
// 'database' => 'vt1',
'username' => 'ionware',
'password' => 'secret'
],
[
// 'database' => 'vt2',
'username' => 'ionware',
'password' => 'secret'
],
];
/*
* Uhm... I might just use the result on the fly, but I can just
* store it and use it later. So, let's create a result variable (as array)
* */
$count = 0;
// Would be our database alias.
$db_alias = 'vt';
$dsn = 'mysql:host=localhost;dbname='.$db_alias;
$result = array_reduce($data_store, function($before, $store) use ($count, $dsn) {
global $count;
$count++;
return array_merge($before, [
'cluster'.$count => Database::connect($dsn.$count, $store['username'], $store['password'])
->get()
]);
}, []);
var_dump($result);
<?php
/*
* THE LOOP APPROACH
* Just a quick hack, it is best to use composer autoloader.
* */
require_once './db.php';
/*
* Uhm... I might just use the result on the fly, but I can just
* store it and use it later. So, let's create a result variable (as array)
* */
$result = [];
// Would be our database alias.
$db_alias = 'vt';
$dsn = 'mysql:host=localhost;dbname='.$db_alias;
$db_name = 'ionware';
$db_pass = 'secret';
/*
* The Loop approach
* */
for($i = 1; $i <= 2; /* where to stop*/ $i++) {
$result = array_merge($result, [
'db'.$i => Database::connect($dsn.$i, $db_name, $db_pass)
->get()
]);
}
var_dump($result);
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `vt2`
--
-- --------------------------------------------------------
--
-- Table structure for table `students`
--
CREATE TABLE `students` (
`id` int(11) NOT NULL,
`name` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `students`
--
INSERT INTO `students` (`id`, `name`) VALUES
(1, 'John Snow');
--
-- Indexes for dumped tables
--
--
-- Indexes for table `students`
--
ALTER TABLE `students`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `students`
--
ALTER TABLE `students`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
@ionware
Copy link
Author

ionware commented Aug 13, 2019

Setup the database, update the data_store variable, and run the index.php file.

Let's discuss!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment