github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/sql/sem/cast/cast_map_gen.sh (about)

     1  #!/bin/bash
     2  
     3  # casts_gen.sh generates castMap entries by reading from Postgres's pg_cast
     4  # table. To use this script, Postgres must be installed with the PostGIS
     5  # extension and already running.
     6  #
     7  # By default the script connects to the "postgres" database. To use a different
     8  # database, supply its name as the first argument to the script.
     9  
    10  DATABASE="${1-postgres}"
    11  
    12  psql $DATABASE -Xqtc "SELECT postgis_full_version()" &> /dev/null
    13  if [ $? -ne 0 ]; then
    14    echo "error: postgis must be installed in database $DATABASE";
    15    echo "hint: you can specify another database as the first argument";
    16    exit 1;
    17  fi
    18  
    19  PG_CAST_QUERY="
    20    SELECT
    21      (
    22        CASE castsource::REGTYPE::TEXT
    23        WHEN 'bigint' THEN 'int8'
    24        WHEN 'bit varying' THEN 'varbit'
    25        WHEN 'boolean' THEN 'bool'
    26        WHEN '\"char\"' THEN 'char'
    27        WHEN 'character' THEN 'bpchar'
    28        WHEN 'character varying' THEN 'varchar'
    29        WHEN 'double precision' THEN 'float8'
    30        WHEN 'integer' THEN 'int4'
    31        WHEN 'real' THEN 'float4'
    32        WHEN 'smallint' THEN 'int2'
    33        WHEN 'timestamp with time zone' THEN 'timestamptz'
    34        WHEN 'timestamp without time zone' THEN 'timestamp'
    35        WHEN 'time with time zone' THEN 'timetz'
    36        WHEN 'time without time zone' THEN 'time'
    37        ELSE castsource::REGTYPE::TEXT
    38        END
    39      ),
    40      (
    41        CASE casttarget::REGTYPE::TEXT
    42        WHEN 'bigint' THEN 'int8'
    43        WHEN 'bit varying' THEN 'varbit'
    44        WHEN 'boolean' THEN 'bool'
    45        WHEN '\"char\"' THEN 'char'
    46        WHEN 'character' THEN 'bpchar'
    47        WHEN 'character varying' THEN 'varchar'
    48        WHEN 'double precision' THEN 'float8'
    49        WHEN 'integer' THEN 'int4'
    50        WHEN 'real' THEN 'float4'
    51        WHEN 'smallint' THEN 'int2'
    52        WHEN 'timestamp with time zone' THEN 'timestamptz'
    53        WHEN 'timestamp without time zone' THEN 'timestamp'
    54        WHEN 'time with time zone' THEN 'timetz'
    55        WHEN 'time without time zone' THEN 'time'
    56        ELSE casttarget::REGTYPE::TEXT
    57        END
    58      ),
    59      (
    60        CASE castcontext
    61        WHEN 'e' THEN 'CastContextExplicit'
    62        WHEN 'a' THEN 'CastContextAssignment'
    63        WHEN 'i' THEN 'CastContextImplicit'
    64        END
    65      )
    66    FROM pg_cast
    67    ORDER BY 1, 2"
    68  
    69  psql $DATABASE --csv -Xqt -c "$PG_CAST_QUERY" |
    70    awk -F, '
    71      {
    72        if ($1 != src)
    73        {
    74          src = $1;
    75          if (NR > 1) print "},";
    76          print "oid.T_" $1 ": {";
    77        }
    78        print "\toid.T_" $2 ": {maxContext: " $3 ", origin: pgCast},";
    79      }
    80      END { print "}"; }'