Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit364efd1

Browse files
Edmund MerglEdmund Mergl
Edmund Mergl
authored and
Edmund Mergl
committed
adapted to pgsql-v6.2
1 parent9e74edd commit364efd1

File tree

3 files changed

+709
-0
lines changed

3 files changed

+709
-0
lines changed

‎src/interfaces/perl5/eg/ApachePg.pl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/local/bin/perl
2+
3+
# demo script, tested with:
4+
# - PostgreSQL-6.2
5+
# - apache_1.2.4
6+
# - mod_perl-1.00
7+
# - perl5.004_01
8+
9+
use CGI;
10+
use Pg;
11+
12+
$query = new CGI;
13+
14+
print$query->header,
15+
$query->start_html(-title=>'A Simple Example'),
16+
$query->startform,
17+
"<CENTER><H3>Testing Module Pg</H3></CENTER>",
18+
"Enter database name:",
19+
$query->textfield(-name=>'dbname'),
20+
"<P>",
21+
"Enter select command:",
22+
$query->textfield(-name=>'cmd', -size=>40),
23+
"<P>",
24+
$query->submit(-value=>'Submit'),
25+
$query->endform;
26+
27+
if ($query->param) {
28+
29+
$dbname =$query->param('dbname');
30+
$conn = Pg::connectdb("dbname =$dbname");
31+
$cmd =$query->param('cmd');
32+
$result =$conn->exec($cmd);
33+
$result->print(STDOUT, 0, 0, 0, 1, 0, 0,'','','');
34+
}
35+
36+
print$query->end_html;
37+
Lines changed: 324 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,324 @@
1+
#!/usr/local/bin/perl
2+
3+
#-------------------------------------------------------
4+
#
5+
# $Id: example.newstyle,v 1.1 1997/09/17 20:48:14 mergl Exp $
6+
#
7+
# Copyright (c) 1997 Edmund Mergl
8+
#
9+
#-------------------------------------------------------
10+
11+
# Before `make install' is performed this script should be runnable with
12+
# `make test'. After `make install' it should work as `perl test.pl'
13+
14+
######################### We start with some black magic to print on failure.
15+
16+
BEGIN {$| = 1;print"1..61\n"; }
17+
END {print"not ok 1\n"unless$loaded;}
18+
use Pg;
19+
$loaded = 1;
20+
print"ok 1\n";
21+
22+
######################### End of black magic.
23+
24+
$dbmain ='template1';
25+
$dbname ='pgperltest';
26+
$trace ='/tmp/pgtrace.out';
27+
$cnt = 2;
28+
$DEBUG = 0;# set this to 1 for traces
29+
30+
$| = 1;
31+
32+
######################### the following methods will be tested
33+
34+
#connectdb
35+
#db
36+
#user
37+
#host
38+
#port
39+
#finish
40+
#status
41+
#errorMessage
42+
#trace
43+
#untrace
44+
#exec
45+
#getline
46+
#endcopy
47+
#putline
48+
#resultStatus
49+
#ntuples
50+
#nfields
51+
#fname
52+
#fnumber
53+
#ftype
54+
#fsize
55+
#cmdStatus
56+
#oidStatus
57+
#cmdTuples
58+
#getvalue
59+
#print
60+
#notifies
61+
#lo_import
62+
#lo_export
63+
#lo_unlink
64+
65+
######################### the following methods will not be tested
66+
67+
#setdb
68+
#conndefaults
69+
#reset
70+
#options
71+
#tty
72+
#getlength
73+
#getisnull
74+
#displayTuples
75+
#printTuples
76+
#lo_open
77+
#lo_close
78+
#lo_read
79+
#lo_write
80+
#lo_creat
81+
#lo_seek
82+
#lo_tell
83+
84+
######################### handles error condition
85+
86+
$SIG{PIPE} =sub {print"broken pipe\n" };
87+
88+
######################### create and connect to test database
89+
# 2-4
90+
91+
$conn = Pg::connectdb("dbname =$dbmain");
92+
cmp_eq(PGRES_CONNECTION_OK,$conn->status);
93+
94+
# might fail if $dbname doesn't exist => don't check resultStatus
95+
$result =$conn->exec("DROP DATABASE$dbname");
96+
97+
$result =$conn->exec("CREATE DATABASE$dbname");
98+
cmp_eq(PGRES_COMMAND_OK,$result->resultStatus);
99+
100+
$conn = Pg::connectdb("dbname =$dbname");
101+
cmp_eq(PGRES_CONNECTION_OK,$conn->status);
102+
103+
######################### debug, PQtrace
104+
105+
if ($DEBUG) {
106+
open(TRACE,">$trace") ||die"can not open$trace:$!";
107+
$conn->trace(TRACE);
108+
}
109+
110+
######################### check PGconn
111+
# 5-8
112+
113+
$db =$conn->db;
114+
cmp_eq($dbname,$db);
115+
116+
$user =$conn->user;
117+
cmp_ne("",$user);
118+
119+
$host =$conn->host;
120+
cmp_ne("",$host);
121+
122+
$port =$conn->port;
123+
cmp_ne("",$port);
124+
125+
######################### create and insert into table
126+
# 9-20
127+
128+
$result =$conn->exec("CREATE TABLE person (id int4, name char16)");
129+
cmp_eq(PGRES_COMMAND_OK,$result->resultStatus);
130+
cmp_eq("CREATE",$result->cmdStatus);
131+
132+
for ($i = 1;$i <= 5;$i++) {
133+
$result =$conn->exec("INSERT INTO person VALUES ($i, 'Edmund Mergl')");
134+
cmp_eq(PGRES_COMMAND_OK,$result->resultStatus);
135+
cmp_ne(0,$result->oidStatus);
136+
}
137+
138+
######################### copy to stdout, PQgetline
139+
# 21-27
140+
141+
$result =$conn->exec("COPY person TO STDOUT");
142+
cmp_eq(PGRES_COPY_OUT,$result->resultStatus);
143+
144+
$i = 1;
145+
while (-1 !=$ret) {
146+
$ret =$conn->getline($string, 256);
147+
lastif$stringeq"\\.";
148+
cmp_eq("$iEdmund Mergl",$string);
149+
$i ++;
150+
}
151+
152+
cmp_eq(0,$conn->endcopy);
153+
154+
######################### delete and copy from stdin, PQputline
155+
# 28-34
156+
157+
$result =$conn->exec("BEGIN");
158+
cmp_eq(PGRES_COMMAND_OK,$result->resultStatus);
159+
160+
$result =$conn->exec("DELETE FROM person");
161+
cmp_eq(PGRES_COMMAND_OK,$result->resultStatus);
162+
cmp_eq("DELETE 5",$result->cmdStatus);
163+
cmp_eq("5",$result->cmdTuples);
164+
165+
$result =$conn->exec("COPY person FROM STDIN");
166+
cmp_eq(PGRES_COPY_IN,$result->resultStatus);
167+
168+
for ($i = 1;$i <= 5;$i++) {
169+
# watch the tabs and do not forget the newlines
170+
$conn->putline("$iEdmund Mergl\n");
171+
}
172+
$conn->putline("\\.\n");
173+
174+
cmp_eq(0,$conn->endcopy);
175+
176+
$result =$conn->exec("END");
177+
cmp_eq(PGRES_COMMAND_OK,$result->resultStatus);
178+
179+
######################### select from person, PQgetvalue
180+
# 35-48
181+
182+
$result =$conn->exec("SELECT * FROM person");
183+
cmp_eq(PGRES_TUPLES_OK,$result->resultStatus);
184+
185+
for ($k = 0;$k <$result->nfields;$k++) {
186+
$fname =$result->fname($k);
187+
$ftype =$result->ftype($k);
188+
$fsize =$result->fsize($k);
189+
if (0 ==$k) {
190+
cmp_eq("id",$fname);
191+
cmp_eq(23,$ftype);
192+
cmp_eq(4,$fsize);
193+
}else {
194+
cmp_eq("name",$fname);
195+
cmp_eq(20,$ftype);
196+
cmp_eq(16,$fsize);
197+
}
198+
$fnumber =$result->fnumber($fname);
199+
cmp_eq($k,$fnumber);
200+
}
201+
202+
for ($k = 0;$k <$result->ntuples;$k++) {
203+
$string ="";
204+
for ($l = 0;$l <$result->nfields;$l++) {
205+
$string .=$result->getvalue($k,$l) ."";
206+
}
207+
$i =$k + 1;
208+
cmp_eq("$i Edmund Mergl",$string);
209+
}
210+
211+
######################### PQnotifies
212+
# 49-51
213+
214+
if (!defined($pid =fork)) {
215+
die"can not fork:$!";
216+
}elsif (!$pid) {
217+
# i'm the child
218+
sleep 2;
219+
bless$conn;
220+
$conn = Pg::connectdb("dbname =$dbname");
221+
$result =$conn->exec("NOTIFY person");
222+
exit;
223+
}
224+
225+
$result =$conn->exec("LISTEN person");
226+
cmp_eq(PGRES_COMMAND_OK,$result->resultStatus);
227+
cmp_eq("LISTEN",$result->cmdStatus);
228+
229+
while (1) {
230+
$result =$conn->exec("");
231+
($table,$pid) =$conn->notifies;
232+
lastif$pid;
233+
}
234+
235+
cmp_eq("person",$table);
236+
237+
######################### PQprint
238+
# 52-53
239+
240+
$result =$conn->exec("SELECT name FROM person WHERE id = 2");
241+
cmp_eq(PGRES_TUPLES_OK,$result->resultStatus);
242+
open(PRINT,"| read IN; read IN; if [\"\$IN\" =\"myName Edmund Mergl\" ]; then echo\"ok$cnt\"; else echo\"not ok$cnt\"; fi") ||die"can not fork:$|";
243+
$cnt ++;
244+
$result->print(PRINT, 0, 0, 0, 0, 1, 0,"","","","myName");
245+
close(PRINT) ||die"bad PRINT:$!";
246+
247+
######################### PQlo_import, PQlo_export, PQlo_unlink
248+
# 54-59
249+
250+
$filename ='ApachePg.pl';
251+
$cwd =`pwd`;
252+
chop$cwd;
253+
254+
$result =$conn->exec("BEGIN");
255+
cmp_eq(PGRES_COMMAND_OK,$result->resultStatus);
256+
257+
$lobjOid =$conn->lo_import("$cwd/$filename");
258+
cmp_ne(0,$lobjOid);
259+
260+
cmp_ne(-1,$conn->lo_export($lobjOid,"/tmp/$filename"));
261+
262+
cmp_eq(-s"$cwd/$filename",-s"/tmp/$filename");
263+
264+
$result =$conn->exec("END");
265+
cmp_eq(PGRES_COMMAND_OK,$result->resultStatus);
266+
267+
cmp_ne(-1,$conn->lo_unlink($lobjOid));
268+
unlink"/tmp/$filename";
269+
270+
######################### debug, PQuntrace
271+
272+
if ($DEBUG) {
273+
close(TRACE) ||die"bad TRACE:$!";
274+
$conn->untrace;
275+
}
276+
277+
######################### disconnect and drop test database
278+
# 60-61
279+
280+
$conn = Pg::connectdb("dbname =$dbmain");
281+
cmp_eq(PGRES_CONNECTION_OK,$conn->status);
282+
283+
$result =$conn->exec("DROP DATABASE$dbname");
284+
cmp_eq(PGRES_COMMAND_OK,$result->resultStatus);
285+
286+
######################### hopefully
287+
288+
print"test sequence finished.\n"if 62 ==$cnt;
289+
290+
######################### utility functions
291+
292+
subcmp_eq {
293+
294+
my$cmp =shift;
295+
my$ret =shift;
296+
my$msg;
297+
298+
if ("$cmp"eq"$ret") {
299+
print"ok$cnt\n";
300+
}else {
301+
$msg =$conn->errorMessage;
302+
print"not ok$cnt:$cmp,$ret\n$msg\n";
303+
exit;
304+
}
305+
$cnt++;
306+
}
307+
308+
subcmp_ne {
309+
310+
my$cmp =shift;
311+
my$ret =shift;
312+
my$msg;
313+
314+
if ("$cmp"ne"$ret") {
315+
print"ok$cnt\n";
316+
}else {
317+
$msg =$conn->errorMessage;
318+
print"not ok$cnt:$cmp,$ret\n$msg\n";
319+
exit;
320+
}
321+
$cnt++;
322+
}
323+
324+
######################### EOF

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp