· 9 years ago · Jan 27, 2017, 02:22 PM
1<?php
2$params = [
3 'c:' => 'count:',
4 't:' => 'table:',
5];
6
7$options = getopt(join('', array_keys($params)), $params);
8if (!isset($options['table'])) {
9 die("Use --table={table_name} to define table name");
10}
11
12if (!isset($options['count'])) {
13 die("Use --count={count} to define rows count to insert");
14}
15
16$tableName = $options['table'];
17$rowsCount = (int)$options['count'];
18if ($rowsCount < 1) {
19 die("Rows count should be positive int!");
20}
21
22print "DROP TABLE IF EXISTS {$tableName};\n";
23print "CREATE TABLE {$tableName} (
24 id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
25 val VARCHAR(10) NOT NULL,
26 INDEX val (val)
27);\n";
28
29
30$rowValues = [];
31for ($k = 0; $k < $rowsCount; $k++) {
32 $value = rand(1, $rowsCount);
33 $rowValues[] = "({$value})";
34}
35
36$sql = "INSERT INTO {$tableName} (val) VALUES " . join(", ", $rowValues) . ";\n";
37print $sql;