forked from AntonTerekhov/OrientDB-PHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.php
133 lines (116 loc) · 3.73 KB
/
example.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
<?php
/**
* This file can be used as a starting point to understand way OrientDB-PHP
* works.
* @author Anton Terekhov <[email protected]>
* @copyright Copyright Anton Terekhov, NetMonsters LLC, 2011
* @license https://github.com/AntonTerekhov/OrientDB-PHP/blob/master/LICENSE
* @link https://github.com/AntonTerekhov/OrientDB-PHP
* @package OrientDB-PHP
* @subpackage Example
*/
$rootPassword = '60F3D52B4374C22B19F2EA5AD2812A45FB1C34985C2532D60E267AADB9E3E130';
$dbName = 'example';
$clusterName = 'default';
require_once 'OrientDB/OrientDB.php';
echo 'Connecting to server...' . PHP_EOL;
try {
$db = new OrientDB('localhost', 2424);
}
catch (Exception $e) {
die('Failed to connect: ' . $e->getMessage());
}
echo 'Connecting as root...' . PHP_EOL;
try {
$connect = $db->connect('root', $rootPassword);
}
catch (OrientDBException $e) {
die('Failed to connect(): ' . $e->getMessage());
}
echo 'Deleting DB (in case of previous example)...' . PHP_EOL;
try {
$db->DBDelete($dbName);
}
catch (OrientDBException $e) {
die('Failed to DBDelete(): ' . $e->getMessage());
}
echo 'Creating DB...' . PHP_EOL;
try {
$result = $db->DBCreate($dbName, OrientDB::DB_TYPE_LOCAL);
}
catch (OrientDBException $e) {
echo $e->getMessage() . PHP_EOL;
}
echo 'Opening DB...' . PHP_EOL;
try {
$clusters = $db->DBOpen($dbName, 'writer', 'writer');
foreach ($clusters['clusters'] as $cluster) {
if ($cluster->name === $clusterName) {
$clusterID = $cluster->id;
}
}
}
catch (OrientDBException $e) {
echo $e->getMessage() . PHP_EOL;
}
echo 'Create record...' . PHP_EOL;
$record = new OrientDBRecord();
$record->data->FirstName = 'Bruce';
$record->data->LastName = 'Wayne';
$record->data->appearance = 1938;
try {
$recordPos = $db->recordCreate($clusterID, $record);
}
catch (OrientDBException $e) {
echo $e->getMessage() . PHP_EOL;
}
echo 'Created record position: ' . $recordPos . PHP_EOL . PHP_EOL;
echo 'Load record...' . PHP_EOL;
try {
$recordLoaded = $db->recordLoad($clusterID . ':' . $recordPos);
}
catch (OrientDBException $e) {
echo $e->getMessage() . PHP_EOL;
}
echo 'Load record result: ' . $recordLoaded . PHP_EOL . PHP_EOL;
printf('%1$s %2$s first appears in %3$d' . PHP_EOL . PHP_EOL, $recordLoaded->data->FirstName, $recordLoaded->data->LastName, $recordLoaded->data->appearance);
echo 'Update record...' . PHP_EOL;
try {
$recordLoaded->data->appearance = 1939;
$version = $db->recordUpdate($recordLoaded->recordID, $recordLoaded);
}
catch (OrientDBException $e) {
echo $e->getMessage() . PHP_EOL;
}
echo 'Updated record version: ' . $version . PHP_EOL . PHP_EOL;
printf('No, %1$s %2$s first appears in %3$d!' . PHP_EOL . PHP_EOL, $recordLoaded->data->FirstName, $recordLoaded->data->LastName, $recordLoaded->data->appearance);
echo 'Delete record with old version (' . $recordLoaded->version . ') ...' . PHP_EOL;
try {
$result = $db->recordDelete($recordLoaded->recordID, $recordLoaded->version);
}
catch (OrientDBException $e) {
echo $e->getMessage() . PHP_EOL;
}
echo 'Delete record with correct version (' . $version . ') ...' . PHP_EOL;
try {
$result = $db->recordDelete($recordLoaded->recordID, $version);
}
catch (OrientDBException $e) {
echo $e->getMessage() . PHP_EOL;
}
echo 'Delete record result: ' . var_export($result, true) . PHP_EOL . PHP_EOL;
echo 'Retry load record...' . PHP_EOL;
try {
$recordLoaded2 = $db->recordLoad($recordLoaded->recordID);
}
catch (OrientDBException $e) {
echo $e->getMessage() . PHP_EOL;
}
echo 'Load record result: ' . var_export($recordLoaded2, true) . PHP_EOL . PHP_EOL;
echo 'Deleting DB...' . PHP_EOL;
try {
$db->DBDelete($dbName);
}
catch (OrientDBException $e) {
die('Failed to DBDelete(): ' . $e->getMessage());
}