Exemplo n.º 1
0
 /**
  * @param array  $values
  * @param string $key
  * @param int    $default
  * @return int
  */
 public static function int(array $values, $key, $default = 0)
 {
     if (isset($values[$key])) {
         return Type::int($values[$key]);
     }
     return $default;
 }
 /**
  * Composite Cassandra types (list, map, set, tuple, and UDT) to be used by
  * data providers
  */
 public function compositeCassandraTypes()
 {
     $collectionType = Type::collection(Type::varchar());
     $setType = Type::set(Type::varchar());
     $mapType = Type::map(Type::varchar(), Type::int());
     $tupleType = Type::tuple(Type::varchar(), Type::int(), Type::bigint());
     $userType = Type::userType("a", Type::varchar(), "b", Type::int(), "c", Type::bigint());
     $userType = $userType->withName(self::userTypeString($userType));
     return array(array($collectionType, array($collectionType->create("a", "b", "c"), $collectionType->create("d", "e", "f"), $collectionType->create("x", "y", "z"))), array($setType, array($setType->create("a", "b", "c"), $setType->create("d", "e", "f"), $setType->create("x", "y", "z"))), array($mapType, array($mapType->create("a", 1, "b", 2, "c", 3), $mapType->create("d", 4, "e", 5, "f", 6), $mapType->create("x", 7, "y", 8, "z", 9))), array($tupleType, array($tupleType->create("a", 1, new Bigint(2)), $tupleType->create("b", 3, new Bigint(4)), $tupleType->create("c", 5, new Bigint(6)))), array($userType, array($userType->create("a", "x", "b", 1, "c", new Bigint(2)), $userType->create("a", "y", "b", 3, "c", new Bigint(4)), $userType->create("a", "z", "b", 5, "c", new Bigint(6)))));
 }
Exemplo n.º 3
0
 /**
  * Int tests
  */
 public function testInt()
 {
     $this->assertSame(1, Type::int(1));
     $this->assertSame(1, Type::int(1.2));
     $this->assertSame(0, Type::int(0.2));
     $this->assertSame(1, Type::int('1'));
     $this->assertSame(0, Type::int('test1'));
     $this->assertSame(1, Type::int('1test2'));
     $this->assertSame(0, Type::int([]));
     $this->assertSame(1, Type::int(['test']));
     $this->assertTrue(Type::isInt(1));
     $this->assertFalse(Type::isInt('1'));
     $this->assertFalse(Type::isInt(1.2));
     $this->assertFalse(Type::isInt([]));
 }
Exemplo n.º 4
0
 public function notEqualTypes()
 {
     $setType = Type::set(Type::int());
     return array(array(Type::collection(Type::int())->create(), Type::collection(Type::varchar())->create()), array(Type::collection(Type::int())->create(1, 2, 3), Type::collection(Type::int())->create(4, 5, 6)), array(Type::collection(Type::int())->create(1, 2, 3), Type::collection(Type::int())->create(1)), array(Type::collection(Type::varchar())->create('a', 'b', 'c'), Type::collection(Type::varchar())->create('a', 'b', 'd')), array(Type::collection($setType)->create($setType->create(1, 2, 3)), Type::collection($setType)->create($setType->create(4, 5, 6))));
 }
Exemplo n.º 5
0
 /**
  * Bind statement with an null map
  */
 public function testEmpty()
 {
     $mapType = Type::map(Type::int(), Type::int());
     $this->createTableInsertAndVerifyValueByIndex($mapType, null);
     $this->createTableInsertAndVerifyValueByName($mapType, null);
 }
 /**
  * Partial user type
  *
  * This test will ensure that partial user types return the correct value.
  *
  * @test
  * @ticket PHP-58
  */
 public function testPartial()
 {
     $userType = Type::userType("a", Type::int(), "b", Type::varchar(), "c", Type::bigint());
     $userType = $userType->withName(self::userTypeString($userType));
     $this->createUserType($userType);
     $user = $userType->create();
     $user->set("a", 99);
     $this->createTableInsertAndVerifyValueByIndex($userType, $user);
     $user = $userType->create();
     $user->set("b", "abc");
     $this->createTableInsertAndVerifyValueByIndex($userType, $user);
     $user = $userType->create();
     $user->set("c", new Bigint("999999999999"));
     $this->createTableInsertAndVerifyValueByIndex($userType, $user);
 }
Exemplo n.º 7
0
 /**
  * Invalid datatypes for tuples.
  *
  * This test will ensure that an exception will occur when an invalid
  * datatype is used inside a tuple; issues from the server.
  *
  * @test
  * @ticket PHP-58
  * @expectedException \Cassandra\Exception\InvalidQueryException
  */
 public function testInvalidType()
 {
     $validType = Type::tuple(Type::int());
     $invalidType = Type::tuple(Type::varchar());
     $tableName = $this->createTable($validType);
     $options = new ExecutionOptions(array('arguments' => array("key", $invalidType->create("value"))));
     $this->insertValue($tableName, $options);
 }
 /**
  * Assert the `user_defined_aggregate` aggregate
  */
 protected function assertUserDefinedAggregate()
 {
     // Get the UDA from the current keyspace
     $keyspace = $this->session->schema()->keyspace($this->keyspaceName);
     $function = $keyspace->aggregate("user_defined_aggregate", Type::int());
     $expectedArgumentTypes = array("int");
     // Assert the UDA
     $this->assertEquals("user_defined_aggregate", $function->simpleName());
     $argumentTypes = array();
     foreach ($function->argumentTypes() as $argumentType) {
         $argumentTypes[] = "{$argumentType}";
     }
     $this->assertEquals($expectedArgumentTypes, $argumentTypes);
     $this->assertUserDefinedFunctionsEqual("user_defined_function", $function->stateFunction());
     $this->assertUserDefinedFunctionsEqual("uda_udf_final", $function->finalFunction());
     $this->assertEquals(0, $function->initialCondition());
     $this->assertEquals("int", $function->stateType());
     $this->assertEquals("int", $function->returnType());
     $this->assertEquals("user_defined_aggregate(int)", $function->signature());
     // Assert the UDFs
     $this->assertUserDefinedFunction();
     $this->assertAggregateUserDefinedFunction();
 }
Exemplo n.º 9
0
 /**
  * Paging advancement does not create memory leak
  *
  * This test will ensure that the driver does not create memory leaks
  * associated advancing to the next page of results.
  *
  * @test
  * @ticket PHP-101
  */
 public function testNoPagingMemoryLeak()
 {
     // Create the user types and table for the test
     $this->session->execute(new SimpleStatement("DROP TABLE {$this->tableNamePrefix}"));
     $this->session->execute(new SimpleStatement("CREATE TYPE price_history (time timestamp, price float)"));
     $priceHistory = Type::userType("time", Type::timestamp(), "price", Type::float());
     $this->session->execute(new SimpleStatement("CREATE TYPE purchase_stats (day_of_week int, total_purchases int)"));
     $purchaseStats = Type::userType("day_of_week", Type::int(), "total_purchases", Type::int());
     $this->session->execute(new SimpleStatement("CREATE TABLE {$this->tableNamePrefix} (id uuid PRIMARY KEY,\n                history frozen<price_history>, stats frozen<purchase_stats>,\n                comments text)"));
     // Populate the table with some random data
     $totalInserts = 500;
     $statement = $this->session->prepare("INSERT INTO {$this->tableNamePrefix}\n            (id, history, stats, comments) VALUES (?, ?, ?, ?)");
     foreach (range(1, $totalInserts) as $i) {
         // Create the values for the insert
         $history = $priceHistory->create("time", new Timestamp(mt_rand(1270094400000, 1459483200000)), "price", new Float(mt_rand(1, 1000) / 100));
         $stats = $purchaseStats->create("day_of_week", mt_rand(0, 6), "total_purchases", mt_rand(0, 1000));
         $values = array(new Uuid(), $history, $stats, $this->randomString());
         $options = new ExecutionOptions(array("arguments" => $values));
         $this->session->execute($statement, $options);
     }
     // Select all the rows in the table using paging
     $statement = new SimpleStatement("SELECT * FROM {$this->tableNamePrefix}");
     $options = new ExecutionOptions(array("page_size" => 2));
     $rows = $this->session->execute($statement, $options);
     // Validate paging and ensure all the rows were read
     $count = $this->validatePageResults($rows);
     $this->assertEquals($totalInserts, $count);
 }
Exemplo n.º 10
0
 /**
  * Bind statment with an null set
  */
 public function testNull()
 {
     $setType = Type::set(Type::int());
     $this->createTableInsertAndVerifyValueByIndex($setType, null);
     $this->createTableInsertAndVerifyValueByName($setType, null);
 }
Exemplo n.º 11
0
 public function notEqualTypes()
 {
     $setType = Type::set(Type::int());
     return array(array(Type::userType('a', Type::int(), 'b', Type::varchar(), 'c', Type::varint())->create(), Type::userType('a', Type::int(), 'b', Type::varchar(), 'c', Type::bigint())->create()), array(Type::userType('a', Type::int(), 'b', Type::varchar(), 'c', Type::bigint())->create(), Type::userType('x', Type::int(), 'y', Type::varchar(), 'z', Type::bigint())->create()), array(Type::userType('a', Type::int(), 'b', Type::varchar(), 'c', Type::bigint())->create('a', 1, 'b', 'x', 'c', new Bigint(99)), Type::userType('a', Type::int(), 'b', Type::varchar(), 'c', Type::bigint())->create('a', 2, 'b', 'y', 'c', new Bigint(999))), array(Type::userType('a', $setType, 'b', Type::varchar())->create('a', $setType->create(1, 2, 3), 'b', 'x'), Type::userType('a', $setType, 'b', Type::varchar())->create('a', $setType->create(4, 5, 6), 'b', 'x')));
 }
Exemplo n.º 12
0
 public function notEqualTypes()
 {
     $setType = Type::set(Type::int());
     return array(array(Type::tuple(Type::int(), Type::varchar(), Type::varint())->create(), Type::tuple(Type::int(), Type::varchar(), Type::bigint())->create()), array(Type::tuple(Type::int(), Type::varchar(), Type::bigint())->create(1, 'a', new Bigint(99)), Type::tuple(Type::int(), Type::varchar(), Type::bigint())->create(2, 'b', new Bigint(99))), array(Type::tuple($setType, Type::varchar())->create($setType->create(1, 2, 3), 'a'), Type::tuple($setType, Type::varchar())->create($setType->create(4, 5, 6), 'a')));
 }