ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

创建自己的postgresql日期数据类型

2022-01-07 10:58:53  阅读:255  来源: 互联网

标签:date1 postgresql int res 数据类型 current 日期 NULL


研究了一下postgresql的日期数据类型的二进制存贮方式。发现是存贮是自2000年1月1号以来的天数。这样直接传入二进制参数会比较麻烦。因些参考了一些postgresql扩展的项目如:https://github.com/pgstuff/base32_4b

增加了一个date1的数据类型,存贮格式为 (year << 16) | ((month - 1) << 8) | (mday - 1)

select current_date1(),current_date;
 current_date1 | current_date
---------------+--------------
 2022-01-07    | 2022-01-07
(1 row)

用如下代码打印二进制内容:

  static const char query_cfg_cmd[] = "select current_date1(),CURRENT_DATE";
  PGconn     *conn;
  PGresult   *res;

  conn = PQconnectdb(conninfo);
  res = PQexecParams(conn, query_cfg_cmd,
                     0,           /* one param */
                     NULL,        /* let the backend deduce param type */
                     NULL,
                     NULL,        /* don't need param lengths since text */
                     NULL,        /* default to all text params */
                     1);          /* ask for binary results */

  show_binary_results(res);

static void
show_binary_results(PGresult *res)
{
  int i, j;
  /* first, print out the attribute names */
  int nFields = PQnfields(res);

  for (i = 0; i < PQntuples(res); i++)
    {
      for(j=0; j< nFields; ++j)
        if(0 == PQgetisnull(res, i, j))
        {
          int len = PQgetlength(res, i, j);
          char* vp = PQgetvalue(res, i, j);
          if(4 == len || 8 == len)
            {
              printf("%-15s=", PQfname(res, j));
              for(int k=0; k<len; ++k, ++vp)
                printf("%02x ", *(uint8_t*)vp);
              printf("\n");
            }
          else
            {
              printf("%-15s=%.*s\n", PQfname(res, j), len, vp);
            }
        }
    }
}

current_date1  =07 e6 00 06
current_date   =00 00 1f 6a

标签:date1,postgresql,int,res,数据类型,current,日期,NULL
来源: https://blog.csdn.net/alpbrook/article/details/122359532

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有