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

Commit215d965

Browse files
author
Barry Lind
committed
Applied patch from Oliver Jowett to clean up some instances where the wrong
type was being reported for PREPAREs. Modified Files: jdbc/org/postgresql/jdbc1/AbstractJdbc1Statement.java jdbc/org/postgresql/test/jdbc2/ServerPreparedStmtTest.java
1 parent7da6afe commit215d965

File tree

2 files changed

+96
-31
lines changed

2 files changed

+96
-31
lines changed

‎src/interfaces/jdbc/org/postgresql/jdbc1/AbstractJdbc1Statement.java

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
importjava.sql.Types;
2727
importjava.util.Vector;
2828

29-
/* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1Statement.java,v 1.37 2003/09/17 05:07:37 barry Exp $
29+
/* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1Statement.java,v 1.38 2003/09/18 04:14:27 barry Exp $
3030
* This class defines methods of the jdbc1 specification. This class is
3131
* extended by org.postgresql.jdbc2.AbstractJdbc2Statement which adds the jdbc2
3232
* methods. The real Statement class (for jdbc1) is org.postgresql.jdbc1.Jdbc1Statement
@@ -893,6 +893,9 @@ public void setNull(int parameterIndex, int sqlType) throws SQLException
893893
caseTypes.TIMESTAMP:
894894
l_pgType =PG_TIMESTAMPTZ;
895895
break;
896+
caseTypes.BIT:
897+
l_pgType =PG_BOOLEAN;
898+
break;
896899
caseTypes.BINARY:
897900
caseTypes.VARBINARY:
898901
caseTypes.LONGVARBINARY:
@@ -930,7 +933,7 @@ public void setBoolean(int parameterIndex, boolean x) throws SQLException
930933
*/
931934
publicvoidsetByte(intparameterIndex,bytex)throwsSQLException
932935
{
933-
bind(parameterIndex,Integer.toString(x),PG_TEXT);
936+
bind(parameterIndex,Integer.toString(x),PG_INT2);
934937
}
935938

936939
/*
@@ -1457,6 +1460,21 @@ public void clearParameters() throws SQLException
14571460
}
14581461
}
14591462

1463+
// Helper method that extracts numeric values from an arbitary Object.
1464+
privateStringnumericValueOf(Objectx)
1465+
{
1466+
if (xinstanceofBoolean)
1467+
return ((Boolean)x).booleanValue() ?"1" :"0";
1468+
elseif (xinstanceofInteger ||xinstanceofLong ||
1469+
xinstanceofDouble ||xinstanceofShort ||
1470+
xinstanceofNumber ||xinstanceofFloat)
1471+
returnx.toString();
1472+
else
1473+
//ensure the value is a valid numeric value to avoid
1474+
//sql injection attacks
1475+
returnnewBigDecimal(x.toString()).toString();
1476+
}
1477+
14601478
/*
14611479
* Set the value of a parameter using an object; use the java.lang
14621480
* equivalent objects for integral values.
@@ -1486,35 +1504,25 @@ public void setObject(int parameterIndex, Object x, int targetSqlType, int scale
14861504
switch (targetSqlType)
14871505
{
14881506
caseTypes.INTEGER:
1489-
if (xinstanceofBoolean)
1490-
bind(parameterIndex,((Boolean)x).booleanValue() ?"1" :"0",PG_BOOLEAN);
1491-
elseif (xinstanceofInteger ||xinstanceofLong ||
1492-
xinstanceofDouble ||xinstanceofShort ||
1493-
xinstanceofNumber ||xinstanceofFloat )
1494-
bind(parameterIndex,x.toString(),PG_INTEGER);
1495-
else
1496-
//ensure the value is a valid numeric value to avoid
1497-
//sql injection attacks
1498-
bind(parameterIndex,newBigDecimal(x.toString()).toString(),PG_INTEGER);
1507+
bind(parameterIndex,numericValueOf(x),PG_INTEGER);
14991508
break;
15001509
caseTypes.TINYINT:
15011510
caseTypes.SMALLINT:
1511+
bind(parameterIndex,numericValueOf(x),PG_INT2);
1512+
break;
15021513
caseTypes.BIGINT:
1514+
bind(parameterIndex,numericValueOf(x),PG_INT8);
1515+
break;
15031516
caseTypes.REAL:
15041517
caseTypes.FLOAT:
1518+
bind(parameterIndex,numericValueOf(x),PG_FLOAT);
1519+
break;
15051520
caseTypes.DOUBLE:
1521+
bind(parameterIndex,numericValueOf(x),PG_DOUBLE);
1522+
break;
15061523
caseTypes.DECIMAL:
15071524
caseTypes.NUMERIC:
1508-
if (xinstanceofBoolean)
1509-
bind(parameterIndex, ((Boolean)x).booleanValue() ?"1" :"0",PG_BOOLEAN);
1510-
elseif (xinstanceofInteger ||xinstanceofLong ||
1511-
xinstanceofDouble ||xinstanceofShort ||
1512-
xinstanceofNumber ||xinstanceofFloat )
1513-
bind(parameterIndex,x.toString(),PG_NUMERIC);
1514-
else
1515-
//ensure the value is a valid numeric value to avoid
1516-
//sql injection attacks
1517-
bind(parameterIndex,newBigDecimal(x.toString()).toString(),PG_NUMERIC);
1525+
bind(parameterIndex,numericValueOf(x),PG_NUMERIC);
15181526
break;
15191527
caseTypes.CHAR:
15201528
caseTypes.VARCHAR:
@@ -1559,7 +1567,7 @@ else if (x instanceof String)
15591567
}
15601568
elseif (xinstanceofNumber)
15611569
{
1562-
bind(parameterIndex, ((Number)x).intValue()==1 ?"'1'" :"'0'",PG_BOOLEAN);
1570+
bind(parameterIndex, ((Number)x).intValue()!=0 ?"'1'" :"'0'",PG_BOOLEAN);
15631571
}
15641572
else
15651573
{
@@ -1572,7 +1580,10 @@ else if (x instanceof Number)
15721580
setObject(parameterIndex,x);
15731581
break;
15741582
caseTypes.OTHER:
1575-
setString(parameterIndex, ((PGobject)x).getValue(),PG_TEXT);
1583+
if (xinstanceofPGobject)
1584+
setString(parameterIndex, ((PGobject)x).getValue(), ((PGobject)x).getType());
1585+
else
1586+
thrownewPSQLException("postgresql.prep.type",PSQLState.INVALID_PARAMETER_TYPE);
15761587
break;
15771588
default:
15781589
thrownewPSQLException("postgresql.prep.type",PSQLState.INVALID_PARAMETER_TYPE);

‎src/interfaces/jdbc/org/postgresql/test/jdbc2/ServerPreparedStmtTest.java

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ protected void setUp() throws Exception
2626
con =TestUtil.openDB();
2727
Statementstmt =con.createStatement();
2828

29-
TestUtil.createTable(con,"testsps","id integer");
29+
TestUtil.createTable(con,"testsps","id integer, value boolean");
3030

31-
stmt.executeUpdate("INSERT INTO testsps VALUES (1)");
32-
stmt.executeUpdate("INSERT INTO testsps VALUES (2)");
33-
stmt.executeUpdate("INSERT INTO testsps VALUES (3)");
34-
stmt.executeUpdate("INSERT INTO testsps VALUES (4)");
35-
stmt.executeUpdate("INSERT INTO testsps VALUES (6)");
36-
stmt.executeUpdate("INSERT INTO testsps VALUES (9)");
31+
stmt.executeUpdate("INSERT INTO testsps VALUES (1,'t')");
32+
stmt.executeUpdate("INSERT INTO testsps VALUES (2,'t')");
33+
stmt.executeUpdate("INSERT INTO testsps VALUES (3,'t')");
34+
stmt.executeUpdate("INSERT INTO testsps VALUES (4,'t')");
35+
stmt.executeUpdate("INSERT INTO testsps VALUES (6,'t')");
36+
stmt.executeUpdate("INSERT INTO testsps VALUES (9,'f')");
3737

3838
stmt.close();
3939
}
@@ -125,6 +125,60 @@ public void testPreparedStatementsWithOneBind() throws Exception
125125
pstmt.close();
126126
}
127127

128+
// Verify we can bind booleans-as-objects ok.
129+
publicvoidtestBooleanObjectBind()throwsException
130+
{
131+
PreparedStatementpstmt =con.prepareStatement("SELECT * FROM testsps WHERE value = ?");
132+
((PGStatement)pstmt).setUseServerPrepare(true);
133+
if (TestUtil.haveMinimumServerVersion(con,"7.3")) {
134+
assertTrue(((PGStatement)pstmt).isUseServerPrepare());
135+
}else {
136+
assertTrue(!((PGStatement)pstmt).isUseServerPrepare());
137+
}
138+
139+
pstmt.setObject(1,newBoolean(false),java.sql.Types.BIT);
140+
ResultSetrs =pstmt.executeQuery();
141+
assertTrue(rs.next());
142+
assertEquals(9,rs.getInt(1));
143+
rs.close();
144+
}
145+
146+
// Verify we can bind booleans-as-integers ok.
147+
publicvoidtestBooleanIntegerBind()throwsException
148+
{
149+
PreparedStatementpstmt =con.prepareStatement("SELECT * FROM testsps WHERE id = ?");
150+
((PGStatement)pstmt).setUseServerPrepare(true);
151+
if (TestUtil.haveMinimumServerVersion(con,"7.3")) {
152+
assertTrue(((PGStatement)pstmt).isUseServerPrepare());
153+
}else {
154+
assertTrue(!((PGStatement)pstmt).isUseServerPrepare());
155+
}
156+
157+
pstmt.setObject(1,newBoolean(true),java.sql.Types.INTEGER);
158+
ResultSetrs =pstmt.executeQuery();
159+
assertTrue(rs.next());
160+
assertEquals(1,rs.getInt(1));
161+
rs.close();
162+
}
163+
164+
// Verify we can bind booleans-as-native-types ok.
165+
publicvoidtestBooleanBind()throwsException
166+
{
167+
PreparedStatementpstmt =con.prepareStatement("SELECT * FROM testsps WHERE value = ?");
168+
((PGStatement)pstmt).setUseServerPrepare(true);
169+
if (TestUtil.haveMinimumServerVersion(con,"7.3")) {
170+
assertTrue(((PGStatement)pstmt).isUseServerPrepare());
171+
}else {
172+
assertTrue(!((PGStatement)pstmt).isUseServerPrepare());
173+
}
174+
175+
pstmt.setBoolean(1,false);
176+
ResultSetrs =pstmt.executeQuery();
177+
assertTrue(rs.next());
178+
assertEquals(9,rs.getInt(1));
179+
rs.close();
180+
}
181+
128182
publicvoidtestPreparedStatementsWithBinds()throwsException
129183
{
130184
PreparedStatementpstmt =con.prepareStatement("SELECT * FROM testsps WHERE id = ? or id = ?");

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp