0

I am involved in a project that is moving databases from Sybase SQL Anywhere to postgres, the main issue with this is that Sybase SQL Anywhere is natively case insensitive and postgres is not.

The patch for this (at the moment) is to use case insensitive comparisons (ILIKE) in the queries embedded in our application (I know that there are issues and tradeoffs with this but it is not my decision - no point discussing it here unless you have some killer alternative).

I'm attempting to put together a python script utilizing SQLGlot to automate this as much as I can - so the question is:

How can I isolate column comparisons (= , <> , like, not like) including the column names on both sides - I need to find them and evaluate the columns, then change the comparison operator as appropriate and inject it all back into the original query.

Example:

SELECT a.case_insensitive_col,
       CASE 
          WHEN b.case_insensitive_col = 'someText' 
              THEN b.someColumn
          ELSE b.someOtherColumn
       END as conditional_selection,
       b.reg_column
FROM tableA a 
     JOIN tableB b ON a.case_insensitive_col = b.case_insensitive_col
WHERE a.case_insensitive_col = ?
      AND b.reg_column = ?

---->

SELECT a.case_insensitive_col,
       CASE 
          WHEN b.case_insensitive_col ILIKE 'someText' 
              THEN b.someColumn
          ELSE b.someOtherColumn
       END as conditional_selection,
       b.reg_column
FROM tableA a 
     JOIN tableB b ON a.case_insensitive_col ILIKE b.case_insensitive_col
WHERE a.case_insensitive_col ILIKE ?
      AND b.reg_column = ?

There are 3 examples (comparison in case in select, comparison in join, comparison in where) of the cases I've seen so far, is this possible?

I also want to avoid affecting the '=' characters in update statements eg(UPDATE tableA SET columnA = 'sometext')

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Ridiculon
  • 323
  • 4
  • 12
  • 1
    Which Sybase database software? (see tag info, among these ones: [tag:sap-ase], [tag:sybase-asa], [tag:sqlanywhere], [tag:sap-iq], [tag:advantage-database-server]) – Sandra Rossi Aug 06 '23 at 18:30
  • _sybase is natively case insensitive and postgres is not_ - Uh. That really depends on your meaning. `citext` [is built-in](https://www.postgresql.org/docs/current/citext.html). – Reinderien Aug 06 '23 at 19:12
  • @SandraRossi sqlanywhere – Ridiculon Aug 07 '23 at 14:03
  • @Reindeien right, sorry, i mean without changing the schema from SQL standard column types. There is also someone looking at converting to citext but its parallel to what I'm doing. – Ridiculon Aug 07 '23 at 14:03

0 Answers0