Skip to content

Commit

Permalink
Update Mariadb & Mysql drivers
Browse files Browse the repository at this point in the history
  • Loading branch information
kekefreedog committed Nov 24, 2024
1 parent f52c6e7 commit cba3822
Show file tree
Hide file tree
Showing 3 changed files with 355 additions and 35 deletions.
8 changes: 8 additions & 0 deletions docs/Docker/Mariadb.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,12 @@ DROP TABLE {table};
Result :
```sh
Query OK, 0 rows affected
```

### 5. Show all tables

Get schema of one table

```sql
SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='Booking';
```
93 changes: 84 additions & 9 deletions src/Library/Database/Driver/Mariadb.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,24 @@ public function newClient(string|int $user = 0):self {

# Get connection string
$connectionArray = self::getConnectionArray($connection);

try {

# Set client
$this->client = new PDO(...$connectionArray);

# Set client
$this->client = new PDO(...$connectionArray);
} catch (PDOException $e) {

# New Exception
throw new CrazyException(
$e->getMessage(),
500,
[
"custom_code" => "mariadb-010",
]
);

}

# Set manager
$this->manager = new Query($this->client);
Expand Down Expand Up @@ -396,6 +411,9 @@ public function createTable($tableName = "", $attributes = [], $replaceTable = f
# Set primary
$primary = "";

# Set reference
$references = [];

# Iteration attributes
foreach($attributes as $attribute){

Expand Down Expand Up @@ -426,6 +444,12 @@ public function createTable($tableName = "", $attributes = [], $replaceTable = f
# Set required
$required = 'NOT NULL';

# Set default
$default = isset($attribute['default'])
? "DEFAULT '{".$attribute['default']."}'"
: ''
;

}else
# If varchar
if($columnTypeTemp == "VARCHAR"){
Expand All @@ -439,19 +463,37 @@ public function createTable($tableName = "", $attributes = [], $replaceTable = f
: 'NULL'
;

# Set default
$default = isset($attribute['default'])
? "DEFAULT '{".$attribute['default']."}'"
: ''
;

}else
# If int
if($columnTypeTemp == "INT"){

# Set column type
$columnType = "INT(11)";

# Check if reference
if(isset($attribute['reference']) && $attribute['reference'])

# Update column type
$columnType = "int(6) UNSIGNED";

# Set required
$required = isset($attribute['required']) && $attribute['required']
? 'NOT NULL'
: 'NULL'
;

# Set default
$default = isset($attribute['default'])
? "DEFAULT '{".$attribute['default']."}'"
: ''
;

}else
# If int
if($columnTypeTemp == "BOOL" || $columnTypeTemp == "BOOLEAN"){
Expand All @@ -465,19 +507,52 @@ public function createTable($tableName = "", $attributes = [], $replaceTable = f
: 'NULL'
;

# Set default
$default = isset($attribute['default'])
? "DEFAULT '{".$attribute['default']."}'"
: ''
;

}else
# If date
if($columnTypeTemp == "DATE" || $columnTypeTemp == "DATETIME"){

# Set column type (DATE)
$columnType = "TIMESTAMP";

# Set required

# Set required
$required = isset($attribute['required']) && $attribute['required']
? 'NOT NULL'
: 'NULL'
;

# Set default
$default = isset($attribute['default'])
? (
$attribute['default'] == "today()"
? "DEFAULT current_timestamp"
: "DEFAULT '{".$attribute['default']."}'"
)
: ''
;

}

# Clean column type
$columnType = strtolower($columnType);

# Set default
$default = isset($attribute['default'])
? "DEFAULT '{".$attribute['default']."}'"
: ''
;

# Push result into columns
$columns[] = trim("`$columnName` $columnType $required $default").($isId ? " AUTO_INCREMENT" : "");

# Check if reference
if(isset($attribute['reference']) && $attribute['reference']){

# Set column type
$references[] = trim("FOREIGN KEY (`$columnName`) REFERENCES `".$attribute['reference']."` (`id`) ON DELETE CASCADE ON UPDATE CASCADE");

}

}

Expand All @@ -488,7 +563,7 @@ public function createTable($tableName = "", $attributes = [], $replaceTable = f
return $result;

# Fill query
$query .= "(". implode(', ', $columns) . ", $primary)";
$query .= "(". implode(', ', $columns) . ", $primary" . (!empty($references) ? ", ".implode(', ', $references) : "" ) . ')';

# Execute the SQL statement to create the table
try {
Expand Down
Loading

0 comments on commit cba3822

Please sign in to comment.