QueryBuilder.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <?php
  2. /**
  3. * xunsearch QueryBuilder class file
  4. *
  5. * @author hightman
  6. * @link http://www.xunsearch.com/
  7. * @copyright Copyright &copy; 2014 HangZhou YunSheng Network Technology Co., Ltd.
  8. * @license http://www.xunsearch.com/license/
  9. * @version $Id$
  10. */
  11. namespace hightman\xunsearch;
  12. use Yii;
  13. use yii\base\BaseObject;
  14. use yii\base\InvalidParamException;
  15. /**
  16. * QueryBuilder builds query string based on the specification given as a [[ActiveQuery]] object.
  17. *
  18. * @author xjflyttp <xjflyttp@gmail.com>
  19. * @author hightman <hightman@twomice.net>
  20. * @since 1.4.9
  21. */
  22. class QueryBuilder extends BaseObject
  23. {
  24. /**
  25. * @var Database the database to be used.
  26. */
  27. public $db;
  28. /**
  29. * @var array map of query condition to builder methods.
  30. * These methods are used by [[buildCondition]] to build SQL conditions from array syntax.
  31. */
  32. protected $conditionBuilders = [
  33. 'NOT' => 'buildNotCondition',
  34. 'AND' => 'buildAndCondition',
  35. 'OR' => 'buildAndCondition',
  36. 'XOR' => 'buildAndCondition',
  37. 'IN' => 'buildInCondition',
  38. 'NOT IN' => 'buildInCondition',
  39. 'BETWEEN' => 'buildBetweenCondition',
  40. 'WEIGHT' => 'buildWeightCondition',
  41. 'WILD' => 'buildAndCondition',
  42. ];
  43. /**
  44. * Constructor.
  45. * @param Database $db the database
  46. * @param array $config name-value pairs that will be used to initialize the object properties
  47. */
  48. public function __construct(Database $db, $config = [])
  49. {
  50. $this->db = $db;
  51. parent::__construct($config);
  52. }
  53. /**
  54. * Generates a query string from a [[ActiveQuery]] object.
  55. * @param ActiveQuery $query
  56. * @return \XSSearch ready XS search object
  57. */
  58. public function build($query)
  59. {
  60. $others = [];
  61. if ($query->query === null) {
  62. $query->query = $this->buildWhere($query->where, $others);
  63. }
  64. $profile = $this->db->getName() . '.build#' . $query->query;
  65. Yii::beginProfile($profile, __METHOD__);
  66. $search = $this->db->getSearch();
  67. $search->setFuzzy($query->fuzzy)->setAutoSynonyms($query->synonyms);
  68. $search->setQuery($query->query);
  69. if (isset($others['range'])) {
  70. $this->buildRange($others['range']);
  71. }
  72. if (isset($others['weight'])) {
  73. $this->buildWeight($others['weight']);
  74. }
  75. if (is_callable($query->buildOther)) {
  76. call_user_func($query->buildOther, $search);
  77. }
  78. $this->buildLimit($query->limit, $query->offset);
  79. $this->buildOrderBy($query->orderBy);
  80. Yii::endProfile($profile, __METHOD__);
  81. return $search;
  82. }
  83. /**
  84. * @param string|array $condition
  85. * @param array $others used to save other query setting
  86. * @return string the query string built from [[QueryTrait::$where]].
  87. */
  88. protected function buildWhere($condition, &$others)
  89. {
  90. return $this->buildCondition($condition, $others);
  91. }
  92. /**
  93. * @param array $ranges
  94. */
  95. protected function buildRange($ranges)
  96. {
  97. foreach ($ranges as $range) {
  98. call_user_func_array(array($this->db->getSearch(), 'addRange'), $range);
  99. }
  100. }
  101. /**
  102. * @param array $weights
  103. */
  104. protected function buildWeight($weights)
  105. {
  106. foreach ($weights as $weight) {
  107. call_user_func_array(array($this->db->getSearch(), 'addWeight'), $weight);
  108. }
  109. }
  110. /**
  111. * @param integer $limit
  112. * @param integer $offset
  113. */
  114. protected function buildLimit($limit, $offset)
  115. {
  116. $limit = intval($limit);
  117. $offset = max(0, intval($offset));
  118. if ($limit > 0) {
  119. $this->db->getSearch()->setLimit($limit, $offset);
  120. }
  121. }
  122. /**
  123. * @param array $columns
  124. */
  125. protected function buildOrderBy($columns)
  126. {
  127. $search = $this->db->getSearch();
  128. if (!empty($columns)) {
  129. if (count($columns) === 1) {
  130. foreach ($columns as $name => $direction) {
  131. $search->setSort($name, $direction === SORT_DESC ? false : true);
  132. }
  133. } else {
  134. $sorts = [];
  135. foreach ($columns as $name => $direction) {
  136. $sorts[$name] = $direction === SORT_DESC ? false : true;
  137. }
  138. $search->setMultiSort($sorts);
  139. }
  140. } else {
  141. $search->setSort(null);
  142. }
  143. }
  144. /**
  145. * Parses the condition specification and generates the corresponding xunsearch query string.
  146. * @param string|array $condition the condition specification. Please refer to [[QueryTrait::where()]]
  147. * on how to specify a condition.
  148. * @param array $others used to save other query setting
  149. * @return string the generated query string
  150. */
  151. protected function buildCondition($condition, &$others)
  152. {
  153. if (!is_array($condition)) {
  154. return strval($condition);
  155. } elseif (empty($condition)) {
  156. return '';
  157. }
  158. if (isset($condition[0])) {
  159. // operator format: operator, operand 1, operand 2, ...
  160. $operator = strtoupper($condition[0]);
  161. if (isset($this->conditionBuilders[$operator])) {
  162. $method = $this->conditionBuilders[$operator];
  163. } else {
  164. $method = 'buildSimpleCondition';
  165. }
  166. array_shift($condition);
  167. return $this->$method($operator, $condition, $others);
  168. } else {
  169. // hash format: 'column1' => 'value1', 'column2' => 'value2', ...
  170. return $this->buildHashCondition($condition, $others);
  171. }
  172. }
  173. /**
  174. * Inverts a query string with `NOT` operator.
  175. * @param string $operator the operator to use for connecting the given operands
  176. * @param array $operands the query expressions to connect
  177. * @param array $others used to save other query setting
  178. * @return string the generated query string
  179. * @throws InvalidParamException if wrong number of operands have been given.
  180. */
  181. public function buildNotCondition($operator, $operands, &$others)
  182. {
  183. if (count($operands) !== 1) {
  184. throw new InvalidParamException("Operator '$operator' requires exactly one operand.");
  185. }
  186. $operand = reset($operands);
  187. if (is_array($operand)) {
  188. $operand = $this->buildCondition($operand, $others);
  189. } else {
  190. $operand = trim($operand);
  191. }
  192. if ($operand === '') {
  193. return '';
  194. } else {
  195. return $operator . ' ' . $this->smartBracket($operand);
  196. }
  197. }
  198. /**
  199. * Connects two or more query expressions with the `AND` or `OR` or `WILD` operator.
  200. * @param string $operator the operator to use for connecting the given operands
  201. * @param array $operands the query expressions to connect.
  202. * @param array $others used to save other query setting
  203. * @return string the generated query string
  204. */
  205. protected function buildAndCondition($operator, $operands, &$others)
  206. {
  207. $parts = [];
  208. foreach ($operands as $operand) {
  209. if (is_array($operand)) {
  210. $operand = $this->buildCondition($operand, $others);
  211. }
  212. $operand = trim($operand);
  213. if ($operand !== '') {
  214. $parts[] = $operand;
  215. }
  216. }
  217. if (count($parts) === 0) {
  218. return '';
  219. } elseif (count($parts) === 1) {
  220. return $parts[0];
  221. } else {
  222. for ($i = 0; $i < count($parts); $i++) {
  223. $parts[$i] = $this->smartBracket($parts[$i]);
  224. }
  225. $delimiter = $operator === 'WILD' ? ' ' : ' ' . $operator . ' ';
  226. return implode($delimiter, $parts);
  227. }
  228. }
  229. /**
  230. * Creates a query string with the `IN` operator.
  231. * @param string $operator the operator to use (e.g. `IN` or `NOT IN`)
  232. * @param array $operands the first operand is the column name. If it is an array
  233. * a composite IN condition will be generated.
  234. * The second operand is an array of values that column value should be among.
  235. * @return string the generated query string
  236. */
  237. protected function buildInCondition($operator, $operands)
  238. {
  239. if (!isset($operands[0], $operands[1])) {
  240. throw new InvalidParamException("Operator '$operator' requires two operands.");
  241. }
  242. $parts = [];
  243. list($column, $values) = $operands;
  244. foreach ($values as $value) {
  245. $value = trim($value);
  246. if ($value !== '') {
  247. $parts[] = $column . ':' . $this->smartBracket($value);
  248. }
  249. }
  250. $query = implode(' OR ', $parts);
  251. if (substr($operator, 0, 3) === 'NOT') {
  252. $query = 'NOT ' . (count($parts) > 1 ? '(' . $query . ')' : $query);
  253. }
  254. return $query;
  255. }
  256. /**
  257. * Creates an search value range.
  258. * @param string $operator the operator to use (now only support `BETWEEN`)
  259. * @param array $operands the first operand is the column name. The second and third operands
  260. * describe the interval that column value should be in, null means unlimited.
  261. * @param array $others used to save other query setting
  262. * @return string the generated query string
  263. * @throws InvalidParamException if wrong number of operands have been given.
  264. */
  265. protected function buildBetweenCondition($operator, $operands, &$others)
  266. {
  267. if (!isset($operands[0], $operands[1], $operands[2])) {
  268. throw new InvalidParamException("Operator '$operator' requires three operands.");
  269. }
  270. $others['range'][] = $operands;
  271. }
  272. /**
  273. * Creates a weigth query
  274. * @param string $operator the operator to use (should be `WEIGHT`)
  275. * @param array $operands the first operand is the column name.
  276. * The second operand is the term to adjust weight.
  277. * The 3rd operand is optional float value, it means to weight scale, default to 1.
  278. * @param array $others used to save other query setting
  279. * @throws InvalidParamException
  280. */
  281. protected function buildWeightCondition($operator, $operands, &$others)
  282. {
  283. if (!isset($operands[0], $operands[1])) {
  284. throw new InvalidParamException("Operator '$operator' requires two operands at least.");
  285. }
  286. $others['weight'][] = $operands;
  287. }
  288. protected function buildSimpleCondition($operator, $operands)
  289. {
  290. return $operator . ' ' . implode(' ', $operands);
  291. }
  292. /**
  293. * Creates a condition based on column-value pairs.
  294. * @param array $condition the condition specification.
  295. * @return string the generated query string
  296. */
  297. protected function buildHashCondition($condition)
  298. {
  299. $parts = [];
  300. foreach ($condition as $column => $value) {
  301. if (is_array($value)) {
  302. $pparts = [];
  303. foreach ($value as $v) {
  304. $v = trim($v);
  305. if ($v !== '') {
  306. $pparts[] = $column . ':' . $this->smartBracket($v);
  307. }
  308. }
  309. if (count($pparts) > 1) {
  310. $part = implode(' OR ', $pparts);
  311. if (count($condition) > 1) {
  312. $part = '(' . $part . ')';
  313. }
  314. $parts[] = $part;
  315. } elseif (count($pparts) === 1) {
  316. $parts[] = $pparts[0];
  317. }
  318. } elseif ($value !== null) {
  319. $value = trim($value);
  320. if ($value !== '') {
  321. $parts[] = $column . ':' . $this->smartBracket($value);
  322. }
  323. }
  324. }
  325. return implode(' ', $parts);
  326. }
  327. private function smartBracket($word)
  328. {
  329. if (strpos($word, ' ') === false || substr($word, 0, 4) === 'NOT ') {
  330. return $word;
  331. } else {
  332. return '(' . $word . ')';
  333. }
  334. }
  335. }