Commit b9137dfe by Paul Klimov

Sphinx command and query added.

parent afcdcb9a
...@@ -18,7 +18,7 @@ before_script: ...@@ -18,7 +18,7 @@ before_script:
- tests/unit/data/travis/cubrid-setup.sh - tests/unit/data/travis/cubrid-setup.sh
script: script:
- phpunit --coverage-clover tests/unit/runtime/coveralls/clover.xml --verbose --exclude-group mssql,oci,wincache,xcache,zenddata - phpunit --coverage-clover tests/unit/runtime/coveralls/clover.xml --verbose --exclude-group mssql,oci,wincache,xcache,zenddata,sphinx
after_script: after_script:
- php vendor/bin/coveralls - php vendor/bin/coveralls
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\sphinx;
/**
* Class Command
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
*/
class Command extends \yii\db\Command
{
//
}
\ No newline at end of file
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\sphinx;
/**
* Class QueryBuilder
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
*/
class QueryBuilder extends \yii\db\mysql\QueryBuilder
{
/**
* Generates a SELECT SQL statement from a [[Query]] object.
* @param Query $query the [[Query]] object from which the SQL statement will be generated
* @return array the generated SQL statement (the first array element) and the corresponding
* parameters to be bound to the SQL statement (the second array element).
*/
public function build($query)
{
$params = $query->params;
$clauses = [
$this->buildSelect($query->select, $query->distinct, $query->selectOption),
$this->buildFrom($query->from),
$this->buildWhere($query->where, $params),
$this->buildGroupBy($query->groupBy),
$this->buildWithin($query->within),
$this->buildOrderBy($query->orderBy),
$this->buildLimit($query->limit, $query->offset),
$this->buildOption($query->options),
];
return [implode($this->separator, array_filter($clauses)), $params];
}
/**
* @param array $columns
* @return string the ORDER BY clause built from [[query]].
*/
public function buildWithin($columns)
{
if (empty($columns)) {
return '';
}
$orders = [];
foreach ($columns as $name => $direction) {
if (is_object($direction)) {
$orders[] = (string)$direction;
} else {
$orders[] = $this->db->quoteColumnName($name) . ($direction === Query::SORT_DESC ? ' DESC' : '');
}
}
return 'WITHIN GROUP ORDER BY ' . implode(', ', $orders);
}
/**
* @param array $options
* @return string the OPTION clause build from [[query]]
*/
public function buildOption(array $options)
{
if (empty($options)) {
return '';
}
$optionLines = [];
foreach ($options as $name => $value) {
$optionLines[] = $name . ' = ' . $value;
}
return 'OPTION ' . implode(', ', $optionLines);
}
}
\ No newline at end of file
...@@ -15,5 +15,13 @@ namespace yii\sphinx; ...@@ -15,5 +15,13 @@ namespace yii\sphinx;
*/ */
class Schema extends \yii\db\mysql\Schema class Schema extends \yii\db\mysql\Schema
{ {
// /**
* Creates a query builder for the database.
* This method may be overridden by child classes to create a DBMS-specific query builder.
* @return QueryBuilder query builder instance
*/
public function createQueryBuilder()
{
return new QueryBuilder($this->db);
}
} }
\ No newline at end of file
...@@ -17,6 +17,7 @@ source yii2_test_article_src ...@@ -17,6 +17,7 @@ source yii2_test_article_src
FROM yii2_test_article FROM yii2_test_article
sql_attr_uint = id sql_attr_uint = id
sql_attr_uint = author_id
sql_attr_timestamp = add_date sql_attr_timestamp = add_date
sql_query_info = SELECT * FROM yii2_test_article WHERE id=$id sql_query_info = SELECT * FROM yii2_test_article WHERE id=$id
...@@ -38,6 +39,8 @@ source yii2_test_item_src ...@@ -38,6 +39,8 @@ source yii2_test_item_src
FROM yii2_test_item FROM yii2_test_item
sql_attr_uint = id sql_attr_uint = id
sql_attr_uint = category_id
sql_attr_float = price
sql_attr_timestamp = add_date sql_attr_timestamp = add_date
sql_query_info = SELECT * FROM yii2_test_item WHERE id=$id sql_query_info = SELECT * FROM yii2_test_item WHERE id=$id
......
-- phpMyAdmin SQL Dump
-- version 3.5.0
-- http://www.phpmyadmin.net
--
-- Host: 10.10.50.201
-- Generation Time: Nov 01, 2013 at 04:38 PM
-- Server version: 5.1.69-log
-- PHP Version: 5.3.3
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00"; SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `yiiexample`
--
-- --------------------------------------------------------
--
-- Table structure for table `yii2_test_article`
--
CREATE TABLE IF NOT EXISTS `yii2_test_article` ( CREATE TABLE IF NOT EXISTS `yii2_test_article` (
`id` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL, `title` varchar(255) NOT NULL,
...@@ -35,20 +10,10 @@ CREATE TABLE IF NOT EXISTS `yii2_test_article` ( ...@@ -35,20 +10,10 @@ CREATE TABLE IF NOT EXISTS `yii2_test_article` (
PRIMARY KEY (`id`) PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ; ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
--
-- Dumping data for table `yii2_test_article`
--
INSERT INTO `yii2_test_article` (`id`, `title`, `content`, `author_id`, `create_date`) VALUES INSERT INTO `yii2_test_article` (`id`, `title`, `content`, `author_id`, `create_date`) VALUES
(1, 'About cats', 'This article is about cats', 1, '2013-10-23 00:00:00'), (1, 'About cats', 'This article is about cats', 1, '2013-10-23 00:00:00'),
(2, 'About dogs', 'This article is about dogs', 2, '2013-11-15 00:00:00'); (2, 'About dogs', 'This article is about dogs', 2, '2013-11-15 00:00:00');
-- --------------------------------------------------------
--
-- Table structure for table `yii2_test_item`
--
CREATE TABLE IF NOT EXISTS `yii2_test_item` ( CREATE TABLE IF NOT EXISTS `yii2_test_item` (
`id` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL, `name` varchar(255) NOT NULL,
...@@ -58,14 +23,6 @@ CREATE TABLE IF NOT EXISTS `yii2_test_item` ( ...@@ -58,14 +23,6 @@ CREATE TABLE IF NOT EXISTS `yii2_test_item` (
PRIMARY KEY (`id`) PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ; ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
--
-- Dumping data for table `yii2_test_item`
--
INSERT INTO `yii2_test_item` (`id`, `name`, `description`, `category_id`, `price`) VALUES INSERT INTO `yii2_test_item` (`id`, `name`, `description`, `category_id`, `price`) VALUES
(1, 'pencil', 'Simple pencil', 1, 2.5), (1, 'pencil', 'Simple pencil', 1, 2.5),
(2, 'table', 'Wooden table', 2, 100); (2, 'table', 'Wooden table', 2, 100);
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
<?php
namespace yiiunit\extensions\sphinx;
use yii\db\DataReader;
/**
* @group sphinx
*/
class CommandTest extends SphinxTestCase
{
public function testExecute()
{
$db = $this->getConnection();
$sql = 'SELECT COUNT(*) FROM yii2_test_item_index WHERE MATCH(\'wooden\')';
$command = $db->createCommand($sql);
$this->assertEquals(1, $command->queryScalar());
$command = $db->createCommand('bad SQL');
$this->setExpectedException('\yii\db\Exception');
$command->execute();
}
public function testQuery()
{
$db = $this->getConnection();
// query
$sql = 'SELECT * FROM yii2_test_item_index';
$reader = $db->createCommand($sql)->query();
$this->assertTrue($reader instanceof DataReader);
// queryAll
$rows = $db->createCommand('SELECT * FROM yii2_test_item_index')->queryAll();
$this->assertEquals(2, count($rows));
$row = $rows[1];
$this->assertEquals(2, $row['id']);
$this->assertEquals(2, $row['category_id']);
$rows = $db->createCommand('SELECT * FROM yii2_test_item_index WHERE id=10')->queryAll();
$this->assertEquals([], $rows);
// queryOne
$sql = 'SELECT * FROM yii2_test_item_index ORDER BY id ASC';
$row = $db->createCommand($sql)->queryOne();
$this->assertEquals(1, $row['id']);
$this->assertEquals(1, $row['category_id']);
$sql = 'SELECT * FROM yii2_test_item_index ORDER BY id ASC';
$command = $db->createCommand($sql);
$command->prepare();
$row = $command->queryOne();
$this->assertEquals(1, $row['id']);
$this->assertEquals(1, $row['category_id']);
$sql = 'SELECT * FROM yii2_test_item_index WHERE id=10';
$command = $db->createCommand($sql);
$this->assertFalse($command->queryOne());
// queryColumn
$sql = 'SELECT * FROM yii2_test_item_index';
$column = $db->createCommand($sql)->queryColumn();
$this->assertEquals(range(1, 2), $column);
$command = $db->createCommand('SELECT id FROM yii2_test_item_index WHERE id=10');
$this->assertEquals([], $command->queryColumn());
// queryScalar
$sql = 'SELECT * FROM yii2_test_item_index ORDER BY id ASC';
$this->assertEquals($db->createCommand($sql)->queryScalar(), 1);
$sql = 'SELECT id FROM yii2_test_item_index ORDER BY id ASC';
$command = $db->createCommand($sql);
$command->prepare();
$this->assertEquals(1, $command->queryScalar());
$command = $db->createCommand('SELECT id FROM yii2_test_item_index WHERE id=10');
$this->assertFalse($command->queryScalar());
$command = $db->createCommand('bad SQL');
$this->setExpectedException('\yii\db\Exception');
$command->query();
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment