1 <?php
2 /**
3 * Cross - a micro PHP 5 framework
4 *
5 * @link http://www.crossphp.com
6 * @license MIT License
7 */
8 namespace Cross\Module;
9
10 use Cross\Exception\CoreException;
11 use Cross\MVC\Module;
12
13 /**
14 * 提供简单的CRUD功能
15 *
16 * @author wonli <wonli@live.com>
17 * Class MysqlModule
18 * @package Cross\Module
19 */
20 class SQLModule extends Module
21 {
22 /**
23 * 要操作的数据表名
24 *
25 * @var string
26 */
27 protected $t;
28
29 /**
30 * @var array()
31 */
32 protected static $instance;
33
34 /**
35 * 是否自动添加表前缀
36 *
37 * @var bool
38 */
39 protected $auto_prefix = true;
40
41 /**
42 * SQLModule constructor.
43 *
44 * @param string $params
45 * @throws CoreException
46 */
47 function __construct($params = '')
48 {
49 parent::__construct($params);
50 if ($this->auto_prefix) {
51 $this->t = $this->getPrefix($this->t);
52 }
53 }
54
55 /**
56 * 实例化当前类的子类
57 *
58 * @param string $args
59 * @return static::get_called_class()
60 */
61 static public function init($args = '')
62 {
63 $called_class_name = get_called_class();
64 if (empty(self::$instance[$called_class_name])) {
65 self::$instance[$called_class_name] = new $called_class_name($args);
66 }
67
68 return self::$instance[$called_class_name];
69 }
70
71 /**
72 * 设置表名
73 *
74 * @param string $table_name
75 * @param bool $add_prefix
76 * @return static ::get_called_class()
77 */
78 public function setTable($table_name, $add_prefix = true)
79 {
80 $this->t = $table_name;
81 if ($add_prefix) {
82 $this->t = $this->getPrefix($table_name);
83 }
84
85 return $this;
86 }
87
88 /**
89 * @see PDOSqlDriver::get()
90 *
91 * @param $condition
92 * @param string $fields
93 * @return mixed
94 * @throws CoreException
95 */
96 public function get($condition, $fields = '*')
97 {
98 return $this->link->get($this->t, $fields, $condition);
99 }
100
101 /**
102 * @see PDOSqlDriver::getAll()
103 *
104 * @param null $where
105 * @param int $order
106 * @param int $group_by
107 * @param int $limit
108 * @param string $fields
109 * @return array
110 * @throws CoreException
111 */
112 public function getAll($where = null, $order = 1, $group_by = 1, $limit = 0, $fields = '*')
113 {
114 return $this->link->getAll($this->t, $fields, $where, $order, $group_by, $limit);
115 }
116
117 /**
118 * @see PDOSqlDriver::find()
119 *
120 * @param $condition
121 * @param array $page
122 * @param int $order
123 * @param int $group_by
124 * @param string $fields
125 * @return array|mixed
126 * @throws CoreException
127 */
128 public function find($condition, array & $page = array(), $order = 1, $group_by = 1, $fields = '*')
129 {
130 return $this->link->find($this->t, $fields, $condition, $order, $page, $group_by);
131 }
132
133 /**
134 * @see PDOSqlDriver::add()
135 *
136 * @param $data
137 * @param bool $multi
138 * @param array $insert_data
139 * @param bool $openTA
140 * @return array|bool|mixed
141 * @throws CoreException
142 */
143 public function add($data, $multi = false, & $insert_data = array(), $openTA = false)
144 {
145 return $this->link->add($this->t, $data, $multi, $insert_data, $openTA);
146 }
147
148 /**
149 * @see PDOSqlDriver::update()
150 *
151 * @param $data
152 * @param $where
153 * @return $this|array|string
154 * @throws CoreException
155 */
156 public function update($data, $where)
157 {
158 return $this->link->update($this->t, $data, $where);
159 }
160
161 /**
162 * @see PDOSqlDriver::del()
163 *
164 * @param $where
165 * @param bool $multi
166 * @param bool $openTA
167 * @return bool|mixed
168 * @throws CoreException
169 */
170 public function del($where, $multi = false, $openTA = false)
171 {
172 return $this->link->del($this->t, $where, $multi, $openTA);
173 }
174 }
175