如何在现有模式中查找 Informix DATETIME 字段限定符

How to find an Informix DATETIME field qualifier in an existing schema

我有一个 table 这样的:

create table t (
  t0 datetime year to fraction,
  t1 datetime year to fraction(1),
  t2 datetime year to fraction(2),
  t3 datetime year to fraction(3),
  t4 datetime year to fraction(4)
);

现在我想对 table 的数据类型信息进行逆向工程。我最感兴趣的是小数秒部分,但如果我能找到其他限定符信息,那就更好了。以下查询无效:

select 
  c.colname::varchar(10) colname,
  informix.schema_coltypename(c.coltype, c.extended_id)::varchar(10) coltypename,
  c.collength,
  informix.schema_precision(c.coltype, c.extended_id, c.collength) precision,
  informix.schema_numscale(c.coltype, c.collength) numscale,
  informix.schema_datetype(c.coltype, c.collength) datetype,
  c.coltype
from syscolumns c
join systables t on c.tabid = t.tabid
where t.tabname = 't'

它产生

|colname   |coltypename|collength|precision  |numscale   |datetype   |coltype|
|----------|-----------|---------|-----------|-----------|-----------|-------|
|t0        |DATETIME   |4365     |4365       |           |60         |10     |
|t1        |DATETIME   |3851     |3851       |           |60         |10     |
|t2        |DATETIME   |4108     |4108       |           |60         |10     |
|t3        |DATETIME   |4365     |4365       |           |60         |10     |
|t4        |DATETIME   |4622     |4622       |           |60         |10     |

collength 似乎包含相关信息,但我无法使用 schema_precisionschema_numscale 提取它,否则可能会出现数字精度。此外,schema_datetype 没有产生有趣的结果。

如何将 coltype 信息反​​向工程回 datetime year to fraction(N)

基于文档 Time data types

For columns of type DATETIME or INTERVAL, collength is determined using the following formula:

(length * 256) + (first_qualifier * 16) + last_qualifier

The length is the physical length of the DATETIME or INTERVAL field, and first_qualifier and last_qualifier have values that the following table shows.

+------------------+--------+------------------+-------+
| Field qualifier  | Value  | Field qualifier  | Value |
+------------------+--------+------------------+-------+
| YEAR             |     0  | FRACTION(1)      |    11 |
| MONTH            |     2  | FRACTION(2)      |    12 |
| DAY              |     4  | FRACTION(3)      |    13 |
| HOUR             |     6  | FRACTION(4)      |    14 |
| MINUTE           |     8  | FRACTION(5)      |    15 |
| SECOND           |    10  |                  |       |
+------------------+--------+------------------+-------+

计算(十六进制值更容易发现模式):

  t1 datetime year to fraction(1),  15*256 + 0*16+11 = 3851   0x0F0B
  t2 datetime year to fraction(2),  16*256 + 0*16+12 = 4108   0x100C
  t3 datetime year to fraction(3),  17*256 + 0*16+13 = 4365   0x110D
  t4 datetime year to fraction(4),  18*256 + 0*16+14 = 4622   0x120E

如果长度已知,那么即使使用 "brute force" 也可以对其进行逆向工程。

查找:

WITH l(v) AS (
  VALUES (12),(13),(14),(15),(16),(17),(18)
), first_q(v, first_qualifier) AS (
  VALUES (0,'YEAR'),(2,'MONTH'),(4,'DAY'),(6,'HOUR'),(8,'MINUTE'),(10, 'SECOND')
), last_q(v, last_qualifier) AS (
  VALUES (11, 'FRACTION(0)'),(12, 'FRACTION(1)'),(13, 'FRACTION(2)'),
         (14, 'FRACTION(3)'),(15, 'FRACTION(4)')
), result AS (
  SELECT  l.v * 256 + (first_q.v * 256) + last_q.v AS collen, *
  FROM l CROSS JOIN first_q CROSS JOIN last_q
)
SELECT *
FROM result
--WHERE collen = 3851

db<>fiddle demo