-
Notifications
You must be signed in to change notification settings - Fork 0
/
View.php
153 lines (134 loc) · 4.21 KB
/
View.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/**
* Simple Database View Class File
*
* PHP versions 5
*
* @author Satoshi Nishimura <nishim314@gmail.com>
* @copyright 2009-2011 Satoshi Nishimura
*/
require_once 'Vo.php';
require_once 'Driver.php';
/**
* Simple Database View Class
*
* @author Satoshi Nishimura <nishim314@gmail.com>
*/
class YokazeDb_View
{
protected $db;
protected $sqlFile;
private $limit;
private $offset;
public function __construct(YokazeDb_Driver $db, $sqlFile)
{
$this->db = $db;
$this->sqlFile = $sqlFile;
}
public function setLimit($limit)
{
$this->limit = $limit;
return $this;
}
public function setOffset($offset)
{
$this->offset = $offset;
return $this;
}
/**
* Execute query and return pdo statement.
*
* @param array $params parameter of prepared statement.
* @param mixed $replace arguments of sprintf for replacement sql string.
* @return PDOStatement
*/
public function prepareStmt($params = null, $replace = null)
{
$file = str_replace('_', '/', $this->sqlFile) . '.sql';
$sql = file_get_contents($file, true);
if ($replace !== null){
$replace = (array)$replace;
array_unshift($replace, $sql);
$sql = call_user_func_array('sprintf', $replace);
if (!$sql){
trigger_error('sprintf');
return false;
}
}
if (is_numeric($this->limit))
$sql .= ' limit ' . $this->limit . ' ';
if (is_numeric($this->offset))
$sql .= ' offset ' . $this->offset . ' ';
if ($params !== null)
$stmt = $this->db->query($sql, $params);
else
$stmt = $this->db->query($sql);
if (!$stmt){
trigger_error('query error! sql="' . $sql . '"', E_USER_WARNING);
return false;
}
if ($stmt->errorCode() != '00000'){
$errInfo = $stmt->errorInfo();
trigger_error('['.$errInfo[0].':'.$errInfo[1].']'.$errInfo[2], E_USER_WARNING);
return false;
}
return $stmt;
}
public function createVo()
{
$sqlFile = str_replace('/', '_', $this->sqlFile);
$className = 'YokazeDb_Vo_' . implode('', array_map('ucfirst', explode('_', $sqlFile)));
if (!class_exists($className, false))
eval("class $className implements YokazeDb_Vo{}");
$className = $className;
return new $className();
}
public function bind(PDOStatement $stmt, YokazeDb_Vo $vo)
{
$columnCount = $stmt->columnCount();
$columnTypes = array();
$columnNames = array();
$VoNames = array();
for ($i = 0; $i < $columnCount; $i++){
$meta = $stmt->getColumnMeta($i);
if (!$meta)
break;
//if (isset($meta['pdo_type']))
$columnTypes[$i] = $meta['pdo_type'];
$columnNames[$i] = $meta['name'];
$name = implode('', array_map('ucfirst', explode('_', $meta['name'])));
$name[0] = strtolower($name[0]);
$voNames[$i] = $name;
}
for($i = 0; $i < $columnCount; $i++){
$name = $voNames[$i];
$vo->$name = null;
$stmt->bindColumn($columnNames[$i], $vo->$name, $columnTypes[$i]);
}
}
/**
* Return VO by sql file, parameter, replacement string.
*
* @param array $params params of prepared statement
* @param string|array $replace replacement string
* @return YokazeDb_Vo
*/
public function getVo($params = null, $replace = null)
{
$stmt = $this->prepareStmt($params, $replace);
if (!$stmt)
return false;
$vo = $this->createVo();
$columnCount = $stmt->columnCount();
$columnData = $stmt->fetch(PDO::FETCH_NUM);
for ($i = 0; $i < $columnCount; $i++){
$meta = $stmt->getColumnMeta($i);
if (!$meta)
break;
$name = implode('', array_map('ucfirst', explode('_', $meta['name'])));
$name[0] = strtolower($name[0]);
$vo->$name = $columnData[$i];
}
return $vo;
}
}