PHP code example of rmtram / simple-text-db
1. Go to this page and download the library: Download rmtram/simple-text-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/ */
rmtram / simple-text-db example snippets
// argument is save storage.
$connector = new Connector(__DIR__ . '/db');
$connector = new Connector(__DIR__ . '/db');
// arg1 = tableName, arg2 = driver(default = ListDriver)
$userManager = $connector->connection('users');
$connector = new Connector(__DIR__ . '/db');
// Exception will occur if it fails.
try {
$connector->drop('users');
} catch (Exception $e) {
// fail process..
}
$connector = new Connector(__DIR__ . '/db');
$userManager = $connector->connection('users');
$userManager->add(['id' => 1, 'name' => 'example']);
$user = $userManager->where(function(Where $where) {
$where->eq('id', 1);
})->first();
var_dump($user);
// array('id' => 1, 'name' => 'example')
var_dump($userManager->all());
// e.g.
// [
// ['id' => 1, 'name' => 'user1'],
// ['id' => 2, 'name' => 'user2']
// ]
$result = $userManager->find(function(Where $where) {
$where->eq('id', 1);
});
$result->get();
// [['id' => 1, 'name' => 'user'1]]
$result->first();
// ['id' => 1, 'name' => 'user1']
$result->count();
// 1
$result->exists();
// true
// (int)$index is list pointer.
// (array)$row => ['id' => 1, 'name' => 'user1']
$result->call(function($index, $row) {
// any process.
});
$userManager->find(function(Where $where) {
// id = 1
$where->eq('id', 1);
// id != 1
$where->notEq('id', 1);
// id > 1
$where->gt('id', 1);
// id >= 1
$where->gte('id', 1);
// id < 1
$where->lt('id', 1);
// id <= 1
$where->lte('id', 1);
// Regex pattern => /^us$/
$where->like('name', 'us');
// Regex pattern => /^us/
$where->like('name', '%us');
// Regex pattern => /us$/
$where->like('name', 'us%')
// Regex pattern => /us/
$where->like('name', '%us%')
// Opposite of LIKE
$where->notLike('name', '%us%')
});
// (id = 10 || name = user1)
$userManager->find(function(Where $where) {
$where->eq('id', 10);
$where->orEq('name', 'user1')
})->first();
// ['id' => 1, 'name' => 'user1']
// (id = 10 && name = user1)
$userManager->find(function(Where $where) {
$where->eq('id', 10);
$where->eq('name', 'user1')
})->first();
// null
$userManager->add(['id' => 3, 'name' => 'user3']);
// true or false
$values = [
['id' => 4, 'name' => 'user4'],
['id' => 5, 'name' => 'user5']
];
$userManager->bulkAdd($values);
// true or false or exception.
$bool = $userManager->update(['name' => 'example', 'age' => 20], function(Where $where) {
$where->eq('id', 1);
});
var_dump($bool) // true || false
// match update data: ['id' => 1, 'name' => 'example', 'age' => 20]
// unmatch update data: ['id' => 2, 'name' => 'user2']
// item all update.
$bool = $userManager->update(['name' => 'demo', 'age' => 25]);
var_dump($bool) // true || false
// match! update data: ['id' => 1, 'name' => 'demo', 'age' => 25]
// match! update data: ['id' => 2, 'name' => 'demo', 'age' => 25]
$deleted = $userManager->delete(function(Where $where) {
$where->eq('id', 1);
});
var_dump($deleted) // true
var_dump($userManager->find(function(Where $where) {
$where->eq('id', 1);
})->exists());
// false
$deleted = $userManager->delete(function(Where $where) {
$where->eq('id', 'ff3i0920jwsss');
});
var_dump($deleted) // false
$deleted = $userManager->delete(function(Where $where) {
});
var_dump($deleted) // false
$userManager->truncate();
var_dump($userManager->all());
// []
$connector = new Connector(__DIR__ . '/db');
/** @var Rmtram\SimpleTextDb\Drivers\HashMapDriver $bookManager */
$bookManager = $connector->connection('books', 'hash');
$bookManager->set('dummy1', 'example');
var_dump($bookManager->get('dummy1'));
// 'example'
$bookManager->set('dummy1', ['name' => 'dummy1', 'age' => 20]);
// update
$bookManager->set('dummy1', ['name' => 'dummy1', 'age' => 25]);
var_dump($bookManager->get('dummy1'));
// ['name' => 'dummy1', 'age' => 25]
$bookManager->set('dummy1', 1);
$bookManager->set('dummy2', 2);
var_dump($bookManager->get('dummy1'));
// 1
var_dump($bookManager->get('none'));
// null
var_dump($bookManager->get(['dummy1', 'dummy2']));
// [1, 2]
$bookManager->set('dummy1', 1);
$bookManager->set('dummy2', 2);
$bookManager->set('dummy3', 3);
var_dump($bookManager->all());
// [1, 2, 3]
$bookManager->set('dummy1', 1);
var_dump($bookManager->has('dummy1'));
// true
var_dump($bookManager->has('none'));
// false
$bookManager->set('dummy1', 1);
$bookManager->set('dummy2', 2);
$bookManager->set('dummy3', 3);
$deleted = $bookManager->delete(['dummy1', 'dummy2']);
var_dump($deleted);
// true
var_dump($bookManager->all());
// [3]
$bookManager->set('dummy1', 1);
$bookManager->set('dummy2', 2);
$bookManager->set('dummy3', 3);
$bookManager->truncate();
var_dump($bookManager->all());
// []
echo Rmtram\SimpleTextDb\Util\Unique::id();
// e.g. microtime() + random string.
// 14615204803576vk9fs8xtiolbah54npy0er7w2m3gq1j