01: /*-------------------------------------------------------------------------
02: *
03: * Copyright (c) 2005, PostgreSQL Global Development Group
04: *
05: * IDENTIFICATION
06: * $PostgreSQL: pgjdbc/org/postgresql/test/jdbc3/ParameterMetaDataTest.java,v 1.4 2006/05/15 09:35:57 jurka Exp $
07: *
08: *-------------------------------------------------------------------------
09: */
10: package org.postgresql.test.jdbc3;
11:
12: import java.sql.*;
13: import junit.framework.TestCase;
14: import org.postgresql.test.TestUtil;
15:
16: public class ParameterMetaDataTest extends TestCase {
17:
18: private Connection _conn;
19:
20: public ParameterMetaDataTest(String name) {
21: super (name);
22: }
23:
24: protected void setUp() throws Exception {
25: _conn = TestUtil.openDB();
26: TestUtil.createTable(_conn, "parametertest",
27: "a int4, b float8, c text, d point");
28: }
29:
30: protected void tearDown() throws SQLException {
31: TestUtil.dropTable(_conn, "parametertest");
32: TestUtil.closeDB(_conn);
33: }
34:
35: public void testParameterMD() throws SQLException {
36: if (!TestUtil.isProtocolVersion(_conn, 3))
37: return;
38:
39: PreparedStatement pstmt = _conn
40: .prepareStatement("SELECT a FROM parametertest WHERE b = ? AND c = ? AND d >^ ? ");
41: ParameterMetaData pmd = pstmt.getParameterMetaData();
42:
43: assertEquals(3, pmd.getParameterCount());
44: assertEquals(Types.DOUBLE, pmd.getParameterType(1));
45: assertEquals("float8", pmd.getParameterTypeName(1));
46: assertEquals("java.lang.Double", pmd.getParameterClassName(1));
47: assertEquals(Types.VARCHAR, pmd.getParameterType(2));
48: assertEquals("text", pmd.getParameterTypeName(2));
49: assertEquals("java.lang.String", pmd.getParameterClassName(2));
50: assertEquals(Types.OTHER, pmd.getParameterType(3));
51: assertEquals("point", pmd.getParameterTypeName(3));
52: assertEquals("org.postgresql.geometric.PGpoint", pmd
53: .getParameterClassName(3));
54:
55: pstmt.close();
56: }
57:
58: public void testFailsOnBadIndex() throws SQLException {
59: if (!TestUtil.isProtocolVersion(_conn, 3))
60: return;
61:
62: PreparedStatement pstmt = _conn
63: .prepareStatement("SELECT a FROM parametertest WHERE b = ? AND c = ?");
64: ParameterMetaData pmd = pstmt.getParameterMetaData();
65: try {
66: pmd.getParameterType(0);
67: fail("Can't get parameter for index < 1.");
68: } catch (SQLException sqle) {
69: }
70: try {
71: pmd.getParameterType(3);
72: fail("Can't get parameter for index 3 with only two parameters.");
73: } catch (SQLException sqle) {
74: }
75: }
76:
77: // Make sure we work when mashing two queries into a single statement.
78: public void testMultiStatement() throws SQLException {
79: if (!TestUtil.isProtocolVersion(_conn, 3))
80: return;
81:
82: PreparedStatement pstmt = _conn
83: .prepareStatement("SELECT a FROM parametertest WHERE b = ? AND c = ? ; SELECT b FROM parametertest WHERE a = ?");
84: ParameterMetaData pmd = pstmt.getParameterMetaData();
85:
86: assertEquals(3, pmd.getParameterCount());
87: assertEquals(Types.DOUBLE, pmd.getParameterType(1));
88: assertEquals("float8", pmd.getParameterTypeName(1));
89: assertEquals(Types.VARCHAR, pmd.getParameterType(2));
90: assertEquals("text", pmd.getParameterTypeName(2));
91: assertEquals(Types.INTEGER, pmd.getParameterType(3));
92: assertEquals("int4", pmd.getParameterTypeName(3));
93:
94: pstmt.close();
95:
96: }
97: }
|