forked from asterisk/asterisk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc_odbc.c
1950 lines (1706 loc) · 51.5 KB
/
func_odbc.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (c) 2005, 2006 Tilghman Lesher
* Copyright (c) 2008, 2009 Digium, Inc.
*
* Tilghman Lesher <[email protected]>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*!
* \file
*
* \brief ODBC lookups
*
* \author Tilghman Lesher <[email protected]>
*
* \ingroup functions
*/
/*** MODULEINFO
<depend>res_odbc</depend>
<depend>generic_odbc</depend>
<support_level>core</support_level>
***/
#include "asterisk.h"
#include "asterisk/module.h"
#include "asterisk/file.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/config.h"
#include "asterisk/res_odbc.h"
#include "asterisk/res_odbc_transaction.h"
#include "asterisk/app.h"
#include "asterisk/cli.h"
#include "asterisk/strings.h"
/*** DOCUMENTATION
<function name="ODBC_FETCH" language="en_US">
<synopsis>
Fetch a row from a multirow query.
</synopsis>
<syntax>
<parameter name="result-id" required="true" />
</syntax>
<description>
<para>For queries which are marked as mode=multirow, the original
query returns a <replaceable>result-id</replaceable> from which results
may be fetched. This function implements the actual fetch of the results.</para>
<para>This also sets <variable>ODBC_FETCH_STATUS</variable>.</para>
<variablelist>
<variable name="ODBC_FETCH_STATUS">
<value name="SUCESS">
If rows are available.
</value>
<value name="FAILURE">
If no rows are available.
</value>
</variable>
</variablelist>
</description>
</function>
<application name="ODBCFinish" language="en_US">
<synopsis>
Clear the resultset of a sucessful multirow query.
</synopsis>
<syntax>
<parameter name="result-id" required="true" />
</syntax>
<description>
<para>For queries which are marked as mode=multirow, this will clear
any remaining rows of the specified resultset.</para>
</description>
</application>
<function name="SQL_ESC" language="en_US">
<synopsis>
Escapes single ticks for use in SQL statements.
</synopsis>
<syntax>
<parameter name="string" required="true" />
</syntax>
<description>
<para>Used in SQL templates to escape data which may contain single ticks
<literal>'</literal> which are otherwise used to delimit data.</para>
<para>Example: SELECT foo FROM bar WHERE baz='${SQL_ESC(${ARG1})}'</para>
</description>
</function>
***/
static char *config = "func_odbc.conf";
#define DEFAULT_SINGLE_DB_CONNECTION 0
static int single_db_connection;
AST_RWLOCK_DEFINE_STATIC(single_db_connection_lock);
enum odbc_option_flags {
OPT_ESCAPECOMMAS = (1 << 0),
OPT_MULTIROW = (1 << 1),
};
struct acf_odbc_query {
AST_RWLIST_ENTRY(acf_odbc_query) list;
char readhandle[5][30];
char writehandle[5][30];
char *sql_read;
char *sql_write;
char *sql_insert;
unsigned int flags;
int rowlimit;
struct ast_custom_function *acf;
};
static void odbc_datastore_free(void *data);
static const struct ast_datastore_info odbc_info = {
.type = "FUNC_ODBC",
.destroy = odbc_datastore_free,
};
/* For storing each result row */
struct odbc_datastore_row {
AST_LIST_ENTRY(odbc_datastore_row) list;
char data[0];
};
/* For storing each result set */
struct odbc_datastore {
AST_LIST_HEAD(, odbc_datastore_row);
char names[0];
};
/* \brief Data source name
*
* This holds data that pertains to a DSN
*/
struct dsn {
/*! A connection to the database */
struct odbc_obj *connection;
/*! The name of the DSN as defined in res_odbc.conf */
char name[0];
};
#define DSN_BUCKETS 37
struct ao2_container *dsns;
static int dsn_hash(const void *obj, const int flags)
{
const struct dsn *object;
const char *key;
switch (flags & OBJ_SEARCH_MASK) {
case OBJ_SEARCH_KEY:
key = obj;
break;
case OBJ_SEARCH_OBJECT:
object = obj;
key = object->name;
break;
default:
ast_assert(0);
return 0;
}
return ast_str_hash(key);
}
static int dsn_cmp(void *obj, void *arg, int flags)
{
const struct dsn *object_left = obj;
const struct dsn *object_right = arg;
const char *right_key = arg;
int cmp;
switch (flags & OBJ_SEARCH_MASK) {
case OBJ_SEARCH_OBJECT:
right_key = object_right->name;
/* Fall through */
case OBJ_SEARCH_KEY:
cmp = strcmp(object_left->name, right_key);
break;
case OBJ_SEARCH_PARTIAL_KEY:
cmp = strncmp(object_left->name, right_key, strlen(right_key));
break;
default:
cmp = 0;
break;
}
if (cmp) {
return 0;
}
return CMP_MATCH;
}
static void dsn_destructor(void *obj)
{
struct dsn *dsn = obj;
if (dsn->connection) {
ast_odbc_release_obj(dsn->connection);
}
}
/*!
* \brief Create a DSN and connect to the database
*
* \param name The name of the DSN as found in res_odbc.conf
* \retval NULL Fail
* \retval non-NULL The newly-created structure
*/
static struct dsn *create_dsn(const char *name)
{
struct dsn *dsn;
if (!dsns) {
return NULL;
}
dsn = ao2_alloc(sizeof(*dsn) + strlen(name) + 1, dsn_destructor);
if (!dsn) {
return NULL;
}
/* Safe */
strcpy(dsn->name, name);
dsn->connection = ast_odbc_request_obj(name, 0);
if (!dsn->connection) {
ao2_ref(dsn, -1);
return NULL;
}
if (!ao2_link_flags(dsns, dsn, OBJ_NOLOCK)) {
ao2_ref(dsn, -1);
return NULL;
}
return dsn;
}
static SQLHSTMT silent_execute(struct odbc_obj *obj, void *data);
/*!
* \brief Determine if the connection has died.
*
* \param connection The connection to check
* \retval 1 Yep, it's dead
* \retval 0 It's alive and well
*/
static int connection_dead(struct odbc_obj *connection)
{
SQLINTEGER dead;
SQLRETURN res;
SQLHSTMT stmt;
if (!connection) {
return 1;
}
res = SQLGetConnectAttr(connection->con, SQL_ATTR_CONNECTION_DEAD, &dead, 0, 0);
if (SQL_SUCCEEDED(res)) {
return dead == SQL_CD_TRUE ? 1 : 0;
}
/* If the Driver doesn't support SQL_ATTR_CONNECTION_DEAD do a direct
* execute of a probing statement and see if that succeeds instead
*/
stmt = ast_odbc_direct_execute(connection, silent_execute, "SELECT 1");
if (!stmt) {
return 1;
}
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
return 0;
}
/*!
* \brief Retrieve a DSN, or create it if it does not exist.
*
* The created DSN is returned locked. This should be inconsequential
* to callers in most cases.
*
* When finished with the returned structure, the caller must call
* \ref release_dsn
*
* \param name Name of the DSN as found in res_odbc.conf
* \retval NULL Unable to retrieve or create the DSN
* \retval non-NULL The retrieved/created locked DSN
*/
static struct dsn *get_dsn(const char *name)
{
struct dsn *dsn;
if (!dsns) {
return NULL;
}
ao2_lock(dsns);
dsn = ao2_find(dsns, name, OBJ_SEARCH_KEY | OBJ_NOLOCK);
if (!dsn) {
dsn = create_dsn(name);
}
ao2_unlock(dsns);
if (!dsn) {
return NULL;
}
ao2_lock(dsn);
if (!dsn->connection) {
dsn->connection = ast_odbc_request_obj(name, 0);
if (!dsn->connection) {
ao2_unlock(dsn);
ao2_ref(dsn, -1);
return NULL;
}
return dsn;
}
if (connection_dead(dsn->connection)) {
ast_odbc_release_obj(dsn->connection);
dsn->connection = ast_odbc_request_obj(name, 0);
if (!dsn->connection) {
ao2_unlock(dsn);
ao2_ref(dsn, -1);
return NULL;
}
}
return dsn;
}
/*!
* \brief Get a DB handle via a DSN or directly
*
* If single db connection then get the DB handle via DSN
* else by requesting a connection directly
*
* \param dsn_name Name of the DSN as found in res_odbc.conf
* \param dsn The pointer to the DSN
* \retval NULL Unable to retrieve the DB handle
* \retval non-NULL The retrieved DB handle
*/
static struct odbc_obj *get_odbc_obj(const char *dsn_name, struct dsn **dsn)
{
struct odbc_obj *obj = NULL;
ast_rwlock_rdlock(&single_db_connection_lock);
if (single_db_connection) {
if (dsn) {
*dsn = get_dsn(dsn_name);
if (*dsn) {
obj = (*dsn)->connection;
}
}
} else {
obj = ast_odbc_request_obj(dsn_name, 0);
}
ast_rwlock_unlock(&single_db_connection_lock);
return obj;
}
/*!
* \brief Release an ODBC obj or a DSN
*
* If single db connection then unlock and unreference the DSN
* else release the ODBC obj
*
* \param obj The pointer to the ODBC obj to release
* \param dsn The pointer to the dsn to unlock and unreference
*/
static inline void release_obj_or_dsn(struct odbc_obj **obj, struct dsn **dsn)
{
if (dsn && *dsn) {
/* If multiple connections are not enabled then the guarantee
* of a single connection already exists and holding on to the
* connection would prevent any other user from acquiring it
* indefinitely.
*/
if (ast_odbc_get_max_connections((*dsn)->name) < 2) {
ast_odbc_release_obj((*dsn)->connection);
(*dsn)->connection = NULL;
}
ao2_unlock(*dsn);
ao2_ref(*dsn, -1);
*dsn = NULL;
/* Some callers may provide both an obj and dsn. To ensure that
* the connection is not released twice we set it to NULL here if
* present.
*/
if (obj) {
*obj = NULL;
}
} else if (obj && *obj) {
ast_odbc_release_obj(*obj);
*obj = NULL;
}
}
static AST_RWLIST_HEAD_STATIC(queries, acf_odbc_query);
static int resultcount = 0;
AST_THREADSTORAGE(sql_buf);
AST_THREADSTORAGE(sql2_buf);
AST_THREADSTORAGE(coldata_buf);
AST_THREADSTORAGE(colnames_buf);
static int acf_fetch(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len);
static void odbc_datastore_free(void *data)
{
struct odbc_datastore *result = data;
struct odbc_datastore_row *row;
if (!result) {
return;
}
AST_LIST_LOCK(result);
while ((row = AST_LIST_REMOVE_HEAD(result, list))) {
ast_free(row);
}
AST_LIST_UNLOCK(result);
AST_LIST_HEAD_DESTROY(result);
ast_free(result);
}
/*!
* \brief Common execution function for SQL queries.
*
* \param obj DB connection
* \param data The query to execute
* \param silent If true, do not print warnings on failure
* \retval NULL Failed to execute query
* \retval non-NULL The executed statement
*/
static SQLHSTMT execute(struct odbc_obj *obj, void *data, int silent)
{
int res;
char *sql = data;
SQLHSTMT stmt;
res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
ast_log(LOG_WARNING, "SQL Alloc Handle failed (%d)!\n", res);
return NULL;
}
res = ast_odbc_execute_sql(obj, stmt, sql);
if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO) && (res != SQL_NO_DATA)) {
if (res == SQL_ERROR && !silent) {
int i;
SQLINTEGER nativeerror=0, numfields=0;
SQLSMALLINT diagbytes=0;
unsigned char state[10], diagnostic[256];
SQLGetDiagField(SQL_HANDLE_STMT, stmt, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
for (i = 0; i < numfields; i++) {
SQLGetDiagRec(SQL_HANDLE_STMT, stmt, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
ast_log(LOG_WARNING, "SQL Execute returned an error %d: %s: %s (%d)\n", res, state, diagnostic, diagbytes);
if (i > 10) {
ast_log(LOG_WARNING, "Oh, that was good. There are really %d diagnostics?\n", (int)numfields);
break;
}
}
}
if (!silent) {
ast_log(LOG_WARNING, "SQL Exec Direct failed (%d)![%s]\n", res, sql);
}
SQLCloseCursor(stmt);
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
return NULL;
}
return stmt;
}
static SQLHSTMT generic_execute(struct odbc_obj *obj, void *data)
{
return execute(obj, data, 0);
}
static SQLHSTMT silent_execute(struct odbc_obj *obj, void *data)
{
return execute(obj, data, 1);
}
/*
* Master control routine
*/
static int acf_odbc_write(struct ast_channel *chan, const char *cmd, char *s, const char *value)
{
struct odbc_obj *obj = NULL;
struct acf_odbc_query *query;
char *t, varname[15];
int i, dsn_num, bogus_chan = 0;
int transactional = 0;
AST_DECLARE_APP_ARGS(values,
AST_APP_ARG(field)[100];
);
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(field)[100];
);
SQLHSTMT stmt = NULL;
SQLLEN rows=0;
struct ast_str *buf = ast_str_thread_get(&sql_buf, 16);
struct ast_str *insertbuf = ast_str_thread_get(&sql2_buf, 16);
const char *status = "FAILURE";
struct dsn *dsn = NULL;
if (!buf || !insertbuf) {
return -1;
}
AST_RWLIST_RDLOCK(&queries);
AST_RWLIST_TRAVERSE(&queries, query, list) {
if (!strcmp(query->acf->name, cmd)) {
break;
}
}
if (!query) {
ast_log(LOG_ERROR, "No such function '%s'\n", cmd);
AST_RWLIST_UNLOCK(&queries);
if (chan) {
pbx_builtin_setvar_helper(chan, "ODBCSTATUS", status);
}
return -1;
}
if (!chan) {
if (!(chan = ast_dummy_channel_alloc())) {
AST_RWLIST_UNLOCK(&queries);
return -1;
}
bogus_chan = 1;
}
if (!bogus_chan) {
ast_autoservice_start(chan);
}
ast_str_make_space(&buf, strlen(query->sql_write) * 2 + 300);
/* We only get here if sql_write is set. sql_insert is optional however. */
if (query->sql_insert) {
ast_str_make_space(&insertbuf, strlen(query->sql_insert) * 2 + 300);
}
/* Parse our arguments */
t = value ? ast_strdupa(value) : "";
if (!s || !t) {
ast_log(LOG_ERROR, "Out of memory\n");
AST_RWLIST_UNLOCK(&queries);
if (!bogus_chan) {
ast_autoservice_stop(chan);
pbx_builtin_setvar_helper(chan, "ODBCSTATUS", status);
} else {
ast_channel_unref(chan);
}
return -1;
}
AST_STANDARD_APP_ARGS(args, s);
for (i = 0; i < args.argc; i++) {
snprintf(varname, sizeof(varname), "ARG%d", i + 1);
pbx_builtin_pushvar_helper(chan, varname, args.field[i]);
}
/* Parse values, just like arguments */
AST_STANDARD_APP_ARGS(values, t);
for (i = 0; i < values.argc; i++) {
snprintf(varname, sizeof(varname), "VAL%d", i + 1);
pbx_builtin_pushvar_helper(chan, varname, values.field[i]);
}
/* Additionally set the value as a whole (but push an empty string if value is NULL) */
pbx_builtin_pushvar_helper(chan, "VALUE", value ? value : "");
ast_str_substitute_variables(&buf, 0, chan, query->sql_write);
if (query->sql_insert) {
ast_str_substitute_variables(&insertbuf, 0, chan, query->sql_insert);
}
if (bogus_chan) {
chan = ast_channel_unref(chan);
} else {
/* Restore prior values */
for (i = 0; i < args.argc; i++) {
snprintf(varname, sizeof(varname), "ARG%d", i + 1);
pbx_builtin_setvar_helper(chan, varname, NULL);
}
for (i = 0; i < values.argc; i++) {
snprintf(varname, sizeof(varname), "VAL%d", i + 1);
pbx_builtin_setvar_helper(chan, varname, NULL);
}
pbx_builtin_setvar_helper(chan, "VALUE", NULL);
}
/*!\note
* Okay, this part is confusing. Transactions belong to a single database
* handle. Therefore, when working with transactions, we CANNOT failover
* to multiple DSNs. We MUST have a single handle all the way through the
* transaction, or else we CANNOT enforce atomicity.
*/
for (dsn_num = 0; dsn_num < 5; dsn_num++) {
if (!ast_strlen_zero(query->writehandle[dsn_num])) {
if (transactional) {
/* This can only happen second time through or greater. */
ast_log(LOG_WARNING, "Transactions do not work well with multiple DSNs for 'writehandle'\n");
}
if ((obj = ast_odbc_retrieve_transaction_obj(chan, query->writehandle[dsn_num]))) {
transactional = 1;
} else {
obj = get_odbc_obj(query->writehandle[dsn_num], &dsn);
transactional = 0;
}
if (obj && (stmt = ast_odbc_direct_execute(obj, generic_execute, ast_str_buffer(buf)))) {
break;
}
if (!transactional) {
release_obj_or_dsn (&obj, &dsn);
}
}
}
if (stmt) {
SQLRowCount(stmt, &rows);
SQLCloseCursor(stmt);
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
if (rows != 0) {
status = "SUCCESS";
} else if (query->sql_insert) {
if (!transactional) {
release_obj_or_dsn (&obj, &dsn);
}
for (transactional = 0, dsn_num = 0; dsn_num < 5; dsn_num++) {
if (!ast_strlen_zero(query->writehandle[dsn_num])) {
if (transactional) {
/* This can only happen second time through or greater. */
ast_log(LOG_WARNING, "Transactions do not work well with multiple DSNs for 'writehandle'\n");
} else {
release_obj_or_dsn (&obj, &dsn);
}
if ((obj = ast_odbc_retrieve_transaction_obj(chan, query->writehandle[dsn_num]))) {
transactional = 1;
} else {
obj = get_odbc_obj(query->writehandle[dsn_num], &dsn);
transactional = 0;
}
if (obj) {
stmt = ast_odbc_direct_execute(obj, generic_execute, ast_str_buffer(insertbuf));
}
}
if (stmt) {
status = "FAILOVER";
SQLRowCount(stmt, &rows);
SQLCloseCursor(stmt);
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
break;
}
}
}
}
AST_RWLIST_UNLOCK(&queries);
/* Output the affected rows, for all cases. In the event of failure, we
* flag this as -1 rows. Note that this is different from 0 affected rows
* which would be the case if we succeeded in our query, but the values did
* not change. */
if (!bogus_chan) {
snprintf(varname, sizeof(varname), "%d", (int)rows);
pbx_builtin_setvar_helper(chan, "ODBCROWS", varname);
pbx_builtin_setvar_helper(chan, "ODBCSTATUS", status);
}
if (!transactional) {
release_obj_or_dsn (&obj, &dsn);
}
if (!bogus_chan) {
ast_autoservice_stop(chan);
}
return 0;
}
static int acf_odbc_read(struct ast_channel *chan, const char *cmd, char *s, char *buf, size_t len)
{
struct odbc_obj *obj = NULL;
struct acf_odbc_query *query;
char varname[15], rowcount[12] = "-1";
struct ast_str *colnames = ast_str_thread_get(&colnames_buf, 16);
int res, x, y, buflen = 0, escapecommas, rowlimit = 1, multirow = 0, dsn_num, bogus_chan = 0;
AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(field)[100];
);
SQLHSTMT stmt = NULL;
SQLSMALLINT colcount=0;
SQLLEN indicator;
SQLSMALLINT collength;
struct odbc_datastore *resultset = NULL;
struct odbc_datastore_row *row = NULL;
struct ast_str *sql = ast_str_thread_get(&sql_buf, 16);
const char *status = "FAILURE";
struct dsn *dsn = NULL;
if (!sql || !colnames) {
if (chan) {
pbx_builtin_setvar_helper(chan, "ODBCSTATUS", status);
}
return -1;
}
ast_str_reset(colnames);
AST_RWLIST_RDLOCK(&queries);
AST_RWLIST_TRAVERSE(&queries, query, list) {
if (!strcmp(query->acf->name, cmd)) {
break;
}
}
if (!query) {
ast_log(LOG_ERROR, "No such function '%s'\n", cmd);
AST_RWLIST_UNLOCK(&queries);
if (chan) {
pbx_builtin_setvar_helper(chan, "ODBCROWS", rowcount);
pbx_builtin_setvar_helper(chan, "ODBCSTATUS", status);
}
return -1;
}
if (!chan) {
if (!(chan = ast_dummy_channel_alloc())) {
AST_RWLIST_UNLOCK(&queries);
return -1;
}
bogus_chan = 1;
}
if (!bogus_chan) {
ast_autoservice_start(chan);
}
AST_STANDARD_APP_ARGS(args, s);
for (x = 0; x < args.argc; x++) {
snprintf(varname, sizeof(varname), "ARG%d", x + 1);
pbx_builtin_pushvar_helper(chan, varname, args.field[x]);
}
ast_str_substitute_variables(&sql, 0, chan, query->sql_read);
if (bogus_chan) {
chan = ast_channel_unref(chan);
} else {
/* Restore prior values */
for (x = 0; x < args.argc; x++) {
snprintf(varname, sizeof(varname), "ARG%d", x + 1);
pbx_builtin_setvar_helper(chan, varname, NULL);
}
}
/* Save these flags, so we can release the lock */
escapecommas = ast_test_flag(query, OPT_ESCAPECOMMAS);
if (!bogus_chan && ast_test_flag(query, OPT_MULTIROW)) {
if (!(resultset = ast_calloc(1, sizeof(*resultset)))) {
pbx_builtin_setvar_helper(chan, "ODBCROWS", rowcount);
pbx_builtin_setvar_helper(chan, "ODBCSTATUS", status);
AST_RWLIST_UNLOCK(&queries);
ast_autoservice_stop(chan);
return -1;
}
AST_LIST_HEAD_INIT(resultset);
if (query->rowlimit) {
rowlimit = query->rowlimit;
} else {
rowlimit = INT_MAX;
}
multirow = 1;
} else if (!bogus_chan) {
if (query->rowlimit > 1) {
rowlimit = query->rowlimit;
if (!(resultset = ast_calloc(1, sizeof(*resultset)))) {
pbx_builtin_setvar_helper(chan, "ODBCROWS", rowcount);
pbx_builtin_setvar_helper(chan, "ODBCSTATUS", status);
AST_RWLIST_UNLOCK(&queries);
ast_autoservice_stop(chan);
return -1;
}
AST_LIST_HEAD_INIT(resultset);
}
}
AST_RWLIST_UNLOCK(&queries);
for (dsn_num = 0; dsn_num < 5; dsn_num++) {
if (!ast_strlen_zero(query->readhandle[dsn_num])) {
obj = get_odbc_obj(query->readhandle[dsn_num], &dsn);
if (!obj) {
continue;
}
stmt = ast_odbc_direct_execute(obj, generic_execute, ast_str_buffer(sql));
}
if (stmt) {
break;
}
release_obj_or_dsn (&obj, &dsn);
}
if (!stmt) {
ast_log(LOG_ERROR, "Unable to execute query [%s]\n", ast_str_buffer(sql));
release_obj_or_dsn (&obj, &dsn);
if (!bogus_chan) {
pbx_builtin_setvar_helper(chan, "ODBCROWS", rowcount);
ast_autoservice_stop(chan);
}
odbc_datastore_free(resultset);
return -1;
}
res = SQLNumResultCols(stmt, &colcount);
if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
ast_log(LOG_WARNING, "SQL Column Count error!\n[%s]\n\n", ast_str_buffer(sql));
SQLCloseCursor(stmt);
SQLFreeHandle (SQL_HANDLE_STMT, stmt);
release_obj_or_dsn (&obj, &dsn);
if (!bogus_chan) {
pbx_builtin_setvar_helper(chan, "ODBCROWS", rowcount);
ast_autoservice_stop(chan);
}
odbc_datastore_free(resultset);
return -1;
}
if (colcount <= 0) {
ast_verb(4, "Returned %d columns [%s]\n", colcount, ast_str_buffer(sql));
buf[0] = '\0';
SQLCloseCursor(stmt);
SQLFreeHandle (SQL_HANDLE_STMT, stmt);
release_obj_or_dsn (&obj, &dsn);
if (!bogus_chan) {
pbx_builtin_setvar_helper(chan, "ODBCROWS", "0");
pbx_builtin_setvar_helper(chan, "ODBCSTATUS", "NODATA");
ast_autoservice_stop(chan);
}
odbc_datastore_free(resultset);
return 0;
}
res = SQLFetch(stmt);
if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
int res1 = -1;
if (res == SQL_NO_DATA) {
ast_verb(4, "Found no rows [%s]\n", ast_str_buffer(sql));
res1 = 0;
buf[0] = '\0';
ast_copy_string(rowcount, "0", sizeof(rowcount));
status = "NODATA";
} else {
ast_log(LOG_WARNING, "Error %d in FETCH [%s]\n", res, ast_str_buffer(sql));
status = "FETCHERROR";
}
SQLCloseCursor(stmt);
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
release_obj_or_dsn (&obj, &dsn);
if (!bogus_chan) {
pbx_builtin_setvar_helper(chan, "ODBCROWS", rowcount);
pbx_builtin_setvar_helper(chan, "ODBCSTATUS", status);
ast_autoservice_stop(chan);
}
odbc_datastore_free(resultset);
return res1;
}
status = "SUCCESS";
for (y = 0; y < rowlimit; y++) {
buf[0] = '\0';
for (x = 0; x < colcount; x++) {
int i;
struct ast_str *coldata = ast_str_thread_get(&coldata_buf, 16);
char *ptrcoldata;
if (!coldata) {
odbc_datastore_free(resultset);
SQLCloseCursor(stmt);
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
release_obj_or_dsn (&obj, &dsn);
if (!bogus_chan) {
pbx_builtin_setvar_helper(chan, "ODBCSTATUS", "MEMERROR");
ast_autoservice_stop(chan);
}
return -1;
}
if (y == 0) {
char colname[256];
SQLLEN octetlength = 0;
res = SQLDescribeCol(stmt, x + 1, (unsigned char *)colname, sizeof(colname), &collength, NULL, NULL, NULL, NULL);
ast_debug(3, "Got collength of %d for column '%s' (offset %d)\n", (int)collength, colname, x);
if (((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) || collength == 0) {
snprintf(colname, sizeof(colname), "field%d", x);
}
SQLColAttribute(stmt, x + 1, SQL_DESC_OCTET_LENGTH, NULL, 0, NULL, &octetlength);
ast_str_make_space(&coldata, octetlength + 1);
if (ast_str_strlen(colnames)) {
ast_str_append(&colnames, 0, ",");
}
ast_str_append_escapecommas(&colnames, 0, colname, sizeof(colname));
if (resultset) {
void *tmp = ast_realloc(resultset, sizeof(*resultset) + ast_str_strlen(colnames) + 1);
if (!tmp) {
ast_log(LOG_ERROR, "No space for a new resultset?\n");
odbc_datastore_free(resultset);
SQLCloseCursor(stmt);
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
release_obj_or_dsn (&obj, &dsn);
if (!bogus_chan) {
pbx_builtin_setvar_helper(chan, "ODBCROWS", rowcount);
pbx_builtin_setvar_helper(chan, "ODBCSTATUS", "MEMERROR");
ast_autoservice_stop(chan);
}
return -1;
}
resultset = tmp;
strcpy((char *)resultset + sizeof(*resultset), ast_str_buffer(colnames));
}
}
buflen = strlen(buf);
res = ast_odbc_ast_str_SQLGetData(&coldata, -1, stmt, x + 1, SQL_CHAR, &indicator);
if (indicator == SQL_NULL_DATA) {
ast_debug(3, "Got NULL data\n");
ast_str_reset(coldata);
res = SQL_SUCCESS;
}
if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
ast_log(LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", ast_str_buffer(sql));
y = -1;
buf[0] = '\0';
goto end_acf_read;
}
ast_debug(2, "Got coldata of '%s'\n", ast_str_buffer(coldata));
if (x) {
buf[buflen++] = ',';
}
/* Copy data, encoding '\' and ',' for the argument parser */
ptrcoldata = ast_str_buffer(coldata);
for (i = 0; i < ast_str_strlen(coldata); i++) {
if (escapecommas && (ptrcoldata[i] == '\\' || ptrcoldata[i] == ',')) {
buf[buflen++] = '\\';
}
buf[buflen++] = ptrcoldata[i];
if (buflen >= len - 2) {
break;
}
if (ptrcoldata[i] == '\0') {
break;
}
}
buf[buflen] = '\0';
ast_debug(2, "buf is now set to '%s'\n", buf);
}