public static function setUpBeforeClass() { $VTROOT = getenv('VTROOT'); if (!$VTROOT) { throw new Exception('VTROOT env var not set; make sure to source dev.env'); } // Pick an unused port. $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); socket_bind($sock, 'localhost'); if (!socket_getsockname($sock, $addr, $port)) { throw new Exception('Failed to find unused port for mock vtgate server.'); } socket_close($sock); $cmd = "{$VTROOT}/bin/vtgateclienttest -logtostderr -lameduck-period 0 -port {$port} -service_map bsonrpc-vt-vtgateservice"; $proc = proc_open($cmd, array(), $pipes); if (!$proc) { throw new Exception("Failed to start mock vtgate server with command: {$cmd}"); } self::$proc = $proc; // Wait for connection to be accepted. $ctx = VTContext::getDefault()->withDeadlineAfter(5.0); $client = new BsonRpcClient(); $level = error_reporting(error_reporting() & ~E_WARNING); while (!$ctx->isCancelled()) { try { $client->dial($ctx, "{$addr}:{$port}"); } catch (GoRpcException $e) { usleep(100000); continue; } break; } error_reporting($level); self::$client = $client; // Test fixtures that can't be statically initialized. self::$BIND_VARS = array('bytes' => 'hello', 'int' => 123, 'uint_from_int' => new VTUnsignedInt(345), 'uint_from_string' => new VTUnsignedInt('678'), 'float' => 1.5); self::$CALLER_ID = new VTCallerId('test_principal', 'test_component', 'test_subcomponent'); self::$KEYSPACE_IDS = array(VTKeyspaceID::fromHex('8000000000000000'), VTKeyspaceID::fromHex('ff000000000000ef')); self::$KEY_RANGES = array(array('', VTKeyspaceID::fromHex('8000000000000000')), array(VTKeyspaceID::fromHex('8000000000000000'), '')); self::$ENTITY_KEYSPACE_IDS = array(VTKeyspaceID::fromHex('1234567800000002') => 'hello', VTKeyspaceID::fromHex('1234567800000000') => 123, VTKeyspaceID::fromHex('1234567800000001') => new VTUnsignedInt('456'), VTKeyspaceID::fromHex('1234567800000002') => 1.5); }
<?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);