-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.php
More file actions
170 lines (146 loc) · 7.32 KB
/
build.php
File metadata and controls
170 lines (146 loc) · 7.32 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
/**
* Alters or creates a database table based on the provided parameters.
*
* This function either creates a new table if it doesn't exist or alters an existing table
* by adding, modifying, or renaming columns as specified in the parameters.
*
* @param mysqli $con The mysqli connection object for database operations.
* @param array $params An associative array containing table and column details:
* - 'table_name': (string) The name of the table to create or alter.
* - 'columns': (array) An array of column definitions, each containing:
* - 'column_name': (string) The name of the column.
* - 'datatype': (string) The SQL datatype of the column.
* - 'nullable': (bool) Whether the column can contain NULL values.
* - 'default_value': (mixed, optional) The default value for the column.
* - 'primary_key': (bool, optional) Whether this column is the primary key.
* - 'auto_increment': (bool, optional) Whether this column auto-increments.
* - 'after_this': (string, optional) The column after which to add this column.
* - 'new_name': (string, optional) New name for the column if renaming.
*
* @return string|void Returns "Table created successfully" if a new table is created,
* "Done " if alterations are successful, or void if an error occurs.
* Errors are echoed directly to the output.
*/
function migrate($con, $params)
{
$table_name = mysqli_real_escape_string($con, $params['table_name']);
// SQL query to check if the table exists
$tableCheck = $con->query("SHOW TABLES LIKE '$table_name'");
$isNewTable = ($tableCheck->num_rows == 0);
// Collect primary key column if specified
$primaryKeyColumn = null;
// If table doesn't exist, create it with the specified columns
if ($isNewTable) {
$createTableSQL = "CREATE TABLE `$table_name` (";
$columns = [];
foreach ($params['columns'] as $column) {
$column_name = mysqli_real_escape_string($con, $column['column_name']);
$datatype = mysqli_real_escape_string($con, $column['datatype']);
$nullable = $column['nullable'] ? "NULL" : "NOT NULL";
$default_value = isset($column['default_value']) ? "DEFAULT '" . mysqli_real_escape_string($con, $column['default_value']) . "'" : "";
$autoIncrement = (isset($column['primary_key']) && $column['primary_key'] && isset($column['auto_increment']) && $column['auto_increment']) ? "AUTO_INCREMENT" : "";
// Check if this column is the primary key
if (isset($column['primary_key']) && $column['primary_key']) {
$primaryKeyColumn = $column_name;
}
$columns[] = "`$column_name` $datatype $nullable $default_value $autoIncrement";
}
// Add primary key to the create table statement
if ($primaryKeyColumn) {
$columns[] = "PRIMARY KEY (`$primaryKeyColumn`)";
}
$createTableSQL .= implode(", ", $columns) . ");";
if ($con->query($createTableSQL) === TRUE) {
return "Table created successfully";
} else {
echo "Oops This is on our end School monitor Internal Error: " . $con->error;
return;
}
}
// If the table exists, proceed with alterations
$alterations = [];
foreach ($params['columns'] as $column) {
$column_name = mysqli_real_escape_string($con, $column['column_name']);
$datatype = mysqli_real_escape_string($con, $column['datatype']);
$nullable = $column['nullable'] ? "NULL" : "NOT NULL";
$default_value = isset($column['default_value']) ? "DEFAULT '" . mysqli_real_escape_string($con, $column['default_value']) . "'" : "";
$after_this = isset($column['after_this']) ? mysqli_real_escape_string($con, $column['after_this']) : NULL;
// Check if new_name is provided to rename the column
$newName = isset($column['new_name']) ? mysqli_real_escape_string($con, $column['new_name']) : $column_name;
// Check if the original column or the new name already exists in the table
$columnCheck = $con->query("SHOW COLUMNS FROM `$table_name` LIKE '$column_name'");
$newNameCheck = $con->query("SHOW COLUMNS FROM `$table_name` LIKE '$newName'");
// Preserve AUTO_INCREMENT attribute if the column has it
$autoIncrementCheck = $con->query("SHOW COLUMNS FROM `$table_name` LIKE '$column_name'");
$autoIncrement = ($autoIncrementCheck->num_rows > 0 && strpos($autoIncrementCheck->fetch_assoc()['Extra'], 'auto_increment') !== false) ? "AUTO_INCREMENT" : "";
if ($columnCheck->num_rows == 0 && $newNameCheck->num_rows == 0) {
// Column doesn't exist with either name, add it
$sql = "ADD `$column_name` $datatype $nullable $default_value $autoIncrement";
if ($after_this !== NULL) {
$sql .= " AFTER `$after_this`";
}
} elseif ($columnCheck->num_rows > 0 && $column_name !== $newName && $newNameCheck->num_rows == 0) {
// Column exists with the original name and new_name is not in use, rename it
$sql = "CHANGE `$column_name` `$newName` $datatype $nullable $default_value"; // Rename if new_name is provided
} else {
// Column exists with the new name or no renaming needed, modify it directly
$sql = "MODIFY `$newName` $datatype $nullable $default_value $autoIncrement";
}
// Add the SQL statement to the list of alterations
$alterations[] = $sql;
}
// Combine all column alterations into a single ALTER TABLE statement
$fullSql = "ALTER TABLE `$table_name` " . implode(", ", $alterations) . ";";
// Execute the query
if ($con->query($fullSql) === TRUE) {
return "Done";
} else {
echo "Oops This is on our end School monitor Internal Error: " . $con->error;
}
}
## DEMO USAGE FOR THE migrate() FUNCTION
/*
$params = [
'table_name' => 'employees',
'columns' => [
// Adding a new column
[
'column_name' => 'id',
'new_name' => 'Employee_id',
'datatype' => 'INT',
'nullable' => false,
'primary_key' => true,
'auto_increment' => true
],
[
'column_name' => 'first_name',
'datatype' => 'VARCHAR(100)',
'nullable' => true
],
[
'column_name' => 'last_name',
'datatype' => 'VARCHAR(100)',
'nullable' => true
],
// Renaming an existing column and modifying its properties
[
'column_name' => 'full_name', // old column name
'new_name' => 'name', // new column name
'datatype' => 'VARCHAR(111)',
'nullable' => false
],
// Adding another column with default value
[
'column_name' => 'email',
'datatype' => 'VARCHAR(255)',
'nullable' => true,
'default_value' => 'example@example.com'
]
]
];
// Call the function to alter the table
$result = migrate($con, $params);
// Output the result of the operation
echo $result;
*/