Initial core-pgsql support based on native libpq; FS starts and stops without errors using core-pgsql.

This commit is contained in:
Eliot Gable
2012-09-30 01:48:48 +00:00
parent 0263ce9247
commit 5cb354dddc
17 changed files with 1283 additions and 90 deletions

View File

@@ -135,6 +135,7 @@
#include "switch_config.h"
#include "switch_nat.h"
#include "switch_odbc.h"
#include "switch_pgsql.h"
#include "switch_json.h"
#include "switch_limit.h"

View File

@@ -2210,12 +2210,14 @@ typedef enum {
typedef enum {
SCDB_TYPE_CORE_DB,
SCDB_TYPE_ODBC
SCDB_TYPE_ODBC,
SCDB_TYPE_PGSQL
} switch_cache_db_handle_type_t;
typedef union {
switch_core_db_t *core_db_dbh;
switch_odbc_handle_t *odbc_dbh;
switch_pgsql_handle_t *pgsql_dbh;
} switch_cache_db_native_handle_t;
typedef struct {
@@ -2228,9 +2230,14 @@ typedef struct {
char *pass;
} switch_cache_db_odbc_options_t;
typedef struct {
char *dsn;
} switch_cache_db_pgsql_options_t;
typedef union {
switch_cache_db_core_db_options_t core_db_options;
switch_cache_db_odbc_options_t odbc_options;
switch_cache_db_pgsql_options_t pgsql_options;
} switch_cache_db_connection_options_t;
struct switch_cache_db_handle;
@@ -2241,6 +2248,11 @@ static inline const char *switch_cache_db_type_name(switch_cache_db_handle_type_
const char *type_str = "INVALID";
switch (type) {
case SCDB_TYPE_PGSQL:
{
type_str = "PGSQL";
}
break;
case SCDB_TYPE_ODBC:
{
type_str = "ODBC";

162
src/include/switch_pgsql.h Normal file
View File

@@ -0,0 +1,162 @@
/*
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2005-2012, Anthony Minessale II <anthm@freeswitch.org>
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
*
* The Initial Developer of the Original Code is
* Anthony Minessale II <anthm@freeswitch.org>
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Anthony Minessale II <anthm@freeswitch.org>
* Eliot Gable <egable@gmail.com>
*
* switch_pgsql.h -- PGSQL Driver
*
*/
#ifndef SWITCH_PGSQL_H
#define SWITCH_PGSQL_H
#include <switch.h>
#define DEFAULT_PGSQL_RETRIES 120
SWITCH_BEGIN_EXTERN_C
struct switch_pgsql_handle;
struct switch_pgsql_result;
typedef enum {
SWITCH_PGSQL_STATE_INIT,
SWITCH_PGSQL_STATE_DOWN,
SWITCH_PGSQL_STATE_CONNECTED,
SWITCH_PGSQL_STATE_ERROR
} switch_pgsql_state_t;
typedef enum {
SWITCH_PGSQL_SUCCESS = 0,
SWITCH_PGSQL_FAIL = -1
} switch_pgsql_status_t;
/*!
\brief Create a new handle for the PGSQL connection.
\param dsn The DSN of the database to connect to. See documentation for PQconnectdb() at
http://www.postgresql.org/docs/9.0/static/libpq-connect.html. The DSN *MUST* be
prefixed with 'pgsql;' to use the switch_cache_db* functionality. However, the DSN
passed to this function directly *MUST NOT* be prefixed with 'pgsql;'.
\return Returns a pointer to a newly allocated switch_pgsql_handle_t type or NULL on failure.
*/
SWITCH_DECLARE(switch_pgsql_handle_t *) switch_pgsql_handle_new(const char *dsn);
/*!
\brief Sets the number of retries if the PGSQL connection fails.
\param handle A fully allocated switch_pgsql_handle_t returned from a call to switch_pgsql_handle_new().
\param num_retries How many times to retry connecting to the database if this connection fails.
*/
SWITCH_DECLARE(void) switch_pgsql_set_num_retries(switch_pgsql_handle_t *handle, int num_retries);
/*!
\brief Disconnects a PGSQL connection from the database.
\param handle The PGSQL database handle to disconnect.
\return Returns SWITCH_PGSQL_SUCCESS or SWITCH_PGSQL_FAIL.
*/
SWITCH_DECLARE(switch_pgsql_status_t ) switch_pgsql_handle_disconnect(switch_pgsql_handle_t *handle);
#if 0
) /* Emacs formatting issue */
#endif
/*!
\brief Connect to the database specified by the DSN passed to the switch_pgsql_handle_new() call which
initialized this handle.
\param The database handle to connect to the database.
\return Returns SWITCH_PGSQL_SUCCESS or SWITCH_PGSQL_FAIL.
*/
SWITCH_DECLARE(switch_pgsql_status_t) switch_pgsql_handle_connect(switch_pgsql_handle_t *handle);
/*!
*/
SWITCH_DECLARE(void) switch_pgsql_handle_destroy(switch_pgsql_handle_t **handlep);
SWITCH_DECLARE(switch_pgsql_status_t) switch_pgsql_send_query(switch_pgsql_handle_t *handle, const char* sql);
SWITCH_DECLARE(switch_pgsql_status_t) switch_pgsql_cancel_real(const char *file, const char *func, int line, switch_pgsql_handle_t *handle);
#define switch_pgsql_cancel(handle) switch_pgsql_cancel_real(__FILE__, (char * )__SWITCH_FUNC__, __LINE__, handle)
SWITCH_DECLARE(switch_pgsql_status_t) switch_pgsql_next_result_timed(switch_pgsql_handle_t *handle, switch_pgsql_result_t **result_out, int seconds);
#define switch_pgsql_next_result(h, r) switch_pgsql_next_result_timed(h, r, 10000)
SWITCH_DECLARE(void) switch_pgsql_free_result(switch_pgsql_result_t **result);
SWITCH_DECLARE(switch_pgsql_status_t) switch_pgsql_finish_results_real(const char* file, const char *func, int line, switch_pgsql_handle_t *handle);
#define switch_pgsql_finish_results(handle) switch_pgsql_finish_results_real(__FILE__, (char * )__SWITCH_FUNC__, __LINE__, handle)
SWITCH_DECLARE(switch_pgsql_status_t) switch_pgsql_handle_exec_base(switch_pgsql_handle_t *handle, const char *sql, char **err);
SWITCH_DECLARE(switch_pgsql_state_t) switch_pgsql_handle_get_state(switch_pgsql_handle_t *handle);
SWITCH_DECLARE(switch_pgsql_status_t) switch_pgsql_handle_exec(switch_pgsql_handle_t *handle, const char *sql, char **err);
SWITCH_DECLARE(switch_pgsql_status_t) switch_pgsql_handle_exec_string(switch_pgsql_handle_t *handle, const char *sql, char *resbuf, size_t len, char **err);
SWITCH_DECLARE(switch_bool_t) switch_pgsql_available(void);
SWITCH_DECLARE(switch_pgsql_status_t) switch_pgsql_SQLSetAutoCommitAttr(switch_pgsql_handle_t *handle, switch_bool_t on);
SWITCH_DECLARE(switch_pgsql_status_t) switch_pgsql_SQLEndTran(switch_pgsql_handle_t *handle, switch_bool_t commit);
/*!
\brief Execute the sql query and issue a callback for each row returned
\param file the file from which this function is called
\param func the function from which this function is called
\param line the line from which this function is called
\param handle the PGSQL handle
\param sql the sql string to execute
\param callback the callback function to execute
\param pdata the state data passed on each callback invocation
\return SWITCH_STATUS_SUCCESS if the operation was successful
\note none
*/
SWITCH_DECLARE(switch_pgsql_status_t) switch_pgsql_handle_callback_exec_detailed(const char *file, const char *func, int line, switch_pgsql_handle_t *handle,
const char *sql, switch_core_db_callback_func_t callback, void *pdata,
char **err);
/*!
\brief Execute the sql query and issue a callback for each row returned
\param handle the PGSQL handle
\param sql the sql string to execute
\param callback the callback function to execute
\param pdata the state data passed on each callback invocation
\return SWITCH_STATUS_SUCCESS if the operation was successful
\note none
*/
#define switch_pgsql_handle_callback_exec(handle, sql, callback, pdata, err) \
switch_pgsql_handle_callback_exec_detailed(__FILE__, (char * )__SWITCH_FUNC__, __LINE__, \
handle, sql, callback, pdata, err)
SWITCH_DECLARE(char *) switch_pgsql_handle_get_error(switch_pgsql_handle_t *handle);
SWITCH_DECLARE(int) switch_pgsql_handle_affected_rows(switch_pgsql_handle_t *handle);
SWITCH_END_EXTERN_C
#endif
/* For Emacs:
* Local Variables:
* mode:c
* indent-tabs-mode:t
* tab-width:4
* c-basic-offset:4
* End:
* For VIM:
* vim:set softtabstop=4 shiftwidth=4 tabstop=4:
*/

View File

@@ -324,7 +324,7 @@ typedef enum {
SCF_CLEAR_SQL = (1 << 17),
SCF_THREADED_SYSTEM_EXEC = (1 << 18),
SCF_SYNC_CLOCK_REQUESTED = (1 << 19),
SCF_CORE_ODBC_REQ = (1 << 20),
SCF_CORE_NON_SQLITE_DB_REQ = (1 << 20),
SCF_DEBUG_SQL = (1 << 21),
SCF_API_EXPANSION = (1 << 22),
SCF_SESSION_THREAD_POOL = (1 << 23)
@@ -1833,6 +1833,8 @@ typedef struct switch_buffer switch_buffer_t;
typedef struct switch_codec_settings switch_codec_settings_t;
typedef struct switch_codec_fmtp switch_codec_fmtp_t;
typedef struct switch_odbc_handle switch_odbc_handle_t;
typedef struct switch_pgsql_handle switch_pgsql_handle_t;
typedef struct switch_pgsql_result switch_pgsql_result_t;
typedef struct switch_io_routines switch_io_routines_t;
typedef struct switch_speech_handle switch_speech_handle_t;