Skip to content

Commit

Permalink
Merge pull request #150 from trackjdbc/REL1_5_STABLE/udttypename
Browse files Browse the repository at this point in the history
Supplies one of the short-term improvements suggested in issue #149.
  • Loading branch information
jcflack committed Jun 10, 2018
2 parents 27916e1 + 141efe2 commit d5172c6
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
*/
package org.postgresql.pljava.example.annotation;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLData;
import java.sql.SQLException;
import java.sql.SQLInput;
Expand All @@ -31,9 +35,12 @@
* Complex (re and im parts are doubles) implemented in Java as a mapped UDT.
*/
@SQLAction(requires={
"complextuple type", "complextuple assertHasValues"}, install=
"complextuple type", "complextuple assertHasValues",
"complextuple setParameter"}, install={
"SELECT javatest.assertHasValues(" +
" CAST('(1,2)' AS javatest.complextuple), 1, 2)"
" CAST('(1,2)' AS javatest.complextuple), 1, 2)",
"SELECT javatest.setParameter()"
}
)
@MappedUDT(schema="javatest", name="complextuple", provides="complextuple type",
structure={
Expand Down Expand Up @@ -79,6 +86,30 @@ public static void assertHasValues(
throw new SQLException("assertHasValues fails");
}

/**
* Pass a 'complextuple' UDT as a parameter to a PreparedStatement
* that returns it, and verify that it makes the trip intact.
*/
@Function(schema="javatest",
requires="complextuple type", provides="complextuple setParameter",
effects=IMMUTABLE, onNullInput=RETURNS_NULL)
public static void setParameter() throws SQLException
{
Connection c = DriverManager.getConnection("jdbc:default:connection");
PreparedStatement ps =
c.prepareStatement("SELECT CAST(? AS javatest.complextuple)");
ComplexTuple ct = new ComplexTuple();
ct.m_x = 1.5;
ct.m_y = 2.5;
ct.m_typeName = "javatest.complextuple";
ps.setObject(1, ct);
ResultSet rs = ps.executeQuery();
rs.next();
ct = (ComplexTuple)rs.getObject(1);
ps.close();
assertHasValues(ct, 1.5, 2.5);
}

private double m_x;
private double m_y;

Expand Down
25 changes: 17 additions & 8 deletions pljava/src/main/java/org/postgresql/pljava/internal/Oid.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
/*
* Copyright (c) 2004, 2005, 2006 TADA AB - Taby Sweden
* Distributed under the terms shown in the file COPYRIGHT
* found in the root folder of this project or at
* http://eng.tada.se/osprojects/COPYRIGHT.html
* Copyright (c) 2004-2018 Tada AB and other contributors, as listed below.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the The BSD 3-Clause License
* which accompanies this distribution, and is available at
* http://opensource.org/licenses/BSD-3-Clause
*
* Contributors:
* Tada AB
* Chapman Flack
*/
package org.postgresql.pljava.internal;

import java.sql.SQLData;
import java.sql.SQLException;
import java.util.HashMap;

Expand Down Expand Up @@ -37,13 +44,15 @@ public class Oid extends Number
}

/**
* Finds the PostgreSQL well known Oid for the given class.
* @param clazz The class.
* Finds the PostgreSQL well known Oid for the given Java object.
* @param obj The object.
* @return The well known Oid or null if no such Oid could be found.
*/
public static Oid forJavaClass(Class clazz)
public static Oid forJavaObject(Object obj) throws SQLException
{
return (Oid)s_class2typeId.get(clazz);
if ( obj instanceof SQLData )
return forTypeName(((SQLData)obj).getSQLTypeName());
return (Oid)s_class2typeId.get(obj.getClass());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2004-2016 Tada AB and other contributors, as listed below.
* Copyright (c) 2004-2018 Tada AB and other contributors, as listed below.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the The BSD 3-Clause License
Expand Down Expand Up @@ -209,7 +209,7 @@ public void setObject(int columnIndex, Object value, int sqlType)
throw new SQLException("Illegal parameter index");

Oid id = (sqlType == Types.OTHER)
? Oid.forJavaClass(value.getClass())
? Oid.forJavaObject(value)
: Oid.forSqlType(sqlType);

// Default to String.
Expand Down

0 comments on commit d5172c6

Please sign in to comment.