/************************************************
 * FUNCTION: Fix varchar length display error. 
 * 
 * WRITE BY: liaosnet@gbasedbt.com 2023-07-19
 ************************************************/
-- 1, change function schema_precision to schema_precision_smallint
UPDATE sysprocedures SET procname = 'schema_precision_smallint' 
WHERE procname = 'schema_precision' AND externalname = '(schema_precision)';

-- create function schema_precision
UPDATE sysprocedures SET mode = 'D' WHERE procname = 'schema_precision';
DROP FUNCTION IF EXISTS schema_precision;

CREATE dba FUNCTION gbasedbt.schema_precision(coltype int, xtype int, collength int)
returning int with (not variant);
  ON EXCEPTION
    RETURN collength;
  END EXCEPTION;
  IF mod(coltype,256) in (13,16) THEN
    RETURN bitand(collength,65535);
  ELSE
    RETURN schema_precision_smallint(coltype,xtype,collength);
  END IF;
END FUNCTION;

-- modify function mode to 'o' which will not display in dbschema/dbexport.
UPDATE sysprocedures SET mode = 'o' WHERE procname = 'schema_precision';


-- 2, change function schema_charlen to schema_charlen_smallint
UPDATE sysprocedures SET procname = 'schema_charlen_smallint' 
WHERE procname = 'schema_charlen' AND externalname = '(schema_charlen)';

-- create function schema_charlen
UPDATE sysprocedures SET mode = 'D' WHERE procname = 'schema_charlen';
DROP FUNCTION IF EXISTS schema_charlen;

CREATE dba FUNCTION gbasedbt.schema_charlen(coltype int, xtype int, collength int)
returning int with (not variant);
  ON EXCEPTION
    RETURN collength;
  END EXCEPTION;
  IF mod(coltype,256) in (13,16) THEN
    RETURN bitand(collength,65535);
  ELSE
    RETURN schema_charlen_smallint(coltype,xtype,collength);
  END IF;
END FUNCTION;

-- modify function mode to 'o' which will not display in dbschema/dbexport.
UPDATE sysprocedures SET mode = 'o' WHERE procname = 'schema_charlen';


-- 3, change function schema_numscale to schema_numscale_smallint
UPDATE sysprocedures SET procname = 'schema_numscale_smallint' 
WHERE procname = 'schema_numscale' AND externalname = '(schema_numscale)';

-- create function schema_numscale
UPDATE sysprocedures SET mode = 'D' WHERE procname = 'schema_numscale';
DROP FUNCTION IF EXISTS schema_numscale;

CREATE dba FUNCTION gbasedbt.schema_numscale(coltype int, collength int)
returning int with (not variant);
  ON EXCEPTION
    RETURN 0;
  END EXCEPTION;
  IF mod(coltype,256) in (13,16) THEN
    RETURN schema_numscale_smallint(coltype,bitand(collength,65535));
  ELSE
    RETURN schema_numscale_smallint(coltype,collength);
  END IF;
END FUNCTION;

-- modify function mode to 'o' which will not display in dbschema/dbexport.
UPDATE sysprocedures SET mode = 'o' WHERE procname = 'schema_numscale';


-- 4, change function schema_datetype to schema_datetype_smallint
UPDATE sysprocedures SET procname = 'schema_datetype_smallint' 
WHERE procname = 'schema_datetype' AND externalname = '(schema_datetype)';

-- create function schema_datetype
UPDATE sysprocedures SET mode = 'D' WHERE procname = 'schema_datetype';
DROP FUNCTION IF EXISTS schema_datetype;

CREATE dba FUNCTION gbasedbt.schema_datetype(coltype int, collength int)
returning int with (not variant);
  ON EXCEPTION
    RETURN 0;
  END EXCEPTION;
  IF mod(coltype,256) in (13,16) THEN
    RETURN schema_datetype_smallint(coltype,bitand(collength,65535));
  ELSE
    RETURN schema_datetype_smallint(coltype,collength);
  END IF;
END FUNCTION;

-- modify function mode to 'o' which will not display in dbschema/dbexport.
UPDATE sysprocedures SET mode = 'o' WHERE procname = 'schema_datetype';


-- 5, change function schema_bufflen to schema_bufflen_smallint
UPDATE sysprocedures SET procname = 'schema_bufflen_smallint' 
WHERE procname = 'schema_bufflen' AND externalname = '(schema_bufflen)';

-- create function schema_bufflen
UPDATE sysprocedures SET mode = 'D' WHERE procname = 'schema_bufflen';
DROP FUNCTION IF EXISTS schema_bufflen;

CREATE dba FUNCTION gbasedbt.schema_bufflen(coltype int, collength int)
returning int with (not variant);
  ON EXCEPTION
    RETURN 0;
  END EXCEPTION;
  IF mod(coltype,256) in (13,16) THEN
    RETURN schema_bufflen_smallint(coltype,bitand(collength,65535));
  ELSE
    RETURN schema_bufflen_smallint(coltype,collength);
  END IF;
END FUNCTION;

-- modify function mode to 'o' which will not display in dbschema/dbexport.
UPDATE sysprocedures SET mode = 'o' WHERE procname = 'schema_bufflen';