-
Notifications
You must be signed in to change notification settings - Fork 2
/
sca_db.c
168 lines (147 loc) · 4.97 KB
/
sca_db.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* $Id$
*
* Copyright (C) 2012 Andrew Mortensen
*
* This file is part of the sca module for sip-router, a free SIP server.
*
* The sca module is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* The sca module is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*
*/
#include "sca_common.h"
#include "sca_db.h"
#include "sca_subscribe.h"
db1_con_t *sca_db_con = NULL;
const str SCA_DB_SUBSCRIBER_COL_NAME = STR_STATIC_INIT( "subscriber" );
const str SCA_DB_AOR_COL_NAME = STR_STATIC_INIT( "aor" );
const str SCA_DB_EVENT_COL_NAME = STR_STATIC_INIT( "event" );
const str SCA_DB_EXPIRES_COL_NAME = STR_STATIC_INIT( "expires" );
const str SCA_DB_STATE_COL_NAME = STR_STATIC_INIT( "state" );
const str SCA_DB_APP_IDX_COL_NAME = STR_STATIC_INIT( "app_idx" );
const str SCA_DB_CALL_ID_COL_NAME = STR_STATIC_INIT( "call_id" );
const str SCA_DB_FROM_TAG_COL_NAME = STR_STATIC_INIT( "from_tag" );
const str SCA_DB_TO_TAG_COL_NAME = STR_STATIC_INIT( "to_tag" );
const str SCA_DB_NOTIFY_CSEQ_COL_NAME = STR_STATIC_INIT( "notify_cseq" );
const str SCA_DB_SUBSCRIBE_CSEQ_COL_NAME = STR_STATIC_INIT( "subscribe_cseq" );
void
sca_db_subscriptions_get_value_for_column( int column, db_val_t *row_values,
void *column_value )
{
assert( column_value != NULL );
assert( row_values != NULL );
assert( column >= 0 && column < SCA_DB_SUBS_BOUNDARY );
switch ( column ) {
case SCA_DB_SUBS_SUBSCRIBER_COL:
case SCA_DB_SUBS_AOR_COL:
case SCA_DB_SUBS_CALL_ID_COL:
case SCA_DB_SUBS_FROM_TAG_COL:
case SCA_DB_SUBS_TO_TAG_COL:
((str *)column_value)->s = (char *)row_values[ column ].val.string_val;
((str *)column_value)->len = strlen(((str *)column_value)->s );
break;
case SCA_DB_SUBS_EXPIRES_COL:
*((time_t *)column_value) = row_values[ column ].val.time_val;
break;
case SCA_DB_SUBS_EVENT_COL:
case SCA_DB_SUBS_STATE_COL:
case SCA_DB_SUBS_NOTIFY_CSEQ_COL:
case SCA_DB_SUBS_SUBSCRIBE_CSEQ_COL:
*((int *)column_value) = row_values[ column ].val.int_val;
break;
default:
column_value = NULL;
}
}
void
sca_db_subscriptions_set_value_for_column( int column, db_val_t *row_values,
void *column_value )
{
assert( column >= 0 && column < SCA_DB_SUBS_BOUNDARY );
assert( column_value != NULL );
assert( row_values != NULL );
switch ( column ) {
case SCA_DB_SUBS_SUBSCRIBER_COL:
case SCA_DB_SUBS_AOR_COL:
case SCA_DB_SUBS_CALL_ID_COL:
case SCA_DB_SUBS_FROM_TAG_COL:
case SCA_DB_SUBS_TO_TAG_COL:
row_values[ column ].val.str_val = *((str *)column_value);
row_values[ column ].type = DB1_STR;
row_values[ column ].nul = 0;
break;
case SCA_DB_SUBS_EXPIRES_COL:
row_values[ column ].val.int_val = (int)(*((time_t *)column_value));
row_values[ column ].type = DB1_INT;
row_values[ column ].nul = 0;
break;
case SCA_DB_SUBS_APP_IDX_COL:
/* for now, don't save appearance index associated with subscriber */
row_values[ column ].val.int_val = 0;
row_values[ column ].type = DB1_INT;
row_values[ column ].nul = 0;
break;
default:
LM_WARN( "sca_db_subscriptions_set_value_for_column: unrecognized "
"column index %d, treating as INT", column );
/* fall through */
case SCA_DB_SUBS_EVENT_COL:
case SCA_DB_SUBS_STATE_COL:
case SCA_DB_SUBS_NOTIFY_CSEQ_COL:
case SCA_DB_SUBS_SUBSCRIBE_CSEQ_COL:
row_values[ column ].val.int_val = *((int *)column_value);
row_values[ column ].type = DB1_INT;
row_values[ column ].nul = 0;
break;
}
}
str **
sca_db_subscriptions_columns( void )
{
static str *subs_columns[] = {
(str *)&SCA_DB_SUBSCRIBER_COL_NAME,
(str *)&SCA_DB_AOR_COL_NAME,
(str *)&SCA_DB_EVENT_COL_NAME,
(str *)&SCA_DB_EXPIRES_COL_NAME,
(str *)&SCA_DB_STATE_COL_NAME,
(str *)&SCA_DB_APP_IDX_COL_NAME,
(str *)&SCA_DB_CALL_ID_COL_NAME,
(str *)&SCA_DB_FROM_TAG_COL_NAME,
(str *)&SCA_DB_TO_TAG_COL_NAME,
(str *)&SCA_DB_NOTIFY_CSEQ_COL_NAME,
(str *)&SCA_DB_SUBSCRIBE_CSEQ_COL_NAME,
NULL
};
return( subs_columns );
}
db1_con_t *
sca_db_get_connection( void )
{
assert( sca && sca->cfg->db_url );
assert( sca->db_api && sca->db_api->init );
if ( sca_db_con == NULL ) {
sca_db_con = sca->db_api->init( sca->cfg->db_url );
/* catch connection error in caller */
}
return( sca_db_con );
}
void
sca_db_disconnect( void )
{
if ( sca_db_con != NULL ) {
sca->db_api->close( sca_db_con );
sca_db_con = NULL;
}
}