Skip to content
This repository was archived by the owner on Oct 20, 2025. It is now read-only.

Commit 01d1ca9

Browse files
authored
Merge pull request #44 from PhpSlides/dev
Dev
2 parents f786a14 + d53c37c commit 01d1ca9

File tree

4 files changed

+66
-45
lines changed

4 files changed

+66
-45
lines changed

src/Database/Connection.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Connection
3232
*
3333
* @return void
3434
*/
35-
static function connect()
35+
static function connect ()
3636
{
3737
// Set connection parameters from environment variables
3838
static::$port = getenv('DB_PORT') ?: 3306;
@@ -44,11 +44,11 @@ static function connect()
4444

4545
// Construct DSN (Data Source Name) for the database connection
4646
DB::$dsn = sprintf(
47-
'%s:host=%s;port=%s;dbname=%s',
48-
static::$db_type,
49-
static::$host,
50-
static::$port,
51-
static::$db_name
47+
'%s:host=%s;port=%s;dbname=%s',
48+
static::$db_type,
49+
static::$host,
50+
static::$port,
51+
static::$db_name
5252
);
5353

5454
// Set the user and password for the database connection
@@ -65,18 +65,18 @@ static function connect()
6565
*
6666
* @return void
6767
*/
68-
static function reconnect()
68+
static function reconnect ()
6969
{
7070
// Disconnect the current database connection
7171
DB::disconnect();
7272

7373
// Recreate the DSN and reconnect with the new parameters
7474
DB::$dsn = sprintf(
75-
'%s:host=%s;port=%s;dbname=%s',
76-
static::$db_type,
77-
static::$host,
78-
static::$port,
79-
static::$db_name
75+
'%s:host=%s;port=%s;dbname=%s',
76+
static::$db_type,
77+
static::$host,
78+
static::$port,
79+
static::$db_name
8080
);
8181
}
8282

@@ -89,10 +89,10 @@ static function reconnect()
8989
*
9090
* @return void
9191
*/
92-
static function init()
92+
static function init ()
9393
{
9494
DB::$host = static::$host ?? getenv('DB_HOST') ?: 3306;
9595
DB::$user = static::$user ?? getenv('DB_USER') ?: 'root';
9696
DB::$password = static::$password ?? getenv('DB_PASS') ?: '';
9797
}
98-
}
98+
}

src/Database/Database.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ abstract class Database extends MeekroORM
2929
* table. It ensures that the class is configured for ORM operations,
3030
* including setting up the table name.
3131
*/
32-
public function __construct()
32+
public function __construct ()
3333
{
3434
static::static();
3535
}

src/Forgery/Database.php

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ abstract class Database extends DB_ORM
2626
*
2727
* @param string $db_name The name of the database to be created.
2828
*/
29-
protected static function createDB($db_name)
29+
protected static function createDB ($db_name)
3030
{
31-
try {
31+
try
32+
{
3233
// Initialize database connection
3334
Connection::init();
3435

@@ -39,11 +40,14 @@ protected static function createDB($db_name)
3940
);
4041

4142
// If the database does not exist, create it
42-
if (empty($query)) {
43-
DB::query("CREATE DATABASE $db_name");
43+
if (empty($query))
44+
{
45+
DB::query("CREATE DATABASE %b", $db_name);
4446
static::log('INFO', "Created Database `$db_name`");
4547
}
46-
} catch (\Exception $e) {
48+
}
49+
catch ( \Exception $e )
50+
{
4751
// Log any exceptions that occur during the database creation
4852
static::log(
4953
'ERROR',
@@ -60,9 +64,10 @@ protected static function createDB($db_name)
6064
*
6165
* @param string $db_name The name of the database to be dropped.
6266
*/
63-
protected static function dropDB($db_name)
67+
protected static function dropDB ($db_name)
6468
{
65-
try {
69+
try
70+
{
6671
// Initialize database connection
6772
Connection::init();
6873

@@ -73,7 +78,8 @@ protected static function dropDB($db_name)
7378
);
7479

7580
// If the database does not exist, log a warning
76-
if (empty($query)) {
81+
if (empty($query))
82+
{
7783
static::log(
7884
'WARNING',
7985
"Cannot drop unexisting database `$db_name`"
@@ -82,9 +88,11 @@ protected static function dropDB($db_name)
8288
}
8389

8490
// Drop the database
85-
DB::query("DROP DATABASE $db_name");
91+
DB::query("DROP DATABASE %b", $db_name);
8692
static::log('INFO', "Dropped Database `$db_name`.");
87-
} catch (\Exception $e) {
93+
}
94+
catch ( \Exception $e )
95+
{
8896
// Log any exceptions that occur during the database drop
8997
static::log(
9098
'ERROR',
@@ -102,9 +110,10 @@ protected static function dropDB($db_name)
102110
* @param string $db_name The name of the database containing the table.
103111
* @param string $db_table The name of the table to be dropped.
104112
*/
105-
protected static function dropTable($db_name, $db_table)
113+
protected static function dropTable ($db_name, $db_table)
106114
{
107-
try {
115+
try
116+
{
108117
// Initialize database connection
109118
Connection::init();
110119

@@ -116,7 +125,8 @@ protected static function dropTable($db_name, $db_table)
116125
);
117126

118127
// If the table does not exist, log a warning
119-
if (empty($query)) {
128+
if (empty($query))
129+
{
120130
static::log(
121131
'WARNING',
122132
"Cannot drop unexisting table `$db_table` in `$db_name` Database"
@@ -125,12 +135,14 @@ protected static function dropTable($db_name, $db_table)
125135
}
126136

127137
// Drop the table
128-
DB::query("DROP TABLE $db_table");
138+
DB::query("DROP TABLE %b.%b", $db_name, $db_table);
129139
static::log(
130140
'INFO',
131141
"Dropped Table `$db_table` in `$db_name` Database."
132142
);
133-
} catch (\Exception $e) {
143+
}
144+
catch ( \Exception $e )
145+
{
134146
// Log any exceptions that occur during the table drop
135147
static::log(
136148
'ERROR',

src/Forgery/Forge.php

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -198,33 +198,42 @@ protected static function table ($db_name)
198198

199199
if ($constraint['PRIMARY'])
200200
{
201-
$key = implode(', ', $constraint['PRIMARY']);
201+
$key = implode(', ', (array) $constraint['PRIMARY']);
202202
$query[] = $table_already_exists
203203
? "ADD PRIMARY KEY ($key)"
204204
: "PRIMARY KEY ($key)";
205205
}
206206

207-
if ($constraint['UNIQUE'])
207+
if ($constraint['INDEX'])
208208
{
209-
$key = implode(', ', $constraint['UNIQUE']);
210-
$query[] = $table_already_exists
211-
? "ADD UNIQUE ($key)"
212-
: "UNIQUE ($key)";
213-
}
209+
$key = implode(', ', (array) $constraint['INDEX']);
214210

215-
if ($constraint['INDEX'])
211+
if ($constraint['UNIQUE'])
212+
{
213+
$query[] = $table_already_exists
214+
? "ADD UNIQUE INDEX ($key)"
215+
: "UNIQUE INDEX ($key)";
216+
}
217+
else
218+
{
219+
$query[] = $table_already_exists
220+
? "ADD INDEX ($key)"
221+
: "INDEX ($key)";
222+
}
223+
}
224+
elseif ($constraint['UNIQUE'])
216225
{
217-
$key = implode(', ', $constraint['INDEX']);
226+
$key = implode(', ', (array) $constraint['UNIQUE']);
218227
$query[] = $table_already_exists
219-
? "ADD INDEX ($key)"
220-
: "INDEX ($key)";
228+
? "ADD UNIQUE ($key)"
229+
: "UNIQUE ($key)";
221230
}
222231

223232
if ($constraint['OTHERS'])
224233
{
225234
$key = $table_already_exists
226-
? 'ADD ' . implode(', ', $constraint['OTHERS'])
227-
: implode(', ', $constraint['OTHERS']);
235+
? 'ADD ' . implode(', ', (array) $constraint['OTHERS'])
236+
: implode(', ', (array) $constraint['OTHERS']);
228237
$query[] = (string) $key;
229238
}
230239

@@ -265,15 +274,15 @@ protected static function table ($db_name)
265274
*/
266275
if ($table_already_exists)
267276
{
268-
DB::query("ALTER TABLE $table_name $query");
277+
DB::query("ALTER TABLE %b %l", $table_name, $query);
269278
static::log(
270279
'INFO',
271280
"Altered Table `$table_name` and adds column `$only_columns`",
272281
);
273282
}
274283
else
275284
{
276-
DB::query("CREATE TABLE $table_name ($query)");
285+
DB::query("CREATE TABLE %b (%l)", $table_name, $query);
277286
static::log(
278287
'INFO',
279288
"Created Table `$table_name` in `$db_name` Database",

0 commit comments

Comments
 (0)