Skip to content

Commit

Permalink
Implement mariadb driver
Browse files Browse the repository at this point in the history
  • Loading branch information
kekefreedog committed Jul 14, 2024
1 parent 6ef86c4 commit e225003
Show file tree
Hide file tree
Showing 14 changed files with 778 additions and 25 deletions.
85 changes: 84 additions & 1 deletion docs/Docker/Mariadb.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,91 @@ docker-compose ps
# Result : crazytest-mariadb-1
```

2. Connect to database as admin
## Explore your MariaDB database

### 1. Connect to database as admin

```sh
docker exec -it crazytest-mariadb-1 mysql -p
```

### 2. Connect to MySql (Mariadb)

Execute the command below :

> You can found the password into the `docker-compose.yml`if you are using docker compose
```sh
mysql -u root -p
```

### 3. Show all databases

Execute the command below :

```sh
SHOW DATABASES;
```

Result :
```sh
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| crazy_db |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
```

### 4. Select database to explore

Execute the command below :

```sh
USE crazy_db;
```

Result :
```sh
Database changed
MariaDB [crazy_db]>
```

### 5. Show all tables

Execute the command below :

```sh
SHOW TABLES;
```

Result :
```sh
MariaDB [crazy_db]> SHOW TABLES;
+--------------------+
| Tables_in_crazy_db |
+--------------------+
| Booking |
+--------------------+
```

### 6. Show all tables

Execute the command below :

```sh
SHOW TABLES;
```

Result :
```sh
MariaDB [crazy_db]> SHOW TABLES;
+--------------------+
| Tables_in_crazy_db |
+--------------------+
| Booking |
+--------------------+
```
1 change: 1 addition & 0 deletions src/Core/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use CrazyPHP\Library\Array\Arrays;
use CrazyPHP\Library\File\Config;
use CrazyPHP\Model\Context;
use PDO;

/**
* Model
Expand Down
28 changes: 28 additions & 0 deletions src/Driver/Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,34 @@ public function setAttributesAsValues(bool $summary = false):self {

}

/** Public methods | Collection / Table
******************************************************
*/

/**
* Create table
*
* @return self
*/
public function createTable():self {

# Return self
return $this;

}

/**
* Create collection
*
* @return self
*/
public function createCollection():self {

# Return self
return $this;

}

/** Public methods
******************************************************
*/
Expand Down
166 changes: 165 additions & 1 deletion src/Driver/Model/Mariadb.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
* Dependances
*/
use CrazyPHP\Library\Database\Driver\Mariadb as MariadbModel;
use CrazyPHP\Library\File\Config as FileConfig;
use CrazyPHP\Interface\CrazyDriverModel;
use CrazyPHP\Library\Model\Schema;
use CrazyPHP\Library\Array\Arrays;

/**
* Crazy Driver Model Interface
Expand All @@ -27,15 +30,24 @@
* @author kekefreedog <[email protected]>
* @copyright 2022-2024 Kévin Zarshenas
*/
class Mariadb extends CrazyDriverModel {
class Mariadb implements CrazyDriverModel {

/** Private parameters
******************************************************
*/

/** @var array $arguments */
private array $arguments;

/** @var Mysql Instance */
public MariadbModel $mariadb;

/** @var Schema $schema */
private Schema|null $schema = null;

/** @var string|null $id for select one item */
private string|null $id = null;

/** @var bool $attributesAsValues Indicate if attributes is set as values in current schema */
# private bool $attributesAsValues = false;

Expand All @@ -52,6 +64,9 @@ public function __construct(...$inputs) {
# Sql connection
$this->newMariadb();

# Create table
$this->createTable();

}

/** Public mathods | Attributes
Expand All @@ -72,6 +87,37 @@ public function setAttributesAsValues():self {

}

/** Public methods | Collection / Table
******************************************************
*/

/**
* Create table
*
* @return self
*/
public function createTable():self {

# Create table
$this->mariadb->createTable($this->arguments["table"], $this->arguments["schema"]);

# Return self
return $this;

}

/**
* Create collection
*
* @return self
*/
public function createCollection():self {

# Return self
return $this;

}

/** Public methods | Parser
******************************************************
*/
Expand Down Expand Up @@ -160,6 +206,15 @@ public function parseSql(string $sql, ?array $options = null):self {
*/
public function ingestData(array $data, ?array $options = null):self {

# New schema
$schema = new Schema($this->arguments["schema"], [$data], [
"flatten" => true,
"skipEmptyValue" => $this->isUpdate()
]);

# Push schema in classe schema
$this->schema = $schema;

# Return self
return $this;

Expand Down Expand Up @@ -200,6 +255,36 @@ public function run():array {
# Set result
$result = [];

# Insert to mongo Check schema
if($this->schema !== null){

# Check collection
$schemaResult = $this->schema->getResult([
"skipAttributes" => ["id"]
]);

# Iteration
foreach($schemaResult as $v){

# Declare data
$data = [];

# Iteration v
foreach($v as $item)

# Push in data
$data[$item["name"]] = $item["value"];

# Unflatten result
$data = Arrays::unflatten($data);

# Insert
$result[] = $this->mariadb->insertToTable($this->arguments["table"], $data);

}

}

# Return result
return $result;

Expand Down Expand Up @@ -271,6 +356,58 @@ public function _pageStateProcess(array $input):array {
*/
private function ingestParameters(array $inputs):void {

# Set arguments
$this->arguments = self::ARGUMENTS;

# Check inputs
if(!empty($inputs))

# Iteration inputs
foreach($inputs as $name => $value)

# Check name in arguments
if(array_key_exists($name, $this->arguments))

# Set value
$this->arguments[$name] = $value;



# Check if collection
if(isset($this->arguments["table"]) && $this->arguments["table"]){

# Get Model Config
$models = FileConfig::getValue("Model");

# Search table
$search = Arrays::filterByKey($models, "name", $this->arguments["table"]);

# Check search
if(!empty($search))

# Get schema
$this->arguments["schema"] = ($search[array_key_first($search)]["attributes"]) ?? [];

}

# Get database
$databases = FileConfig::getValue("Database.collection.mariadb.database");

# If empty database
if(empty($databases))

# New error
throw new CrazyException(
"No mongodb database defined in config.",
500,
[
"custom_code" => "model-mongodb-001",
]
);

# Set database
$this->arguments["database"] = $databases[array_key_first($databases)];

}

/**
Expand All @@ -290,4 +427,31 @@ private function newMariadb():void {

}

/** Private methods | Options
******************************************************
*/

/**
* Is Update
*
* @return bool
*/
private function isUpdate():bool {

return $this->id !== null;

}

/** Public constants
******************************************************
*/

/** @const array */
public const ARGUMENTS = [
"table" => "",
"schema" => [],
"database" => [],
"pageStateProcess" => false,
];

}
Loading

0 comments on commit e225003

Please sign in to comment.