Beispiel #1
0
 *
 * Before running this, start up a local example cluster as described in the
 * README.md file.
 *
 * Then run:
 * vitess/examples/local$ php client.php --server=localhost:15991
 */
require_once __DIR__ . '/../../php/vendor/autoload.php';
$opts = getopt('', array('server:'));
$keyspace = 'test_keyspace';
// An unsharded keyspace is the same as custom sharding (0, 1, 2, ...),
// but with only a single shard (0).
$shards = array('0');
// Create a connection.
$ctx = VTContext::getDefault();
$conn = new VTGateConn(new VTGrpcClient($opts['server']));
// Insert something.
echo "Inserting into master...\n";
$tx = $conn->begin($ctx);
$tx->executeShards($ctx, 'INSERT INTO test_table (msg) VALUES (:msg)', $keyspace, $shards, array('msg' => 'V is for speed'));
$tx->commit($ctx);
// Read it back from the master.
echo "Reading from master...\n";
$cursor = $conn->executeShards($ctx, 'SELECT * FROM test_table', $keyspace, $shards, array(), \topodata\TabletType::MASTER);
while (($row = $cursor->next()) !== FALSE) {
    printf("(%s)\n", implode(', ', $row));
}
// Read from a replica.
// Note that this may be behind master due to replication lag.
echo "Reading from replica...\n";
$cursor = $conn->executeShards($ctx, 'SELECT * FROM test_table', $keyspace, $shards, array(), \topodata\TabletType::REPLICA);
Beispiel #2
0
<?php

/*
 * This is a sample for using the PHP Vitess client.
 *
 * Before running this, start up a local demo cluster by running:
 * vitess$ examples/demo/run.py
 *
 * Then in another terminal:
 * vitess/php$ php demo.php --server=localhost:12346
 */
require_once './src/VTGateConn.php';
require_once './src/VTGrpcClient.php';
$opts = getopt('', array('server:'));
// Create a connection.
$ctx = VTContext::getDefault();
$conn = new VTGateConn(new VTGrpcClient($opts['server']));
// Insert something.
$tx = $conn->begin($ctx);
$tx->execute($ctx, 'INSERT INTO user (name) VALUES (:name)', array('name' => 'sugu'));
$tx->commit($ctx);
// Read it back.
$cursor = $conn->execute($ctx, 'SELECT * FROM user', array(), \topodata\TabletType::MASTER);
while (($row = $cursor->next()) !== FALSE) {
    print_r($row);
}
$conn->close();
Beispiel #3
0
 public static function tearDownAfterClass()
 {
     if (self::$client) {
         try {
             $ctx = Context::getDefault()->withDeadlineAfter(5.0);
             $conn = new VTGateConn(self::$client);
             $conn->execute($ctx, 'quit://', array(), 0);
             self::$client->close();
         } catch (Exception $e) {
         }
     }
     if (self::$proc) {
         proc_terminate(self::$proc, 9);
         proc_close(self::$proc);
     }
 }
Beispiel #4
0
<?php

/*
 * This is a sample for using the PHP Vitess client.
 *
 * Before running this, start up a local demo cluster by running:
 * vitess$ test/demo.py
 *
 * The demo.py script will print the vtgate port, which you pass in like this:
 * vitess/php$ php demo.php --server localhost:port
 */
require_once './src/VTGateConn.php';
require_once './src/BsonRpcClient.php';
$opts = getopt('', array('server:'));
// Create a connection.
$ctx = VTContext::getDefault();
$client = new BsonRpcClient();
$client->dial($ctx, $opts['server']);
$conn = new VTGateConn($client);
// Insert something.
$tx = $conn->begin($ctx);
$tx->execute($ctx, 'INSERT INTO user (name) VALUES (:name)', array('name' => 'sugu'));
$tx->commit($ctx);
// Read it back.
$result = $conn->execute($ctx, 'SELECT * FROM user', array(), VTTabletType::MASTER);
print_r($result->rows);