⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 postgresql_backend.c

📁 gxsnmp SNMP MANAGER 的实现
💻 C
📖 第 1 页 / 共 2 页
字号:
    {      gchar * name;      name = g_strdup (PQgetvalue (result, i, 0));      dblist = g_list_append (dblist, name);    }  PQclear (result);  D_FUNC_END;  return dblist;}/***********************************************************************************  db_select ()  --  Select a database on an open PostgreSQL server connection****  For PostgreSQL, we have to close the current connection and open a new**  connection.****  Returns:  TRUE  -- if the select operation was successful**            FALSE -- if the select operation failed*********************************************************************************/static gbooleansql_select (G_sql_connection * dbc, gchar * database) {  PGconn * oldpgconn;  D_FUNC_START;  g_return_val_if_fail (NULL != dbc, FALSE);  g_return_val_if_fail (NULL != database, FALSE);  d_print (DEBUG_DUMP, "selecting database %s\n", database);  /* Check if the same database is selected */  if (0 == strcmp (database, PQdb (dbc->pgconn)))    {      D_FUNC_END;      return TRUE;    }  oldpgconn = dbc->pgconn;  dbc->pgconn = PQsetdbLogin (PQhost (oldpgconn), PQport (oldpgconn),                              NULL, NULL, database, PQuser (oldpgconn), 			      PQpass (oldpgconn));  if (NULL == dbc->pgconn ||      CONNECTION_BAD == PQstatus (dbc->pgconn))    {      g_print ("PostgreSQL backend: Unable to select '%s' database.\n",	       database);      g_print ("PostgreSQL backend: %s\n", PQerrorMessage (dbc->pgconn));      PQfinish (dbc->pgconn);      dbc->pgconn = oldpgconn;      D_FUNC_END;      return FALSE;    }  else    PQfinish (oldpgconn);  D_FUNC_END;  return TRUE;}/***********************************************************************************  sql_query ()  --  Issue a database query to an open PostgreSQL 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){  G_sql_query *dbq;  ExecStatusType status;  D_FUNC_START;  if (!(dbq = (G_sql_query *) g_new0 (G_sql_query, 1)))    {      g_warning ("PostgreSQL backend: out of memory\n");      return NULL;    }  dbq->connection = dbc;  d_print (DEBUG_QUERIES, "query: %s\n", query);  dbq->result = PQexec (dbc->pgconn, query);  if (NULL == dbq->result ||      PGRES_BAD_RESPONSE == (status = PQresultStatus (dbq->result)) ||      PGRES_NONFATAL_ERROR == status ||      PGRES_FATAL_ERROR == status)    {      g_print ("PostgreSQL Backend: Query '%s' failed\n", query);      g_print ("PostgreSQL Backend: %s\n", PQresultErrorMessage (dbq->result));      PQclear (dbq->result);      g_free (dbq);      return NULL;    }  dbq->tuples = PQntuples (dbq->result);  dbq->fields = PQnfields (dbq->result);  dbq->row = -1;  D_FUNC_END;  return dbq;}/***********************************************************************************  sql_free_query ()  --  Free up a query structure*********************************************************************************/static gbooleansql_free_query (G_sql_query * dbq){  D_FUNC_START;  g_return_val_if_fail (dbq != NULL, FALSE);  PQclear (dbq->result);  g_free (dbq);  D_FUNC_END;  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){  D_FUNC_START;  g_return_val_if_fail (dbq != NULL, FALSE);  ++dbq->row;  if (dbq->row >= dbq->tuples)    return FALSE;  D_FUNC_END;  return TRUE;}/***********************************************************************************  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){  int i;     D_FUNC_START;  g_return_val_if_fail (dbq != NULL, FALSE);  g_return_val_if_fail (dbq->row >= 0 && dbq->row < dbq->tuples, FALSE);  d_print (DEBUG_DUMP, "field %s\n", field);  if (!*accel)      for (i = 0; i < dbq->fields; i++)        if (!g_strcasecmp (field, PQfname (dbq->result, i)))          *accel = (G_sql_accelerator) (i + 1);  if (!*accel)    {      d_print (DEBUG_DUMP, "Field %s not found in selected row\n", field);      D_FUNC_END;      return NULL;    }  i = (int)(*accel) - 1;  *length = (gint) PQgetlength (dbq->result, dbq->row, i);  D_FUNC_END;  return PQgetvalue(dbq->result, dbq->row, i);}/**********************************************************************************  Subroutine to load the plugin.  Set the plugin type to PLUGIN_DATABASE.********************************************************************************/int load_plugin (PluginData * pd){  D_FUNC_START;  pd->type = PLUGIN_DATABASE;  pd->name = g_strdup ("PostgreSQL DB backend");  D_FUNC_END;  return 0;}/********************************************************************************** Subroutine to unload the plugin********************************************************************************/voidunload_plugin (PluginData * pd){  D_FUNC_START;  g_sql_unregister_backend (&backend);  D_FUNC_END;}/**********************************************************************************  Subroutine to start the plugin********************************************************************************/voidstart_plugin (PluginData * pd){  D_FUNC_START;  app_update_init_status ("Starting plugins.", "PostgreSQL backend");  g_sql_register_backend (&backend);  D_FUNC_START;}/* EOF */

⌨️ 快捷键说明

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