/**
  * destructor - rollbacks the active transaction if php script ends prematurely
  * and commit is forgotten to be called
  *
  * also the connection is being closed here
  */
 public function __destruct()
 {
     if ($this->isInTransaction()) {
         $this->rollbackTransaction();
     }
     $this->connection->close();
 }
Example #2
0
<?php

/*
 * This is a sample for using the PHP Vitess client with VTGateV3.
 *
 * Before running this, start up a local V3 demo cluster by running:
 * vitess/examples/demo$ ./run.py
 *
 * Then in another terminal:
 * vitess/examples/demo$ php client.php --server=localhost:12346
 */
require_once __DIR__ . '/../../php/vendor/autoload.php';
use Vitess\Context;
use Vitess\VTGateConn;
use Vitess\Proto\Topodata\TabletType;
$opts = getopt('', array('server:'));
// Create a connection.
$ctx = Context::getDefault();
$conn = new VTGateConn(new \Vitess\Grpc\Client($opts['server'], ['credentials' => Grpc\ChannelCredentials::createInsecure()]));
// 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(), TabletType::MASTER);
while (($row = $cursor->next()) !== FALSE) {
    print_r($row);
}
$conn->close();
Example #3
0
 *
 * Then run:
 * vitess/examples/local$ php client.php --server=localhost:15991
 */
require_once __DIR__ . '/../../php/vendor/autoload.php';
use Vitess\Context;
use Vitess\VTGateConn;
use Vitess\Proto\Topodata\TabletType;
$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 = Context::getDefault();
$conn = new VTGateConn(new \Vitess\Grpc\Client($opts['server'], ['credentials' => Grpc\ChannelCredentials::createInsecure()]));
// 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(), 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(), TabletType::REPLICA);
Example #4
0
 *
 * You will also need to install the gRPC PHP extension as described in
 * vitess/php/README.md, and download dependencies with:
 * vitess$ composer install
 *
 * Then run:
 * vitess/examples/local$ php client.php --server=localhost:15991
 */
require_once __DIR__ . '/../../php/vendor/autoload.php';
use Vitess\Context;
use Vitess\VTGateConn;
use Vitess\Proto\Topodata\TabletType;
$opts = getopt('', array('server:'));
// Create a connection.
$ctx = Context::getDefault();
$conn = new VTGateConn(new \Vitess\Grpc\Client($opts['server'], ['credentials' => Grpc\ChannelCredentials::createInsecure()]));
// Insert some messages on random pages.
echo "Inserting into master...\n";
for ($i = 0; $i < 3; $i++) {
    $page = rand(1, 100);
    $time_created = sprintf('%.0f', microtime(true) * 1000000000);
    $tx = $conn->begin($ctx);
    $tx->execute($ctx, 'INSERT INTO messages (page,time_created_ns,message) VALUES (:page,:time_created_ns,:message)', array('page' => $page, 'time_created_ns' => $time_created, 'message' => 'V is for speed'));
    $tx->commit($ctx);
}
// Read it back from the master.
echo "Reading from master...\n";
$cursor = $conn->execute($ctx, 'SELECT page, time_created_ns, message FROM messages', array(), TabletType::MASTER);
while (($row = $cursor->next()) !== FALSE) {
    printf("(%d, %d, %s)\n", $row[0], $row[1], $row[2]);
}