百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
S

SphinxQL-Query-Builder

> 数据库
开源

SphinxQL 查询构建器生成了 SphinxQL,这是一个 SQL 方言,用于查询 Sphinx 搜索引擎。 (Composer 包)

323 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

SphinxQL 查询构建器生成了 SphinxQL,这是一个 SQL 方言,用于查询 Sphinx 搜索引擎。 (Composer 包)

SphinxQL Query Builder

A fluent PHP query builder for SphinxQL and ManticoreQL.

It supports:

  • SELECT, INSERT, REPLACE, UPDATE, DELETE
  • MATCH() building (including MatchBuilder)
  • FACET queries
  • batched/multi-queries
  • helper commands (SHOW, CALL, maintenance operations)
  • percolate workflows for Manticore
  • both mysqli and PDO drivers

Installation

bash
composer require foolz/sphinxql-query-builder

Requirements:

  • PHP 8.2+
  • mysqli or pdo_mysql
  • Running Sphinx Search or Manticore Search server

Quick Start

php
setParams([
    'host' => '127.0.0.1',
    'port' => 9306,
]);

$rows = (new SphinxQL($conn))
    ->select('id', 'gid', 'title')
    ->from('rt')
    ->match('title', 'vacation')
    ->where('gid', '>', 300)
    ->orderBy('id', 'DESC')
    ->limit(5)
    ->execute()
    ->getStored();

Connection Setup

mysqli driver

php
setParams([
    'host' => '127.0.0.1',
    'port' => 9306,
    'options' => [
        MYSQLI_OPT_CONNECT_TIMEOUT => 2,
    ],
]);

PDO driver

php
setParams([
    'host' => '127.0.0.1',
    'port' => 9306,
    'charset' => 'utf8',
]);

Query Builder Examples

Compile SQL before executing

php
select('a.id')
    ->from('rt a')
    ->leftJoin('rt b', 'a.id', '=', 'b.id')
    ->where('a.id', '>', 1)
    ->compile()
    ->getCompiled();

// SELECT a.id FROM rt a LEFT JOIN rt b ON a.id = b.id WHERE a.id > 1

Insert rows

php
insert()
    ->into('rt')
    ->columns('id', 'gid', 'title', 'content')
    ->values(10, 9003, 'modifying the same line again', 'because i am that lazy')
    ->values(11, 201, 'replacing value by value', 'i have no idea who would use this directly')
    ->execute();

Replace rows

php
replace()
    ->into('rt')
    ->set([
        'id' => 10,
        'gid' => 9002,
        'title' => 'modified',
        'content' => 'this field was modified with replace',
    ])
    ->execute();

Update rows (including MVA)

php
update('rt')
    ->where('id', '=', 15)
    ->value('tags', [111, 222])
    ->execute();

Delete rows

php
delete()
    ->from('rt')
    ->where('id', 'IN', [11, 12, 13])
    ->match('content', 'content')
    ->execute()
    ->getStored();

Grouped boolean filters

php
select()
    ->from('rt')
    ->where('gid', 200)
    ->orWhereOpen()
    ->where('gid', 304)
    ->where('id', '>', 12)
    ->whereClose()
    ->compile()
    ->getCompiled();

// SELECT * FROM rt WHERE gid = 200 OR ( gid = 304 AND id > 12 )

MATCH with builder callback

php
select()
    ->from('rt')
    ->match(function ($m) {
        $m->field('content')
          ->match('directly')
          ->orMatch('lazy');
    })
    ->execute()
    ->getStored();

ORDER BY KNN

php
select('id')
    ->from('rt')
    ->orderByKnn('embeddings', 5, [0.1, 0.2, 0.3])
    ->compile()
    ->getCompiled();

// SELECT id FROM rt ORDER BY KNN(embeddings, 5, [0.1,0.2,0.3]) ASC

Subqueries

php
select('id')
    ->from('rt')
    ->orderBy('id', 'DESC');

$sql = (new SphinxQL($conn))
    ->select()
    ->from($subquery)
    ->orderBy('id', 'ASC')
    ->compile()
    ->getCompiled();

// SELECT * FROM (SELECT id FROM rt ORDER BY id DESC) ORDER BY id ASC

Helper API Example

php
showTables()->execute()->getStored();
$variables = Helper::pairsToAssoc($helper->showVariables()->execute()->getStored());
$keywords = $helper->callKeywords('test case', 'rt', 1)->execute()->getStored();

Compile examples from tests:

  • $helper->showTables()->compile()->getCompiled() -> SHOW TABLES
  • $helper->showTables('rt')->compile()->getCompiled() -> SHOW TABLES LIKE 'rt'
  • $helper->showTableStatus()->compile()->getCompiled() -> SHOW TABLE STATUS
  • $helper->showTableStatus('rt')->compile()->getCompiled() -> SHOW TABLE rt STATUS
  • $helper->callSuggest('teh', 'rt', ['limit' => 5])->compile()->getCompiled() -> CALL SUGGEST('teh', 'rt', 5 AS limit)

FACET Example

php
facet(['gid'])
    ->orderBy('gid', 'ASC');

$batchRows = (new SphinxQL($conn))
    ->select()
    ->from('rt')
    ->facet($facet)
    ->executeBatch()
    ->getStored();

// $batchRows[0] is SELECT data
// $batchRows[1] is FACET aggregation data

Multi Query / Batch Example

php
select()
    ->from('rt')
    ->where('gid', 9003)
    ->enqueue()
    ->select()
    ->from('rt')
    ->where('gid', 201)
    ->enqueue((new Helper($conn))->showMeta())
    ->executeBatch();

$all = $batch->getStored();

Percolate Example (Manticore)

php
insert('@subject orange')
    ->into('pq')
    ->tags(['tag2', 'tag3'])
    ->filter('price>3')
    ->execute();

$matches = (new Percolate($conn))
    ->callPQ()
    ->from('pq')
    ->documents(['{"subject":"document about orange"}'])
    ->options([
        Percolate::OPTION_QUERY => 1,
        Percolate::OPTION_DOCS => 1,
    ])
    ->execute()
    ->fetchAllAssoc();

Capability Checks

php
getCapabilities();

if ($helper->supports('call_autocomplete')) {
    $rows = $helper->callAutocomplete('te', 'rt', ['fuzzy' => 1])->execute()->getStored();
}

Result Objects

execute() returns ResultSetInterface:

  • getStored()
  • fetchAllAssoc()
  • fetchAllNum()
  • fetchAssoc()
  • fetchNum()
  • getAffectedRows()

executeBatch() returns MultiResultSetInterface:

  • getStored()
  • getNext()

Documentation Map

  • Main docs index: docs/index.rst
  • Builder guide: docs/query-builder.rst
  • Helper guide: docs/helper.rst
  • Facets: docs/features/facet.rst
  • Multi-query: docs/features/multi-query-builder.rst
  • Migration guide: docs/migrating.rst

Running Tests

bash
./scripts/run-tests-docker.sh

This runs the repository test matrix (mysqli + pdo) in Docker.

Contributing

Pull requests are welcome. Please include tests for behavior changes and keep docs in sync with API updates.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHPdatabasequery-buildersql

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类数据库
定价开源

> 相关工具

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库