SphinxQL Query Builder for PHP

A SphinxQL query builder for any PHP 5.3+ project, composer compatible.

This project is maintained by FoolCode

Query Builder for SphinxQL

About

This is a SphinxQL Query Builder used to work with SphinxQL, a SQL dialect used with the Sphinx search engine. It maps most of the functions listed in the SphinxQL reference and is generally faster than the available Sphinx API.

This Query Builder has no dependencies besides PHP 5.3, \MySQLi extension, and Sphinx.

This package is BETA QUALITY. It is recommended that you do extensive testing in development before using it in a production environment.

Missing methods?

SphinxQL evolves very fast.

Most of the new functions are static one liners like SHOW PLUGINS. We'll avoid trying to keep up with these methods, as they are easy to just call directly (SphinxQL::create($conn)->query($sql)->execute()). You're free to submit pull requests to support these methods.

If any feature is unreachable through this library, open a new issue or send a pull request.

Code Quality

The majority of the methods in the package have been unit tested. The only methods that have not been tested are single queries such as flushRtIndex, but these are independent and should work fine.

We have tested our package locally and remotely with Travis-CI:

Build Status

How to Contribute

Pull Requests

  1. Fork the SphinxQL Query Builder repository
  2. Create a new branch for each feature or improvement
  3. Submit a pull request from each branch to the dev branch

It is very important to separate new features or improvements into separate feature branches, and to send a pull request for each branch. This allows me to review and pull in new features or improvements individually.

Style Guide

All pull requests must adhere to the PSR-2 standard.

Unit Testing

All pull requests must be accompanied by passing unit tests and complete code coverage. The SphinxQL Query Builder uses phpunit for testing.

Learn about PHPUnit

Installation

This is a Composer package. You can install this package with the following command: php composer.phar install

Usage

The following examples will omit the namespace.

<?php
use Foolz\SphinxQL\SphinxQL;
use Foolz\SphinxQL\Connection;

// create a SphinxQL Connection object to use with SphinxQL
$conn = new Connection();
$conn->setConnectionParams('domain.tld', 9306);

$query = SphinxQL::create($conn)->select('column_one', 'colume_two')
    ->from('index_delta', 'index_main', 'index_ancient')
    ->match('comment', 'my opinion is superior to yours')
    ->where('banned', '=', 1);

$result = $query->execute();

Connection

More methods are available in the Connection class, but usually not necessary as these are handled automatically.

SphinxQL

Bypass Query Escaping

Often, you would need to call and run SQL functions that shouldn't be escaped in the query. You can bypass the query escape by wrapping the query in an \Expression.

Query Escaping

There are cases when an input must be escaped in the SQL statement. The following functions are used to handle any escaping required for the query.

SELECT

INSERT, REPLACE

This will return an INT with the number of rows affected.

UPDATE

This will return an INT with the number of rows affected.

DELETE

Will return an array with an INT as first member, the number of rows deleted.

WHERE

MATCH

GROUP, WITHIN GROUP, ORDER, OFFSET, LIMIT, OPTION

TRANSACTION

Executing and Compiling

Multi-Query

<?php
$result = SphinxQL::create($this->conn)
    ->select()
    ->from('rt')
    ->match('title', 'sora')
    ->enqueue(SphinxQL::create($this->conn)->query('SHOW META')) // this returns the object with SHOW META query
    ->enqueue() // this returns a new object
    ->select()
    ->from('rt')
    ->match('content', 'nymph')
    ->executeBatch();

$result[0] will contain the first select. result[1] will contain the META for the first query. result[2] will contain the second select.

Helper

The Helper class contains useful methods that don't need "query building".

Remember to ->execute() to get a result.

The following methods return a prepared SphinxQL object. You can also use ->enqueue($next_object):

<?php
$result = SphinxQL::create($this->conn)
    ->select()
    ->from('rt')
    ->where('gid', 9003)
    ->enqueue(Helper::create($this->conn)->showMeta()) // this returns the object with SHOW META query prepared
    ->enqueue() // this returns a new object
    ->select()
    ->from('rt')
    ->where('gid', 201)
    ->executeBatch();