7844e74c66996382443a91d76fa8d8d27aba79dc.svn-base 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. <?php namespace Maze\Data\Mongo;
  2. use Maze\Data\Sql;
  3. use Maze\File\Path;
  4. use Maze\Page\Main as Page;
  5. use Maze\Debug\Process as Debug;
  6. use Maze\Config\Load as Config;
  7. class Store
  8. {
  9. /**
  10. * read
  11. *
  12. * @var Maze\Data\Mongo\Connect
  13. */
  14. protected $read;
  15. /**
  16. * update
  17. *
  18. * @var Maze\Data\Mongo\Connect
  19. */
  20. protected $update;
  21. /**
  22. * table
  23. *
  24. * @var string
  25. */
  26. protected $table;
  27. /**
  28. * value
  29. *
  30. * @var array
  31. */
  32. protected $value = array();
  33. /**
  34. * instance
  35. *
  36. * @var string
  37. */
  38. static protected $instance;
  39. /**
  40. * getInstance
  41. *
  42. * @return Maze\Data\Mongo\Store;
  43. */
  44. static public function getInstance($config)
  45. {
  46. if(empty(self::$instance))
  47. {
  48. self::$instance = new self();
  49. }
  50. self::$instance->register($config);
  51. return self::$instance;
  52. }
  53. /**
  54. * __construct
  55. *
  56. * @return mixd
  57. */
  58. public function __construct($config)
  59. {
  60. $this->register($config);
  61. }
  62. /**
  63. * register
  64. *
  65. * @return mixd
  66. */
  67. private function register($config)
  68. {
  69. $this->connect = $this->update = Connect::getInstance($config);
  70. }
  71. /**
  72. * table
  73. *
  74. * @return object
  75. */
  76. public function table($table)
  77. {
  78. if(defined('MAZE_PROJECT') && MAZE_PROJECT != 'default')
  79. {
  80. $table = MAZE_PROJECT . '_' . $table;
  81. }
  82. $this->table = $this->connect->table($table);
  83. return $this;
  84. }
  85. /**
  86. * file
  87. *
  88. * @return mixed
  89. */
  90. private function file()
  91. {
  92. //$file = Path::create(MAZE_PATH . 'data/database/', $this->table . '.php');
  93. $file = Path::create(MAZE_PATH . 'data/database/', $this->table);
  94. return $file;
  95. }
  96. /**
  97. * create
  98. *
  99. * @return mixed
  100. */
  101. public function create($struct)
  102. {
  103. if(isset(Config::$global['base']['create']))
  104. {
  105. return false;
  106. }
  107. $file = $this->file();
  108. if(file_exists($file))
  109. {
  110. return false;
  111. }
  112. $data['time'] = MAZE_TIME;
  113. $data['table'] = $this->table;
  114. $data['create'] = $struct;
  115. $this->log($data);
  116. file_put_contents($file, '<?php return ' . var_export($data, true) . ';');
  117. return true;
  118. }
  119. /**
  120. * create index
  121. *
  122. * @return mixed
  123. */
  124. public function index($index)
  125. {
  126. if(empty($index))
  127. {
  128. return false;
  129. }
  130. $file = $this->file();
  131. if(!file_exists($file))
  132. {
  133. return false;
  134. }
  135. $data = include($file);
  136. if(isset($index['version']))
  137. {
  138. $version = $index['version'];
  139. unset($index['version']);
  140. }
  141. else
  142. {
  143. $version = 1;
  144. }
  145. if(empty($data['index']) || (isset($data['index']) && $data['index'] < $version))
  146. {
  147. $return = $this->table->ensureIndex($index, array('name' => ''));
  148. $this->log($index);
  149. $data['index'] = $version;
  150. file_put_contents($file, '<?php return ' . var_export($data, true) . ';');
  151. }
  152. return true;
  153. }
  154. /**
  155. * alter table
  156. *
  157. * @return mixed
  158. */
  159. public function alter($alter)
  160. {
  161. return true;
  162. }
  163. /**
  164. * insert the default value
  165. *
  166. * @return mixed
  167. */
  168. public function inserts($value)
  169. {
  170. $file = $this->file();
  171. if(!file_exists($file))
  172. {
  173. return false;
  174. }
  175. if(isset($value['col']) && isset($value['value']))
  176. {
  177. $col = explode(',', $value['col']);
  178. $value = explode(',', $value['value']);
  179. foreach($col as $k => $v)
  180. {
  181. $this->value['add'][$v] = $value[$k];
  182. $this->insert();
  183. }
  184. $this->log($value);
  185. $data = include($file);
  186. $data['insert'] = $value;
  187. file_put_contents($file, '<?php return ' . var_export($data, true) . ';');
  188. }
  189. return true;
  190. }
  191. /**
  192. * all
  193. *
  194. * @return array
  195. */
  196. public function all($col)
  197. {
  198. $key = false;
  199. if(strpos($col, '|') !== false)
  200. {
  201. $array = explode('|', $col);
  202. $key = $array[1];
  203. $col = $array[0];
  204. }
  205. $data = $this->select($col, 'find');
  206. $result = array();
  207. if($data)
  208. {
  209. foreach($data as $k => $v)
  210. {
  211. $v['id'] = (array) $v['_id'];
  212. $v['id'] = $v['id']['$id'];
  213. if(isset($v[$key]))
  214. {
  215. if(isset($array[3]) && isset($v[$array[2]]))
  216. {
  217. $result[$v[$key]][$v[$array[2]]] = $v;
  218. }
  219. elseif(isset($array[2]) && isset($v[$array[2]]))
  220. {
  221. $result[$v[$key]] = $v[$array[2]];
  222. }
  223. elseif(isset($array[2]))
  224. {
  225. $result[$v[$key]][] = $v;
  226. }
  227. else
  228. {
  229. $result[$v[$key]] = $v;
  230. }
  231. }
  232. else
  233. {
  234. $result[] = $v;
  235. }
  236. }
  237. }
  238. return $result;
  239. }
  240. /**
  241. * one
  242. *
  243. * @return array
  244. */
  245. public function one($col = '')
  246. {
  247. $data = $this->select($col, 'findOne');
  248. if($data)
  249. {
  250. $data['id'] = (array) $data['_id'];
  251. $data['id'] = $data['id']['$id'];
  252. }
  253. return $data;
  254. }
  255. /**
  256. * count
  257. *
  258. * @return array
  259. */
  260. public function count($col = 'clear')
  261. {
  262. return $this->select($col, 'count');
  263. }
  264. /**
  265. * insert
  266. *
  267. * @return int
  268. */
  269. public function insert()
  270. {
  271. $insert = $this->value['add'];
  272. $return = $this->table->insert($insert);
  273. $this->log($this->value);
  274. $this->value = array();
  275. return $insert['_id'];
  276. }
  277. /**
  278. * update
  279. *
  280. * @return int
  281. */
  282. public function update()
  283. {
  284. $method = '$set';
  285. $return = $this->table->update($this->value['where'], array($method => $this->value['set']));
  286. $this->log($this->value);
  287. $this->value = array();
  288. return $return;
  289. }
  290. /**
  291. * delete
  292. *
  293. * @return int
  294. */
  295. public function delete()
  296. {
  297. $this->update('$unset');
  298. return $result;
  299. }
  300. /**
  301. * select
  302. *
  303. * @return array
  304. */
  305. private function select($col = '', $method = 'find')
  306. {
  307. if(isset($this->value['where']))
  308. {
  309. //print_r($this->value['where']);
  310. $return = $cursor = $this->table->$method($this->value['where']);
  311. }
  312. else
  313. {
  314. $return = $cursor = $this->table->$method();
  315. }
  316. if($method != 'count')
  317. {
  318. if(isset($this->value['order']))
  319. {
  320. $return = $cursor->sort($this->value['order']);
  321. }
  322. if(isset($this->value['limit']))
  323. {
  324. foreach($this->value['limit'] as $k => $v)
  325. {
  326. $return = $cursor->limit($k)->skip($v);
  327. }
  328. }
  329. if($col && $col != '*' && $col != 'clear')
  330. {
  331. if(is_string($col))
  332. {
  333. $temp = explode(',', $col);
  334. $col = array();
  335. foreach($temp as $k => $v)
  336. {
  337. $col[$v] = true;
  338. }
  339. }
  340. $return = $cursor->fields($col);
  341. }
  342. }
  343. $this->log($this->value);
  344. if($col != 'clear')
  345. {
  346. $this->value = array();
  347. }
  348. return $return;
  349. }
  350. /**
  351. * page
  352. *
  353. * @return object
  354. */
  355. public function page($num, $config = array())
  356. {
  357. empty($config[0]) && $config[0] = 'list';
  358. empty($config[1]) && $config[1] = 'current';
  359. empty($config[2]) && $config[2] = '';
  360. $page = Page::getInstance($config[1]);
  361. $page->template($config[0]);
  362. $page->link($config[2]);
  363. $page->total($this->count());
  364. $this->limit($num, $page->offset($num));
  365. return $this;
  366. }
  367. /**
  368. * __call
  369. *
  370. * @return object
  371. */
  372. public function __call($method, $param)
  373. {
  374. if(is_array($param[0]))
  375. {
  376. foreach($param[0] as $k => $v)
  377. {
  378. if($method == 'order')
  379. {
  380. $this->call($method, array($k, $v));
  381. }
  382. else
  383. {
  384. $this->call($method, $v);
  385. }
  386. }
  387. }
  388. else
  389. {
  390. $this->call($method, $param);
  391. }
  392. return $this;
  393. }
  394. /**
  395. * call
  396. *
  397. * @return mixd
  398. */
  399. private function call($method, $param)
  400. {
  401. if(is_array($param) && isset($param[0]))
  402. {
  403. if(is_string($param[0]) && strpos($param[0], ','))
  404. {
  405. $temp = explode(',', str_replace('`', '', $param[0]));
  406. $index = count($temp)-1;
  407. foreach($temp as $k => $v)
  408. {
  409. if($k == $index)
  410. {
  411. $param[0] = $v;
  412. }
  413. else
  414. {
  415. $array = explode(' ', $v);
  416. $this->call($method, $array);
  417. }
  418. }
  419. }
  420. if($param[0] == 'id')
  421. {
  422. $param[0] = '_id';
  423. }
  424. $func = 'convert_' . $method;
  425. if(method_exists($this, $func))
  426. {
  427. $this->$func($param);
  428. }
  429. $this->value[$method][$param[0]] = $param[1];
  430. }
  431. else
  432. {
  433. $this->value[$method] = $param;
  434. }
  435. }
  436. /**
  437. * convert_order
  438. *
  439. * @return mixed
  440. */
  441. private function convert_order(&$param)
  442. {
  443. switch($param[1])
  444. {
  445. case 'desc':
  446. $param[1] = -1;
  447. break;
  448. case 'asc':
  449. $param[1] = 1;
  450. break;
  451. }
  452. }
  453. /**
  454. * convert_group
  455. *
  456. * @return mixed
  457. */
  458. private function convert_group(&$param)
  459. {
  460. print_r($param);die;
  461. }
  462. /**
  463. * convert_where
  464. *
  465. * @return mixed
  466. */
  467. private function convert_where(&$param)
  468. {
  469. if(isset($param[2]))
  470. {
  471. $state = true;
  472. switch($param[2])
  473. {
  474. case 'like':
  475. # 模糊查询
  476. if(strpos($param[1], '%') !== false)
  477. {
  478. $param[1] = str_replace('%', '(.*?)', $param[1]);
  479. $param[1] = new \MongoRegex('/'.$param[1].'/i');
  480. }
  481. else
  482. {
  483. $param[1] = new \MongoRegex('/(.*?)'.$param[1].'(.*?)/i');
  484. }
  485. $state = false;
  486. break;
  487. case 'in':
  488. case 'nin':
  489. # in查询
  490. $param[1] = explode(',', $param[1]);
  491. if($param[0] == '_id')
  492. {
  493. foreach($param[1] as $k => $v)
  494. {
  495. $param[1][$k] = new \MongoId($v);
  496. }
  497. }
  498. $param[2] = '$' . $param[2];
  499. break;
  500. case '>':
  501. $param[2] = '$gt';
  502. break;
  503. case '>=':
  504. $param[2] = '$gte';
  505. break;
  506. case '<':
  507. $param[2] = '$lt';
  508. break;
  509. case '<=':
  510. $param[2] = '$lte';
  511. break;
  512. case '!=':
  513. $param[2] = '$ne';
  514. break;
  515. case '%':
  516. $param[2] = '$mod';
  517. break;
  518. case 'bt':
  519. $state = false;
  520. $param[1] = array('gt' => $param[1][0], 'lt' => $param[1][1]);
  521. break;
  522. case 'bte':
  523. $state = false;
  524. $param[1] = array('gte' => $param[1][0], 'lte' => $param[1][1]);
  525. break;
  526. default:
  527. $param[2] = '$' . $param[2];
  528. break;
  529. }
  530. if($state == true)
  531. {
  532. $param[1] = array($param[2] => $param[1]);
  533. }
  534. }
  535. if($param[0] == '_id' && is_string($param[1]))
  536. {
  537. $param[1] = new \MongoId($param[1]);
  538. }
  539. }
  540. /**
  541. * log
  542. *
  543. * @return log
  544. */
  545. private function log($value)
  546. {
  547. Debug::log(array('value' => $value));
  548. }
  549. }