-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpg.exq.php
100 lines (81 loc) · 2.67 KB
/
pg.exq.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
<?php
/**
*
* Copyright (C) 2010, 2011 Robin Harvey ([email protected])
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
namespace pg\exq;
use pg\wire as wire;
class Statement
{
const ST_PARSED = 1;
const ST_DESCRIBED = 2;
const ST_BOUND = 4;
const ST_EXECD = 8;
private $conn;
private $sql;
private $name = false;
private $ppTypes = array();
private $st = 0;
function __construct (\pg\Connection $conn) {
$this->conn = $conn;
}
function getState () { return $this->st; }
function setSql ($q) {
$this->sql = $q;
}
function setParseParamTypes (array $oids) {
$this->ppTypes;
}
function setName ($name) {
$this->name = $name;
}
// Sends protocol messages: parse, sync, blocks for response
function parse () {
$w = new wire\Writer;
printf("Write parse for %s\n", $this->name);
$w->writeParse($this->name, $this->sql, $this->ppTypes);
$w->writeSync();
$this->conn->write($w->get());
$this->readAndDump();
$this->st = $this->st | self::ST_PARSED;
$this->st = $this->st & ~self::ST_DESCRIBED;
}
function describe () {
// ...
$this->st = $this->st | self::ST_DESCRIBED;
}
// Sends protocol messages: bind, execute, sync, blocks for response
function execute (array $params=array(), $rowLimit=0) {
$w = new wire\Writer;
$w->writeBind($this->name, $this->name, $params);
printf("Write bind for %s\n", $this->name);
$w->writeExecute($this->name, $rowLimit);
$w->writeSync();
printf("Write execute for %s\n", $this->name);
$this->conn->write($w->get());
$this->readAndDump(); // return
}
function close () {
}
private function readAndDump () {
$r = new wire\Reader($this->conn->read());
foreach($r->chomp() as $m) {
printf("Read %s\n", $m->getName());
}
}
}