Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Modifications;
- $configs = [
- 'host' => 'localhost',
- 'port' => 3306,
- 'user' => 'root',
- 'pass' => 'root',
- 'db_name' => 'test',
- 'table_name' => 'test_modification',
- ];
- $test = new Script1($configs);
- $test->run();
- class Script1
- {
- /** @var array */
- private $configs = [];
- private $connection = NULL;
- // ########################################
- public function __construct(array $configs)
- {
- $this->configs = $configs;
- }
- // ########################################
- public function run()
- {
- try {
- $this->preparationWorkingEnvironment();
- while (true) {
- if ($this->getConnection()->query($this->getRandomQuery()) === false) {
- throw $this->createException('Could not execute query.');
- }
- }
- } catch (\Exception $exception) {
- echo $exception->getMessage();
- }
- }
- // ########################################
- public function preparationWorkingEnvironment()
- {
- $createTableSql = <<<SQL
- CREATE TABLE IF NOT EXISTS {$this->configs['table_name']} (
- id int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
- field_int int(10) NOT NULL,
- field_float float NOT NULL,
- field_varchar varchar(100) NOT NULL,
- field_text longtext NOT NULL,
- field_datetime datetime NOT NULL,
- PRIMARY KEY (id)
- )
- ENGINE = INNODB
- CHARACTER SET utf8
- COLLATE utf8_unicode_ci;
- SQL;
- if ($this->getConnection()->query($createTableSql) === false) {
- throw $this->createException('Could not create table.');
- }
- $preparedInserts = [];
- for ($i = 1; $i <= 1000; $i++) {
- $floatVal = mt_rand() / mt_getrandmax();
- $dateTime = new \DateTime('now', new \DateTimeZone('UTC'));
- $dateTime->modify("+ {$i} day");
- $preparedInserts[] = "({$i}, {$floatVal}, '{$this->generateRandomString(25)}', '{$this->generateRandomString(200)}', '{$dateTime->format('Y-m-d H:i:s')}')";
- }
- $insertQuery = "INSERT INTO {$this->configs['table_name']} (field_int, field_float, field_varchar, field_text, field_datetime)
- VALUES " . implode(',', $preparedInserts);
- if ($this->getConnection()->query($insertQuery) === false) {
- throw $this->createException('Can not fill the table with test data.');
- }
- }
- // ########################################
- private function generateRandomString($length = 10)
- {
- $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
- $charactersLength = strlen($characters);
- $randomString = '';
- for ($i = 0; $i < $length; $i++) {
- $randomString .= $characters[rand(0, $charactersLength - 1)];
- }
- return $randomString;
- }
- // ########################################
- private function getRandomQuery()
- {
- $queries = [
- "INSERT INTO {$this->configs['table_name']} (field_int, field_float, field_varchar, field_text, field_datetime)
- VALUES (51, 0.51, '{$this->generateRandomString(25)}', '{$this->generateRandomString(200)}', '{$this->getCurrentDateTime()->format('Y-m-d H:i:s')}')",
- "UPDATE {$this->configs['table_name']} SET field_text = '{$this->generateRandomString(150)}' WHERE field_int > 10 LIMIT 400",
- "DELETE FROM {$this->configs['table_name']} WHERE field_float = 0.51",
- ];
- return $queries[rand(0, 2)];
- }
- // ########################################
- private function getConnection()
- {
- if (!is_null($this->connection)) {
- return $this->connection;
- }
- $connection = new \PDO("mysql:host={$this->configs['host']};dbname={$this->configs['db_name']};port={$this->configs['port']}",
- $this->configs['user'], $this->configs['pass'],
- [\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION]);
- return $this->connection = $connection;
- }
- // ########################################
- private function createException($message)
- {
- return new \Exception($message . PHP_EOL . 'Error info:' . PHP_EOL . print_r($this->getConnection()->errorInfo(), true));
- }
- private function getCurrentDateTime()
- {
- return new \DateTime('now', new \DateTimeZone('UTC'));
- }
- // ########################################
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement