PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 8ms app.codeStats 0ms

/Kernel/ThinkPHP/Mode/Thin/Model.class.php

https://github.com/liujinsong668/epptime
PHP | 295 lines | 124 code | 14 blank | 157 comment | 16 complexity | a8d95936f9cddfda8dd897a5532a0902 MD5 | raw file
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2010 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. // $Id$
  12. /**
  13. +------------------------------------------------------------------------------
  14. * ThinkPHP 简洁模式Model模型类
  15. * 只支持原生SQL操作 支持多数据库连接和切换
  16. +------------------------------------------------------------------------------
  17. */
  18. class Model extends Think
  19. {
  20. // 数据库连接对象列表
  21. private $_db = array();
  22. // 当前数据库操作对象
  23. protected $db = null;
  24. // 数据表前缀
  25. protected $tablePrefix = '';
  26. // 模型名称
  27. protected $name = '';
  28. // 数据库名称
  29. protected $dbName = '';
  30. // 数据表名(不包含表前缀)
  31. protected $tableName = '';
  32. // 实际数据表名(包含表前缀)
  33. protected $trueTableName ='';
  34. // 最近错误信息
  35. protected $error = '';
  36. /**
  37. +----------------------------------------------------------
  38. * 架构函数
  39. * 取得DB类的实例对象
  40. +----------------------------------------------------------
  41. * @param string $name 模型名称
  42. +----------------------------------------------------------
  43. * @access public
  44. +----------------------------------------------------------
  45. */
  46. public function __construct($name='')
  47. {
  48. // 模型初始化
  49. $this->_initialize();
  50. // 获取模型名称
  51. if(!empty($name)) {
  52. $this->name = $name;
  53. }elseif(empty($this->name)){
  54. $this->name = $this->getModelName();
  55. }
  56. // 数据库初始化操作
  57. import("Db");
  58. // 获取数据库操作对象
  59. $this->db = Db::getInstance(empty($this->connection)?'':$this->connection);
  60. // 设置表前缀
  61. $this->tablePrefix = $this->tablePrefix?$this->tablePrefix:C('DB_PREFIX');
  62. // 设置默认的数据库连接
  63. $this->_db[0] = $this->db;
  64. }
  65. // 回调方法 初始化模型
  66. protected function _initialize() {}
  67. /**
  68. +----------------------------------------------------------
  69. * SQL查询
  70. +----------------------------------------------------------
  71. * @access public
  72. +----------------------------------------------------------
  73. * @param mixed $sql SQL指令
  74. +----------------------------------------------------------
  75. * @return array
  76. +----------------------------------------------------------
  77. */
  78. public function query($sql)
  79. {
  80. if(is_array($sql)) {
  81. return $this->patchQuery($sql);
  82. }
  83. if(!empty($sql)) {
  84. if(strpos($sql,'__TABLE__')) {
  85. $sql = str_replace('__TABLE__',$this->getTableName(),$sql);
  86. }
  87. return $this->db->query($sql);
  88. }else{
  89. return false;
  90. }
  91. }
  92. /**
  93. +----------------------------------------------------------
  94. * 执行SQL语句
  95. +----------------------------------------------------------
  96. * @access public
  97. +----------------------------------------------------------
  98. * @param string $sql SQL指令
  99. +----------------------------------------------------------
  100. * @return false | integer
  101. +----------------------------------------------------------
  102. */
  103. public function execute($sql='')
  104. {
  105. if(!empty($sql)) {
  106. if(strpos($sql,'__TABLE__')) {
  107. $sql = str_replace('__TABLE__',$this->getTableName(),$sql);
  108. }
  109. $result = $this->db->execute($sql);
  110. return $result;
  111. }else {
  112. return false;
  113. }
  114. }
  115. /**
  116. +----------------------------------------------------------
  117. * 得到当前的数据对象名称
  118. +----------------------------------------------------------
  119. * @access public
  120. +----------------------------------------------------------
  121. * @return string
  122. +----------------------------------------------------------
  123. */
  124. public function getModelName()
  125. {
  126. if(empty($this->name)) {
  127. $this->name = substr(get_class($this),0,-5);
  128. }
  129. return $this->name;
  130. }
  131. /**
  132. +----------------------------------------------------------
  133. * 得到完整的数据表名
  134. +----------------------------------------------------------
  135. * @access public
  136. +----------------------------------------------------------
  137. * @return string
  138. +----------------------------------------------------------
  139. */
  140. public function getTableName()
  141. {
  142. if(empty($this->trueTableName)) {
  143. $tableName = !empty($this->tablePrefix) ? $this->tablePrefix : '';
  144. if(!empty($this->tableName)) {
  145. $tableName .= $this->tableName;
  146. }else{
  147. $tableName .= parse_name($this->name);
  148. }
  149. if(!empty($this->dbName)) {
  150. $tableName = $this->dbName.'.'.$tableName;
  151. }
  152. $this->trueTableName = strtolower($tableName);
  153. }
  154. return $this->trueTableName;
  155. }
  156. /**
  157. +----------------------------------------------------------
  158. * 启动事务
  159. +----------------------------------------------------------
  160. * @access public
  161. +----------------------------------------------------------
  162. * @return void
  163. +----------------------------------------------------------
  164. */
  165. public function startTrans()
  166. {
  167. $this->commit();
  168. $this->db->startTrans();
  169. return ;
  170. }
  171. /**
  172. +----------------------------------------------------------
  173. * 提交事务
  174. +----------------------------------------------------------
  175. * @access public
  176. +----------------------------------------------------------
  177. * @return boolean
  178. +----------------------------------------------------------
  179. */
  180. public function commit()
  181. {
  182. return $this->db->commit();
  183. }
  184. /**
  185. +----------------------------------------------------------
  186. * 事务回滚
  187. +----------------------------------------------------------
  188. * @access public
  189. +----------------------------------------------------------
  190. * @return boolean
  191. +----------------------------------------------------------
  192. */
  193. public function rollback()
  194. {
  195. return $this->db->rollback();
  196. }
  197. /**
  198. +----------------------------------------------------------
  199. * 增加数据库连接
  200. +----------------------------------------------------------
  201. * @access public
  202. +----------------------------------------------------------
  203. * @param mixed $config 数据库连接信息
  204. * 支持批量添加 例如 array(1=>$config1,2=>$config2)
  205. * @param mixed $linkNum 创建的连接序号
  206. +----------------------------------------------------------
  207. * @return boolean
  208. +----------------------------------------------------------
  209. */
  210. public function addConnect($config,$linkNum=NULL) {
  211. if(isset($this->_db[$linkNum]))
  212. return false;
  213. if(NULL === $linkNum && is_array($config)) {
  214. // 支持批量增加数据库连接
  215. foreach ($config as $key=>$val)
  216. $this->_db[$key] = Db::getInstance($val);
  217. return true;
  218. }
  219. // 创建一个新的实例
  220. $this->_db[$linkNum] = Db::getInstance($config);
  221. return true;
  222. }
  223. /**
  224. +----------------------------------------------------------
  225. * 删除数据库连接
  226. +----------------------------------------------------------
  227. * @access public
  228. +----------------------------------------------------------
  229. * @param integer $linkNum 创建的连接序号
  230. +----------------------------------------------------------
  231. * @return boolean
  232. +----------------------------------------------------------
  233. */
  234. public function delConnect($linkNum) {
  235. if(isset($this->_db[$linkNum])) {
  236. $this->_db[$linkNum]->close();
  237. unset($this->_db[$linkNum]);
  238. return true;
  239. }
  240. return false;
  241. }
  242. /**
  243. +----------------------------------------------------------
  244. * 关闭数据库连接
  245. +----------------------------------------------------------
  246. * @access public
  247. +----------------------------------------------------------
  248. * @param integer $linkNum 创建的连接序号
  249. +----------------------------------------------------------
  250. * @return boolean
  251. +----------------------------------------------------------
  252. */
  253. public function closeConnect($linkNum) {
  254. if(isset($this->_db[$linkNum])) {
  255. $this->_db[$linkNum]->close();
  256. return true;
  257. }
  258. return false;
  259. }
  260. /**
  261. +----------------------------------------------------------
  262. * 切换数据库连接
  263. +----------------------------------------------------------
  264. * @access public
  265. +----------------------------------------------------------
  266. * @param integer $linkNum 创建的连接序号
  267. +----------------------------------------------------------
  268. * @return boolean
  269. +----------------------------------------------------------
  270. */
  271. public function switchConnect($linkNum) {
  272. if(isset($this->_db[$linkNum])) {
  273. // 在不同实例直接切换
  274. $this->db = $this->_db[$linkNum];
  275. return true;
  276. }else{
  277. return false;
  278. }
  279. }
  280. };
  281. ?>