-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6ef86c4
commit e225003
Showing
14 changed files
with
778 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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; | ||
|
||
|
@@ -52,6 +64,9 @@ public function __construct(...$inputs) { | |
# Sql connection | ||
$this->newMariadb(); | ||
|
||
# Create table | ||
$this->createTable(); | ||
|
||
} | ||
|
||
/** Public mathods | Attributes | ||
|
@@ -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 | ||
****************************************************** | ||
*/ | ||
|
@@ -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; | ||
|
||
|
@@ -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; | ||
|
||
|
@@ -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)]; | ||
|
||
} | ||
|
||
/** | ||
|
@@ -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, | ||
]; | ||
|
||
} |
Oops, something went wrong.