sweetrdf/sparql-client 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sweetrdf/sparql-client

Composer 安装命令:

composer require sweetrdf/sparql-client

包简介

README 文档

README

Latest Stable Version Build status Coverage Status License

A SPARQL client library for the rdfInterface ecosystem with the API inspired by the PDO.

  • It can work with any PSR-17 / PSR-18 compliant HTTP libraries.
  • It can work with huge query results.
    The response is parsed in a lazy way as a stream (the next row is parsed only when you try to read it). This assures iterating through results without acumulating them in an array, which has neglectable memory footprint.

Installation

  • Obtain the Composer
  • Run composer require sweetrdf/sparql-client
  • Run composer require guzzlehttp/guzzle to install an HTTP client (but you can use any client supporting PSR-18).
  • Run composer require http-interop/http-factory-guzzle to install PSR-17 bindinds for Guzzle (but you can use any PSR-17 implementation)
  • Run composer require sweetrdf/quick-rdf to install RDF terms factory (you can use any terms factory compatible with the rdfInterface).

Automatically generated documentation

https://sweetrdf.github.io/sparqlClient/namespaces/sparqlclient.html

It's very incomplete but better than nothing.
RdfInterface documentation is included which provides documentation for terms (objects representing RDF named nodes, literals, etc.).

Usage

include 'vendor/autoload.php';
$connection = new \sparqlClient\StandardConnection('https://query.wikidata.org/sparql', new \quickRdf\DataFactory());
$results    = $connection->query('select * where {?a ?b ?c} limit 10');
foreach ($results as $i) {
    print_r($i);
}

Parameterized queries

The StandardConnection class provides a PDO-like API for parameterized queries (aka prepared statements). Parameterized queries:

  • Are the right way to assure all named nodes/blank nodes/literals/quads/etc. in the SPARQL query are properly escaped.
  • Protect against SPARQL injections.
  • Don't provide any speedup (in contrary to SQL parameterized queries).
include 'vendor/autoload.php';
$factory    = new \quickRdf\DataFactory();
$connection = new \sparqlClient\StandardConnection('https://query.wikidata.org/sparql', $factory);

// pass parameters using execute()
// the single `?` is a positional parameter while the `:sf` is a named parameter
$query      = $connection->prepare('SELECT * WHERE {?a ? ?c . ?a :sf ?d .} LIMIT 10');
$query->execute([
    $factory::namedNode('http://creativecommons.org/ns#license'),
    'sf' => $factory::namedNode('http://schema.org/softwareVersion'),
]);
foreach ($query as $i) {
    print_r($i);
}

// bind a (positional) parameter to a variable
$query = $connection->prepare('SELECT * WHERE {?a ? ?c .} LIMIT 2');
$value = $factory::namedNode('http://creativecommons.org/ns#license');
$query->bindParam(0, $value);
$query->execute();
foreach ($query as $i) {
    print_r($i);
}
$value = $factory::namedNode('http://schema.org/softwareVersion');
$query->execute();
foreach ($query as $i) {
    print_r($i);
}

There are some differences comparing to the PDO API:

  • Mixing positional and named parameters in one query is allowed (see the example above).
  • Mixing bindParam()/bindValue() and passing (some or all) parameters trough execute() is allowed.
    • The only constraint is that all parameters have to be set.
    • Parameters passed to execute() overwrite ones set with bindParam()/bindValue().
  • As strong typing is used parameter values have to be of type rdfInterface\Term.
    • It makes unnecessary specifying the type in bindParam()/bindvalue().

Advanced usage

  • You may also provide any PSR-18 HTTP client and/or PSR-17 HTTP request factory to the \sparqlClient\StandardConnection constructor. E.g. let's assume your SPARQL endpoint requires authorization and you want to benefit from Guzzle connection allowing to set global request options:
    $connection = new \sparqlClient\StandardConnection(
        'https://query.wikidata.org/sparql', 
        new \quickRdf\DataFactory(),
        new \GuzzleHttp\Client(['auth' => ['login', 'pswd']])
    );
  • If your SPARQL endpoint doesn't follow the de facto standard of accepting the SPARQL query as the query request parameter, you may use the \sparqlClient\Connection class which takes PSR-7 requests instead of a query string and allows you to use any specific HTTP request you need.

FAQ

  • What about integration of INSERT/UPDATE/DELETE queries with the \rdfInterface\Dataset or \rdfInterface\QuadIterator?
    Will be added in the future.

sweetrdf/sparql-client 适用场景与选型建议

sweetrdf/sparql-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 210 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 03 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 sweetrdf/sparql-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 sweetrdf/sparql-client 我们能提供哪些服务?
定制开发 / 二次开发

基于 sweetrdf/sparql-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 210
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 5
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-03-29