Database.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * xunsearch Connection 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\base\BaseObject;
  13. /**
  14. * xunsearch Database is used to wrapper XS class
  15. *
  16. * @property-read \XSTokenizerScws $scws
  17. * @property-read \XSIndex $index
  18. * @property-read \XSSearch $search
  19. * @property-read QueryBuilder $queryBuilder
  20. *
  21. * @author xjflyttp <xjflyttp@gmail.com>
  22. * @author hightman <hightman@twomice.net>
  23. * @since 1.4.9
  24. */
  25. class Database extends BaseObject
  26. {
  27. public $iniFile;
  28. public $charset;
  29. /**
  30. * @var \XS
  31. */
  32. public $xs;
  33. /**
  34. * @var \XSTokenizerScws
  35. */
  36. private $_scws;
  37. /**
  38. * @var QueryBuilder
  39. */
  40. private $_builder;
  41. /**
  42. * Initializes, create XS object
  43. */
  44. public function init()
  45. {
  46. $this->xs = new \XS($this->iniFile);
  47. if ($this->charset !== null) {
  48. $this->xs->setDefaultCharset($this->charset);
  49. }
  50. }
  51. /**
  52. * @return string database name (ini file)
  53. */
  54. public function getName()
  55. {
  56. return pathinfo($this->iniFile, PATHINFO_FILENAME);
  57. }
  58. /**
  59. * @return \XSTokenizerScws get scws tokenizer
  60. * @throws \XSException
  61. */
  62. public function getScws()
  63. {
  64. if ($this->_scws === null) {
  65. $this->_scws = new \XSTokenizerScws;
  66. }
  67. return $this->_scws;
  68. }
  69. /**
  70. * @return \XSIndex get xunsearch index object
  71. */
  72. public function getIndex()
  73. {
  74. return $this->xs->index;
  75. }
  76. /**
  77. * @return \XSSearch get xunsearch search object
  78. */
  79. public function getSearch()
  80. {
  81. return $this->xs->search;
  82. }
  83. /**
  84. * @return QueryBuilder
  85. */
  86. public function getQueryBuilder()
  87. {
  88. if ($this->_builder === null) {
  89. $this->_builder = new QueryBuilder($this);
  90. }
  91. return $this->_builder;
  92. }
  93. /**
  94. * @return \XSDocument
  95. */
  96. public function createDoc()
  97. {
  98. return new \XSDocument($this->charset);
  99. }
  100. /**
  101. * Quickly add a new document (without checking key conflicts)
  102. * @param mixed $data \XSDocument object or data array to be added
  103. */
  104. public function add($data)
  105. {
  106. $this->update($data, true);
  107. }
  108. /**
  109. * @param mixed $data \XSDocument object or data array to be updated
  110. * @param boolean $add whether to add directly, default to false
  111. */
  112. public function update($data, $add = false)
  113. {
  114. if ($data instanceof \XSDocument) {
  115. $this->xs->index->update($data, $add);
  116. } else {
  117. $doc = new \XSDocument($data, $this->charset);
  118. $this->xs->index->update($doc, $add);
  119. }
  120. }
  121. /**
  122. * @return array
  123. */
  124. public function __sleep()
  125. {
  126. return array('iniFile', 'charset');
  127. }
  128. public function __wakeup()
  129. {
  130. $this->init();
  131. }
  132. /**
  133. * Forward all methods to \XS, \XSIndex, \XSSearch
  134. * @param string $name
  135. * @param array $parameters
  136. * @return mixed Database or actual return value
  137. */
  138. public function __call($name, $parameters)
  139. {
  140. // check methods of xs
  141. if (method_exists($this->xs, $name)) {
  142. return call_user_func_array(array($this->xs, $name), $parameters);
  143. }
  144. // check methods of index object
  145. if (method_exists('\XSIndex', $name)) {
  146. $ret = call_user_func_array(array($this->xs->index, $name), $parameters);
  147. if ($ret === $this->xs->index) {
  148. return $this;
  149. }
  150. return $ret;
  151. }
  152. // check methods of search object
  153. if (method_exists('\XSSearch', $name)) {
  154. $ret = call_user_func_array(array($this->xs->search, $name), $parameters);
  155. if ($ret === $this->xs->search) {
  156. return $this;
  157. }
  158. return $ret;
  159. }
  160. return parent::__call($name, $parameters);
  161. }
  162. }