forked from intel/php_pgo_training_scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.php
145 lines (132 loc) · 4.96 KB
/
init.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
/**************************************************************************
* Pgo Train Benchmark
*
* Copyright (c) 2016, Intel Corporation.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
***************************************************************************/
require_once('constants.php');
function printExistingDB() {
echo "\n\t\t-=- Existing Databases -=-\n";
$conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect);
}
$command = "SHOW DATABASES";
$result = $conn->query($command);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo $row["Database"] . "\n";
}
} else {
echo "0 available databases\n";
}
$conn->close();
}
function insert_random_stuff($conn) {
$yes_no = array("\"yes\"", "\"no\"");
$names = array("\"WinnieThePooh\"" , "\"Mickey Mouse\"", "\"something\"", "\"random_name\"", "\"YetAnotherName\"");
$values = array("\"valoare 1\"", "\"valoare 2\"", "\"limitless value\"", "\"minus valoare\"", "\"something\"");
// for table 1
for ($i=0; $i < 100; $i++) {
$rand_1 = rand(0, $i);
$rand_2 = rand(0, $i);
$rand_3 = rand(0, $i);
$command = "INSERT INTO table_one (col2, col3, col4) VALUES (" . $names[($i+$rand_1)%5] . "," . $values[($i+$rand_2)%5] . "," . $yes_no[($i+$rand_3)%2] .")";
if ($conn->query($command) == FALSE) {
die("Error populating tables");
}
}
for ($i=0; $i < 15; $i++) {
$rand_1 = rand(0, $i);
$rand_2 = rand(0, $i);
$rand_3 = rand(0, $i);
$command = "INSERT INTO table_two (col2, col3) VALUES (" . $names[($i+$rand_1)%5] . "," . $values[($i+$rand_2)%5] . ")";
if ($conn->query($command) == FALSE) {
die("Error populating tables");
}
}
for ($i=0; $i < 1000; $i++) {
$rand_1 = rand(0, $i);
$rand_2 = rand(0, $i);
$rand_3 = rand(0, $i);
$command = "INSERT INTO table_three (col2, col3) VALUES (" . $names[($i+$rand_1)%5] . "," . $values[($i+$rand_2)%5] . ")";
if ($conn->query($command) == FALSE) {
die("Error populating tables");
}
}
}
function initDB() {
echo "\n\t\t-=- init Database(s) -=-\n";
// Connect to mysql
$conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect);
}
if ($conn->select_db(DB_NAME)) {
$command = "DROP DATABASE " . DB_NAME ;
if ($conn->query($command) == TRUE) {
echo "Database " . DB_NAME . " succesfully dropped\n";
} else {
echo "Error dropping database " . DB_NAME . ": " . $conn->error . "\n";
}
}
// Create database
$command = "CREATE DATABASE " . DB_NAME;
if ($conn->query($command) == TRUE) {
echo "Database " . DB_NAME . " created succesfully\n";
} else {
echo "Error creating database: " . DB_NAME . $conn->error . "\n";
}
$command = "USE " . DB_NAME;
$conn->query($command);
/* drop tables if exitst */
$conn->query("DROP TABLE table_one");
$conn->query("DROP TABLE table_two");
$conn->query("DROP TABLE table_three");
/* create required tables */
$command = "
CREATE TABLE IF NOT EXISTS table_one (
col1 bigint(5) NOT NULL AUTO_INCREMENT,
col2 varchar(50) DEFAULT NULL,
install_date DATE DEFAULT NULL,
col3 varchar(64) DEFAULT NULL,
col4 varchar(250) DEFAULT NULL,
PRIMARY KEY(col1)
)
";
$conn->query($command);
$command = "
CREATE TABLE IF NOT EXISTS table_two (
col1 bigint(5) NOT NULL AUTO_INCREMENT,
col2 varchar(50) DEFAULT NULL,
col3 varchar(64) DEFAULT NULL,
PRIMARY KEY(col1)
)";
$conn->query($command);
$command = "
CREATE TABLE IF NOT EXISTS table_three (
col1 bigint(5) NOT NULL AUTO_INCREMENT,
col2 varchar(50) DEFAULT NULL,
col3 varchar(64) DEFAULT NULL,
PRIMARY KEY(col1)
)";
if ($conn->query($command) == TRUE) {
echo "Created tables in " . DB_NAME . "\n";
} else {
echo "Error creating tables in : " . DB_NAME . $conn->error . "\n";
}
insert_random_stuff($conn);
$conn->close();
}
initDB();
printExistingDB();
?>