Giter Club home page Giter Club logo

sql-decode-to-case's Introduction

sql-convert-decode-to-case

==================

Convert oracle style deocde statements to "simple form" case statements

Useful for migrating from oracle to postgres.

If you use enterprisedb decode statement may work but case statements will give you better performance compared with decode.

Thanks to Enterprisedb support for the testcases.

Decode statements whithin Dynamic Commands is not handled by this script

Usage

Usage: convert_decode_to_case [options]
    -h, --help                       Display usage information
    -v, --[no-]verbose               Run verbosely
    -p, --parallel                   Process all input files in parallel
    -i, --input-dir=INPUT            Read from INPUT, may be - for STDIN (default), a single file or the path to a directory containing multiple *.sql files to be processsed
    -o, --output-destination=DEST    Write output to DEST, if omitted STDOUT will be used if input is STDIN or a single file, if input is a directory a directory must be used as output destination
    -f, --force                      Overwrite existing output files

If run without any options it will read sql from STDIN and write converted sql to STDOUT.

Example

testcase.sql have been used to test that decodes get converted in a reliable manner.

$ cat testcase.sql 

SELECT DECODE(1 /* , () ' hello () ' */ , 1 , 'Return a string )' , 'else return something with ticks and a parenthesis '')''') FROM DUAL;

select decode /*(*/ (1 , 1 , 'what') from dual;

SELECT
  -- THIS DECODE( statement should not be included
DECODE( 1 , 1 , 'But this one should' )
FROM DUAL;

/* 

   /* 
      and a nested comment , lets try some DECODE( 
   */ 
  And another decode( 'with unbalanced parenthesis' , 'inside a comment' , 'should not be converted'
*/

SELECT 
DECODE( 

       DECODE( 1 , 1 , '( phony retval' ) , 

 -- Comment/newlines shoule be ignored

       '' , '' , 'y0 dAwG - I heard you liked decoeds - so I put a decode in your decode' 
       ) 
FROM DUAL;

SELECT DECODE( NVL(1,1) , 1 , 'foo', 2 , NVL('','bar') ) FROM DUAL;

/* Trailing comment */

$ ./convert_decode_to_case -i testcase.sql 

SELECT  CASE 1 /* , () ' hello () ' */  WHEN  1  THEN  'Return a string )'  ELSE  'else return something with ticks and a parenthesis '')''' END  FROM DUAL;

select  CASE 1  WHEN  1  THEN  'what' END  from dual;

SELECT
  -- THIS DECODE( statement should not be included
 CASE  1  WHEN  1  THEN  'But this one should'  END 
FROM DUAL;

/* 

   /* 
      and a nested comment , lets try some DECODE( 
   */ 
  And another decode( 'with unbalanced parenthesis' , 'inside a comment' , 'should not be converted'
*/

SELECT 
 CASE  

        CASE  1  WHEN  1  THEN  '( phony retval'  END   WHEN  

 -- Comment/newlines shoule be ignored

       ''  THEN  ''  ELSE  'y0 dAwG - I heard you liked decoeds - so I put a decode in your decode' 
        END  
FROM DUAL;

SELECT  CASE  NVL(1,1)  WHEN  1  THEN  'foo' WHEN  2  THEN  NVL('','bar')  END  FROM DUAL;

/* Trailing comment */

sql-decode-to-case's People

Contributors

np422 avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

sql-decode-to-case's Issues

NVL to CASE feature request

I'm using a ANSI92 SQL product that does not support COALESCE and thus need to convert all NVL to CASE statements. Is that an easy addition to this utility? DECODE conversions work great, presumably NVL would help and is very similar.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.