-
Notifications
You must be signed in to change notification settings - Fork 0
/
pq.php
140 lines (109 loc) · 3.22 KB
/
pq.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
<?php
require_once 'MDB2.php';
require_once './libs/SQL/Abstract.class.php';
require_once './libs/List/RubyLike.class.php';
PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, function ($e) { die($e->getMessage()); });
/* SQL_Abstract のUIを変更したというだけでは物足りないのでもう少し考えたい */
class PQ_Table {
protected $_table;
protected $_limit;
protected $_fields = array('*');
protected $_offset;
protected $_order = array();
protected $_where = array();
public function __construct ($table) {
$this->_table = $table;
}
public function where ($where) {
$this->_where = array_merge($this->_where, $where);
return $this;
}
/* function and() とかできたらいいんだけどなー */
/*
function and_where ($where) {
$this->_where =& array_merge($this->_where, $where);
return $this;
}
function or_where ($where) {
$this->_where[] = $where;
return $this;
}
*/
public function fields ($fields) {
$this->_fields = $fields;
return $this;
}
public function order ($order) {
$this->_order[] = $order;
return $this;
}
public function limit ($limit) {
$this->_limit = $limit;
return $this;
}
public function offset ($offset) {
$this->_offset = $offset;
return $this;
}
public function to_sql() {
$sql = new SQL_Abstract();
list($sql, $bind) = $sql->select(
$this->_table,
$this->_fields,
$this->_where,
$this->_order
);
// SQL::Abstract の limit 関連
// http://d.hatena.ne.jp/dayflower/20061107/1162894215
if (isset($this->_offset) or isset($this->_limit)) {
$sql .= ' LIMIT ';
if ( isset($this->_offset) )
$sql .= sprintf("%d,", $this->_offset);
$sql .= sprintf("%d", $this->_limit);
}
return array($sql, $bind);
}
}
class PQ_Query extends PQ_Table {
protected $_dsn;
protected $_table;
public function __construct($dsn, $table) {
$this->_dsn = $dsn;
parent::__construct($table);
}
public function exec() {
$db = MDB2::connect($this->_dsn);
list($sql, $binds) = $this->to_sql();
// echo $sql . "\n";
$st = $db->prepare($sql);
$rs = $st->execute($binds);
$rows = $rs->fetchAll();
return LR( $rows );
}
}
class PQ {
protected $_dsn;
public function dsn ($dsn) {
$this->_dsn = $dsn;
return $this;
}
public function query($table) {
return new PQ_Query($this->_dsn, $table);
}
}
function main () {
$pq = new PQ();
$pq->dsn('mysqli://nobody:nobody@localhost/sample?charset=utf8');
echo $pq->query('users')
->where( array('age' => array('>' => 20)) )
->where( array('mail' => array('-like' => '%@example.com')) )
->fields("mail, name")
->order("updated desc")
->offset(0)
->limit(10)
->exec()
->map(function ($o) { return $o[0]; })
->map(function ($v) { return strtoupper($v); })
->join("\n") . "\n";
}
main();