forked from Ostico/PhpOrient
/
PhpOrient.php
655 lines (577 loc) · 17.5 KB
/
PhpOrient.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
<?php
/**
* Client Class
*
*/
namespace PhpOrient;
use PhpOrient\Protocols\Binary\Data\ID;
use PhpOrient\Protocols\Binary\Data\Record;
use PhpOrient\Protocols\Binary\Operations\RecordCreate;
use PhpOrient\Protocols\Binary\Operations\RecordDelete;
use PhpOrient\Protocols\Binary\Operations\RecordLoad;
use PhpOrient\Protocols\Binary\Operations\RecordUpdate;
use PhpOrient\Protocols\Common\ClustersMap;
use PhpOrient\Protocols\Common\ConfigurableInterface;
use PhpOrient\Protocols\Common\ConfigurableTrait;
use PhpOrient\Protocols\Common\Constants;
use PhpOrient\Protocols\Common\TransportInterface;
use PhpOrient\Protocols\Binary\SocketTransport;
use PhpOrient\Exceptions\TransportException;
/**
* Class PhpOrient
*
* @package PhpOrient
*/
class PhpOrient implements ConfigurableInterface {
use ConfigurableTrait;
const DATABASE_TYPE_DOCUMENT = Constants::DATABASE_TYPE_DOCUMENT;
const DATABASE_TYPE_GRAPH = Constants::DATABASE_TYPE_GRAPH;
const CLUSTER_TYPE_PHYSICAL = Constants::CLUSTER_TYPE_PHYSICAL;
const CLUSTER_TYPE_MEMORY = Constants::CLUSTER_TYPE_MEMORY;
const SERIALIZATION_DOCUMENT2CSV = Constants::SERIALIZATION_DOCUMENT2CSV;
const SERIALIZATION_SERIAL_BIN = Constants::SERIALIZATION_SERIAL_BIN;
const STORAGE_TYPE_LOCAL = Constants::STORAGE_TYPE_LOCAL;
const STORAGE_TYPE_PLOCAL = Constants::STORAGE_TYPE_PLOCAL;
const STORAGE_TYPE_MEMORY = Constants::STORAGE_TYPE_MEMORY;
/**
* The server host.
*
* @var string
*/
public $hostname;
/**
* The port for the server.
*
* @var string
*/
public $port;
/**
* The username for the server.
*
* @var string
*/
public $username;
/**
* The password for the server.
*
* @var string
*/
public $password;
/**
* The transport to use for the connection to the server.
*
* @var TransportInterface
*/
protected $_transport;
/**
* @var bool|string
*/
protected static $fetchClass = null;
/**
* Class Constructor
*
* @param string $hostname The server host.
* @param string $port The server port.
* @param string|bool $token An old connection Token to reuse,
* or a flag to set a new token instance initialization
*/
public function __construct( $hostname = '', $port = '', $token = '' ) {
if ( !empty( $hostname ) ) {
if( $hostname == 'localhost' ) $hostname = '127.0.0.1';
$this->hostname = $hostname;
}
if ( !empty( $port ) ) {
$this->port = $port;
}
$this->setSessionToken( $token );
}
/**
* Set the session token to re-use old
* connection credentials
* or a flag to set a new token instance initialization
*
* @param string|bool $token
*
* @return PhpOrient
*/
public function setSessionToken( $token = '' ){
if ( !empty( $token ) ){
if ( $token !== true ) {
$this->getTransport()->setToken( $token );
}
$this->getTransport()->setRequestToken();
}
return $this;
}
/**
* Get the token for this connection
*
* @return string
*/
public function getSessionToken(){
return $this->getTransport()->getToken();
}
/**
* Sets the transport
*
* @param \PhpOrient\Protocols\Common\TransportInterface $transport
*
* @return $this the current object
*/
public function setTransport( TransportInterface $transport ) {
$this->_transport = $this->createTransport( $transport );
return $this;
}
/**
* Gets the transport
*
* @return \PhpOrient\Protocols\Binary\SocketTransport
*/
public function getTransport() {
if ( $this->_transport === null ) {
$this->_transport = $this->createTransport();
}
return $this->_transport;
}
/**
* Start a new Transaction
*
* @return \PhpOrient\Protocols\Binary\Transaction\TxCommit
*/
public function getTransactionStatement() {
return $this->getTransport()->getTransaction();
}
/**
* Create a transport instance.
*
* @param TransportInterface|null $transport
*
* @return Protocols\Binary\SocketTransport the transport interface
* @throws Exceptions\TransportException
* @throws Exceptions\PhpOrientException
*/
protected function createTransport( $transport = null ) {
if ( !$transport instanceof TransportInterface ) {
if ( is_string( $transport ) ) {
$_transport = new $transport();
if ( !$_transport instanceof TransportInterface ) {
throw New TransportException( $transport . " is not a valid TransportInterface instance" );
}
} else {
//override with default
$_transport = new SocketTransport();
}
$_transport->configure( array(
'hostname' => $this->hostname,
'port' => $this->port,
'username' => $this->username,
'password' => $this->password
) );
} else {
$_transport = $transport;
}
return $_transport;
}
/**
* Execute the given operation.
*
* @param string $operation The name of the operation to prepare.
* @param array $params The parameters for the operation.
*
* @return mixed The result of the operation.
*/
public function execute( $operation, Array $params = array() ) {
$result = $this->getTransport()->execute( $operation, $params );
$this->setFetchClass(false);
return $result;
}
/**
*
* This is the first operation requested by the client when<br />
* it needs to work with the server instance.<br />
* It returns the session id of the client.
*
* @param string $username
* @param string $password
* @param string $serializationType
*
* @return mixed
*/
public function connect(
$username = '',
$password = '',
$serializationType = Constants::SERIALIZATION_DOCUMENT2CSV ) {
if ( !empty( $username ) ) {
$params[ 'username' ] = $username;
}
if ( !empty( $password ) ) {
$params[ 'password' ] = $password;
}
$params[ 'serializationType' ] = $serializationType;
return $this->getTransport()->execute( 'connect', $params );
}
/**
* Send a command to shutdown the server
* Requires "shutdown" permission to be set in orientdb-server-config.xml file
*
* @param string $username
* @param string $password
*/
public function shutDown( $username = '', $password = '' ){
$this->execute( 'shutDown', [ 'username' => $username, 'password' => $password ] );
}
/**
* Execute a not idempotent SQL command
*
* @see \PhpOrient\Protocols\Binary\Operations\Command
*
* @param string $query
*
* @return mixed
*/
public function command( $query ) {
$params = [ ];
$params[ 'command' ] = Constants::QUERY_CMD;
$params[ 'query' ] = $query;
$result = $this->getTransport()->execute( 'command', $params );
$this->setFetchClass(false);
return $result;
}
/**
* Execute an idempotent SQL command ( Select usually )
*
* @see \PhpOrient\Protocols\Binary\Operations\Command
*
* @param string $query
* @param int $limit
* @param string $fetchPlan
*
* @return mixed
*/
public function query( $query, $limit = 20, $fetchPlan = '*:0' ) {
$params = [ ];
$params[ 'command' ] = Constants::QUERY_SYNC;
$params[ 'query' ] = $query;
$params[ 'limit' ] = ( !stripos( $query, ' limit ' ) ? $limit : -1 );
$params[ 'fetch_plan' ] = $fetchPlan;
$result = $this->getTransport()->execute( 'command', $params );
$this->setFetchClass(false);
return $result;
}
/**
* Execute an idempotent SQL command in Async mode( Select usually )<br />
* A callback function is needed
*
* @see \PhpOrient\Protocols\Binary\Operations\Command
*
* @param string $query
* @param mixed $params
*
* @return mixed
*/
public function queryAsync( $query, Array $params = array() ) {
$params[ 'command' ] = Constants::QUERY_ASYNC;
$params[ 'query' ] = $query;
$result = $this->getTransport()->execute( 'command', $params );
$this->setFetchClass(false);
return $result;
}
/**
* Execute an SQL Batch Script command<br />
*
* @see \PhpOrient\Protocols\Binary\Operations\Command
*
* @param string $param
*
* @return mixed
*/
public function sqlBatch( $param ) {
$params = [ ];
$params[ 'command' ] = Constants::QUERY_SCRIPT;
$params[ 'query' ] = $param;
$result = $this->getTransport()->execute( 'command', $params );
$this->setFetchClass(false);
return $result;
}
/**
* Update a Record
*
* @param Record $record
*
* @return RecordUpdate|Record
*/
public function recordUpdate( Record $record ) {
$result = $this->getTransport()->execute( 'recordUpdate',
[
'rid' => $record->getRid(),
'record' => $record,
'record_version' => $record->getVersion()
]
);
$this->setFetchClass(false);
return $result;
}
/**
* 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;
}
/**
* Delete a Record
*
* @param ID $rid
*
* @return RecordDelete|bool
*/
public function recordDelete( ID $rid ) {
$result = $this->getTransport()->execute( 'recordDelete', [
'rid' => $rid
] );
$this->setFetchClass(false);
return $result;
}
/**
* Load a Record
*
* @param ID $rid
* @param array $params
*
* @return RecordLoad/Record
*/
public function recordLoad( ID $rid, Array $params = array() ) {
$params[ 'rid' ] = $rid;
$result = $this->getTransport()->execute( 'recordLoad', $params );
$this->setFetchClass(false);
return $result;
}
/**
* Get the size of a Database
*
* @return int|string
*/
public function dbSize() {
return $this->getTransport()->execute( 'dbSize', [] );
}
/**
* Reload the structure of a Database
*
* @return ClustersMap
*/
public function dbReload() {
return $this->getTransport()->execute( 'dbReload', [] );
}
/**
* Release the structure of a Database
*
* @param string $db_name
* @param string $storage_type
*
* @return true
*/
public function dbRelease( $db_name, $storage_type = Constants::STORAGE_TYPE_PLOCAL ) {
return $this->getTransport()->execute( 'dbRelease', [
'database' => $db_name,
'storage_type' => $storage_type
] );
}
/**
* Open a Database and perform a connection<br />
* if it is not established before
*
* @param string $database
* @param string $username
* @param string $password
* @param array $params {<code>
* 'serializationType' => PhpOrient::SERIALIZATION_DOCUMENT2CSV,
* 'databaseType' => PhpOrient::DATABASE_TYPE_GRAPH
* }</code>
*
* @return ClustersMap
*/
public function dbOpen( $database, $username = '', $password = '', Array $params = [] ) {
$default = [
'databaseType' => Constants::DATABASE_TYPE_GRAPH,
'serializationType' => Constants::SERIALIZATION_DOCUMENT2CSV,
];
$params = array_merge( $default, $params );
return $this->getTransport()->execute( 'dbOpen',
array(
'database' => $database,
'type' => $params['databaseType'],
'username' => $username,
'password' => $password,
'serializationType' => $params['serializationType']
)
);
}
/**
* List all databases inside OrientDB instance
*
* @return array
*/
public function dbList() {
return $this->getTransport()->execute( 'dbList', [] );
}
/**
* Freeze a database ( need Release to unlock )<br />
* Flushes all cached content to the disk storage and allows to perform only read commands
*
* @see http://www.orientechnologies.com/docs/last/orientdb.wiki/Console-Command-Freeze-Db.html
*
* @param string $db_name
* @param string $storage_type
*
* @return bool
*/
public function dbFreeze( $db_name, $storage_type = Constants::STORAGE_TYPE_PLOCAL ) {
return $this->getTransport()->execute( 'dbFreeze', [
'database' => $db_name,
'storage_type' => $storage_type
] );
}
/**
* Check if a database exists
*
* @param $database
* @param string $database_type
*
* @return bool
*/
public function dbExists( $database, $database_type = Constants::DATABASE_TYPE_GRAPH ) {
return $this->getTransport()->execute( 'dbExists',
array(
'database' => $database,
'database_type' => $database_type
)
);
}
/**
* Drop an existent database
*
* @param $database
* @param string $storage_type
*
* @return true
*/
public function dbDrop( $database, $storage_type = Constants::STORAGE_TYPE_PLOCAL ) {
return $this->getTransport()->execute( 'dbDrop',
array(
'database' => $database,
'storage_type' => $storage_type
)
);
}
/**
* Create a new Database
*
* @param string $database
* @param string $storage_type
* @param string $database_type
*
* @return bool
*/
public function dbCreate( $database,
$storage_type = Constants::STORAGE_TYPE_PLOCAL,
$database_type = Constants::DATABASE_TYPE_GRAPH ) {
return $this->getTransport()->execute( 'dbCreate',
array(
'database' => $database,
'database_type' => $database_type,
'storage_type' => $storage_type
)
);
}
/**
* Create a new Database
*
* @return int|string numeric
*/
public function dbCountRecords() {
return $this->getTransport()->execute( 'dbCountRecords', [] );
}
/**
* Close a database a drop the connection
*
* @return int
*/
public function dbClose() {
return $this->getTransport()->execute( 'dbClose', [] );
}
/**
* Drop a data cluster
*
* @param int $cluster_id
*
* @return boolean True if the DataCluster was immediately deleted.
*/
public function dataClusterDrop( $cluster_id ) {
return $this->getTransport()->execute( 'dataClusterDrop', [ 'id' => $cluster_id ] );
}
/**
* Returns the range of record ids for a cluster.
*
* @param int $cluster_id
*
* @return int[]|string[] numeric
*/
public function dataClusterDataRange( $cluster_id ) {
return $this->getTransport()->execute( 'dataClusterDataRange', [ 'id' => $cluster_id ] );
}
/**
* Returns the number of records in one or more clusters.
*
* @param int[]|string[] numeric $cluster_ids
*
* @return int|string numeric
*/
public function dataClusterCount( Array $cluster_ids = array() ) {
return $this->getTransport()->execute( 'dataClusterCount', [ 'ids' => $cluster_ids ] );
}
/**
* Add a new data cluster
*
* @param $cluster_name
* @param string $cluster_type
*
* @return int the new cluster ID
*/
public function dataClusterAdd( $cluster_name, $cluster_type = Constants::CLUSTER_TYPE_PHYSICAL ) {
return $this->getTransport()->execute( 'dataClusterAdd', [
'cluster_name' => $cluster_name,
'cluster_type' => $cluster_type
] );
}
/**
* Allow the next query to be fetched in a custom class
*
* @param bool|string $className
*
* @return $this
*/
public function setFetchClass( $className = false ) {
if ( class_exists( $className ) && is_subclass_of( new $className, 'PhpOrient\Protocols\Binary\Data\Record' ) ) {
self::$fetchClass = $className;
} else {
self::$fetchClass = null;
}
return $this;
}
/**
* Get the current value of the fetch custom class name
*
* @return string|bool
*/
public static function getFetchClass() {
return self::$fetchClass;
}
}