if (!parseStatement)
{
try
{
// send dataset:
SimpleResultSet rs = new SimpleResultSet();
rs.addColumn("max_identifier_length", Types.VARCHAR, 255, 0);
rs.addRow(new Object[] { "63" });
ResultSetMetaData meta = rs.getMetaData();
sendRowDescription(meta);
while (rs.next())
{
sendDataRow(rs);
}
sendCommandComplete(s, 0);
s = "";
}
catch(Exception e) {}
}
}
// ignore show escape_string_warning:
else if (lower.startsWith("show escape_string_warning"))
{
if (!parseStatement)
{
try
{
// send dataset:
SimpleResultSet rs = new SimpleResultSet();
rs.addColumn("escape_string_warning", Types.VARCHAR, 255, 0);
rs.addRow(new Object[] { "on" });
ResultSetMetaData meta = rs.getMetaData();
sendRowDescription(meta);
while (rs.next())
{
sendDataRow(rs);
}
sendCommandComplete(s, 0);
s = "";
}
catch(Exception e) {}
}
}
// ignore setting encoding:
else if (lower.startsWith("set client_encoding to"))
{
if (!parseStatement)
{
sendCommandComplete(s, 0);
s = "";
}
else
{
s = "CALL PG_CATALOG.DUMMY_PROCEDURE('')";
}
}
// ignore all LISTEN, NOTIFY & transaction-related commands:
else if (lower.startsWith("begin") || lower.startsWith("end") || lower.startsWith("commit") || lower.startsWith("start transaction") || lower.startsWith("rollback") || lower.startsWith("notify") || lower.startsWith("listen") || lower.startsWith("unlisten"))
{
if (!parseStatement)
{
sendCommandComplete(s, 0);
s = "";
}
// else - replace with relly callable statement:
else
{
// start transaction:
if (lower.startsWith("begin") || lower.startsWith("start transaction"))
{
s = "CALL PG_CATALOG.DUMMY_PROCEDURE('START TRANSACTION')";
}
// commit:
else if (lower.startsWith("commit") || lower.startsWith("end"))
{
s = "CALL PG_CATALOG.DUMMY_PROCEDURE('COMMIT')";
}
// rollback:
else if (lower.startsWith("rollback"))
{
s = "CALL PG_CATALOG.DUMMY_PROCEDURE('ROLLBACK')";
}
// else:
else
{
s = "CALL PG_CATALOG.DUMMY_PROCEDURE('')";
}
}
}
// if not specified FROM clause - replace with pg_catalog.dual table
// TODO: use regex for better pattern matching
else if (lower.startsWith("select ") && lower.indexOf("from ") == -1)
{
// replace NULL with cast(NULL as char(1))
s = replaceAll(s, "NULL", "cast(NULL as char(1))");
// replace current_schema() with current_schema:
s = replaceAll(s, "current_schema()", "current_schema");
s = s + " from pg_catalog.dual";
}
// columns discovery ODBC query:
// TODO: ensure that this query will also work in new ODBC provider versions
else if (lower.startsWith("select n.nspname, c.relname, a.attname, a.atttypid, t.typname, a.attnum, a.attlen, a.atttypmod, a.attnotnull,"))
{
// replaced query:
String replacedQuery = "select * from pg_catalog.internal_columns_view ";
// where clause:
String whereClause = "";
// if search by oid:
Matcher m = COLUMNS_Q_SEARCH_BY_IDS_1.matcher(s);
if (m.matches())
{
whereClause = " oid = " + m.group(1) + " ";
m = COLUMNS_Q_SEARCH_BY_IDS_1.matcher(s);
if (m.find())
{
if (whereClause != "")
whereClause += " and ";
whereClause += " ordinal_position = " + m.group(1) + " ";
}
}
// else:
else
{
m = COLUMNS_Q_SCHEMA.matcher(s);
if (m.find())
{
if (whereClause != "")
whereClause += " and ";
// remove \\_ escaping:
whereClause += " nspname " + m.group(1) + " '" + m.group(2).toString().replace("\\\\_", "_").toUpperCase() + "'";
}
m = COLUMNS_Q_TABLE.matcher(s);
if (m.find())
{
if (whereClause != "")
whereClause += " and ";
// remove \\_ escaping:
whereClause += " relname " + m.group(1) + " '" + m.group(2).toString().replace("\\\\_", "_").toUpperCase() + "'";
}
}
if (whereClause != "")
replacedQuery += " where " + whereClause;
replacedQuery += " order by nspname, relname, attnum";
s = replacedQuery;
}
// ODBC indexes view (ignore it at the moment):
else if (lower.startsWith("select c.relname, i.indkey, i.indisunique, i.indisclustered, a.amname, c.relhasrules, n.nspname, c.oid from pg_catalog.pg_index"))
{
if (!parseStatement)
{
try
{
// send dataset:
SimpleResultSet rs = new SimpleResultSet();
rs.addColumn("RELNAME", Types.VARCHAR, 255, 0);
rs.addColumn("INDKEY", Types.INTEGER, 0, 0);
rs.addColumn("INDISUNIQUE", Types.BOOLEAN, 0, 0);
rs.addColumn("INDISCLUSTERED", Types.BOOLEAN, 0, 0);
rs.addColumn("AMNAME", Types.VARCHAR, 0, 0);
rs.addColumn("RELHASRULES", Types.BOOLEAN, 0, 0);
rs.addColumn("NSPNAME", Types.VARCHAR, 0, 0);
rs.addColumn("OID", Types.INTEGER, 0, 0);
ResultSetMetaData meta = rs.getMetaData();
sendRowDescription(meta);
while (rs.next())
{
sendDataRow(rs);
}
sendCommandComplete(s, 0);
s = "";