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

Commitbb7b718

Browse files
author
Peter Mount
committed
Web Feb 14 17:29:00 GMT 2001 peter@retep.org.uk
- Fixed bug in LargeObject & BlobOutputStream where the stream's output was not flushed when either the stream or the blob were closed. - Fixed PreparedStatement.setBinaryStream() where it ignored the length
1 parentc1abe85 commitbb7b718

File tree

7 files changed

+224
-8
lines changed

7 files changed

+224
-8
lines changed

‎src/interfaces/jdbc/CHANGELOG

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
Web Feb 14 17:29:00 GMT 2001 peter@retep.org.uk
2+
- Fixed bug in LargeObject & BlobOutputStream where the stream's output
3+
was not flushed when either the stream or the blob were closed.
4+
- Fixed PreparedStatement.setBinaryStream() where it ignored the length
5+
16
Tue Feb 13 16:33:00 GMT 2001 peter@retep.org.uk
27
- More TestCases implemented. Refined the test suite api's.
38
- Removed need for SimpleDateFormat in ResultSet.getDate() improving

‎src/interfaces/jdbc/build.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
build file to allow ant (http://jakarta.apache.org/ant/) to be used
44
to build the PostgreSQL JDBC Driver.
55
6-
$Id: build.xml,v 1.5 2001/02/07 09:13:20 peter Exp $
6+
$Id: build.xml,v 1.6 2001/02/14 17:45:13 peter Exp $
77
88
-->
99

@@ -26,6 +26,7 @@
2626
<propertyname="database"value="jdbc:postgresql:test" />
2727
<propertyname="username"value="test" />
2828
<propertyname="password"value="password" />
29+
<propertyname="tablename"value="jdbctest" />
2930
<propertyname="junit.ui"value="textui" />
3031

3132
<!--

‎src/interfaces/jdbc/org/postgresql/jdbc2/PreparedStatement.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,9 +431,11 @@ public void setBinaryStream(int parameterIndex, InputStream x, int length) throw
431431
// is buffered internally anyhow, so there would be no performance
432432
// boost gained, if anything it would be worse!
433433
intc=x.read();
434-
while(c>-1) {
434+
intp=0;
435+
while(c>-1 &&p<length) {
435436
los.write(c);
436437
c=x.read();
438+
p++;
437439
}
438440
los.close();
439441
}catch(IOExceptionse) {

‎src/interfaces/jdbc/org/postgresql/largeobject/BlobOutputStream.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ public void flush() throws IOException {
9292
*/
9393
publicvoidclose()throwsIOException {
9494
try {
95+
flush();
9596
lo.close();
9697
lo=null;
9798
}catch(SQLExceptionse) {

‎src/interfaces/jdbc/org/postgresql/largeobject/LargeObject.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ public class LargeObject
6262
privateintoid;// OID of this object
6363
privateintfd;// the descriptor of the open large object
6464

65+
privateBlobOutputStreamos;// The current output stream
66+
67+
privatebooleanclosed=false;// true when we are closed
68+
6569
/**
6670
* This opens a large object.
6771
*
@@ -100,9 +104,25 @@ public int getOID()
100104
*/
101105
publicvoidclose()throwsSQLException
102106
{
103-
FastpathArgargs[] =newFastpathArg[1];
104-
args[0] =newFastpathArg(fd);
105-
fp.fastpath("lo_close",false,args);// true here as we dont care!!
107+
if(!closed) {
108+
// flush any open output streams
109+
if(os!=null) {
110+
try {
111+
// we can't call os.close() otherwise we go into an infinite loop!
112+
os.flush();
113+
}catch(IOExceptionioe) {
114+
thrownewSQLException(ioe.getMessage());
115+
}finally {
116+
os=null;
117+
}
118+
}
119+
120+
// finally close
121+
FastpathArgargs[] =newFastpathArg[1];
122+
args[0] =newFastpathArg(fd);
123+
fp.fastpath("lo_close",false,args);// true here as we dont care!!
124+
closed=true;
125+
}
106126
}
107127

108128
/**
@@ -279,7 +299,9 @@ public InputStream getInputStream() throws SQLException
279299
*/
280300
publicOutputStreamgetOutputStream()throwsSQLException
281301
{
282-
returnnewBlobOutputStream(this);
302+
if(os==null)
303+
os =newBlobOutputStream(this);
304+
returnos;
283305
}
284306

285307
}

‎src/interfaces/jdbc/org/postgresql/test/JDBC2Tests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,14 @@ public static TestSuite suite() {
207207

208208
// MetaData
209209

210-
// Fastpath/LargeObject
211-
212210
// Other misc tests, based on previous problems users have had or specific
213211
// features some applications require.
214212
suite.addTestSuite(JBuilderTest.class);
215213
suite.addTestSuite(MiscTest.class);
216214

215+
// Fastpath/LargeObject
216+
suite.addTestSuite(BlobTest.class);
217+
217218
// That's all folks
218219
returnsuite;
219220
}
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
packageorg.postgresql.test.jdbc2;
2+
3+
importorg.postgresql.test.JDBC2Tests;
4+
importjunit.framework.TestCase;
5+
importjava.io.*;
6+
importjava.sql.*;
7+
8+
importorg.postgresql.largeobject.*;
9+
10+
/**
11+
* $Id: BlobTest.java,v 1.1 2001/02/14 17:45:17 peter Exp $
12+
*
13+
* Some simple tests based on problems reported by users. Hopefully these will
14+
* help prevent previous problems from re-occuring ;-)
15+
*
16+
*/
17+
publicclassBlobTestextendsTestCase {
18+
19+
publicBlobTest(Stringname) {
20+
super(name);
21+
}
22+
23+
/**
24+
* The table format used by this TestCase
25+
*/
26+
privatestaticfinalStringBLOB_TABLE_FMT ="id name,lo oid";
27+
28+
/**
29+
* Tests one method of uploading a blob to the database
30+
*/
31+
publicvoidtestUploadBlob_LOOP() {
32+
try {
33+
Connectioncon =JDBC2Tests.openDB();
34+
35+
JDBC2Tests.createTable(con,BLOB_TABLE_FMT);
36+
37+
con.setAutoCommit(false);
38+
assert(!con.getAutoCommit());
39+
40+
assert(uploadFile(con,"build.xml",LOOP)>0);
41+
42+
// Now compare the blob & the file. Note this actually tests the
43+
// InputStream implementation!
44+
assert(compareBlobs(con));
45+
46+
JDBC2Tests.closeDB(con);
47+
}catch(Exceptionex) {
48+
assert(ex.getMessage(),false);
49+
}
50+
}
51+
52+
/**
53+
* Tests one method of uploading a blob to the database
54+
*/
55+
publicvoidtestUploadBlob_NATIVE() {
56+
try {
57+
Connectioncon =JDBC2Tests.openDB();
58+
59+
JDBC2Tests.createTable(con,BLOB_TABLE_FMT);
60+
61+
con.setAutoCommit(false);
62+
assert(!con.getAutoCommit());
63+
64+
assert(uploadFile(con,"build.xml",NATIVE_STREAM)>0);
65+
66+
// Now compare the blob & the file. Note this actually tests the
67+
// InputStream implementation!
68+
assert(compareBlobs(con));
69+
70+
JDBC2Tests.closeDB(con);
71+
}catch(Exceptionex) {
72+
assert(ex.getMessage(),false);
73+
}
74+
}
75+
76+
privatestaticfinalintLOOP =0;// LargeObject API using loop
77+
privatestaticfinalintNATIVE_STREAM =1;// LargeObject API using OutputStream
78+
privatestaticfinalintJDBC_STREAM =2;// JDBC API using OutputStream
79+
80+
/**
81+
* Helper - uploads a file into a blob using old style methods. We use this
82+
* because it always works, and we can use it as a base to test the new
83+
* methods.
84+
*/
85+
privateintuploadFile(Connectioncon,Stringfile,intmethod)throwsException {
86+
LargeObjectManagerlom = ((org.postgresql.Connection)con).getLargeObjectAPI();
87+
88+
FileInputStreamfis =newFileInputStream(file);
89+
90+
intoid =lom.create(LargeObjectManager.READWRITE);
91+
LargeObjectblob =lom.open(oid);
92+
93+
ints,t;
94+
bytebuf[];
95+
OutputStreamos;
96+
97+
switch(method)
98+
{
99+
caseLOOP:
100+
buf =newbyte[2048];
101+
t=0;
102+
while((s=fis.read(buf,0,buf.length))>0) {
103+
t+=s;
104+
blob.write(buf,0,s);
105+
}
106+
break;
107+
108+
caseNATIVE_STREAM:
109+
os =blob.getOutputStream();
110+
s=fis.read();
111+
while(s>-1) {
112+
os.write(s);
113+
s=fis.read();
114+
}
115+
os.close();
116+
break;
117+
118+
caseJDBC_STREAM:
119+
Filef =newFile(file);
120+
PreparedStatementps =con.prepareStatement(JDBC2Tests.insert("?"));
121+
ps.setBinaryStream(1,fis,(int)f.length());
122+
ps.execute();
123+
break;
124+
125+
default:
126+
assert("Unknown method in uploadFile",false);
127+
}
128+
129+
blob.close();
130+
fis.close();
131+
132+
// Insert into the table
133+
Statementst =con.createStatement();
134+
st.executeUpdate(JDBC2Tests.insert("id,lo","'"+file+"',"+oid));
135+
con.commit();
136+
st.close();
137+
138+
returnoid;
139+
}
140+
141+
/**
142+
* Helper - compares the blobs in a table with a local file. Note this alone
143+
* tests the InputStream methods!
144+
*/
145+
privatebooleancompareBlobs(Connectioncon)throwsException {
146+
booleanresult=true;
147+
148+
LargeObjectManagerlom = ((org.postgresql.Connection)con).getLargeObjectAPI();
149+
150+
Statementst =con.createStatement();
151+
ResultSetrs =st.executeQuery(JDBC2Tests.select("id,lo"));
152+
assert(rs!=null);
153+
154+
while(rs.next()) {
155+
Stringfile =rs.getString(1);
156+
intoid =rs.getInt(2);
157+
158+
FileInputStreamfis =newFileInputStream(file);
159+
LargeObjectblob =lom.open(oid);
160+
InputStreambis =blob.getInputStream();
161+
162+
intf=fis.read();
163+
intb=bis.read();
164+
intc=0;
165+
while(f>=0 &&b>=0 &result) {
166+
result=(f==b);
167+
f=fis.read();
168+
b=bis.read();
169+
c++;
170+
}
171+
result=result &&f==-1 &&b==-1;
172+
173+
if(!result)
174+
System.out.println("\nBlob compare failed at "+c+" of "+blob.size());
175+
176+
blob.close();
177+
fis.close();
178+
}
179+
rs.close();
180+
st.close();
181+
182+
returnresult;
183+
}
184+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp