PHP code example of lanous / db

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/ */

    

lanous / db example snippets


$database = new Database\Connect(new LanousConfig);

$Table = $database->OpenTable (MyLanous\Table\Users::class);

$Where = $Table->Where(MyLanous\Table\Users::ID,"=",1);

$data = $Table->Select(
    column: "*",
    distinct: true
)->Extract();

$data->LastRow($data::ObjectType)->first_name->value;

        $this->AddColumn("ID")
            ->DataType(\MyLanous\DataTypes\Integer::class)
            ->Size(255)
            ->AutoIncrement(true)
            ->Constraints(Primary: true);

        $this->AddColumn("first_name")
            ->DataType(\MyLanous\DataTypes\Varchar::class)
            ->Size(255);

        $this->AddColumn("last_name")
            ->DataType(\MyLanous\DataTypes\Varchar::class)
            ->Size(255);

        $this->AddColumn("password")
            ->DataType(\MyLanous\DataTypes\Varchar::class)
            ->Size(255);

        $this->AddColumn("address")
            ->DataType(\MyLanous\DataTypes\ArrayData::class);
            
        # ---------- Data Handling
        $this->Injection("first_name")
            ->Edit(fn($data) => strtolower($data));
        $this->Injection("last_name")
            ->Edit(fn($data) => strtolower($data));

        # Base64 encode/decode password
        $this->Injection("password")
            ->Edit(fn($data) => base64_encode($data));
        $this->Extract("password")
            ->Edit(fn($data) => base64_decode($data));

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...


class LanousConfig {
    const hostname = '127.0.0.1';
    const username = 'root';
    const password = '';
    const database = 'lanous';
    const dbsm = 'mysql';
    const project_name = 'MyLanous';
    const project_dir = __DIR__;
}

    // ...
    const dbsm = Database\Lanous::DBSM_Mysql; // Choose the appropriate constant
    // ...

$database = new Database\Connect(new LanousConfig);



anous\db as Database;

class LanousConfig {
    const hostname = '127.0.0.1';
    const username = 'root';
    const password = '';
    const database = "lanous";
    const dbsm = Database\Lanous::DBSM_Mysql;
    const project_name = "MyLanous";
    const project_dir = __DIR__;
}

$database = new Database\Connect(new LanousConfig);

namespace {project_name}\Tables;

class {table_name} extends \Lanous\db\Structure\Table {

    // Your table-specific code resides here...

}

AddColumn(string $name)

Injection($column_name)

Extract($column_name)


# project_name = MyLanous
# MyLanous/Tables/Information.php

namespace MyLanous\Tables;

class Information extends \Lanous\db\Structure\Table {
    public function __construct() {
        $this->AddColumn("user_id")
            ->DataType(\MyLanous\DataTypes\Integer::class) # MyLanous/DataTypes/Integer.php
            ->Size(255)
            ->AutoIncrement(true)
            ->Constraints(Primary: true);

        $this->AddColumn("first_name")
            ->DataType(\MyLanous\DataTypes\Varchar::class) # MyLanous/DataTypes/Varchar.php
            ->Size(255)
            ->Constraints(not_null: true);

        $this->AddColumn("last_name")
            ->DataType(\MyLanous\DataTypes\Varchar::class) # MyLanous/DataTypes/Varchar.php
            ->Size(255)
            ->Constraints(not_null: true);

        $this->AddColumn("password")
            ->DataType(\MyLanous\DataTypes\Varchar::class) # MyLanous/DataTypes/Varchar.php
            ->Size(255)
            ->Constraints(not_null: true);

        // Automatic encryption - decryption (base64)
        $this->Injection(self::password)
            ->Edit(fn($data) => base64_encode($data));
        $this->Extract(self::password)
            ->Edit(fn($data) => base64_decode($data));
    }
}

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);
$UsersTable->Insert()
    ->Set("first_name","Mohammad")
    ->Set("last_name","Azad")
    ->Set("password","123456789")
->Push();

$Table = $database->OpenTable ("Users");

// {project_name}/Tables/Users.php
        # ---------- Data Handling
        $this->Injection("first_name")
            ->Edit(fn($data) => strtolower($data));
        $this->Injection("last_name")
            ->Edit(fn($data) => strtolower($data));

        # Base64 encode/decode password
        $this->Injection("password")
            ->Edit(fn($data) => base64_encode($data));
        $this->Extract("password")
            ->Edit(fn($data) => base64_decode($data));

...
    $data->LastRow()["password"];
    // 123456789
...

Order(array $columns, string $direction = null)


$Order = $Table::Order([
    "first_name" => Lanous\db\Lanous::ORDER_ASC,
    "last_name" => Lanous\db\Lanous::ORDER_DESC
]);


$Order = $Table::Order(["first_name", "last_name"], Lanous\db\Lanous::ORDER_ASC);

$Table->Select(
    column: "*",
    distinct: false,
    order_by: $Order,
    limit: 100,
    offset: 0
);

$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: "*");

Extract (Where $where = null, $primary_value = null): false | RowReturn

$Find_ID1 = $Select->Extract(primary_value: 1);

$Where = $Table->Where("first_name", "=", "mohammad")
                            ->AND("last_name", "=", "azad");

$Find_MohammadAzad = $Select->Extract(where: $Where);

$row[0] == "1";
$row[1] == "mohammad";
$row[2] == "azad";

$row->first_name->test($a, $b);

$row[0] == "id";
$row[1] == "first_name";
$row[2] == "last_name";

$row["first_name"] == "mohammad";

$row->first_name->value == "mohammad"
$row->first_name->methods->test('foo', 'bar')
$row->first_name->my_property == "i'm varchar"

$Table->Select(column: "*")->Extract($Where)->Rows;

FirstRow(int $mode=self::ArrayType)

LastRow(int $mode=self::ArrayType)

$UsersTable = $database->OpenTable (MyLanous\Tables\Users::class);
$User = $UsersTable->Select(column: "*")->Extract(primary_value: 1);
$UserValues = $User->LastRow(\Lanous\db\Table\RowReturn::Values);
return $UserValues[1];
// mohammad

$UsersTable = $database->OpenTable (MyLanous\Tables\Users::class);
$User = $UsersTable->Select(column: "*")->Extract(primary_value: 1);
$UserMethods = $User->LastRow(\Lanous\db\Table\RowReturn::Methods);
return $UserMethods->first_name->test('foo','bar');
// Hello mohammad p.a = foo and p.b = bar

$UsersTable = $database->OpenTable (MyLanous\Tables\Users::class);
$User = $UsersTable->Select(column: "*")->Extract(primary_value: 1);
$UserColumns = $User->LastRow(\Lanous\db\Table\RowReturn::Keys);
return $UserColumns[1];
// first_name

$UsersTable = $database->OpenTable (MyLanous\Tables\Users::class);
$User = $UsersTable->Select(column: "*")->Extract(primary_value: 1);
$UserColumns = $User->LastRow(\Lanous\db\Table\RowReturn::ArrayType);
return $UserColumns["first_name"];
// mohammad

$UsersTable = $database->OpenTable (MyLanous\Tables\Users::class);
$User = $UsersTable->Select(column: "*")->Extract(primary_value: 1);
$UserData = $User->LastRow(\Lanous\db\Table\RowReturn::ObjectType);
$UserData->first_name->value; # mohammad
$UserData->first_name->my_property; # i'm varchar
$UserData->first_name->methods->test('foo','bar'); # Hello mohammad p.a = foo and p.b = bar

Update() : Update

Edit(string $column_name, mixed $to) : Update

Push(Where $where = null, $primary_value = null)

$Table = $database->OpenTable (MyLanous\Tables\Users::class);
$Table->Update()
    ->Edit("password", "987654321")
    ->Edit("first_name", "new_name")
    ->Edit("last_name", "new_lastname")
->Push(primary_value: 1);

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';
    ...
}

class LanousConfig {
    ...
    const username = 'incorrect_username';
    const password = 'incorrect_password';
    ...
}

$Table->Insert()
    ->Set("unknown_column","Mohammad")
->Push();

Connect -> function NewJob () : Jobs\Job

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);
$UserID1->Callback(function ($column_name,$value) {
    echo "Column name: $column_name - Column value: $value".PHP_EOL;
});
/*
Column name: id - Column value: 1
Column name: first_name - Column value: mohammad
Column name: last_name - Column value: azad
...
*/

$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_test->FirstRow(RowReturn::ArrayType);
/*
array(4) {
  ["id"]=> int(1)
  ["first_name"]=> string(8) "new name"
  ["last_name"]=> string(4) "azad"
  ["password"]=> string(0) ""
}
*/

$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

RowReturn : function Parent() : RowReturn

$Wallet = $database->OpenTable (MyLanous\Tables\Wallet::class);
$UserID1 = $Wallet->Select(column: "*")->Extract(primary_value: 1);
$Users = $UserID1->Parent()->LastRow();


$Users = $database->OpenTable (MyLanous\Tables\Users::class);
$UserID1 = $Users->Select(column: "*")->Extract(primary_value: 1);
$Wallet = $UserID1->Child(MyLanous\Tables\Wallet::class)->LastRow();

OpenTable(string $table_class) : Table

Call(string $plugin_class, $data=null)

NewJob() : Job


#location: {project_name}/Plugins/Test.php

namespace MyLanous\Plugins;

class Test extends \Lanous\db\Structure\Plugins {
    public function GetName ($id) {
        $Users = $this->OpenTable(\MyLanous\Tables\Users::class);
        $Find = $Users->Where("id","=",$id);
        $User = $Users->Select("*")->Extract($Find)->LastRow();
        return $User['first_name'];
    }
}

function LoadPlugin(string $plugin_class, mixed $data=null) : {plugin_class}


#location: index.php

$UsersPlugin = $database->LoadPlugin(MyLanous\Plugins\Test::class);

echo "Hi ".$UsersPlugin->GetName (1);
# Hi mohammad