1. Go to this page and download the library: Download lanous/db library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
class ArrayData implements \Lanous\db\Structure\DataType {
const Query = "JSON";
# The data being input and output is passed to the construct.
private $data;
public function __construct($data) {
$this->data = $data;
}
# Before data is entered into the database, it passes through this function.
public function Injection($data) {
return json_encode($data);
}
# After data is extracted from the database, it also passes through this function.
public function Extraction($data) {
return json_decode($data,1);
}
# By using this function, we prevent specific types or expressions from entering the database.
public function Validation($data): bool {
return is_array($data);
}
# You can also add specific functions to these data types.
# Stay tuned for more documentation!
}
// index.php
atabase;
// Your project logic begins here...
// Initialize the Lanous\db package and start interacting with your database.
// Example:
$database = new Database();
// Perform database operations using $database...
namespace MyLanous\DataTypes;
class ArrayData implements \Lanous\db\Structure\DataType {
const Query = "JSON";
private $data;
public function __construct($data) {
$this->data = $data;
}
public function Injection($data) {
return json_encode($data);
}
public function Extraction($data) {
return json_decode($data, true);
}
public function Validation($data): bool {
return is_array($data);
}
}
namespace MyLanous\DataTypes;
class Varchar implements \Lanous\db\Structure\DataType {
const Query = "varchar";
private $data;
public function __construct($data) { $this->data = $data; }
public function Injection($data) { return $data; }
public function Extraction($data) { return $data; }
public function Validation($data): bool { return true; }
public function test($a,$b) : string {
return "Hello ".$this->data." p.a = ".$a." and p.b = ".$b;
}
}
$Table = $database->OpenTable (MyLanous\Tables\Users::class);
$Select = $Table->Select();
// To write the code more cleanly, it is better to write it like this
// $Select = $Table->Select(column: "*");
try {
$Job = $database->NewJob();
$Job->Sensitivity(2);
$User1 = $Job->Get(MyLanous\Table\Users::class,1);
$User2 = $Job->Get(MyLanous\Table\Users::class,2);
$Job->Edit($User1,"amount",50000);
$Job->Edit($User2,"amount",100000);
} catch (\Lanous\db\Exceptions\Jobs $error) {
if ($error->getCode() == $error::ERR_RECOVERY) {
// -- Be sure to specify this case in the catch --
// If the error code is $error::ERR_RECOVERY, it means that the data recovery has encountered an error
// and it is better to check the operation manually.
} elseif ($error->getCode() == $error::ERR_NOCHANGE) {
// No changes were made to one of the rows
} elseif ($error->getCode() == $error::ERR_EXPERROR) {
// An error occurred while applying the changes.
} elseif ($error->getCode() == $error::ERR_CANTFIND) {
// One of the data was not found in the get method.
} elseif ($error->getCode() == $error::ERR_DUPLICTE) {
// When the repeated get method is written, you will encounter this error.
}
}
class LanousConfig {
// const hostname = '127.0.0.1';
...
}
try {
$Job = $database->NewJob();
$Job->Sensitivity(level: 3);
$Wallet1 = $Job->Get(MyLanous\Tables\Wallet::class,1);
$USD1 = $Wallet1->data['usd']->value;
$Wallet2 = $Job->Get(MyLanous\Tables\Wallet::class,2);
$USD2 = $Wallet2->data['usd']->value;
if ($USD2 < 5000)
throw new \Exception("The balance of the first user is insufficient for the transfer",100);
$Job->Edit($Wallet1,"usd",$USD1 + 5000);
$Job->Edit($Wallet2,"usd",$USD2 - 5000);
} catch (\Lanous\db\Exceptions\Jobs $error) {
if ($error->getCode() == $error::ERR_RECOVERY) {
// -- Be sure to specify this case in the catch --
// If the error code is ERR_RECOVERY, it means that the data recovery has encountered an error
// and it is better to check the operation manually.
} elseif ($error->getCode() == $error::ERR_NOCHANGE) {
// No changes were made to one of the rows
} elseif ($error->getCode() == $error::ERR_EXPERROR) {
// An error occurred while applying the changes.
} elseif ($error->getCode() == $error::ERR_CANTFIND) {
// One of the data was not found in the get method.
} elseif ($error->getCode() == $error::ERR_DUPLICTE) {
// When the repeated get method is written, you will encounter this error.
}
} catch (\Exception $e) {
if ($e->getCode() == 100) {
echo ("Your inventory is insufficient.");
}
}
function Sensitivity($level=3)
function Get ($table_class,$primary_value) : object
function Edit (object $row,string $key,$value)
...
} catch (\Lanous\db\Exceptions\Jobs $e) {
if ($error->getCode() == $error::ERR_RECOVERY) {
$recovery_data = $error->data;
$ErrorDetails = $error->getMessage();
$time = time();
file_put_contents($time."_data_losted.json",json_encode($recovery_data,128|256));
print("recovery error!");
// Both users should be disabled in this section.
// The file and $Error should be sent to the site administrator via email.
unlink($time."_data_losted.json"); // After the data is sent to the email, delete the file
// After manually recovering the data or resolving the issue, re-enable the users
}
...
RowReturn : function Callback (callable $callback)
$Users = $database->OpenTable (MyLanous\Tables\Users::class);
$UserID1 = $Users->Select(column: "*")->Extract(primary_value: 1);
$callback_test = $UserID1->Callback(function ($column_name,$value) {
if ($column_name == "first_name"){
return "new name";
}
});
$callback_in_callback = $callback_test->Callback(function ($column_name,$value) {
if ($column_name == "last_name"){
return "new last name";
}
});
$callback_in_callback->FirstRow();
/*
array(4) {
["id"]=> int(1)
["first_name"]=> string(8) "new name"
["last_name"]=> string(4) "new last name"
["password"]=> string(0) ""
}
*/
$callback_test->FirstRow();
/*
array(4) {
["id"]=> int(1)
["first_name"]=> string(8) "new name"
["last_name"]=> string(4) "azad"
["password"]=> string(0) ""
}
*/
$database
->Setting # Open Settings
->Table(MyLanous\Tables\Wallet::class) # Navigate to the Wallet table settings.
->FOREIGN_KEY("user_id",MyLanous\Tables\Users::class,"id");
# Link the user_id column (associated with the wallet table) to the id column of the Users table.
# The Users table is considered the parent in this relationship.
RowReturn : function Child ($table_class) : RowReturn