PHP code example of sw-serv / relational-db

1. Go to this page and download the library: Download sw-serv/relational-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/ */

    

sw-serv / relational-db example snippets


use \Small\SwooleDb\Registry\TableRegistry;

$testTable = TableRegistry::getInstance()->createTable('testTable', 128);

use \Small\SwooleDb\Registry\TableRegistry;
use \Small\SwooleDb\Core\Column;
use \Small\SwooleDb\Core\Enum\ColumnType;

TableRegistry::getInstance()->getTable('testTable')
    ->addColumn(new Column('firstname', ColumnType::string, 256))
    ->addColumn(new Column('credit', ColumnType::float))
    ->addColumn(new Column('rank', ColumnType::int, 8))

use \Small\SwooleDb\Registry\TableRegistry;

$success = TableRegistry::getInstance()->getTable('testTable')->create();

use \Small\SwooleDb\Registry\TableRegistry;

$table = TableRegistry::getInstance()->getTable('testTable')
$table->set(0, ['franck', 12.5, 11]);
$table->set(1, ['joe', 55.2, 26]);

use Small\SwooleDb\Registry\TableRegistry;

$table = TableRegistry::getInstance()->createTable('testSelectJoin', 5);
$table->addColumn(new Column('name', ColumnType::string, 255));
$table->addColumn(new Column('price', ColumnType::float));
$table->create();
$table->set(0, ['name' => 'john', 'price' => 12.5]);
$table->set(1, ['name' => 'paul', 'price' => 34.9]);

$table2 = TableRegistry::getInstance()->createTable('testSelectJoinPost', 5);
$table2->addColumn(new Column('message', ColumnType::string, 255));
$table2->addColumn(new Column('ownerId', ColumnType::int, 16));
$table2->create();
$table2->set(0, ['message' => 'ceci est un test', 'ownerId' => 0]);
$table2->set(1, ['message' => 'ceci est un autre test', 'ownerId' => 1]);
$table2->set(2, ['message' => 'ceci est une suite de test', 'ownerId' => 1]);

$table2->addForeignKey('messageOwner', 'testSelectJoin', 'ownerId');

use \Small\SwooleDb\Registry\TableRegistry;

$table = TableRegistry::getInstance()->destroy('testTable');

use \Small\SwooleDb\Registry\TableRegistry;

TableRegistry::getInstance()->persist('testTable');

use \Small\SwooleDb\Registry\TableRegistry;

TableRegistry::getInstance()->loadFromChannel('testTable');

use Small\SwooleDb\Registry\ParamRegistry;
use Small\SwooleDb\Registry\Enum\ParamType;

ParamRegistry::getInstance()->set(ParamType::varLibDir, '/home/some-user');

use Small\SwooleDb\Registry\ParamRegistry;
use Small\SwooleDb\Registry\Enum\ParamType;

ParamRegistry::getInstance()->set(ParamType::dataDirName, 'persistence');

use Small\SwooleDb\Selector\TableSelector;

$selector = new TableSelector('testSelect');
$records = $selector->execute();

foreach ($records as $record) {
    echo $record['testSelect']->getValue('name');
}

use Small\SwooleDb\Selector\TableSelector;
use Small\SwooleDb\Selector\Enum\ConditionElementType;
use Small\SwooleDb\Selector\Enum\ConditionOperator;

$selector = new TableSelector('testSelect');
$selector->where()
    ->firstCondition(new Condition(
        new ConditionElement(ConditionElementType::var, 'price', 'testSelect'),
        ConditionOperator::superior,
        new ConditionElement(ConditionElementType::const, 15)
    ));
$records = $selector->execute();

foreach ($records as $record) {
    echo $record['testSelect']->getValue('name');
}

use Small\SwooleDb\Selector\TableSelector;

$result = (new TableSelector('user'))
    ->join('post', 'messageOwner', 'message')
    ->execute()

foreach ($result as $record) {
    echo $record['message']->getValue('body') . ' : by ' . $record['user']->getValue('name');
}

use Small\SwooleDb\Selector\TableSelector;
use Small\SwooleDb\Selector\Enum\ConditionElementType;
use Small\SwooleDb\Selector\Enum\ConditionOperator;

$selector = (new TableSelector('user'))
    ->join('post', 'messageOwner', 'message')
;

$selector->where()
    ->firstCondition(new Condition(
        new ConditionElement(ConditionElementType::var, 'name', 'user'),
        ConditionOperator::equal,
        new ConditionElement(ConditionElementType::const, 'john')
    ))->andCondition(new Condition(
        new ConditionElement(ConditionElementType::var, 'subject', 'message'),
        ConditionOperator::like,
        new ConditionElement(ConditionElementType::const, '%hiring%')
    ))
;

$result = $selector->execute();

foreach ($result as $record) {
    echo $record['message']->getValue('body') . ' : by ' . $record['user']->getValue('name');
    
    $record['message']
        ->setValue('read', 1)
        ->persist()
    ;
}