informix_backend.ec

来自「gxsnmp SNMP MANAGER 的实现」· EC 代码 · 共 659 行 · 第 1/2 页

EC
659
字号
      return TRUE;    }  g_warning("DATABASE %s failed.", database);  print_error();    return FALSE;}/***********************************************************************************  sql_query ()  --  Issue a database query to an open Informix server connection****  Returns Values:****  The return value is an open database query structure, or NULL if the**  operation failed.*********************************************************************************/static G_sql_query  * sql_query (G_sql_connection * dbc, gchar * query, gint querylen){  EXEC SQL BEGIN DECLARE SECTION;  char *execstr;  char *cname;  char *qname;  char *dname;  EXEC SQL END DECLARE SECTION;  G_sql_query  *dbq;  if (!(dbq = (G_sql_query *) g_new0 (G_sql_query, 1)))    {      g_print ("Informix backend: out of memory\n");      return NULL;    }  dbq->connection = dbc;  g_snprintf(dbq->curname, sizeof(dbq->curname), "c_%d", cursor_cnt);  g_snprintf(dbq->desname, sizeof(dbq->desname), "d_%d", cursor_cnt);  g_snprintf(dbq->qidname, sizeof(dbq->qidname), "q_%d", cursor_cnt++);  cname = dbq->curname;  dname = dbq->desname;  qname = dbq->qidname;  g_print ("cname: %s, dname: %s, qname: %s\n", cname, dname, qname);  execstr=query;  EXEC SQL PREPARE :qname FROM :execstr;  EXEC SQL ALLOCATE DESCRIPTOR :dname;  EXEC SQL DESCRIBE :qname USING SQL DESCRIPTOR :dname;  if (SQLCODE)          /* no SELECT statement */    {      g_print("No select statement in %s, no cursor needed.\n", query);      strcpy(dbq->curname, "");      EXEC SQL EXECUTE :qname;      return dbq;    }  EXEC SQL DECLARE :cname CURSOR FOR :qname;  if(SQLCODE)    {      g_warning("Error in SELECT statement.");      print_error();      EXEC SQL FREE :cname;      EXEC SQL FREE :qname;      EXEC SQL DEALLOCATE DESCRIPTOR :dname;      return NULL;    }  g_print("Opening CURSOR for %s.\n",query);  EXEC SQL OPEN :cname;  return dbq;}/***********************************************************************************  sql_free_query ()  --  Free up a query structure*********************************************************************************/static gbooleansql_free_query (G_sql_query *dbq){  EXEC SQL BEGIN DECLARE SECTION;  char *cname;  char *qname;  char *dname;  EXEC SQL END DECLARE SECTION;  cname = dbq->curname;  dname = dbq->desname;  qname = dbq->qidname;  if (strcmp(cname,""))    {      EXEC SQL CLOSE :cname;      EXEC SQL FREE :cname;    }  EXEC SQL FREE :qname;  EXEC SQL DEALLOCATE DESCRIPTOR :dname;  g_free (dbq);  return TRUE;}/***********************************************************************************  sql_next_row ()  --  Select the next row in a query result****  This subroutine returns the private handle of the next available row**  in a query result.****  Return values:****  TRUE  --  If the operation succeeded**  FALSE --  If the operation failed*********************************************************************************/static gboolean sql_next_row (G_sql_query * dbq){  EXEC SQL BEGIN DECLARE SECTION;  char *cname;  char *qname;  char *dname;  EXEC SQL END DECLARE SECTION;  cname = dbq->curname;  dname = dbq->desname;  qname = dbq->qidname;  EXEC SQL fetch :cname USING SQL DESCRIPTOR :dname;  if(!SQLCODE)    {      return TRUE;    }  return FALSE;}/***********************************************************************************  sql_field ()  --  Read a named field from a database row,**                          and return a pointer to the data, and the length**			    of the returned data.****  This subroutine returns the named field from a selected database row.**  This subroutine may read binary data that might contain zeroes.   ****  Return values:****  The return value is a pointer to the raw database field data, or NULL if**  the operation failed.  The length of the result is stored in the "length"**  parameter.****  The caller is responsible for g_free()'ing the allocated storage.****  Since searching through the field table each time we are called is **  expensive, accelerators are used to speed up this process for subsequent**  row lookups on the same query.  The accelerator field, provided by the**  caller, will be unique for each field name.  We simply store the found**  row index plus one in the caller's accelerator field.  Then, on**  subsequent calls, we can skip the search by using the accelerator**  value minus one for the field index.*********************************************************************************/static gchar * sql_field (G_sql_query * dbq,   G_sql_accelerator *accel,			  gchar       * field, gint  * length){EXEC SQL BEGIN DECLARE SECTION;  int i, type, dec, ind, count, len;  char name[40];  static char ch[1024];  long dat;  float flt;  char *cname;  char *qname;  char *dname;EXEC SQL END DECLARE SECTION;  static char buf [128];  int j;  cname = dbq->curname;  dname = dbq->desname;  qname = dbq->qidname;  EXEC SQL GET DESCRIPTOR :dname :count = COUNT;  if (!*accel)      for (i = 1; i <= count; i++)        {          EXEC SQL GET DESCRIPTOR :dname VALUE :i :type = TYPE, :name = NAME;          j = strlen(name);          if (j) j--;          while(j && name[j] == ' ')            {              name[j] = '\0';              j--;            }          if (!g_strcasecmp (field, name))            *accel = (G_sql_accelerator) i;        }  if (!*accel)    {      g_print ("Informix backend:  Field %s not found in selected row\n", field);      return NULL;    }  i = (int)(*accel);  EXEC SQL GET DESCRIPTOR :dname VALUE :i :type = TYPE;  switch (type)    {      case SQLSERIAL:      case SQLINT:      case SQLSMINT:      case SQLDECIMAL:      case SQLMONEY:        EXEC SQL GET DESCRIPTOR :dname VALUE :i :ind = INDICATOR, :dec = DATA;        if (ind == -1)          {            *length = 0;            return NULL;          }        g_snprintf(buf, sizeof(buf), "%d", dec);        *length = strlen(buf);        return buf;        break;      case SQLFLOAT:        EXEC SQL GET DESCRIPTOR :dname VALUE :i :ind = INDICATOR, :flt = DATA;        if (ind == -1)          {            *length = 0;            return NULL;          }        g_snprintf(buf, sizeof(buf), "%f", flt);        *length = strlen(buf);        return buf;        break;      case SQLDATE:        EXEC SQL GET DESCRIPTOR :dname VALUE :i :ind = INDICATOR, :dat = DATA;        if (ind == -1)          {            *length = 0;            return NULL;          }        rfmtdate(dat, "mmm. dd, yyyy", buf);        *length = strlen(buf);        return buf;        break;      case SQLVCHAR:      case SQLCHAR:        EXEC SQL GET DESCRIPTOR :dname VALUE :i :ind = INDICATOR,                                                 :ch = DATA, :len =LENGTH;        if (ind == -1)          {            *length = 0;            return NULL;          }#if 0	/* Apparently the len parameter means the length of the field, not           the lenther of the response in Informix */        *length = len;        ch[len] = '\0';#else        j = strlen(ch);        if (j) j--;        while(j && ch[j] == ' ')          {            ch[j] = '\0';            j--;          }        *length = strlen(ch);#endif        return ch;    }}/**********************************************************************************  Subroutine to load the plugin.  Set the plugin type to PLUGIN_DATABASE.********************************************************************************/int load_plugin (PluginData * pd){  pd->type = PLUGIN_DATABASE;  pd->name = g_strdup ("Informix DB backend");  return 0;}/********************************************************************************** Subroutine to unload the plugin********************************************************************************/voidunload_plugin (PluginData * pd){  g_sql_unregister_backend (&backend);}/**********************************************************************************  Subroutine to start the plugin********************************************************************************/voidstart_plugin (PluginData * pd){  g_print ("Starting Informix backend plugin ... ");  g_sql_register_backend (&backend);}/* EOF */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?