shen2/fluent-cql

an php CQL building library

dev-master 2015-07-19 07:44 UTC

This package is auto-updated.

Last update: 2024-12-23 20:33:48 UTC


README

Dependency

Initialize

$connection = new Cassandra\Connection(['127.0.0.1'], 'my_keyspace');

FluentCQL\Query

  • SELECT COMMAND
$query = FluentCQL\Query::select('count(*)')
    ->from('table_name')
    ->where('id = ?', 123);

$query->assemble(); // SELECT count(*) FROM table_name where id = 123
$query->setDbAdapter($connection)->querySync(); // execute query syncronously
  • INSERT COMMAND
$query = FluentCQL\Query::insertInto('table_name')
    ->clause('(from_id, to_id, updated_uuid)')
    ->values('(?, ?, ?)', 321, 123, new \Cassandra\Type\Timeuuid('2dc65ebe-300b-11e4-a23b-ab416c39d509'));

$query->assemble(); // INSERT INTO table_name (from_id, to_id, updated_uuid) values (321, 123, 2dc65ebe-300b-11e4-a23b-ab416c39d509);
$query->setDbAdapter($connection)->querySync(); // execute query syncronously
  • UPDATE COMMAND
$query = FluentCQL\Query::update('table_name')
    ->set('a = :a, b = :b', $a, $b)
    ->where('c = :c', $c)
    ->and('d = :d', $d)
    ->ifExists();

$query->assemble(); // 'UPDATE table_name SET a = :a, b = :b WHERE c = :c AND d = :d'
$query->setDbAdapter($connection)->querySync(); // execute query syncronously
  • DELETE COMMAND
$query = FluentCQL\Query::delete()
    ->from('table_name')
    ->where('id = ?', 123);

$query->assemble(); // DELETE FROM table_name where id = 123
$query->setDbAdapter($connection)->querySync(); // execute query syncronously

FluentCQL\Table

Table Class Definition

class Friendship extends \FluentCQL\Table{

    protected static $_name = 'friendship';

    protected static $_primary = array('from_id', 'to_id');

    protected static $_columns = array(
            'from_id'      => Type\Base::BIGINT,
            'to_id'        => Type\Base::BIGINT,
            'updated_uuid' => Type\Base::TIMEUUID,
    );

    protected static $_readConsistency = 0x0001;

    protected static $_writeConsistency = 0x0002;
}

Initialize

$connection = new Cassandra\Connection(['127.0.0.1'], 'my_keyspace');
FluentCQL\Table::setDefaultDbAdapter($connection);

Queries

  • Table::find()
$response = Friendship::find(321, 123);     // synchronously query and get binary response 
$response->fetchAll(); // get all rows in a SplFixedArray
$response->fetchRow(); // get first row in a ArrayObject from response
  • Table::select()
// this way totally equal to Friendship::find()
$response = Friendship::select()
    ->where('from_id = ? AND to_id = ?', 321, 123)
    ->querySync();
  • Table::insert()
$response = Friendship::insert()
    ->clause('(from_id, to_id, updated_uuid)')
    ->values('(?, ?, ?)', 321, 123, new \Cassandra\Type\Timeuuid('2dc65ebe-300b-11e4-a23b-ab416c39d509'))
    ->querySync();
  • Table::update()
$response = Friendship::update()
    ->set('update_uuid = ?', new \Cassandra\Type\Timeuuid('2dc65ebe-300b-11e4-a23b-ab416c39d509'))
    ->where('from_id = ? AND to_id = ?', 321, 123)
    ->querySync();
  • Table::delete()
$response = Friendship::delete()
    ->where('from_id = ? AND to_id = ?', 321, 123)
    ->if('updated_uuid = ?', new \Cassandra\Type\Timeuuid('2dc65ebe-300b-11e4-a23b-ab416c39d509'))
    ->querySync();
  • Table::insertRow($data)
// Insert a row through an array.
$response = Friendship::insertRow([
    'from_id' => 123,
    'to_id'   => 321,
    'updated_uuid'=> '2dc65ebe-300b-11e4-a23b-ab416c39d509',
])->querySync();
  • Table::updateRow($primary, $data)
// Update a row by primary key.
$response = Friendship::updateRow([123, 321], ['updated_uuid'=> '2dc65ebe-300b-11e4-a23b-ab416c39d509'])->querySync();
  • Table::deleteRow($primary)
// Delete a row or rows by primary key.
$response = Friendship::deleteRow([123])->querySync();

Custom Consistency and Options

$query = new FluentCQL\Query();
$query->setConsistency(0x0001)
    ->setOptions(['page_size' => 20]);

ActiveRecord-like Usage

$post = new Friendship(); 
$post['from_id']      = 123;
$post['to_id']        = 321;
$post['updated_uuid'] = '2dc65ebe-300b-11e4-a23b-ab416c39d509';
$post->save();