do365 / 9523-php-orientdb
PHPOrient 是基于 OrientDB 二进制协议的一个优秀的 PHP 驱动。
Requires
- php: >=5.4.0
- psr/log: ~1.0
This package is not auto-updated.
Last update: 2024-09-28 18:48:04 UTC
README
PHPOrient 是基于 OrientDB 二进制协议的 PHP 驱动。
状态:稳定 请报告任何您发现的错误,以便我们可以为所有人改进这个库。
需求
- PHP 版本 >= 5.4(启用 Socket 扩展)
- Orientdb 版本 1.7.4 或更高。
PhpOrient 在 32 位和 64 位平台上都能正常工作。
警告,如果您使用 32 位平台,您必须在您的应用程序中使用以下库之一以避免 Java 长整型数的有效数字丢失。此外,应将这些 php 模块加载到系统中以实现更好的驱动程序性能。
- BCMath 随机精度数学(更快,推荐)
- GNU 多精度
由于 PHP 平台相关性的性质,PhpOrient 设计上总是将数字视为字符串。在 32 位平台上,数字必须被视为字符串,因为大于 2^32 的值将会丢失,并且必须使用 BCMath/GMP 模块来避免这种情况。为了使所有平台(32 位和 64 位)的结果一致,并将如何使用自己的数据(通过手动转换)的决定权留给用户/开发者,所有数值数据类型都使用字符串。
安装
PhpOrient 的主要公共仓库托管在 https://github.com/Ostico/PhpOrient.git。
要安装库的最新版本,只需输入
git clone https://github.com/Ostico/PhpOrient.git
您希望其文件所在的位置。
如果您尚未全局安装,您必须下载 composer。只需在 PhpOrient 目录内运行此命令。
php -r "readfile('https://composer.php.ac.cn/installer');" | php
现在获取与 PhpOrient 一起工作的所需库
php composer.phar --no-dev install
注意
如果您已经安装了 composer 或者您的现有项目使用它,您可以通过 Composer https://packagist.org.cn/packages/ostico/phporient 安装/添加 PhpOrient,它与这个 GitHub 仓库链接(因此始终是更新的),并将其作为依赖项添加到您的项目中。
php composer.phar require "ostico/phporient:dev-master" --update-no-dev
贡献
- 分支项目。
- 进行更改。
- 为它添加测试。这很重要,这样我就不会在未来版本中无意中破坏它。
- 向我发送拉取请求(将拒绝 master 的拉取请求)
- ???
- 盈利
如何运行测试
- 使用以下命令获取开发库
php composer.phar install - 通过从项目目录运行 ./ci/ci-start.sh 启动 orient,它将下载最新的 orient 并对配置和数据库进行一些更改以进行测试
- 运行:./vendor/bin/phpunit
用法
PhpOrient 指定自动加载信息,Composer 生成 vendor/autoload.php 文件。您可以简单地将此文件包含进来,这样您就可以免费获得自动加载,并使用完全限定名声明 PhpOrient 客户端的使用。
require "vendor/autoload.php";
use PhpOrient\PhpOrient;
完整的 phpdoc 参考信息可以在这里找到 ApiIndex 或在 PhpOrient Wiki 中。
客户端初始化
有几种初始化客户端的方法
$client = new PhpOrient( 'localhost', 2424 );
$client->username = 'root';
$client->password = 'root_pass';
$client = new PhpOrient();
$client->hostname = 'localhost';
$client->port = 2424;
$client->username = 'root';
$client->password = 'root_pass';
$client = new PhpOrient();
$client->configure( array(
'username' => 'root',
'password' => 'root_pass',
'hostname' => 'localhost',
'port' => 2424,
) );
连接以执行服务器管理操作
$client = new PhpOrient( 'localhost', 2424 );
$client->username = 'root';
$client->password = 'root_pass';
$client->connect();
$client = new PhpOrient( 'localhost', 2424 );
$client->connect( 'root', 'root_pass' );
创建数据库
$new_cluster_id = $client->dbCreate( 'my_new_database',
PhpOrient::STORAGE_TYPE_MEMORY, # optional, default: STORAGE_TYPE_PLOCAL
PhpOrient::DATABASE_TYPE_GRAPH # optional, default: DATABASE_TYPE_GRAPH
);
删除数据库
$client->dbDrop( $this->db_name,
PhpOrient::STORAGE_TYPE_MEMORY # optional, default: STORAGE_TYPE_PLOCAL
);
检查是否存在数据库
$client->dbExists( 'my_database',
PhpOrient::DATABASE_TYPE_GRAPH # optional, default: DATABASE_TYPE_GRAPH
);
获取数据库列表
$client->dbList();
打开数据库
$ClusterMap = $client->dbOpen( 'GratefulDeadConcerts', 'admin', 'admin' );
发送命令
这应该仅用于在数据库上执行非幂等命令
$client->command( 'create class Animal extends V' );
$client->command( "insert into Animal set name = 'rat', specie = 'rodent'" );
执行查询
$client->query( 'select from followed_by limit 10' );
执行异步查询(回调)
$myFunction = function( Record $record) { var_dump( $record ); };
$client->queryAsync( 'select from followed_by', [ 'fetch_plan' => '*:1', '_callback' => $myFunction ] );
加载一条记录
$record = $client->recordLoad( new ID( '#3:0' ) )[0];
$record = $client->recordLoad( new ID( 3, 0 ) )[0];
$record = $client->recordLoad( new ID( [ 'cluster' => 3, 'position' => 0 ] ) )[0];
创建一条记录
$recordContent = [ 'accommodation' => 'houses', 'work' => 'bazar', 'holiday' => 'sea' ];
$rec = ( new Record() )->setOData( $recordContent )->setRid( new ID( 9 /* set only the cluster ID */ ) );
$record = $this->client->recordCreate( $rec );
更新一条记录
要更新一条记录,你必须有一个。
如果没有记录,你可以指定RID和数据创建一个新的。
$_recUp = [ 'accommodation' => 'hotel', 'work' => 'office', 'holiday' => 'mountain' ];
$recUp = ( new Record() )->setOData( $_recUp )->setOClass( 'V' )->setRid( new ID( 9, 0 ) );
$updated = $client->recordUpdate( $recUp );
否则,你可以使用之前加载/创建的记录。
/*
Create/Load or Query for a Record
*/
$recordContent = [ 'accommodation' => 'houses', 'work' => 'bazar', 'holiday' => 'sea' ];
$rec = ( new Record() )->setOData( $recordContent )->setRid( new ID( 9 ) );
$record = $client->recordCreate( $rec );
/*
or Query for an existent one
*/
$record = $client->query( "select from V where @rid = '#9:0'" )[0];
/*
NOW UPDATE
*/
$_recUp = [ 'accommodation' => 'bridge', 'work' => 'none', 'holiday' => 'what??' ];
$recUp = $record->setOData( $_recUp );
$updated = $client->recordUpdate( $recUp );
使用节点深度导航加载一条记录(回调)
$myFunction = function( Record $record) { var_dump( $record ); };
$client->recordLoad( new ID( "9", "1" ), [ 'fetch_plan' => '*:2', '_callback' => $myFunction ] );
删除一条记录
$delete = $client->recordDelete( new ID( 11, 1 ) );
执行OrientDB SQL批处理
$cmd = 'begin;' .
'let a = create vertex set script = true;' .
'let b = select from v limit 1;' .
'let e = create edge from $a to $b;' .
'commit retry 100;';
$lastRecord = $client->sqlBatch( $cmd );
事务
// create some record stuffs
$rec2Create = [ 'oClass' => 'V', 'oData' => [ 'alloggio' => 'albergo' ] ];
$rec = Record::fromConfig( $rec2Create );
$first_rec = $client->recordCreate( $rec );
$rec3Create = [ 'oClass' => 'V', 'oData' => [ 'alloggio' => 'house' ] ];
$rec = Record::fromConfig( $rec3Create );
$sec_rec = $client->recordCreate();
//get the transaction and start it
$tx = $client->getTransactionStatement();
//BEGIN THE TRANSACTION
$tx = $tx->begin();
//IN TRANSACTION
$recUp = [ 'accommodation' => 'mountain cabin' ];
$rec2 = new Record();
$rec2->setOData( $recUp );
$rec2->setOClass( 'V' );
$rec2->setRid( $first_rec->getRid() );
$rec2->setVersion( $first_rec->getVersion() );
$updateCommand = $client->recordUpdate( $rec2 );
$createCommand = $client->recordCreate(
( new Record() )
->setOData( [ 'accommodation' => 'bungalow' ] )
->setOClass( 'V' )
->setRid( new ID( 9 ) )
);
$deleteCommand = $client->recordDelete( $sec_rec->getRid() );
//Attach to the transaction statement, they will be executed in the same order
$tx->attach( $updateCommand ); // first
$tx->attach( $createCommand ); // second
$tx->attach( $deleteCommand ); // third
$result = $tx->commit();
/**
* @var Record $record
*/
foreach ( $result as $record ){
if( $record->getRid() == $first_rec->getRid() ){
$this->assertEquals( $record->getOData(), [ 'accommodation' => 'mountain cabin' ] );
$this->assertEquals( $record->getOClass(), $first_rec->getOClass() );
} else {
$this->assertEquals( $record->getOData(), [ 'accommodation' => 'bungalow' ] );
$this->assertEquals( $record->getOClass(), 'V' );
}
}
//check for deleted record
$deleted = $client->recordLoad( $sec_rec->getRid() );
$this->assertEmpty( $deleted );
获取数据库的大小(需要打开数据库)
$client->dbSize();
获取集群的记录ID范围
$data = $client->dataClusterDataRange( 9 );
获取一个或多个集群中的记录数量
$client->dataClusterCount( $client->getTransport()->getClusterMap()->getIdList() );
获取开放数据库中的记录数量
$result = $client->dbCountRecords();
重新加载数据库信息
此方法会自动更新客户端集群图。可以在创建类或添加/删除数据集群后使用。
$reloaded_list = $client->dbReload(); # $reloaded_list === $client->getTransport()->getClusterMap()
创建新的数据集群
$client->dataClusterAdd( 'new_cluster',
PhpOrient::CLUSTER_TYPE_MEMORY # optional, default: PhpOrient::CLUSTER_TYPE_PHYSICAL
);
删除数据集群
$client->dataClusterDrop( 11 );
持久连接(会话令牌)
从版本27开始,引入了一个扩展,允许使用基于令牌的会话。此功能必须在服务器配置中启用。
- 在第一次协商中,客户端可以使用`PhpOrient::setSessionToken`方法请求基于令牌的认证。
- 服务器将回复一个令牌或空字符串,表示它不支持基于令牌的会话,并使用旧式会话。
- 对于每个请求,客户端将发送令牌,并在令牌有效期结束时最终获得一个新令牌。
在使用基于令牌的认证时,连接可以在同一服务器的用户之间共享。
$client = new PhpOrient( 'localhost', 2424 );
$client->setSessionToken( true ); // set true to enable the token based authentication
$clusterID = $client->dbOpen( "GratefulDeadConcerts", 'admin', 'admin' );
$sessionToken = $client->getSessionToken(); // store this token somewhere
//destroy the old client, equals to another user/socket/ip ecc.
unset($client);
// create a new client
$client = new PhpOrient( 'localhost', 2424 );
// set the previous obtained token to re-attach to the old session
$client->setSessionToken( $sessionToken );
//now the dbOpen is not needed to perform database operations
$records = $client->query( 'select * from V limit 10' );
//set the flag again to true if you want to renew the token
$client->setSessionToken( true ); // set true
$clusterID = $client->dbOpen( "GratefulDeadConcerts", 'admin', 'admin' );
$new_sessionToken = $client->getSessionToken();
$this->assertNotEquals( $sessionToken, $new_sessionToken );
一个图形示例
require "vendor/autoload.php";
use PhpOrient\PhpOrient;
$client = new PhpOrient();
$client->configure( array(
'username' => 'admin',
'password' => 'admin',
'hostname' => 'localhost',
'port' => 2424,
) );
$client->connect();
$client->dbCreate( 'animals', PhpOrient::STORAGE_TYPE_MEMORY );
$client->dbOpen( 'animals', 'admin', 'admin' );
$client->command( 'create class Animal extends V' );
$client->command( "insert into Animal set name = 'rat', specie = 'rodent'" );
$animal = $client->query( "select * from Animal" );
$client->command( 'create class Food extends V' );
$client->command( "insert into Food set name = 'pea', color = 'green'" );
$client->command( 'create class Eat extends E' );
$client->command( "create edge Eat from ( select from Animal where name = 'rat' ) to ( select from Food where name = 'pea' )" );
$pea_eaters = $client->query( "select expand( in( Eat )) from Food where name = 'pea'" );
$animal_foods = $client->query( "select expand( out( Eat )) from Animal" );
foreach ( $animal_foods as $food ) {
$animal = $client->query(
"select name from ( select expand( in('Eat') ) from Food where name = 'pea' )"
)[0];
$this->assertEquals( 'pea', $food[ 'name' ] );
$this->assertEquals( 'green', $food[ 'color' ] );
$this->assertEquals( 'rat', $animal[ 'name' ] );
}
许可证
Apache许可证,版本2.0,请参阅LICENSE.md。