コード例 #1
0
<?php

require './../vendor/autoload.php';
$pdo = \wh1tew0lf\DB\BasePDO::create('mysql:dbname=test;host=localhost', 'root', '');
$ids = [];
$iSql = \wh1tew0lf\SQLBuilder\MySQLBuilder::start()->insert('author', ['name' => ':name', 'birthday' => ':birthday']);
$stmt = $pdo->prepare($iSql);
if ($stmt instanceof PDOStatement) {
    foreach (['Vasay', 'Petya', 'Nikolay', 'Kolya', 'Masha'] as $fio) {
        $stmt->execute([':name' => $fio, ':birthday' => date('Y-m-d', time() - rand(1, 500) * 86400)]);
        $ids[] = $pdo->lastInsertId();
    }
}
$newsIds = [];
foreach (['first' => 'Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explica', 'second' => 'But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, becau', 'third' => 'cepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quid', 'fourth' => 'o blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a'] as $title => $content) {
    $iSql = \wh1tew0lf\SQLBuilder\MySQLBuilder::start()->insert('news', ['authorId' => ':authorId', 'title' => ':title', 'content' => ':content', 'date' => ':date']);
    $stmt = $pdo->prepare($iSql);
    if ($stmt instanceof PDOStatement) {
        $stmt->bindValue(':authorId', $ids[rand(0, count($ids) - 1)]);
        $stmt->bindValue(':title', $title);
        $stmt->bindValue(':content', $content);
        $stmt->bindValue(':date', date('Y-m-d', time() - rand(1, 500) * 86400));
        $stmt->execute();
        $newsIds[] = $pdo->lastInsertId();
    }
}
foreach ($newsIds as $id) {
    $iSql = \wh1tew0lf\SQLBuilder\MySQLBuilder::start()->insert('like', ['newsId' => ':newsId']);
    for ($i = 0; $i < rand(1, 5); ++$i) {
        $stmt = $pdo->prepare($iSql);
        if ($stmt instanceof PDOStatement) {
コード例 #2
0
ファイル: Transfer.php プロジェクト: wh1tew0lf/SQLBuilder
 /**
  * Copy tables from fromDB to toDB
  * @param string|array $tables
  * @param array $params
  * @throws Exception
  */
 public function copy($tables, $params = [])
 {
     $tables = !is_array($tables) ? [$tables] : $tables;
     foreach ($tables as $tableName => $tableParams) {
         if (is_int($tableName) && is_string($tableParams)) {
             $fromTable = $tableParams;
             $toTable = $tableParams;
         } elseif (is_string($tableName) && is_string($tableParams)) {
             $fromTable = $tableName;
             $toTable = $tableParams;
         } elseif (is_int($tableName) && is_array($tableParams) && isset($tableParams['sql'])) {
             $select = $tableParams['sql'];
             $fromTable = false;
             $toTable = $tableParams['table'];
         } elseif (is_string($tableName) && is_array($tableParams) && !isset($tableParams['sql'])) {
             $fromTable = $tableName;
             $toTable = $tableParams['table'];
         } else {
             throw new Exception('Incorrect tables');
         }
         if (is_array($tableParams)) {
             $params = array_merge($tableParams, $params);
         }
         if (false !== $fromTable && !$this->fromDB->isTableExists($fromTable)) {
             throw new Exception('Table does not exists at first table');
         }
         $select = isset($select) ? $select : $this->fromDB->getSQLBuilder()->from($fromTable);
         $useType = !(is_array($tableParams) && isset($tableParams['sql']));
         if (is_array($tableParams) && isset($tableParams['sql']) && isset($params['columns'])) {
             $sourceColumns = $params['columns'];
         } elseif (is_array($tableParams) && isset($tableParams['sql']) && !isset($params['columns'])) {
             $sourceColumns = $this->fromDB->extractColumns($select);
             $select->select($this->fromDB->getSelectAll($select));
         } else {
             $sourceColumns = $this->fromDB->getColumns($fromTable);
         }
         $columns = $this->processColumns($sourceColumns, $params);
         $columnsMap = $this->createColumnsMap($sourceColumns, $params);
         if ($this->toDB->isTableExists($toTable)) {
             if ('rewrite' == $this->existsAction) {
                 $this->toDB->dropTable($toTable);
                 $this->toDB->createTable($toTable, $columns);
             } elseif ('truncate' == $this->existsAction && $this->toDB->isTableEqual($toTable, $columns)) {
                 $this->toDB->truncateTable($toTable);
             } elseif ('truncate' == $this->existsAction && !$this->toDB->isTableEqual($toTable, $columns)) {
                 $this->toDB->dropTable($toTable);
                 $this->toDB->createTable($toTable, $columns);
             } elseif ('skip' == $this->existsAction) {
                 echo "TABLE '{$fromTable}' SKIPPED BECAUSE '{$toTable}' EXISTS!\n";
                 continue;
             } elseif ('stop' == $this->existsAction) {
                 throw new \Exception("TABLE '{$fromTable}' DO STOP BECAUSE '{$toTable}' EXISTS!");
             } elseif ('append' == $this->existsAction && $this->toDB->isTableEqual($toTable, $columns)) {
                 //Do nothing
             }
         } else {
             $this->toDB->createTable($toTable, $columns);
         }
         $cursor = $this->fromDB->execute($select->getSQL());
         while ($row = $cursor->fetch(BasePDO::FETCH_ASSOC)) {
             $row = $this->processFields($row, $columnsMap, array_merge($params, ['sourceColumns' => $useType ? $sourceColumns : false]));
             $sqlBuilder = $this->toDB->getSQLBuilder();
             if (method_exists($sqlBuilder, 'insertOnDuplicateUpdate')) {
                 $insertUpdate = $sqlBuilder->insertOnDuplicateUpdate($toTable, array_combine(array_keys($row), array_map(function ($item) {
                     return ":{$item}";
                 }, array_keys($row))));
             } else {
                 $insertUpdate = $sqlBuilder->insert($toTable, array_combine(array_keys($row), array_map(function ($item) {
                     return ":{$item}";
                 }, array_keys($row))));
             }
             //try {
             if (false === $this->toDB->execute($insertUpdate, $row)) {
                 throw new Exception('Row can not be inserted');
             }
             /*
                             } catch (\Exception $e) {
                                 echo $insertUpdate;
                                 var_dump($row);
                                 throw $e;
                             }//*/
         }
         unset($select);
         //for next table
     }
 }