This repository has been archived by the owner on Dec 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Agent
Kerem Güneş edited this page Jan 28, 2017
·
8 revisions
Agent is your worker on database (aka adapter or something like that).
Work with agent
// get agent
$agent = $db->getLink()->getAgent();
// do work and get worker Result object
// that can be iterated after each "select" command
$result = $agent->query('select * from `users`');
foreach ($result as $user) {
print $user->name;
}
// or
$agent->query('select * from `users`');
$result = $agent->getResult();
...
var_dump($result);
var_dump($result->getData());
CRUD operations with agent
// result is iterable
$result = $agent->query('select * from `users`');
if ($result->count()) foreach ($result ...)
// or
if ($agent->rowsCount()) foreach ($agent->getResult() ...)
// result has LAST_INSERT_ID
$result = $agent->query('insert into `users`(name,old) values(?,?)', ['Bob', 30]);
print $result->getId();
// or
print $agent->id();
// result has AFFECTED_ROWS info
$result = $agent->query('update `users` set old = ?', [35]);
print $result->getRowsAffected();
// or
print $agent->rowsAffected();
Shortcut methods
// returns object or array by your fetch type configuration
$agent->get('select * from `users` where `id` = ?', [1]);
// returns array
$agent->getAll('select * from `users` where `id` in(?,?,?)', [1,2,3]);
// returns integer value as last insert id or null on fail
$agent->insert('users', ['name' => 'Bob', 'old' => 35]);
// returns array that contains last insert ids
$agent->insert('users', [['name' => 'Bob', 'old' => 35], ['name' => 'Rick', 'old' => 45]]);
// returns integer value that indicates affected rows
$agent->update('users', ['name' => 'Veli', 'old' => 60], 'id = ?', [1]);
// returns integer value that indicates affected rows
$agent->delete('users', 'id = ?', [1]);
Escape methods
// auto-detect id is integer
$agent->escape('id = ?', [1]); // id = 1
// says id is string and cast as string
$agent->escape('id = ?', ['1']); // id = '1'
$agent->escape('id = %s', ['1']); // id = '1'
$agent->escape('id = :id', ['id' => '1']); // id = '1'
$agent->escape('id = ?', ['1"']); // id = '1\"'
// casts id as int or float
$agent->escape('id = %i', [1]); // id = 1
$agent->escape('id = %f', [1]); // id = 1.000000
// all in one, if coder is some crazy but non-named params must be in order
// out: sid = 'sid value', pid = 'pid value', a = 'aaa', tid = 'tid value', b = 999, c = 900.123000, word = word_value, string = 'string_value'
$agent->prepare('sid = :sid, pid = :pid, a = ?, tid = :tid, b = %i, c = %f, word = %w, string = %s', [
'pid' => 'pid value',
'sid' => 'sid value',
'aaa',
999,
'tid' => 'tid value',
900.123,
'word_value',
'string_value'
]);
// escape string
$agent->escapeString("Hel'lo"); // mysql: 'Hel\'lo', pgsql: 'Hel''lo'
$agent->escapeString("Hel'lo", false); // // mysql: Hel\'lo, pgsql: Hel''lo
// escape an identifier, result is `id`
$agent->escapeIdentifier('id');
$agent->escapeIdentifier('`id`');
// pgsql methods
$agent->escapeBytea(...);
$agent->unescapeBytea(...);