cnsaturn / SphinxQL-Query-Builder

SphinxQL Query Builder generates SphinxQL, a SQL dialect, which is used to query the Sphinx search engine. (Composer Package)

Home Page:http://foolcode.github.com/SphinxQL-Query-Builder/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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 every function 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.

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.

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);

// use SphinxQL::forge($conn) to initialize and bind the connection to be used for future calls
SphinxQL::forge($conn);

$query = SphinxQL::forge()->select('column_one', 'colume_two')
	->from('index_delta', 'index_main', 'index_ancient')
	->match('comment', 'my opinion is better')
	->where('banned', '=', 1);

$result = $query->execute();

Connection

  • $conn = new Connection()

    Create a new Connection instance to be used with the following methods or SphinxQL class.

  • $conn->silenceConnectionWarning($enable = true)

    Suppresses any warnings and errors displayed by the \MySQLi extension upon connection failure. This is disabled by default.

  • $conn->setConnectionParams($host = '127.0.0.1', $port = 9306)

    Sets the connection parameters used to establish a connection to the server.

  • $conn->query($query)

    Performs the query on the server. Returns an array of results for SELECT, or an int with the number of rows affected.

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

SphinxQL

  • SphinxQL::forge($conn = null)

    Creates a SphinxQL instance used for generating queries.

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.

  • SphinxQL::expr($string)

    Returns the string without being escaped.

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.

  • $sq->escape($value)

    Returns the escaped value. This is processed with the \MySQLi::real_escape_string() function.

  • $sq->quoteIdentifier($identifier)

    Adds backtick quotes to the identifier. For array elements, use $sq->quoteIdentifierArray($arr).

  • $sq->quote($value)

    Adds quotes to the value and escapes it. For array elements, use $sq->quoteArr($arr).

  • $sq->escapeMatch($value)

    Escapes the string to be used in MATCH.

  • $sq->halfEscapeMatch($value)

    Escapes the string to be used in MATCH. The following characters are allowed: -, |, and ".

    Refer to $sq->match() for more information.

SET VARIABLE

  • SphinxQL::forge()->setVariable($name, $value, $global = false)

    Sets a variable server-side.

SHOW

  • SphinxQL::forge()->meta() => 'SHOW META'
  • SphinxQL::forge()->warnings() => 'SHOW WARNINGS'
  • SphinxQL::forge()->status() => 'SHOW STATUS'
  • SphinxQL::forge()->tables() => 'SHOW TABLES'
  • SphinxQL::forge()->variables() => 'SHOW VARIABLES'
  • SphinxQL::forge()->variablesSession() => 'SHOW SESSION VARIABLES'
  • SphinxQL::forge()->variablesGlobal() => 'SHOW GLOBAL VARIABLES'

SELECT

  • $sq = SphinxQL::forge()->select($column1, $column2, ...)->from($index1, $index2, ...)

    Begins a SELECT query statement. If no column is specified, the statement defaults to using *. Both $column1 and $index1 can be arrays.

INSERT, REPLACE

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

  • $sq = SphinxQL::forge()->insert()->into($index)

    Begins an INSERT.

  • $sq = SphinxQL::forge()->replace()->into($index)

    Begins an REPLACE.

  • $sq->set($associative_array)

    Inserts an associative array, with the keys as the columns and values as the value for the respective column.

  • $sq->value($column1, $value1)->value($column2, $value2)->value($column3, $value3)

    Sets the value of each column individually.

  • $sq->columns($column1, $column2, $column3)->values($value1, $value2, $value3)->values($value11, $value22, $value33)

    Allows the insertion of multiple arrays of values in the specified columns.

    Both $column1 and $index1 can be arrays.

UPDATE

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

  • $sq = SphinxQL::forge()->update($index)

    Begins an UPDATE.

  • $sq->value($column1, $value1)->value($column2, $value2)

    Updates the selected columns with the respective value.

  • $sq->set($associative_array)

    Inserts the associative array, where the keys are the columns and the respective values are the column values.

DELETE

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

  • $sq = SphinxQL::forge()->delete()->from($column)

    Begins a DELETE.

WHERE

  • $sq->where($column, $operator, $value)

    Standard WHERE, extended to work with Sphinx filters and full-text.

      // WHERE `column` = 'value'
      $sq->where('column', 'value');
    
      // WHERE `column` = 'value'
      $sq->where('column', '=', 'value');
    
      // WHERE `column` >= 'value'
      $sq->where('column', '>=', 'value')
    
      // WHERE `column` IN ('value1', 'value2', 'value3')
      $sq->where('column', 'IN', array('value1', 'value2', 'value3'));
    
      // WHERE `column` BETWEEN 'value1' AND 'value2'
      // WHERE `example` BETWEEN 10 AND 100
      $sq->where('column', 'BETWEEN', array('value1', 'value2'))
    

    It should be noted that OR and parenthesis are not supported and implemented in the SphinxQL dialect yet.

MATCH

  • $sq->match($column, $value, $half = false)

    Search in full-text fields. Can be used multiple times in the same query.

      $sq->match('title', 'Otoshimono')
      	->match('character', 'Nymph');
    

    By default, all inputs are fully escaped. The usage of SphinxQL::expr($value) is required to bypass the statement escapes.

    The $half argument, if set to true, will not escape and allow the usage of the following characters: -, |, ". If you plan to use this feature and expose it to public interfaces, it is recommended that you wrap the query in a try catch block as the character order may throw a query error.

      try
      {
      	$result = SphinxQL::forge()->select()
      		->from('rt')
      		->match('title', 'Sora no || Otoshimono')
      		->execute();
      }
      catch (\Foolz\SphinxQL\DatabaseException $e)
      {
      	// an error is thrown because two `|` one after the other aren't allowed
      }
    

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

  • $sq->groupBy($column)

    GROUP BY $column

  • $sq->withinGroupOrderBy($column, $direction = null)

    WITHIN GROUP ORDER BY $column [$direction]

    Direction can be omitted with null, or be ASC or DESC case insensitive.

  • $sq->orderBy($column, $direction = null)

    ORDER BY $column [$direction]

    Direction can be omitted with null, or be ASC or DESC case insensitive.

  • $sq->offset($offset)

    LIMIT $offset, 9999999999999

    Set the offset. Since SphinxQL doesn't support the OFFSET keyword, LIMIT has been set at an extremely high number.

  • $sq->limit($limit)

    LIMIT $limit

  • $sq->limit($offset, $limit)

    LIMIT $offset, $limit

  • $sq->option($name, $value)

    OPTION $name = $value

    Set a SphinxQL option such as max_matches or reverse_scan for the query.

TRANSACTION

  • SphinxQL::forge()->transactionBegin()

    Begins a transaction.

  • SphinxQL::forge()->transactionCommit()

    Commits a transaction.

  • SphinxQL::forge()->transactionRollback()

    Rollbacks a transaction.

Executing and Compiling

  • $sq->execute()

    Compiles, executes, and returns an array of results of a query.

  • $sq->executeBatch()

    Compiles, executes, and returns an array of results for a multi-query.

  • $sq->compile()

    Compiles the query.

  • $sq->getCompiled()

    Returns the last query compiled.

  • $sq->getResult()

    Returns the last result.

Multi-Query

  • $sq->enqueue()

    Queues the query.

  • $sq->executeBatch()

    Returns an array of the results of all the queued queries.

More

There's several more functions to complete the SphinxQL library:

  • SphinxQL::forge()->callSnippets($data, $index, $extra = array())
  • SphinxQL::forge()->callKeywords($text, $index, $hits = null)
  • SphinxQL::forge()->describe($index)
  • SphinxQL::forge()->createFunction($udf_name, $returns, $soname)
  • SphinxQL::forge()->dropFunction($udf_name)
  • SphinxQL::forge()->attachIndex($disk_index, $rt_index)
  • SphinxQL::forge()->flushRtIndex($index)

About

SphinxQL Query Builder generates SphinxQL, a SQL dialect, which is used to query the Sphinx search engine. (Composer Package)

http://foolcode.github.com/SphinxQL-Query-Builder/


Languages

Language:PHP 100.0%