Inheritance: implements ArrayAccess, implements JsonSerializable, implements PhpOrient\Protocols\Binary\Abstracts\SerializableInterface, use trait PhpOrient\Protocols\Common\ConfigurableTrait
Esempio n. 1
0
 public function testVersionProperty()
 {
     $this->markTestSkipped('Pupù');
     $recOrig = ['name' => 'foo', 'version' => '1.0.0'];
     $rec = new Record();
     $rec->setOData($recOrig);
     $rec->setOClass('Package');
     $rec->setRid(new ID(9));
     $result = $this->client->recordCreate($rec);
     var_export($result);
     $result = $this->client->query('select from V');
     var_export($result);
 }
Esempio n. 2
0
 public function testDeserializeEmbeddedRecord()
 {
     $result = CSV::unserialize('Test@attr1:"test",attr2:(TestInfo@subAttr1:"sub test",subAttr2:123)');
     $payload = [];
     if (isset($result['oClass'])) {
         $payload['oClass'] = $result['oClass'];
         unset($result['oClass']);
     }
     $payload['oData'] = $result;
     $result = Record::fromConfig($payload);
     $testRecord = Record::fromConfig(['oClass' => 'Test', 'oData' => array('attr1' => 'test', 'attr2' => Record::fromConfig(['oClass' => 'TestInfo', 'version' => 0, 'oData' => array('subAttr1' => 'sub test', 'subAttr2' => '123')]))]);
     $this->assertEquals($testRecord, $result);
 }
Esempio n. 3
0
 public function testUpdate()
 {
     $config = static::getConfig();
     $this->client = new PhpOrient();
     $this->client->configure(array('username' => $config['username'], 'password' => $config['password'], 'hostname' => $config['hostname'], 'port' => $config['port']));
     $this->client->dbOpen($this->db_name, 'admin', 'admin');
     $tx = $this->client->getTransactionStatement();
     $this->assertInstanceOf('PhpOrient\\Protocols\\Binary\\Transaction\\TxCommit', $tx);
     $tx = $tx->begin();
     $this->assertInstanceOf('PhpOrient\\Protocols\\Binary\\Transaction\\TxCommit', $tx);
     $recUp = ['accommodation' => 'mountain cabin'];
     $rec2 = new Record();
     $rec2->setOData($recUp);
     $rec2->setOClass('V');
     $rec2->setRid($this->first_rec->getRid());
     $rec2->setVersion($this->first_rec->getVersion());
     $updateCommand = $this->client->recordUpdate($rec2);
     //UPDATE AGAIN!!!
     $recUp = ['accommodation' => 'mountain cabin 2'];
     $rec3 = new Record();
     $rec3->setOData($recUp);
     $rec3->setOClass('V');
     $rec3->setRid($this->first_rec->getRid());
     $rec3->setVersion($this->first_rec->getVersion());
     $updateCommand = $this->client->recordUpdate($rec3);
     $createCommand = $this->client->recordCreate((new Record())->setOData(['accommodation' => 'bungalow'])->setOClass('V')->setRid(new ID(9)));
     //CREATE ANOTHER ONE
     $createCommand2 = $this->client->recordCreate((new Record())->setOData(['accommodation' => 'under the sky'])->setOClass('V')->setRid(new ID(9)));
     $deleteCommand = $this->client->recordDelete($this->sec_rec->getRid());
     $this->assertInstanceOf('PhpOrient\\Protocols\\Binary\\operations\\RecordUpdate', $updateCommand);
     $tx->attach($updateCommand);
     $tx->attach($createCommand);
     $tx->attach($createCommand2);
     $tx->attach($deleteCommand);
     $result = $tx->commit();
     /**
      * @var Record $record
      */
     foreach ($result as $record) {
         if ($record->getRid() == $this->first_rec->getRid()) {
             $this->assertEquals($record->getOData(), ['accommodation' => 'mountain cabin 2']);
             $this->assertEquals($record->getOClass(), $this->first_rec->getOClass());
         } elseif ($record->getRid()->__toString() == '#9:2') {
             $this->assertEquals($record->getOData(), ['accommodation' => 'bungalow']);
             $this->assertEquals($record->getOClass(), 'V');
         } elseif ($record->getRid()->__toString() == '#9:3') {
             $this->assertEquals($record->getOData(), ['accommodation' => 'under the sky']);
             $this->assertEquals($record->getOClass(), 'V');
         }
     }
     //check for deleted record
     $deleted = $this->client->recordLoad($this->sec_rec->getRid());
     $this->assertEmpty($deleted);
 }
 public function testHiLevelCreateDelete()
 {
     $config = static::getConfig();
     $this->client = new PhpOrient();
     $this->client->configure(array('username' => $config['username'], 'password' => $config['password'], 'hostname' => $config['hostname'], 'port' => $config['port']));
     $this->client->dbOpen($this->db_name, 'admin', 'admin');
     $recOrig = ['accommodation' => 'case', 'work' => 'mercato', 'holiday' => 'mare'];
     $rec = new Record();
     $rec->setOData($recOrig);
     $rec->setOClass('V');
     $rec->setRid(new ID(9));
     $result = $this->client->recordCreate($rec);
     $this->assertInstanceOf('\\PhpOrient\\Protocols\\Binary\\Data\\Record', $result);
     $this->assertEquals('#9:0', (string) $result->getRid());
     $this->assertEquals('9', $result->getRid()->cluster);
     $this->assertEquals('0', $result->getRid()->position);
     $load = $this->client->recordLoad($result->getRid());
     $this->assertInstanceOf('\\PhpOrient\\Protocols\\Binary\\Data\\Record', $load[0]);
     $this->assertEquals((string) $result->getRid(), (string) $load[0]->getRid());
     $this->assertEquals((string) $result, (string) $load[0]);
     $_recUp = ['accommodation' => 'hotel', 'work' => 'office', 'holiday' => 'mountain'];
     $recUp = (new Record())->setOData($_recUp)->setOClass('V')->setRid($result->getRid());
     $updated0 = $this->client->recordUpdate($recUp);
     $this->assertInstanceOf('\\PhpOrient\\Protocols\\Binary\\Data\\Record', $updated0);
     $delete = $this->client->recordDelete($load[0]->getRid());
     $this->assertTrue($delete);
     //try load again, this should be empty
     $reLoad = $this->client->recordLoad($result->getRid());
     $this->assertEmpty($reLoad);
     $result = $this->client->dataClusterCount([9]);
     $this->assertEmpty($result);
 }
Esempio n. 5
0
 /**
  * Read sync command payloads
  *
  * @return array|null
  * @throws PhpOrientBadMethodCallException
  * @throws PhpOrientException
  * @throws SocketException
  */
 public function _read_sync()
 {
     # type of response
     # decode body char with flag continue ( Header already read )
     $response_type = $this->_readChar();
     $res = [];
     switch ($response_type) {
         case 'n':
             # get end Line \x00
             $this->_readChar();
             $res = array(null);
             break;
         case $response_type == 'r' || $response_type == 'w':
             $res = [Record::fromConfig($this->_readRecord())];
             # get end Line \x00
             $this->_readChar();
             break;
         case 'a':
             $res = [$this->_readString()];
             # get end Line \x00
             $this->_readChar();
             break;
         case 'l':
             $list_len = $this->_readInt();
             for ($n = 0; $n < $list_len; $n++) {
                 $res[] = Record::fromConfig($this->_readRecord());
             }
             # async-result-type can be:
             # 0: no records remain to be fetched
             # 1: a record is returned as a result set
             # 2: a record is returned as pre-fetched to be loaded in client's
             #       cache only. It's not part of the result set but the client
             #       knows that it's available for later access
             $cached_results = $this->_read_prefetch_record();
             $res = array_merge($res, $cached_results);
             # cache = cached_results['cached']
             break;
         default:
             # debug errors
             if (!Constants::$LOGGING) {
                 throw new PhpOrientException('Unknown payload type ' . $response_type);
             }
             $msg = '';
             $m = $this->_transport->getSocket()->read(1);
             while ($m != '') {
                 $msg .= $m;
                 $this->_transport->hexDump($msg);
                 $m = $this->_transport->getSocket()->read(1);
             }
             break;
     }
     return $res;
 }
Esempio n. 6
0
 /**
  *
  */
 public function testLinkSetCreation()
 {
     $config = static::getConfig('connect');
     $client = new PhpOrient('localhost', 2424);
     $client->configure(array('username' => $config['username'], 'password' => $config['password']));
     $client->connect();
     try {
         $client->dbDrop('temp', Constants::STORAGE_TYPE_MEMORY);
     } catch (\Exception $e) {
         //            echo $e->getMessage();
         $client->getTransport()->debug($e->getMessage());
     }
     $client->dbCreate('temp', Constants::STORAGE_TYPE_MEMORY, Constants::DATABASE_TYPE_DOCUMENT);
     $client->dbOpen('temp');
     $client->sqlBatch('
         create class Test1;
         create property Test1.aString string;
         insert into Test1 (aString) VALUES ("b"),("c"),("d");
         create class Test2;
         create property Test2.aString string;
         create property Test2.anEmbeddedSetOfString embeddedset string;
         create property Test2.aLinkedSetOfTest1 linkset Test1;');
     $clusterTest1 = $client->query("select classes[name='Test1'].defaultClusterId from 0:1", -1)[0]['classes'];
     $clusterTest2 = $client->query("select classes[name='Test2'].defaultClusterId from 0:1", -1)[0]['classes'];
     $this->assertEquals('9', $clusterTest1);
     $this->assertEquals('10', $clusterTest2);
     $newRecord = ['oClass' => 'Test2', 'oData' => ['aString' => 'Test record', 'anEmbeddedSetOfString' => ['something 1', 'something 2', 'more other'], 'aLinkedSetOfTest1' => [new ID($clusterTest1, 1), new ID($clusterTest1, 2)]]];
     $newRecordObject = Record::fromConfig($newRecord);
     $newRecordObject->setRid(new ID($clusterTest2));
     $tmp = $client->recordCreate($newRecordObject);
     $this->assertInstanceOf('\\PhpOrient\\Protocols\\Binary\\Data\\Record', $tmp);
     /**
      * @var \PhpOrient\Protocols\Binary\Data\Record $record
      */
     $record = $client->recordLoad($tmp->getRid())[0];
     $this->assertEquals('Test record', $record->aString);
     $this->assertArrayHasKey(0, $record->anEmbeddedSetOfString);
     $this->assertArrayHasKey(1, $record->anEmbeddedSetOfString);
     $this->assertArrayHasKey(2, $record->anEmbeddedSetOfString);
     $this->assertEquals('something 1', $record->anEmbeddedSetOfString[0]);
     $this->assertEquals('something 2', $record->anEmbeddedSetOfString[1]);
     $this->assertEquals('more other', $record->anEmbeddedSetOfString[2]);
     $this->assertArrayHasKey(0, $record->aLinkedSetOfTest1);
     $this->assertArrayHasKey(1, $record->aLinkedSetOfTest1);
     $this->assertInstanceOf('\\PhpOrient\\Protocols\\Binary\\Data\\ID', $record->aLinkedSetOfTest1[0]);
     $this->assertInstanceOf('\\PhpOrient\\Protocols\\Binary\\Data\\ID', $record->aLinkedSetOfTest1[1]);
     $aLinkedSetOfTest1 = $record->aLinkedSetOfTest1;
     /**
      * @var \PhpOrient\Protocols\Binary\Data\ID[] $aLinkedSetOfTest1
      */
     $this->assertEquals('#9:1', $aLinkedSetOfTest1[0]->jsonSerialize());
     $this->assertEquals('#9:2', $aLinkedSetOfTest1[1]->__toString());
 }
Esempio n. 7
0
 /**
  * Consume an embedded record.
  *
  * @param string $input The input to unserialize.
  *
  * @return array The collected record and any remaining content.
  */
 protected static function eatRecord($input)
 {
     $record = [];
     $input = ltrim($input, ' ');
     if ($input[0] === ')') {
         // this is an empty record.
         return [$record, substr($input, 1)];
     }
     $chunk = self::eatFirstKey($input);
     if ($chunk[2]) {
         // this is actually a class name.
         $record['oClass'] = $chunk[0];
         $input = ltrim($chunk[1], ' ');
         if ($input[0] === ')') {
             return [$record, substr($input, 1)];
         }
         $chunk = self::eatKey($input);
         $key = $chunk[0];
         $input = $chunk[1];
     } else {
         $key = $chunk[0];
         $input = $chunk[1];
     }
     $chunk = self::eatValue($input);
     $value = $chunk[0];
     $input = ltrim($chunk[1], ' ');
     $record[$key] = $value;
     while (strlen($input) > 0) {
         if ($input[0] === ',') {
             $input = ltrim(substr($input, 1), ' ');
         } elseif ($input[0] === ')') {
             $input = ltrim(substr($input, 1), ' ');
             break;
         }
         $chunk = self::eatKey($input);
         $key = $chunk[0];
         $input = ltrim($chunk[1], ' ');
         if (strlen($input) > 0) {
             $chunk = self::eatValue($input);
             $value = $chunk[0];
             $input = $chunk[1];
             $record[$key] = $value;
         } else {
             $record[$key] = null;
         }
     }
     $payload = [];
     if (isset($record['oClass'])) {
         $payload['oClass'] = $record['oClass'];
         unset($record['oClass']);
     }
     if (isset($record['@type'])) {
         $payload['type'] = $record['@type'];
         unset($record['@type']);
     }
     $payload['oData'] = $record;
     $record = \PhpOrient\Protocols\Binary\Data\Record::fromConfig($payload);
     return [$record, $input];
 }
Esempio n. 8
0
 public function testNullValueHandling()
 {
     try {
         $client = new PhpOrient('localhost', 2424);
         $client->username = '******';
         $client->password = '******';
         $client->connect();
         $this->skipTestByOrientDBVersion(['2.1.3', '2.0.13', '1.7.10']);
         try {
             $client->dbDrop('temp', Constants::STORAGE_TYPE_MEMORY, Constants::DATABASE_TYPE_DOCUMENT);
         } catch (\Exception $e) {
             //            echo $e->getMessage();
             $client->getTransport()->debug($e->getMessage());
         }
         $client->dbCreate('temp', Constants::STORAGE_TYPE_MEMORY, Constants::DATABASE_TYPE_GRAPH);
         $client->dbOpen('temp');
         $client->sqlBatch('create class Prova1;' . 'create property Prova1.aString string;' . 'insert into Prova1 (aString) VALUES ("b"),("c"),("d");' . 'create class Prova2;' . 'create property Prova2.aString string;' . 'create property Prova2.anEmbeddedSetOfString embeddedset string;' . 'create property Prova2.prova1 link Prova1;');
         $clusterProva1 = $client->query("select classes[name='Prova1'].defaultClusterId from 0:1", -1)[0]['classes'];
         $clusterProva2 = $client->query("select classes[name='Prova2'].defaultClusterId from 0:1", -1)[0]['classes'];
         //            echo "Default cluster for Prova1: $clusterProva1\n";
         //            echo "Default cluster for Prova2: $clusterProva2\n\n";
         $newRecord = ['oClass' => 'Prova2', 'oData' => ['aString' => 'record di prova', 'anEmbeddedSetOfString' => ['qualcosa 1', 'qualcosa 2', 'ancora altro'], 'prova1' => null]];
         $newRecordObject = Record::fromConfig($newRecord);
         $newRecordObject->setRid(new ID($clusterProva2));
         $tmp = $client->recordCreate($newRecordObject);
         $record = $client->recordLoad($tmp->getRid())[0];
         $this->assertEquals('record di prova', $record->getOData()['aString']);
         $this->assertEquals(null, $record->getOData()['prova1']);
         //            print_r($record->getOData());
     } catch (\Exception $e) {
         //            echo $e . "\n";
     }
 }
Esempio n. 9
0
 /**
  * Create a record
  *
  * @param Record $record
  *
  * @return RecordCreate|Record
  */
 public function recordCreate(Record $record)
 {
     $result = $this->getTransport()->execute('recordCreate', ['cluster_id' => $record->getRid()->cluster, 'record' => $record]);
     $this->setFetchClass(false);
     return $result;
 }
Esempio n. 10
0
 /**
  * @param $record
  * @return \stdClass
  * @throws \Exception
  */
 protected function populateRecord(Record $record)
 {
     $classMap = $this->em->classMap;
     $key = $record->getOClass();
     if (array_key_exists($key, $classMap)) {
         $entity = new $classMap[$key]();
         /* @var EntityInterface $entity */
         $entity->setRid($record->getRid());
         $oData = $record->getOData();
         if (!empty($oData) && is_array($oData)) {
             foreach ($oData as $key => $value) {
                 $entity->{$key} = $value;
             }
         }
         return $entity;
     }
     throw new Exception('Key not found');
 }
Esempio n. 11
0
 /**
  * Hydrate a SpiderRecord from an OrientRecord
  *
  * @param $orientRecord
  * @return Response
  */
 protected function mapOrientRecordToCollection(OrientRecord $orientRecord)
 {
     // Or we map a single record to a Spider Record
     $collection = new Collection($orientRecord->getOData());
     $collection->add(['id' => $orientRecord->getRid()->jsonSerialize(), 'label' => $orientRecord->getOClass(), 'meta.rid' => $orientRecord->getRid(), 'meta.version' => $orientRecord->getVersion(), 'meta.oClass' => $orientRecord->getOClass()]);
     $collection->protect('id');
     $collection->protect('label');
     $collection->protect('meta');
     return $collection;
 }
Esempio n. 12
0
 /**
  * Read the response from the socket.
  *
  * @return int The session id.
  */
 protected function _read()
 {
     $payloads = [];
     $status = $this->_readByte();
     if ($status != 0) {
         $payload = [];
         // a normal record
         if ($this->_transport->getProtocolVersion() > 27) {
             $type = $this->_readChar();
             $version = $this->_readInt();
             if ($type == 'b') {
                 $data[] = $this->_readString();
             } else {
                 $data = CSV::unserialize($this->_readString());
             }
         } else {
             $string = $this->_readString();
             $data = CSV::unserialize($string);
             $version = $this->_readInt();
             $type = $this->_readChar();
             if ($type == 'b') {
                 $data = $string;
             }
         }
         $payload['rid'] = new ID($this->cluster_id, $this->cluster_position);
         $payload['type'] = $type;
         $payload['version'] = $version;
         if (isset($data['oClass'])) {
             $payload['oClass'] = $data['oClass'];
             unset($data['oClass']);
         }
         $payload['oData'] = $data;
         $record = Record::fromConfig($payload);
         $payloads[] = $record;
         $prefetched = $this->_read_prefetch_record();
         # read cache and prefetch with callback
         $payloads = array_merge($payloads, $prefetched);
     }
     return $payloads;
 }
use PhpOrient\PhpOrient;
use PhpOrient\Protocols\Binary\Data\ID;
use PhpOrient\Protocols\Binary\Data\Record;
require 'vendor/autoload.php';
$client = new PhpOrient('orientdb3', 2424);
$client->connect('root', 'rootpwd');
if ($client->dbExists('demo')) {
    $client->dbDrop('demo');
}
$client->dbCreate('demo');
$client->dbOpen('demo');
// Create a new collection
$clusterId = $client->command('create class Elements extends V');
// Add new documents
$document1 = new Record();
$document1->setOData(['now' => date('Y-m-d H:i:s')]);
$document1->setRid(new ID($clusterId));
$document2 = new Record();
$document2->setOData(["created" => ['during' => ['VilniusPHP', 'event']]]);
$document2->setRid(new ID($clusterId));
$record1 = $client->recordCreate($document1);
$record2 = $client->recordCreate($document2);
// Read them all
$documents = $client->query('select from Elements');
foreach ($documents as $document) {
    /** @var $document Record */
    echo '<h2>' . $document->getRid() . '</h2>';
    echo "<div><b>Class:</b> {$document->getOClass()}</b>";
    echo "<div><b>Revision:</b> {$document->getVersion()}</b>";
    echo '<pre>' . json_encode($document->getOData(), JSON_PRETTY_PRINT) . '</pre>';
}
 protected static function isEdge(Record &$record)
 {
     $data = $record->getOData();
     if (isset($data['in']) && isset($data['out'])) {
         return true;
     }
     return false;
 }
Esempio n. 15
0
 /**
  * Create a record
  *
  * @param Record $record
  *
  * @return RecordCreate|Record
  */
 public function recordCreate(Record $record)
 {
     return $this->getTransport()->execute('recordCreate', ['cluster_id' => $record->getRid()->cluster, 'record' => $record]);
 }