public function __construct($database, $tableName, $options = null) { if (isset($database) && !is_a($database, "\\r\\Db")) { throw "Database is not a Db object."; } if (!\is_string($tableName)) { throw new RqlDriverError("Table name must be a string."); } if (isset($options)) { if (!is_array($options)) { throw new RqlDriverError("Options must be an array."); } foreach ($options as $key => &$val) { if (!is_string($key)) { throw new RqlDriverError("Option keys must be strings."); } if (!(is_object($val) && is_subclass_of($val, "\\r\\Query"))) { $val = nativeToDatum($val); } unset($val); } } $i = 0; if (isset($database)) { $this->setPositionalArg($i++, $database); } $this->setPositionalArg($i++, new StringDatum($tableName)); if (isset($options)) { foreach ($options as $key => $val) { $this->setOptionalArg($key, $val); } } }
public function __construct(ValuedQuery $sequence, $value) { if (!(is_object($value) && is_subclass_of($value, "\\r\\Query"))) { $value = nativeToDatum($value); } $this->setPositionalArg(0, $sequence); $this->setPositionalArg(1, $value); }
function binary($str) { $encodedStr = base64_encode($str); if ($encodedStr === FALSE) { throw new RqlDriverError("Failed to Base64 encode '" . $str . "'"); } $pseudo = array('$reql_type$' => 'BINARY', 'data' => $encodedStr); return nativeToDatum($pseudo); }
public function __construct($termType, ValuedQuery $value, $other) { if (!(is_object($other) && is_subclass_of($other, "\\r\\Query"))) { $other = nativeToDatum($other); } $this->termType = $termType; $this->setPositionalArg(0, $value); $this->setPositionalArg(1, $other); }
public function __construct(ValuedQuery $sequence, $attribute, ValuedQuery $otherSequence, $opts = null) { $attribute = nativeToDatumOrFunction($attribute); $this->setPositionalArg(0, $sequence); $this->setPositionalArg(1, $attribute); $this->setPositionalArg(2, $otherSequence); if (isset($opts)) { if (!is_array($opts)) { throw new RqlDriverError("opts argument must be an array"); } foreach ($opts as $k => $v) { $this->setOptionalArg($k, nativeToDatum($v)); } } }
public function __construct(Query $test, $trueBranch, $falseBranch) { if (!(is_object($trueBranch) && is_subclass_of($trueBranch, "\\r\\Query"))) { try { $trueBranch = nativeToDatum($trueBranch); } catch (RqlDriverError $e) { $trueBranch = nativeToFunction($trueBranch); } } if (!(is_object($falseBranch) && is_subclass_of($falseBranch, "\\r\\Query"))) { try { $falseBranch = nativeToDatum($falseBranch); } catch (RqlDriverError $e) { $falseBranch = nativeToFunction($falseBranch); } } $this->setPositionalArg(0, $test); $this->setPositionalArg(1, $trueBranch); $this->setPositionalArg(2, $falseBranch); }
public function __construct(ValuedQuery $sequence, $predicate) { if (!(is_object($predicate) && is_subclass_of($predicate, "\\r\\Query"))) { try { $predicate = nativeToDatum($predicate); if (!is_subclass_of($predicate, "\\r\\Datum")) { // $predicate is not a simple datum. Wrap it into a function: $predicate = new RFunction(array(new RVar('_')), $predicate); } } catch (RqlDriverError $e) { $predicate = nativeToFunction($predicate); } } else { if (!(is_object($predicate) && is_subclass_of($predicate, "\\r\\FunctionQuery"))) { $predicate = new RFunction(array(new RVar('_')), $predicate); } } $this->setPositionalArg(0, $sequence); $this->setPositionalArg(1, $predicate); }
public function __construct(ValuedQuery $selection, $delta, $nonAtomic = null) { if (isset($nonAtomic) && !\is_bool($nonAtomic)) { throw new RqlDriverError("nonAtomic must be bool."); } if (!(is_object($delta) && is_subclass_of($delta, "\\r\\Query"))) { // If we can make it an object, we will wrap that object into a function. // Otherwise, we will try to make it a function. try { $delta = nativeToDatum($delta); $delta = new RFunction(array(new RVar('_')), $delta); } catch (RqlDriverError $e) { $delta = nativeToFunction($delta); } } $this->setPositionalArg(0, $selection); $this->setPositionalArg(1, $delta); if (isset($nonAtomic)) { $this->setOptionalArg('non_atomic', new BoolDatum($nonAtomic)); } }
public function __construct(ValuedQuery $sequence, $grouping, $mapping, $reduction, $base = null) { if (!(is_object($grouping) && is_subclass_of($grouping, "\\r\\Query"))) { $grouping = nativeToFunction($grouping); } if (!(is_object($mapping) && is_subclass_of($mapping, "\\r\\Query"))) { $mapping = nativeToFunction($mapping); } if (!(is_object($reduction) && is_subclass_of($reduction, "\\r\\Query"))) { $reduction = nativeToFunction($reduction); } if (isset($base) && !(is_object($base) && is_subclass_of($base, "\\r\\Query"))) { // Convert base automatically $base = nativeToDatum($base); } $this->setPositionalArg(0, $sequence); $this->setPositionalArg(1, $grouping); $this->setPositionalArg(2, $mapping); $this->setPositionalArg(3, $reduction); if (isset($base)) { $this->setOptionalArg('base', $base); } }
public function __construct(Query $tables, $opts = null) { $this->setPositionalArg(0, $tables); if (isset($opts)) { foreach ($opts as $opt => $val) { $this->setOptionalArg($opt, nativeToDatum($val)); } } }
function nativeToDatumOrFunction($f) { if (!(is_object($f) && is_subclass_of($f, "\\r\\Query"))) { try { $f = nativeToDatum($f); if (!is_subclass_of($f, "\\r\\Datum")) { // $f is not a simple datum. Wrap it into a function: $f = new RFunction(array(new RVar('_')), $f); } } catch (RqlDriverError $e) { $f = nativeToFunction($f); } } return wrapImplicitVar($f); }
public function __construct(Table $table, $indexNames = null) { if (isset($indexNames) && !is_array($indexNames)) { $indexNames = array($indexNames); } $this->setPositionalArg(0, $table); if (isset($indexNames)) { $pos = 1; foreach ($indexNames as $v) { $this->setPositionalArg($pos++, nativeToDatum($v)); } } }
public function __construct(ValuedQuery $sequence, $index, $value) { $index = nativeToDatum($index); $value = nativeToDatum($value); $this->setPositionalArg(0, $sequence); $this->setPositionalArg(1, $index); $this->setPositionalArg(2, $value); }
public function __construct(ValuedQuery $sequence, $attributeOrOpts = null) { $this->setPositionalArg(0, $sequence); if (isset($attributeOrOpts)) { if (is_array($attributeOrOpts)) { foreach ($attributeOrOpts as $opt => $val) { $this->setOptionalArg($opt, nativeToDatum($val)); } } else { $attribute = nativeToDatumOrFunction($attributeOrOpts); $this->setPositionalArg(1, $attribute); } } }
public function __construct($p1, $p2) { $this->setPositionalArg(0, nativeToDatum($p1)); $this->setPositionalArg(1, nativeToDatum($p2)); }
public function __construct($left = null, $right = null, $opts = null) { if (isset($left)) { $this->setPositionalArg(0, nativeToDatum($left)); } if (isset($right)) { $this->setPositionalArg(1, nativeToDatum($right)); } if (isset($opts)) { foreach ($opts as $opt => $val) { $this->setOptionalArg($opt, nativeToDatum($val)); } } }
public function __construct(ValuedQuery $sequence, $n) { $n = nativeToDatum($n); $this->setPositionalArg(0, $sequence); $this->setPositionalArg(1, $n); }
public function __construct(ValuedQuery $time, $startTime, $endTime, $opts = null) { $startTime = nativeToDatum($startTime); $endTime = nativeToDatum($endTime); $this->setPositionalArg(0, $time); $this->setPositionalArg(1, $startTime); $this->setPositionalArg(2, $endTime); if (isset($opts)) { if (!is_array($opts)) { throw new RqlDriverError("opts argument must be an array"); } foreach ($opts as $k => $v) { $this->setOptionalArg($k, nativeToDatum($v)); } } }
function expr($obj) { if (is_object($obj) && is_subclass_of($obj, "\\r\\Query")) { return $obj; } return nativeToDatum($obj); }
public function _run(Query $query, $options) { if (isset($options) && !is_array($options)) { throw new RqlDriverError("Options must be an array."); } if (!$this->isOpen()) { throw new RqlDriverError("Not connected."); } // Generate a token for the request $tries = 0; $maxToken = 1 << 30; do { $token = rand(0, $maxToken); $haveCollision = isset($this->activeTokens[$token]); } while ($haveCollision && $tries++ < 1024); if ($haveCollision) { throw new RqlDriverError("Unable to generate a unique token for the query."); } // Send the request $pbTerm = $query->_getPBTerm(); $pbQuery = $this->makeQuery(); $pbQuery->setToken($token); $pbQuery->setType(pb\Query_QueryType::PB_START); $pbQuery->setQuery($pbTerm); if (isset($this->defaultDb)) { $pair = new pb\Query_AssocPair(); $pair->setKey('db'); $pair->setVal($this->defaultDb->_getPBTerm()); $pbQuery->appendGlobalOptargs($pair); } if (isset($options)) { foreach ($options as $key => $value) { $pair = new pb\Query_AssocPair(); $pair->setKey($key); $pair->setVal(nativeToDatum($value)->_getPBTerm()); $pbQuery->appendGlobalOptargs($pair); } } $this->sendProtobuf($pbQuery); if (isset($options) && isset($options['noreply']) && $options['noreply'] === true) { return null; } else { // Await the response $response = $this->receiveResponse($token, $query); if ($response->getType() == pb\Response_ResponseType::PB_SUCCESS_PARTIAL) { $this->activeTokens[$token] = true; } if ($response->getType() == pb\Response_ResponseType::PB_SUCCESS_ATOM) { return $this->createDatumFromResponse($response); } else { return $this->createCursorFromResponse($response); } } }
function nativeToDatum($v) { if (is_array($v) || is_object($v) && in_array(get_class($v), array("stdClass", "ArrayObject"))) { $datumArray = array(); $hasNonNumericKey = false; $mustUseMakeTerm = false; if (is_object($v)) { // Handle "stdClass" objects $hasNonNumericKey = true; // Force conversion into an ObjectDatum $v = (array) $v; } foreach ($v as $key => $val) { if (!is_numeric($key) && !is_string($key)) { throw new RqlDriverError("Key must be a string."); } if (is_object($val) && is_subclass_of($val, "\\r\\Query") && !(is_object($val) && is_subclass_of($val, "\\r\\Datum"))) { $subDatum = $val; $mustUseMakeTerm = true; } else { $subDatum = nativeToDatum($val); if (!is_subclass_of($subDatum, "\\r\\Datum")) { $mustUseMakeTerm = true; } } if (is_string($key)) { $hasNonNumericKey = true; $datumArray[$key] = $subDatum; } else { $datumArray[$key] = $subDatum; } } // Note: In the case of $hasNonNumericKey === false, we cannot // know if we should convert to an array or an object. We // currently assume array, but this is not overly clean. // Of course the user always has the option to wrap data // into a Datum manually. // We use this behavior because it is consistent to json_encode, // which we sometimes use as a transparent replacement for // nativeToDatum(). if ($hasNonNumericKey) { if ($mustUseMakeTerm) { return new MakeObject($datumArray); } else { return new ObjectDatum($datumArray); } } else { if ($mustUseMakeTerm) { return new MakeArray($datumArray); } else { return new ArrayDatum($datumArray); } } } else { if (is_null($v)) { return new NullDatum(); } else { if (is_bool($v)) { return new BoolDatum($v); } else { if (is_int($v) || is_float($v)) { return new NumberDatum($v); } else { if (is_string($v)) { return new StringDatum($v); } else { if (is_object($v) && is_subclass_of($v, "\\r\\Query")) { return $v; } else { if (is_object($v) && (is_subclass_of($v, "DateTimeInterface") || is_a($v, "DateTime"))) { // PHP prior to 5.5.0 doens't have DateTimeInterface, so we test for DateTime directly as well ^^^^^ $iso8601 = $v->format(\DateTime::ISO8601); return new Iso8601($iso8601); } else { throw new RqlDriverError("Unhandled type " . get_class($v)); } } } } } } } }
public function __construct($value) { $this->setPositionalArg(0, nativeToDatum($value)); }
public function __construct(ValuedQuery $selection, $delta, $opts) { if (isset($opts) && !\is_array($opts)) { throw new RqlDriverError("Options must be an array."); } if (!(is_object($delta) && is_subclass_of($delta, "\\r\\Query"))) { // If we can make it an object, we will wrap that object into a function. // Otherwise, we will try to make it a function. try { $json = tryEncodeAsJson($delta); if ($json !== false) { $delta = new Json($json); } else { $delta = nativeToDatum($delta); } } catch (RqlDriverError $e) { $delta = nativeToFunction($delta); } } $delta = wrapImplicitVar($delta); $this->setPositionalArg(0, $selection); $this->setPositionalArg(1, $delta); if (isset($opts)) { foreach ($opts as $opt => $val) { $this->setOptionalArg($opt, nativeToDatum($val)); } } }
public function __construct($dbName) { $dbName = nativeToDatum($dbName); $this->setPositionalArg(0, $dbName); }
public function __construct() { if (func_num_args() > 0) { $value = func_get_arg(0); if (!(is_object($value) && is_subclass_of($value, "\\r\\Query"))) { $value = nativeToDatum($value); } $this->setPositionalArg(0, $value); } }
function nativeToDatum($v) { if (is_array($v)) { $datumArray = array(); $fullyAssociative = true; $mustUseMakeTerm = false; foreach ($v as $key => $val) { if (!is_numeric($key) && !is_string($key)) { throw new RqlDriverError("Key must be a string."); } if (is_object($val) && is_subclass_of($val, "\\r\\Query") && !(is_object($val) && is_subclass_of($val, "\\r\\Datum"))) { $subDatum = $val; $mustUseMakeTerm = true; } else { $subDatum = nativeToDatum($val); if (!is_subclass_of($subDatum, "\\r\\Datum")) { $mustUseMakeTerm = true; } } if (is_string($key)) { $datumArray[$key] = $subDatum; } else { $fullyAssociative = false; $datumArray[] = $subDatum; } } // TODO: In the case of $fullyAssociative === true, we cannot // know if we should convert to an array or an object. We // currently assume object, but this is not overly clean. // Of course the user always has the option to wrap data // into a Datum manually. if ($fullyAssociative) { if ($mustUseMakeTerm) { return new MakeObject($datumArray); } else { return new ObjectDatum($datumArray); } } else { if ($mustUseMakeTerm) { return new MakeArray($datumArray); } else { return new ArrayDatum($datumArray); } } } else { if (is_null($v)) { return new NullDatum(); } else { if (is_bool($v)) { return new BoolDatum($v); } else { if (is_numeric($v)) { return new NumberDatum($v); } else { if (is_string($v)) { return new StringDatum($v); } else { throw new RqlDriverError("Unhandled type " . get_class($v)); } } } } } }
public function _run(Query $query, $options, &$profile) { if (isset($options) && !is_array($options)) { throw new RqlDriverError("Options must be an array."); } if (!$this->isOpen()) { throw new RqlDriverError("Not connected."); } // Grab PHP-RQL specific options $toNativeOptions = array(); foreach (array('binaryFormat', 'timeFormat') as $opt) { if (isset($options) && isset($options[$opt])) { $toNativeOptions[$opt] = $options[$opt]; unset($options[$opt]); } } // Generate a token for the request $token = $this->generateToken(); // Send the request $jsonTerm = $query->_getJSONTerm(); $globalOptargs = array(); if (isset($this->defaultDb)) { $globalOptargs['db'] = $this->defaultDb->_getJSONTerm(); } if (isset($options)) { foreach ($options as $key => $value) { $globalOptargs[$key] = nativeToDatum($value)->_getJSONTerm(); } } $jsonQuery = array(pb\Query_QueryType::PB_START, $jsonTerm, (object) $globalOptargs); $this->sendQuery($token, $jsonQuery); if (isset($options) && isset($options['noreply']) && $options['noreply'] === true) { return null; } else { // Await the response $response = $this->receiveResponse($token, $query); if ($response['t'] == pb\Response_ResponseType::PB_SUCCESS_PARTIAL) { $this->activeTokens[$token] = true; } if (isset($response['p'])) { $profile = decodedJSONToDatum($response['p'])->toNative($toNativeOptions); } if ($response['t'] == pb\Response_ResponseType::PB_SUCCESS_ATOM) { return $this->createDatumFromResponse($response)->toNative($toNativeOptions); } else { return $this->createCursorFromResponse($response, $token, $response['n'], $toNativeOptions); } } }
public function __construct(ValuedQuery $sequence, $predicate, $default = null) { $predicate = nativeToDatumOrFunction($predicate); if (isset($default)) { $default = nativeToDatum($default); } $this->setPositionalArg(0, $sequence); $this->setPositionalArg(1, $predicate); if (isset($default)) { $this->setOptionalArg('default', $default); } }