Skip to content

Commit

Permalink
Set search_path according to query parameter.
Browse files Browse the repository at this point in the history
Make sure the jruby version of do_postgres behaves identical to
the native version with respect to a search_path query parameter
in the connection string (full_connect in do_postgres.c).
  • Loading branch information
ckoenig committed Feb 25, 2010
1 parent 4dba701 commit b954233
Showing 1 changed file with 42 additions and 0 deletions.
Expand Up @@ -7,6 +7,7 @@
import java.sql.Statement;
import java.sql.Types;
import java.util.Map;
import java.util.regex.Pattern;

import org.jruby.RubyBoolean;
import org.jruby.RubyString;
Expand Down Expand Up @@ -115,6 +116,47 @@ public void setPreparedStatementParam(PreparedStatement ps,
public void afterConnectionCallback(IRubyObject doConn,
Connection conn, Map<String, String> query) throws SQLException {
checkStandardConformingStrings(doConn, conn);
setSearchPath(conn, query);
}

private final static Pattern validValue = Pattern.compile("^[a-zA-Z][a-zA-Z0-9-]*(,[a-zA-Z][a-zA-Z0-9-]*)*$");

/**
* Escape the given value to be used in a SET command.
* Right now the method only checks if the given value
* contains only the following characters: a-zA-Z0-9,-
*
* @param value Value to escape
* @return Escaped value which can be safely used in a
* SET command.
*/
private String escapeValue(String value) throws SQLException {
if (!validValue.matcher(value).matches())
throw new SQLException("Invalid query parameter value: " + value);
return value;
}

/**
* Sets the search_path for the given connection based on
* the search_path query parameter.
*
* @param conn Connection to set search_path for.
* @param query Map containing all query parameters.
*/
private void setSearchPath(Connection conn, Map<String, String> query) throws SQLException {
final String search_path = "search_path";
if (query == null || !query.containsKey(search_path))
return;

PreparedStatement st = null;
try {
st = conn.prepareStatement("SET search_path = " + escapeValue(query.get(search_path)));
st.executeUpdate();
} catch (SQLException e) {
// Ignore.
} finally {
JDBCUtil.close(st);
}
}

/**
Expand Down

0 comments on commit b954233

Please sign in to comment.