6d6c284cce81f4c58118804de57825479c472643.svn-base 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. <?php namespace Maze\Data\Mysql;
  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\Mysql\Connect
  13. */
  14. protected $read;
  15. /**
  16. * update
  17. *
  18. * @var Maze\Data\Mysql\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\Mysql\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. $this->sql = Sql::getInstance();
  62. }
  63. /**
  64. * register
  65. *
  66. * @return mixd
  67. */
  68. private function register($config)
  69. {
  70. # read update
  71. if(strpos($config['host'], ',') !== false)
  72. {
  73. $host = explode(',', $config['host']);
  74. $config['host'] = $host[0];
  75. $this->read = Connect::getInstance($config);
  76. $config['host'] = $host[1];
  77. $this->update = Connect::getInstance($config);
  78. }
  79. else
  80. {
  81. $this->read = $this->update = Connect::getInstance($config);
  82. }
  83. }
  84. /**
  85. * table
  86. *
  87. * @return object
  88. */
  89. public function table($table)
  90. {
  91. if(defined('MAZE_PROJECT') && MAZE_PROJECT != 'default')
  92. {
  93. $table = MAZE_PROJECT . '_' . $table;
  94. }
  95. $this->table = $table;
  96. return $this;
  97. }
  98. /**
  99. * file
  100. *
  101. * @return mixed
  102. */
  103. private function file()
  104. {
  105. //$file = Path::create(MAZE_PATH . 'data/database/', $this->table . '.php');
  106. $file = Path::create(MAZE_PATH . 'data/database/', $this->table);
  107. return $file;
  108. }
  109. /**
  110. * create
  111. *
  112. * @return mixed
  113. */
  114. public function create($struct)
  115. {
  116. if(isset(Config::$global['base']['create']))
  117. {
  118. return false;
  119. }
  120. $file = $this->file();
  121. if(file_exists($file))
  122. {
  123. return include($file);
  124. }
  125. $sql = $this->sql->create($this->table, $struct);
  126. $this->update->query($sql);
  127. $this->log($sql, 'create');
  128. $data['time'] = MAZE_TIME;
  129. $data['table'] = $this->table;
  130. $data['create'] = $sql;
  131. $data['struct'] = array_flip(array_keys($struct));
  132. file_put_contents($file, '<?php return ' . var_export($data, true) . ';');
  133. return true;
  134. }
  135. /**
  136. * create index
  137. *
  138. * @return mixed
  139. */
  140. public function index($index)
  141. {
  142. if(empty($index))
  143. {
  144. return false;
  145. }
  146. $file = $this->file();
  147. if(!file_exists($file))
  148. {
  149. return false;
  150. }
  151. $data = include($file);
  152. if(isset($index['version']))
  153. {
  154. $version = $index['version'];
  155. unset($index['version']);
  156. }
  157. else
  158. {
  159. $version = 1;
  160. }
  161. if(empty($data['index']) || (isset($data['index']) && $data['index'] != $version))
  162. {
  163. $sql = $this->sql->showIndex($this->table);
  164. $handle = $this->update->query($sql);
  165. $info = $handle->fetchAll();
  166. if($info)
  167. {
  168. foreach($info as $k => $v)
  169. {
  170. if($v['Key_name'] != 'PRIMARY')
  171. {
  172. $sql = $this->sql->dropIndex($this->table, $v['Key_name']);
  173. $this->update->query($sql);
  174. }
  175. }
  176. }
  177. $sql = $this->sql->index($this->table, $index);
  178. $this->update->query($sql);
  179. $this->log($sql, 'index');
  180. $data['index'] = $version;
  181. file_put_contents($file, '<?php return ' . var_export($data, true) . ';');
  182. }
  183. return true;
  184. }
  185. /**
  186. * alter table
  187. *
  188. * @return mixed
  189. */
  190. public function alter($alter, $struct = array())
  191. {
  192. if(empty($alter))
  193. {
  194. return false;
  195. }
  196. $file = $this->file();
  197. if(!file_exists($file))
  198. {
  199. return false;
  200. }
  201. $data = include($file);
  202. if($struct)
  203. {
  204. $sql = $this->sql->alter($this->table, $alter);
  205. $this->update->query($sql);
  206. $this->log($sql, 'alter');
  207. $data['struct'] = array_flip(array_keys($struct));
  208. file_put_contents($file, '<?php return ' . var_export($data, true) . ';');
  209. }
  210. else
  211. {
  212. if(isset($alter['version']))
  213. {
  214. $version = $alter['version'];
  215. }
  216. else
  217. {
  218. $version = 1;
  219. }
  220. if(isset($alter[$version]) && (empty($data['alter']) || (isset($data['alter']) && $data['alter'] != $version)))
  221. {
  222. $sql = $this->sql->alter($this->table, $alter[$version]);
  223. $this->update->query($sql);
  224. $this->log($sql, 'alter');
  225. $data['alter'] = $version;
  226. file_put_contents($file, '<?php return ' . var_export($data, true) . ';');
  227. }
  228. }
  229. return true;
  230. }
  231. /**
  232. * insert the default value
  233. *
  234. * @return mixed
  235. */
  236. public function inserts($value)
  237. {
  238. $file = $this->file();
  239. if(!file_exists($file))
  240. {
  241. return false;
  242. }
  243. if(isset($value['col']) && isset($value['value']))
  244. {
  245. $sql = $this->sql->inserts($this->table, $value['col'], $value['value']);
  246. $this->update->query($sql);
  247. $this->log($sql, 'inserts');
  248. $data = include($file);
  249. $data['insert'] = $sql;
  250. file_put_contents($file, '<?php return ' . var_export($data, true) . ';');
  251. }
  252. return true;
  253. }
  254. /**
  255. * all
  256. *
  257. * @return array
  258. */
  259. public function all($col)
  260. {
  261. $key = false;
  262. if(strpos($col, '|') !== false)
  263. {
  264. $array = explode('|', $col);
  265. $key = $array[1];
  266. $col = $array[0];
  267. }
  268. $data = $this->select($col, 'fetchAll');
  269. if($data && $key)
  270. {
  271. $result = array();
  272. foreach($data as $k => $v)
  273. {
  274. if(isset($v[$key]))
  275. {
  276. if(isset($array[3]) && isset($v[$array[2]]))
  277. {
  278. $result[$v[$key]][$v[$array[2]]] = $v;
  279. }
  280. elseif(isset($array[2]) && isset($v[$array[2]]))
  281. {
  282. $result[$v[$key]] = $v[$array[2]];
  283. }
  284. elseif(isset($array[2]))
  285. {
  286. $result[$v[$key]][] = $v;
  287. }
  288. else
  289. {
  290. $result[$v[$key]] = $v;
  291. }
  292. }
  293. }
  294. return $result;
  295. }
  296. return $data;
  297. }
  298. /**
  299. * one
  300. *
  301. * @return array
  302. */
  303. public function one($col)
  304. {
  305. return $this->select($col);
  306. }
  307. /**
  308. * count
  309. *
  310. * @return array
  311. */
  312. public function count($col = 'clear')
  313. {
  314. return $this->select($col, 'fetchColumn', 'count');
  315. }
  316. /**
  317. * insert
  318. *
  319. * @return int
  320. */
  321. public function insert()
  322. {
  323. $sql = $this->sql->insert($this->table);
  324. $handle = $this->update->prepare($sql);
  325. $handle->execute($this->value);
  326. $id = $this->update->id();
  327. $this->log($sql, $this->value);
  328. $this->value = array();
  329. return $id;
  330. }
  331. /**
  332. * update
  333. *
  334. * @return int
  335. */
  336. public function update()
  337. {
  338. $sql = $this->sql->update($this->table);
  339. $result = false;
  340. if($sql)
  341. {
  342. $handle = $this->update->prepare($sql);
  343. $handle->execute($this->value);
  344. $result = $handle->rowCount();
  345. $this->log($sql, $this->value);
  346. }
  347. $this->value = array();
  348. return $result;
  349. }
  350. /**
  351. * delete
  352. *
  353. * @return int
  354. */
  355. public function delete()
  356. {
  357. $sql = $this->sql->delete($this->table);
  358. $result = false;
  359. if($sql)
  360. {
  361. $handle = $this->update->prepare($sql);
  362. $handle->execute($this->value);
  363. $result = $handle->rowCount();
  364. $this->log($sql, $this->value);
  365. }
  366. $this->value = array();
  367. return $result;
  368. }
  369. /**
  370. * select
  371. *
  372. * @return array
  373. */
  374. private function select($col = '', $method = 'fetch', $type = 'select')
  375. {
  376. $sql = $this->sql->{$type}($this->table, $col);
  377. if($type == 'count' && strpos($sql, 'group by `'))
  378. {
  379. $method = 'fetchAll';
  380. }
  381. $handle = $this->read->prepare($sql);
  382. $handle->execute($this->value);
  383. $data = $handle->$method();
  384. $this->log($sql, $this->value);
  385. if($col != 'clear')
  386. {
  387. $this->value = array();
  388. }
  389. return $data;
  390. }
  391. /**
  392. * page
  393. *
  394. * @return object
  395. */
  396. public function page($num, $config = array())
  397. {
  398. $this->reset('limit');
  399. empty($config[0]) && $config[0] = 'list';
  400. empty($config[1]) && $config[1] = 'current';
  401. empty($config[2]) && $config[2] = '';
  402. $page = Page::getInstance($config[1]);
  403. $page->template($config[0]);
  404. $page->link($config[2]);
  405. $page->total($this->count());
  406. $this->limit($num, $page->offset($num));
  407. return $this;
  408. }
  409. /**
  410. * __call
  411. *
  412. * @return object
  413. */
  414. public function __call($method, $param)
  415. {
  416. if(is_array($param[0]) && $method != 'order')
  417. {
  418. foreach($param[0] as $k => $v)
  419. {
  420. $this->call($method, $v);
  421. }
  422. }
  423. else
  424. {
  425. $this->call($method, $param);
  426. }
  427. return $this;
  428. }
  429. /**
  430. * call
  431. *
  432. * @return mixd
  433. */
  434. private function call($method, $param)
  435. {
  436. if($method == 'where' || $method == 'set' || $method == 'add')
  437. {
  438. # 特殊处理in
  439. if(isset($param[2]) && $param[2] == 'in')
  440. {
  441. if(is_string($param[1]))
  442. {
  443. $param[1] = explode(',', $param[1]);
  444. }
  445. $prefix = 'in_';
  446. foreach($param[1] as $k => $v)
  447. {
  448. $k = ':' . $prefix . $k;
  449. $key[] = $k;
  450. $this->value[$k] = $v;
  451. }
  452. $param[1] = '(' . implode(',', $key) . ')';
  453. }
  454. else
  455. {
  456. $key = ':' . count($this->value);
  457. $this->value[$key] = $param[1];
  458. $param[1] = $key;
  459. }
  460. }
  461. $this->sql->$method($param);
  462. }
  463. /**
  464. * log
  465. *
  466. * @return log
  467. */
  468. private function log($sql, $value)
  469. {
  470. Debug::log(array('sql' => $sql, 'value' => $value));
  471. }
  472. }