pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
How to retrieve column names for tables with underscores
8+ messages / 5 participants
[nested] [flat]

* How to retrieve column names for tables with underscores
@ 2011-03-24 13:12  Thomas Kellerer <spam_eater@gmx.net>
  0 siblings, 1 reply; 8+ messages in thread

From: Thomas Kellerer @ 2011-03-24 13:12 UTC (permalink / raw)
  To: pgsql-jdbc

Hi,

this is probably more of a generic JDBC question, but since I'm trying to solve this with Postgres I'm posting it here:

I have two tables

table1_  (yes there is an underscore at the end)
table12

Now when I try to retrieve the columns for "table_" the columns of table12 are also returned because the table name parameter for DatabaseMetaData.getColumns() is used as a LIKE expression.

Is there a way (preferrably standard JDBC) to avoid the LIKE evaluation with the getColumns() call e.g. by specifying an escape character?. I tried to replace the underscore with the standard backslash escape ("table1\_") but that didn't work.

Or is the only reliable way to check the value of the returned TABLE_NAME whether it matches my input value?

Thanks
Thomas




^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: How to retrieve column names for tables with underscores
@ 2011-03-24 15:18  ml-tb <ml-tb@emagixx.de>
  parent: Thomas Kellerer <spam_eater@gmx.net>
  0 siblings, 1 reply; 8+ messages in thread

From: ml-tb @ 2011-03-24 15:18 UTC (permalink / raw)
  To: pgsql-jdbc

Hi Thomas,

Am Donnerstag, 24. März 2011 schrieb Thomas Kellerer:
> backslash escape ("table1\_") but that didn't work.
IMHO it should be "table1\\_"?

Bye Thomas




^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: How to retrieve column names for tables with underscores
@ 2011-03-24 20:18  Kris Jurka <books@ejurka.com>
  parent: ml-tb <ml-tb@emagixx.de>
  0 siblings, 1 reply; 8+ messages in thread

From: Kris Jurka @ 2011-03-24 20:18 UTC (permalink / raw)
  To: ml-tb <ml-tb@emagixx.de>; +Cc: pgsql-jdbc



On Thu, 24 Mar 2011, ml-tb wrote:

> Hi Thomas,
>
> Am Donnerstag, 24. M?rz 2011 schrieb Thomas Kellerer:
>> backslash escape ("table1\_") but that didn't work.
> IMHO it should be "table1\\_"?
>

The escape needed depends on the value of standard_conforming_strings. 
You should use this call to find the escape needed:

http://download.oracle.com/javase/6/docs/api/java/sql/DatabaseMetaData.html#getSearchStringEscape()

Kris Jurka



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: How to retrieve column names for tables with underscores
@ 2011-03-24 20:45  Thomas Kellerer <spam_eater@gmx.net>
  parent: Kris Jurka <books@ejurka.com>
  0 siblings, 2 replies; 8+ messages in thread

From: Thomas Kellerer @ 2011-03-24 20:45 UTC (permalink / raw)
  To: pgsql-jdbc

Kris Jurka wrote on 24.03.2011 21:18:
>> Am Donnerstag, 24. M?rz 2011 schrieb Thomas Kellerer:
>>> backslash escape ("table1\_") but that didn't work.
>> IMHO it should be "table1\\_"?
>>
>
> The escape needed depends on the value of standard_conforming_strings. You should use this call to find the escape needed:
>
> http://download.oracle.com/javase/6/docs/api/java/sql/DatabaseMetaData.html#getSearchStringEscape()
>
That works perfectly.

Actually the fact that "it didn't work" right way was that I used replaceAll() instead of replace() somehow forgetting that replaceAll() needs a regex...

Interesting enough, the setting of standard_conforming_strings does not seem to affect this at all. Am I missing something?

Thanks
Thomas




^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: How to retrieve column names for tables with underscores
@ 2011-03-24 21:02  Maciek Sakrejda <msakrejda@truviso.com>
  parent: Thomas Kellerer <spam_eater@gmx.net>
  1 sibling, 1 reply; 8+ messages in thread

From: Maciek Sakrejda @ 2011-03-24 21:02 UTC (permalink / raw)
  To: Thomas Kellerer <spam_eater@gmx.net>; +Cc: pgsql-jdbc

> Actually the fact that "it didn't work" right way was that I used
> replaceAll() instead of replace() somehow forgetting that replaceAll() needs
> a regex...
>
> Interesting enough, the setting of standard_conforming_strings does not seem
> to affect this at all. Am I missing something?

Doesn't seem to affect what, exactly? It definitely affects how
backslashes are interpreted, and unless the current version of the
JDBC driver injects the 'E' escape syntax (with the 'E' prefix,
backslash escape sequences are interpreted regardless of the setting)
in a cavalier manner into the LIKE expression comparisons for
getColumns() (the one I have handy doesn't seem to), you should see
different behavior.

---
Maciek Sakrejda | System Architect | Truviso

1065 E. Hillsdale Blvd., Suite 215
Foster City, CA 94404
(650) 242-3500 Main
www.truviso.com



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: How to retrieve column names for tables with underscores
@ 2011-03-24 21:10  Kris Jurka <books@ejurka.com>
  parent: Thomas Kellerer <spam_eater@gmx.net>
  1 sibling, 0 replies; 8+ messages in thread

From: Kris Jurka @ 2011-03-24 21:10 UTC (permalink / raw)
  To: Thomas Kellerer <spam_eater@gmx.net>; +Cc: pgsql-jdbc

On 3/24/2011 1:45 PM, Thomas Kellerer wrote:
>
> Kris Jurka wrote on 24.03.2011 21:18:
>> The escape needed depends on the value of
>> standard_conforming_strings. You should use this call to find the
>> escape needed:
>>
>>
>> http://download.oracle.com/javase/6/docs/api/java/sql/DatabaseMetaData.html#getSearchStringEscape()
>
>>
>
> Interesting enough, the setting of standard_conforming_strings does
> not seem to affect this at all. Am I missing something?

Nope, that's my memory failing me.  The comment in getSearchStringEscape 
escape gives some details on why this doesn't depend on 
standard_conforming_strings.  Turning a search pattern into a literal 
happens as a separate step and it's at that time you need to know the 
value of s_c_s.

     public String getSearchStringEscape() throws SQLException
     {
         // This method originally returned "\\\\" assuming that it
         // would be fed directly into pg's input parser so it would
         // need two backslashes.  This isn't how it's supposed to be
         // used though.  If passed as a PreparedStatement parameter
         // or fed to a DatabaseMetaData method then double backslashes
         // are incorrect.  If you're feeding something directly into
         // a query you are responsible for correctly escaping it.
         // With 8.2+ this escaping is a little trickier because you
         // must know the setting of standard_conforming_strings, but
         // that's not our problem.

         return "\\";
     }

Kris Jurka



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: How to retrieve column names for tables with underscores
@ 2011-03-24 21:41  Thomas Kellerer <spam_eater@gmx.net>
  parent: Maciek Sakrejda <msakrejda@truviso.com>
  0 siblings, 1 reply; 8+ messages in thread

From: Thomas Kellerer @ 2011-03-24 21:41 UTC (permalink / raw)
  To: pgsql-jdbc

Maciek Sakrejda wrote on 24.03.2011 22:02:
>> Interesting enough, the setting of standard_conforming_strings does not seem
>> to affect this at all. Am I missing something?
>
> Doesn't seem to affect what, exactly?

The escape character returned by getSearchStringEscape()

>It definitely affects how
> backslashes are interpreted, and unless the current version of the
> JDBC driver injects the 'E' escape syntax (with the 'E' prefix,
> backslash escape sequences are interpreted regardless of the setting)
> in a cavalier manner into the LIKE expression comparisons for
> getColumns() (the one I have handy doesn't seem to), you should see
> different behavior.

Regardless of the setting of standard_conforming_strings passing \_ to getColumns() always works properly.

See Kris' answer for details.

Regards
Thomas




^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: How to retrieve column names for tables with underscores
@ 2011-03-24 22:52  David Johnston <polobo@yahoo.com>
  parent: Thomas Kellerer <spam_eater@gmx.net>
  0 siblings, 0 replies; 8+ messages in thread

From: David Johnston @ 2011-03-24 22:52 UTC (permalink / raw)
  To: 'Thomas Kellerer' <spam_eater@gmx.net>; pgsql-jdbc


>>It definitely affects how
>> backslashes are interpreted, and unless the current version of the  
>>JDBC driver injects the 'E' escape syntax (with the 'E' prefix,  
>>backslash escape sequences are interpreted regardless of the setting)  
>>in a cavalier manner into the LIKE expression comparisons for
>> getColumns() (the one I have handy doesn't seem to), you should see  
>>different behavior.

>Regardless of the setting of standard_conforming_strings passing \_ to
getColumns() always works properly.


Standard conforming strings makes sequences such as '\n' be treated as
literals unless you prefix an "E".  When you use LIKE the '_' and '%' take
on special properties (which they do not have in a regular string) -
regardless of standard conforming strings since the use of LIKE means you
intend to make use of those special characters.  In order to avoid the
special meaning of those two characters LIKE defines an escape character.
Since PostgreSQL controls both, you request the escape from PostgreSQL and
it provides what you need to perform the escape properly based upon the
current system configuration.

I hope this helps and that I'm not getting anything really wrong...




^ permalink  raw  reply  [nested|flat] 8+ messages in thread


end of thread, other threads:[~2011-03-24 22:52 UTC | newest]

Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2011-03-24 13:12 How to retrieve column names for tables with underscores Thomas Kellerer <spam_eater@gmx.net>
2011-03-24 15:18 ` ml-tb <ml-tb@emagixx.de>
2011-03-24 20:18   ` Kris Jurka <books@ejurka.com>
2011-03-24 20:45     ` Thomas Kellerer <spam_eater@gmx.net>
2011-03-24 21:02       ` Maciek Sakrejda <msakrejda@truviso.com>
2011-03-24 21:41         ` Thomas Kellerer <spam_eater@gmx.net>
2011-03-24 22:52           ` David Johnston <polobo@yahoo.com>
2011-03-24 21:10       ` Kris Jurka <books@ejurka.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox