1 /***************************************************************************
2 * iODBC support for FreeRadius
3 * www.iodbc.org - iODBC info
4 * Jeff Carneal - Author
5 ***************************************************************************/
14 /*************************************************************************
16 * Function: sql_create_socket
18 * Purpose: Establish connection to the db
20 *************************************************************************/
21 SQLSOCK *sql_create_socket(SQL_INST *inst)
25 if((socket = malloc(sizeof(SQLSOCK))) == NULL) {
26 radlog(L_CONS|L_ERR, "sql_create_socket: no memory");
30 if(SQLAllocEnv(&socket->env_handle) != SQL_SUCCESS) {
31 radlog(L_CONS|L_ERR, "sql_create_socket: SQLAllocEnv failed: %s",
36 if(SQLAllocConnect(socket->env_handle, &socket->dbc_handle) != SQL_SUCCESS) {
37 radlog(L_CONS|L_ERR, "sql_create_socket: SQLAllocConnect failed: %s",
42 if (SQLConnect(socket->dbc_handle, inst->config->sql_db, SQL_NTS,
43 inst->config->sql_login, SQL_NTS, inst->config->sql_password,
44 SQL_NTS) != SQL_SUCCESS) {
45 radlog(L_CONS|L_ERR, "sql_create_socket: SQLConnectfailed: %s",
50 if(SQLAllocStmt(socket->dbc_handle, &socket->stmt_handle) != SQL_SUCCESS) {
51 radlog(L_CONS|L_ERR, "sql_create_socket: SQLAllocStmt failed: %s",
59 /*************************************************************************
63 * Purpose: Issue a non-SELECT query (ie: update/delete/insert) to
66 *************************************************************************/
67 int sql_query(SQL_INST *inst, SQLSOCK *socket, char *querystr)
69 if (inst->config->sqltrace)
71 if (socket->dbc_handle == NULL) {
72 radlog(L_ERR, "sql_query: Socket not connected");
76 if(SQLExecDirect(socket->stmt_handle, querystr, SQL_NTS) != SQL_SUCCESS) {
77 radlog(L_CONS|L_ERR, "sql_query: failed: %s",
86 /*************************************************************************
88 * Function: sql_select_query
90 * Purpose: Issue a select query to the database
92 *************************************************************************/
93 int sql_select_query(SQL_INST *inst, SQLSOCK *socket, char *querystr)
99 /*************************************************************************
101 * Function: sql_store_result
103 * Purpose: database specific store_result function. Returns a result
106 *************************************************************************/
107 int sql_store_result(SQLSOCK *socket) {
112 /*************************************************************************
114 * Function: sql_num_fields
116 * Purpose: database specific num_fields function. Returns number
117 * of columns from query
119 *************************************************************************/
120 int sql_num_fields(SQLSOCK *socket) {
124 /*************************************************************************
126 * Function: sql_num_rows
128 * Purpose: database specific num_rows. Returns number of rows in
131 *************************************************************************/
132 int sql_num_rows(SQLSOCK *socket) {
137 /*************************************************************************
139 * Function: sql_fetch_row
141 * Purpose: database specific fetch_row. Returns a SQL_ROW struct
142 * with all the data for the query
144 *************************************************************************/
145 SQL_ROW sql_fetch_row(SQLSOCK *socket)
152 /*************************************************************************
154 * Function: sql_free_result
156 * Purpose: database specific free_result. Frees memory allocated
159 *************************************************************************/
160 void sql_free_result(SQLSOCK *socket) {
165 /*************************************************************************
167 * Function: sql_error
169 * Purpose: database specific error. Returns error associated with
172 *************************************************************************/
173 char *sql_error(SQLSOCK *socket)
175 SQLINTEGER errornum = 0;
176 SQLSMALLINT length = 0;
177 SQLCHAR state[256] = "";
178 static SQLCHAR error[256] = "";
180 SQLError(socket->env_handle, socket->dbc_handle, socket->stmt_handle,
181 state, &errornum, error, 256, &length);
186 /*************************************************************************
188 * Function: sql_close
190 * Purpose: database specific close. Closes an open database
191 * connection and cleans up any open handles.
193 *************************************************************************/
194 void sql_close(SQLSOCK *socket)
197 SQLFreeStmt(socket->stmt_handle, SQL_DROP);
198 SQLDisconnect(socket->dbc_handle);
199 SQLFreeConnect(socket->dbc_handle);
200 SQLFreeEnv(socket->env_handle);
202 socket->stmt_handle = NULL;
203 socket->dbc_handle = NULL;
204 socket->env_handle = NULL;
208 /*************************************************************************
210 * Function: sql_finish_query
212 * Purpose: End the query, such as freeing memory
214 *************************************************************************/
215 void sql_finish_query(SQLSOCK *socket)
221 /*************************************************************************
223 * Function: sql_finish_select_query
225 * Purpose: End the select query, such as freeing memory or result
227 *************************************************************************/
228 void sql_finish_select_query(SQLSOCK *socket)
233 /*************************************************************************
235 * Function: sql_affected_rows
237 * Purpose: Return the number of rows affected by the query (update,
240 *************************************************************************/
241 int sql_affected_rows(SQLSOCK *socket) {
244 SQLRowCount(socket->stmt_handle, &count);
249 /*************************************************************************
251 * Function: sql_escape_string
253 * Purpose: Esacpe "'" and any other wierd charactors
255 *************************************************************************/
256 int sql_escape_string(char *to, char *from, int length)
260 for(x=0, y=0; (x < length) && (from[x]!='\0'); x++) {