~ubuntu-branches/ubuntu/warty/ncbi-tools6/warty

« back to all changes in this revision

Viewing changes to network/taxon1/taxon2/txcproc.c

  • Committer: Bazaar Package Importer
  • Author(s): Aaron M. Ucko
  • Date: 2002-04-04 22:13:09 UTC
  • Revision ID: james.westby@ubuntu.com-20020404221309-vfze028rfnlrldct
Tags: upstream-6.1.20011220a
ImportĀ upstreamĀ versionĀ 6.1.20011220a

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  txcproc.c
 
2
* ===========================================================================
 
3
*
 
4
*                            PUBLIC DOMAIN NOTICE                          
 
5
*               National Center for Biotechnology Information
 
6
*                                                                          
 
7
*  This software/database is a "United States Government Work" under the   
 
8
*  terms of the United States Copyright Act.  It was written as part of    
 
9
*  the author's official duties as a United States Government employee and 
 
10
*  thus cannot be copyrighted.  This software/database is freely available 
 
11
*  to the public for use. The National Library of Medicine and the U.S.    
 
12
*  Government have not placed any restriction on its use or reproduction.  
 
13
*                                                                          
 
14
*  Although all reasonable efforts have been taken to ensure the accuracy  
 
15
*  and reliability of the software and data, the NLM and the U.S.          
 
16
*  Government do not and cannot warrant the performance or results that    
 
17
*  may be obtained by using this software or data. The NLM and the U.S.    
 
18
*  Government disclaim all warranties, express or implied, including       
 
19
*  warranties of performance, merchantability or fitness for any particular
 
20
*  purpose.                                                                
 
21
*                                                                          
 
22
*  Please cite the author in any work or product based on this material.   
 
23
*
 
24
* ===========================================================================
 
25
*
 
26
* File Name:  txcproc.c
 
27
*
 
28
* Author:  Vladimir Soussov
 
29
*   
 
30
* File Description:  taxonomy client communication procedures
 
31
*
 
32
*
 
33
* $Log: txcproc.c,v $
 
34
* Revision 1.6  1999/12/20 17:05:17  soussov
 
35
* taxid4gi added
 
36
*
 
37
* Revision 1.5  1998/07/27 16:55:08  soussov
 
38
* Subtree version
 
39
*
 
40
* Revision 1.4  1998/04/01 17:33:57  soussov
 
41
* changed tp include <>
 
42
*
 
43
* Revision 1.3  1998/03/13 19:18:54  soussov
 
44
* add CS_CANCELED case for ct_results
 
45
*
 
46
* Revision 1.2  1998/02/11 15:53:13  soussov
 
47
* ctpublic.h was added
 
48
*
 
49
* Revision 1.1  1998/02/10 20:12:06  soussov
 
50
* taxon2 related soft
 
51
*
 
52
*
 
53
*/
 
54
 
 
55
#include <ctpublic.h>
 
56
#include <txclient.h>
 
57
 
 
58
static CS_CONTEXT* te_context;
 
59
static CS_CONNECTION* te_link;
 
60
static CS_INT my_last_update= 0;
 
61
 
 
62
CS_RETCODE c_msg_proc(CS_CONTEXT* cp, CS_CONNECTION* chp, CS_CLIENTMSG* emsgp)
 
63
{
 
64
    char buff[512];
 
65
 
 
66
    if(emsgp->msgstringlen > 0) {
 
67
        sprintf(buff, "! ct error: %s\n", emsgp->msgstring);
 
68
        ErrPostEx(SEV_ERROR, 100, 1, buff);     
 
69
    }
 
70
    if((emsgp->osnumber != 0) && (emsgp->osstringlen > 0)) {
 
71
        sprintf(buff, "! os error: %s\n", emsgp->osstring);
 
72
        ErrPostEx(SEV_ERROR, 100, 2, buff);
 
73
    }
 
74
    if((emsgp->sqlstatelen > 0) && (strcmp(emsgp->sqlstate, "ZZZZZ") != 0)) {
 
75
        sprintf(buff, "! sql statement: %s\n", emsgp->sqlstate);
 
76
        ErrPostEx(SEV_ERROR, 100, 3, buff);
 
77
    }
 
78
 
 
79
    return CS_SUCCEED;
 
80
}
 
81
 
 
82
CS_RETCODE s_msg_proc(CS_CONTEXT* cp, CS_CONNECTION* chp, CS_SERVERMSG* msg)
 
83
{
 
84
    char buff[512];
 
85
 
 
86
    if((msg->textlen > 0) && (msg->severity > 0)) {
 
87
        sprintf(buff, "! server message: %s\t %s\t %s\t %s\n", msg->svrname, msg->proc, msg->text, msg->sqlstate);
 
88
        ErrPostEx(SEV_ERROR, 100, 4, buff);
 
89
    }
 
90
    return CS_SUCCEED;
 
91
}
 
92
 
 
93
Boolean txc_connect2Server(CharPtr srv_name, CharPtr usr, CharPtr passwd, CharPtr applic)
 
94
{
 
95
    CS_BOOL val= CS_TRUE;
 
96
    CS_INT t_out= 40;
 
97
 
 
98
    cs_ctx_global(CS_VERSION_110, &te_context);
 
99
    ct_init(te_context, CS_VERSION_110);
 
100
    ct_config(te_context, CS_SET, CS_TIMEOUT, &t_out, CS_UNUSED, NULL);
 
101
 
 
102
    ct_callback(te_context, NULL, CS_SET, CS_CLIENTMSG_CB, c_msg_proc);
 
103
    ct_callback(te_context, NULL, CS_SET, CS_SERVERMSG_CB, s_msg_proc);
 
104
 
 
105
    ct_con_alloc(te_context, &te_link);
 
106
    ct_con_props(te_link, CS_SET, CS_USERNAME, usr, CS_NULLTERM, NULL);
 
107
    ct_con_props(te_link, CS_SET, CS_PASSWORD, passwd, CS_NULLTERM, NULL);
 
108
    ct_con_props(te_link, CS_SET, CS_APPNAME, applic, CS_NULLTERM, NULL);
 
109
    if(ct_connect(te_link, srv_name, CS_NULLTERM) != CS_SUCCEED) {
 
110
        return FALSE;
 
111
    }
 
112
    return TRUE;
 
113
}
 
114
 
 
115
Int4 tax_findByName(CharPtr sname, int mode, TaxNamePtr* res_name)
 
116
{
 
117
    CS_COMMAND *cmd;
 
118
    CS_DATAFMT parfmt;
 
119
    CS_TINYINT smode;
 
120
    CS_INT res_type, rows_read, tax_id;
 
121
    char nbuff[256], ubuff[256];
 
122
    Boolean more_results= TRUE;
 
123
    Boolean more_fetch;
 
124
    CS_SMALLINT has_unique;
 
125
    CS_TINYINT class_cde, designator;
 
126
    Int4 n= 0, alloc_room= 0;
 
127
    TaxNamePtr fnd_name= NULL;
 
128
    int nnn= 64;
 
129
 
 
130
    if(sname == NULL) return 0;
 
131
    
 
132
    if(ct_cmd_alloc(te_link, &cmd) != CS_SUCCEED) return 0;
 
133
 
 
134
 
 
135
    /* build command: tax_findByName @name_txt, @mode */
 
136
 
 
137
    if(ct_command(cmd, CS_RPC_CMD, "tax_findByName", CS_NULLTERM, CS_UNUSED) != CS_SUCCEED) {
 
138
        ct_cmd_drop(cmd);
 
139
        return 0;
 
140
    }
 
141
    
 
142
    memset(&parfmt, 0, sizeof(CS_DATAFMT));
 
143
    strcpy(parfmt.name, "@name_txt");
 
144
    parfmt.namelen= CS_NULLTERM;
 
145
    parfmt.datatype= CS_CHAR_TYPE;
 
146
    parfmt.maxlength= strlen(sname);
 
147
    parfmt.status= CS_INPUTVALUE;
 
148
    parfmt.locale= NULL;
 
149
    if(ct_param(cmd, &parfmt, sname, parfmt.maxlength, CS_UNUSED) != CS_SUCCEED) {
 
150
        ct_cmd_drop(cmd);
 
151
        return 0;
 
152
    }
 
153
 
 
154
    strcpy(parfmt.name, "@mode");
 
155
    parfmt.datatype= CS_TINYINT_TYPE;
 
156
    parfmt.maxlength= 1;
 
157
    parfmt.locale= NULL;
 
158
    smode= mode;
 
159
    if(ct_param(cmd, &parfmt, &smode, 1, CS_UNUSED) != CS_SUCCEED) {
 
160
        ct_cmd_drop(cmd);
 
161
        return 0;
 
162
    }
 
163
 
 
164
    /* send this command to server */
 
165
    if(ct_send(cmd) != CS_SUCCEED) {
 
166
        ct_cmd_drop(cmd);
 
167
        return 0;
 
168
    }
 
169
    
 
170
    while(more_results && (--nnn > 0)) {
 
171
        switch(ct_results(cmd, &res_type)) {
 
172
        case CS_FAIL :
 
173
            if(ct_cancel(NULL, cmd, CS_CANCEL_ALL) != CS_SUCCEED) {
 
174
                ct_close(te_link, CS_FORCE_CLOSE);
 
175
            }
 
176
        case CS_CANCELED :
 
177
            ct_cmd_drop(cmd);
 
178
            return 0;
 
179
 
 
180
        case CS_END_RESULTS :
 
181
            more_results= FALSE;
 
182
            break;
 
183
 
 
184
        case CS_SUCCEED :
 
185
            if(res_type == CS_ROW_RESULT) {
 
186
                /* bind and fetch these data */
 
187
                more_fetch= TRUE;
 
188
                parfmt.datatype= CS_INT_TYPE;
 
189
                parfmt.format= CS_FMT_UNUSED;
 
190
                parfmt.maxlength= 4;
 
191
                parfmt.scale= CS_SRC_VALUE;
 
192
                parfmt.precision= CS_SRC_VALUE;
 
193
                parfmt.count= 1;
 
194
                parfmt.locale= NULL;
 
195
                if(ct_bind(cmd, 1, &parfmt, &tax_id, NULL, NULL) != CS_SUCCEED) break;
 
196
 
 
197
                parfmt.datatype= CS_CHAR_TYPE;
 
198
                parfmt.format= CS_FMT_NULLTERM;
 
199
                parfmt.maxlength= 255;
 
200
                if((ct_bind(cmd, 2, &parfmt, nbuff, NULL, NULL) != CS_SUCCEED) ||
 
201
                   (ct_bind(cmd, 3, &parfmt, ubuff, NULL, &has_unique) != CS_SUCCEED)) break;
 
202
 
 
203
                parfmt.datatype= CS_TINYINT_TYPE;
 
204
                parfmt.format= CS_FMT_UNUSED;
 
205
                parfmt.maxlength= 1;
 
206
                if((ct_bind(cmd, 4, &parfmt, &class_cde, NULL, NULL) != CS_SUCCEED) ||
 
207
                   (ct_bind(cmd, 5, &parfmt, &designator, NULL, NULL) != CS_SUCCEED)) break;
 
208
 
 
209
                while(more_fetch) {
 
210
                    switch(ct_fetch(cmd,CS_UNUSED,CS_UNUSED,CS_UNUSED,&rows_read)) {
 
211
                    case CS_SUCCEED :
 
212
                        if(n == 0) {
 
213
                            *res_name= fnd_name= malloc(sizeof(TaxName));
 
214
                            alloc_room= 1;
 
215
                        }
 
216
                        else if(n >= alloc_room) {
 
217
                            
 
218
                            alloc_room+= (8 > (alloc_room/4))? 8 : (alloc_room/4);
 
219
                            *res_name= realloc(fnd_name, alloc_room*sizeof(TaxName));
 
220
                            if(*res_name == NULL) {
 
221
                                alloc_room= n--;
 
222
                                *res_name= fnd_name;
 
223
                            }
 
224
                            else fnd_name= *res_name;
 
225
                        }
 
226
 
 
227
                        fnd_name[n].tax_id= tax_id;
 
228
                        fnd_name[n].class_cde= class_cde;
 
229
                        fnd_name[n].designator= designator;
 
230
                        fnd_name[n].name_txt= StringSave(nbuff);
 
231
                        fnd_name[n].unique_name= (has_unique < 0)? NULL : StringSave(ubuff);
 
232
                        n++;
 
233
                        continue;
 
234
 
 
235
                    case CS_ROW_FAIL :
 
236
                        break;
 
237
 
 
238
                    case CS_END_DATA :
 
239
                        more_fetch= FALSE;
 
240
                        continue;
 
241
 
 
242
                    default :
 
243
                        /* this is a error */
 
244
                        more_fetch= FALSE;
 
245
                        if(ct_cancel(NULL, cmd, CS_CANCEL_ALL) != CS_SUCCEED) {
 
246
                            ct_close(te_link, CS_FORCE_CLOSE);
 
247
                        }
 
248
                        ct_cmd_drop(cmd);
 
249
                        return FALSE;
 
250
                    }
 
251
                }
 
252
            }
 
253
            continue;
 
254
 
 
255
        default : break;
 
256
        }
 
257
    }
 
258
    ct_cmd_drop(cmd);
 
259
    return n;
 
260
}    
 
261
 
 
262
Int4 tax_getDesignator(char* sname)
 
263
{
 
264
    CS_COMMAND *cmd;
 
265
    CS_DATAFMT parfmt;
 
266
    CS_INT res_type, rows_read, tax_id= 0;
 
267
    Boolean more_results= TRUE;
 
268
    Boolean more_fetch;
 
269
    int nnn= 64;
 
270
 
 
271
    if((sname == NULL) || (*sname == '\0')) return 0;
 
272
    
 
273
    if(ct_cmd_alloc(te_link, &cmd) != CS_SUCCEED) return 0;
 
274
 
 
275
    /* build command: tax_getDesignator @name_txt */
 
276
 
 
277
    if(ct_command(cmd, CS_RPC_CMD, "tax_getDesignator", CS_NULLTERM, CS_UNUSED) != CS_SUCCEED) {
 
278
        ct_cmd_drop(cmd);
 
279
        return 0;
 
280
    }
 
281
        
 
282
    memset(&parfmt, 0, sizeof(CS_DATAFMT));
 
283
    strcpy(parfmt.name, "@name_txt");
 
284
    parfmt.namelen= CS_NULLTERM;
 
285
    parfmt.datatype= CS_CHAR_TYPE;
 
286
    parfmt.maxlength= strlen(sname);
 
287
    parfmt.status= CS_INPUTVALUE;
 
288
    parfmt.locale= NULL;
 
289
    if(ct_param(cmd, &parfmt, sname, parfmt.maxlength, CS_UNUSED) != CS_SUCCEED) {
 
290
        ct_cmd_drop(cmd);
 
291
        return 0;
 
292
    }
 
293
 
 
294
    /* send this command to server */
 
295
    if(ct_send(cmd) != CS_SUCCEED) {
 
296
        ct_cmd_drop(cmd);
 
297
        return 0;
 
298
    }
 
299
        
 
300
    while(more_results && (--nnn > 0)) {
 
301
        switch(ct_results(cmd, &res_type)) {
 
302
        case CS_FAIL :
 
303
            if(ct_cancel(NULL, cmd, CS_CANCEL_ALL) != CS_SUCCEED) {
 
304
                ct_close(te_link, CS_FORCE_CLOSE);
 
305
            }
 
306
        case CS_CANCELED :
 
307
            ct_cmd_drop(cmd);
 
308
            return 0;
 
309
 
 
310
        case CS_END_RESULTS :
 
311
            more_results= FALSE;
 
312
            break;
 
313
 
 
314
        case CS_SUCCEED :
 
315
            if(res_type == CS_STATUS_RESULT) {
 
316
                more_fetch= TRUE;
 
317
                parfmt.datatype= CS_INT_TYPE;
 
318
                parfmt.format= CS_FMT_UNUSED;
 
319
                parfmt.maxlength= 4;
 
320
                parfmt.scale= CS_SRC_VALUE;
 
321
                parfmt.precision= CS_SRC_VALUE;
 
322
                parfmt.count= 1;
 
323
                if(ct_bind(cmd, 1, &parfmt, &tax_id, NULL, NULL) != CS_SUCCEED) break;
 
324
                while(more_fetch) {
 
325
                    switch(ct_fetch(cmd,CS_UNUSED,CS_UNUSED,CS_UNUSED,&rows_read)) {
 
326
                    case CS_SUCCEED :
 
327
                        continue;
 
328
 
 
329
                    case CS_ROW_FAIL :
 
330
                        break;
 
331
 
 
332
                    case CS_END_DATA :
 
333
                        more_fetch= FALSE;
 
334
                        continue;
 
335
 
 
336
                    default :
 
337
                        /* this is a error */
 
338
                        more_fetch= FALSE;
 
339
                        if(ct_cancel(NULL, cmd, CS_CANCEL_ALL) != CS_SUCCEED) {
 
340
                            ct_close(te_link, CS_FORCE_CLOSE);
 
341
                        }
 
342
                        ct_cmd_drop(cmd);
 
343
                        return 0;
 
344
                    }
 
345
                }
 
346
            }
 
347
            continue;
 
348
 
 
349
        default : break;
 
350
        }
 
351
    }
 
352
    ct_cmd_drop(cmd);
 
353
    return tax_id;              
 
354
}
 
355
 
 
356
 
 
357
Int4 tax_uniqueName(char* sname, Int4 id)
 
358
{
 
359
    CS_COMMAND *cmd;
 
360
    CS_DATAFMT parfmt;
 
361
    CS_INT res_type, rows_read, tax_id= 0;
 
362
    Boolean more_results= TRUE;
 
363
    Boolean more_fetch;
 
364
    int nnn= 64;
 
365
 
 
366
    if((sname == NULL) || (*sname == '\0')) return 0;
 
367
    
 
368
    if(ct_cmd_alloc(te_link, &cmd) != CS_SUCCEED) return 0;
 
369
 
 
370
    /* build command: tax_checkUnique @name_txt, @tax_id */
 
371
 
 
372
    if(ct_command(cmd, CS_RPC_CMD, "tax_checkUnique", CS_NULLTERM, CS_UNUSED) != CS_SUCCEED) {
 
373
        ct_cmd_drop(cmd);
 
374
        return 0;
 
375
    }
 
376
        
 
377
    memset(&parfmt, 0, sizeof(CS_DATAFMT));
 
378
    strcpy(parfmt.name, "@name_txt");
 
379
    parfmt.namelen= CS_NULLTERM;
 
380
    parfmt.datatype= CS_CHAR_TYPE;
 
381
    parfmt.maxlength= strlen(sname);
 
382
    parfmt.status= CS_INPUTVALUE;
 
383
    parfmt.locale= NULL;
 
384
    if(ct_param(cmd, &parfmt, sname, parfmt.maxlength, CS_UNUSED) != CS_SUCCEED) {
 
385
        ct_cmd_drop(cmd);
 
386
        return 0;
 
387
    }
 
388
 
 
389
    strcpy(parfmt.name, "@tax_id");
 
390
    parfmt.datatype= CS_INT_TYPE;
 
391
    parfmt.maxlength= sizeof(CS_INT);
 
392
    if(ct_param(cmd, &parfmt, &id, parfmt.maxlength, CS_UNUSED) != CS_SUCCEED) {
 
393
        ct_cmd_drop(cmd);
 
394
        return 0;
 
395
    }
 
396
 
 
397
    /* send this command to server */
 
398
    if(ct_send(cmd) != CS_SUCCEED) {
 
399
        ct_cmd_drop(cmd);
 
400
        return 0;
 
401
    }
 
402
        
 
403
    while(more_results && (--nnn > 0)) {
 
404
        switch(ct_results(cmd, &res_type)) {
 
405
        case CS_FAIL :
 
406
            if(ct_cancel(NULL, cmd, CS_CANCEL_ALL) != CS_SUCCEED) {
 
407
                ct_close(te_link, CS_FORCE_CLOSE);
 
408
            }
 
409
        case CS_CANCELED :
 
410
            ct_cmd_drop(cmd);
 
411
            return 0;
 
412
 
 
413
        case CS_END_RESULTS :
 
414
            more_results= FALSE;
 
415
            break;
 
416
 
 
417
        case CS_SUCCEED :
 
418
            if(res_type == CS_STATUS_RESULT) {
 
419
                more_fetch= TRUE;
 
420
                parfmt.datatype= CS_INT_TYPE;
 
421
                parfmt.format= CS_FMT_UNUSED;
 
422
                parfmt.maxlength= 4;
 
423
                parfmt.scale= CS_SRC_VALUE;
 
424
                parfmt.precision= CS_SRC_VALUE;
 
425
                parfmt.count= 1;
 
426
                if(ct_bind(cmd, 1, &parfmt, &tax_id, NULL, NULL) != CS_SUCCEED) break;
 
427
                while(more_fetch) {
 
428
                    switch(ct_fetch(cmd,CS_UNUSED,CS_UNUSED,CS_UNUSED,&rows_read)) {
 
429
                    case CS_SUCCEED :
 
430
                        continue;
 
431
 
 
432
                    case CS_ROW_FAIL :
 
433
                        break;
 
434
 
 
435
                    case CS_END_DATA :
 
436
                        more_fetch= FALSE;
 
437
                        continue;
 
438
 
 
439
                    default :
 
440
                        /* this is a error */
 
441
                        more_fetch= FALSE;
 
442
                        if(ct_cancel(NULL, cmd, CS_CANCEL_ALL) != CS_SUCCEED) {
 
443
                            ct_close(te_link, CS_FORCE_CLOSE);
 
444
                        }
 
445
                        ct_cmd_drop(cmd);
 
446
                        return 0;
 
447
                    }
 
448
                }
 
449
            }
 
450
            continue;
 
451
 
 
452
        default : break;
 
453
        }
 
454
    }
 
455
    ct_cmd_drop(cmd);
 
456
    return tax_id;              
 
457
}
 
458
 
 
459
 
 
460
Int4 tax_getIdByName(char* sname, char* qualif, Int1 id)
 
461
{
 
462
    CS_COMMAND *cmd;
 
463
    CS_DATAFMT parfmt;
 
464
    CS_INT res_type, rows_read, tax_id= 0;
 
465
    int nnn= 64;
 
466
    Boolean more_results= TRUE;
 
467
    Boolean more_fetch;
 
468
    char s_sname[100];
 
469
    char s_qualif[120];
 
470
 
 
471
    if((sname == NULL) || (*sname == '\0')) return 0;
 
472
    
 
473
    if(ct_cmd_alloc(te_link, &cmd) != CS_SUCCEED) return 0;
 
474
 
 
475
    /* build command: tax_getIdByName(@org_name, @qualif, @q_type) */
 
476
 
 
477
    if(ct_command(cmd, CS_RPC_CMD, "tax_getIdByName", CS_NULLTERM, CS_UNUSED) != CS_SUCCEED) {
 
478
        ct_cmd_drop(cmd);
 
479
        return 0;
 
480
    }
 
481
        
 
482
    memset(&parfmt, 0, sizeof(CS_DATAFMT));
 
483
    strcpy(parfmt.name, "@org_name");
 
484
    parfmt.namelen= CS_NULLTERM;
 
485
    parfmt.datatype= CS_CHAR_TYPE;
 
486
    strncpy(s_sname, sname, 96);
 
487
    s_sname[96]= '\0';
 
488
    parfmt.maxlength= strlen(s_sname);
 
489
    parfmt.status= CS_INPUTVALUE;
 
490
    parfmt.locale= NULL;
 
491
    if(ct_param(cmd, &parfmt, s_sname, parfmt.maxlength, CS_UNUSED) != CS_SUCCEED) {
 
492
        ct_cmd_drop(cmd);
 
493
        return 0;
 
494
    }
 
495
    strcpy(parfmt.name, "@qualif");
 
496
    if(qualif == NULL) {
 
497
        s_qualif[0]= s_qualif[1]= '\0';
 
498
        parfmt.maxlength= 1;
 
499
    }
 
500
    else {
 
501
        strncpy(s_qualif, qualif, 116);
 
502
        s_qualif[116]= '\0';
 
503
        parfmt.maxlength= strlen(s_qualif);
 
504
    }
 
505
 
 
506
    if(ct_param(cmd, &parfmt, s_qualif, parfmt.maxlength, CS_UNUSED) != CS_SUCCEED) {
 
507
        ct_cmd_drop(cmd);
 
508
        return 0;
 
509
    }
 
510
 
 
511
    strcpy(parfmt.name, "@q_type");
 
512
    parfmt.datatype= CS_TINYINT_TYPE;
 
513
    parfmt.maxlength= sizeof(CS_TINYINT);
 
514
    if(ct_param(cmd, &parfmt, &id, parfmt.maxlength, CS_UNUSED) != CS_SUCCEED) {
 
515
        ct_cmd_drop(cmd);
 
516
        return 0;
 
517
    }
 
518
 
 
519
    /* send this command to server */
 
520
    if(ct_send(cmd) != CS_SUCCEED) {
 
521
        ct_cmd_drop(cmd);
 
522
        return 0;
 
523
    }
 
524
        
 
525
    while(more_results && (--nnn > 0)) {
 
526
        switch(ct_results(cmd, &res_type)) {
 
527
        case CS_FAIL :
 
528
            if(ct_cancel(NULL, cmd, CS_CANCEL_ALL) != CS_SUCCEED) {
 
529
                ct_close(te_link, CS_FORCE_CLOSE);
 
530
            }
 
531
        case CS_CANCELED :
 
532
            ct_cmd_drop(cmd);
 
533
            return 0;
 
534
 
 
535
        case CS_END_RESULTS :
 
536
            more_results= FALSE;
 
537
            break;
 
538
 
 
539
        case CS_SUCCEED :
 
540
            if(res_type == CS_STATUS_RESULT) {
 
541
                more_fetch= TRUE;
 
542
                parfmt.datatype= CS_INT_TYPE;
 
543
                parfmt.format= CS_FMT_UNUSED;
 
544
                parfmt.maxlength= 4;
 
545
                parfmt.scale= CS_SRC_VALUE;
 
546
                parfmt.precision= CS_SRC_VALUE;
 
547
                parfmt.count= 1;
 
548
                if(ct_bind(cmd, 1, &parfmt, &tax_id, NULL, NULL) != CS_SUCCEED) break;
 
549
                while(more_fetch) {
 
550
                    switch(ct_fetch(cmd,CS_UNUSED,CS_UNUSED,CS_UNUSED,&rows_read)) {
 
551
                    case CS_SUCCEED :
 
552
                        continue;
 
553
 
 
554
                    case CS_ROW_FAIL :
 
555
                        break;
 
556
 
 
557
                    case CS_END_DATA :
 
558
                        more_fetch= FALSE;
 
559
                        continue;
 
560
 
 
561
                    default :
 
562
                        /* this is a error */
 
563
                        more_fetch= FALSE;
 
564
                        if(ct_cancel(NULL, cmd, CS_CANCEL_ALL) != CS_SUCCEED) {
 
565
                            ct_close(te_link, CS_FORCE_CLOSE);
 
566
                        }
 
567
                        ct_cmd_drop(cmd);
 
568
                        return 0;
 
569
                    }
 
570
                }
 
571
            }
 
572
            continue;
 
573
 
 
574
        default : break;
 
575
        }
 
576
    }
 
577
    ct_cmd_drop(cmd);
 
578
    return tax_id;              
 
579
}
 
580
 
 
581
 
 
582
Int4 tax_getOrgNames(Int4 tax_id, TaxNamePtr* res_name)
 
583
{
 
584
    CS_COMMAND *cmd;
 
585
    CS_DATAFMT parfmt;
 
586
    CS_TINYINT smode;
 
587
    CS_INT res_type, rows_read;
 
588
    Boolean more_results= TRUE;
 
589
    Boolean more_fetch;
 
590
    char nbuff[256], ubuff[256];
 
591
    CS_SMALLINT has_unique;
 
592
    CS_TINYINT class_cde, designator;
 
593
    Int4 n= 0, alloc_room= 0;
 
594
    TaxNamePtr fnd_name= NULL;
 
595
    int nnn= 64;
 
596
 
 
597
    if(tax_id <= 0) return 0;
 
598
    
 
599
    if(ct_cmd_alloc(te_link, &cmd) != CS_SUCCEED) return 0;
 
600
 
 
601
 
 
602
    /* build command: tax_getOrgNames @tax_id */
 
603
 
 
604
    if(ct_command(cmd, CS_RPC_CMD, "tax_getOrgNames", CS_NULLTERM, CS_UNUSED) != CS_SUCCEED) {
 
605
        ct_cmd_drop(cmd);
 
606
        return 0;
 
607
    }
 
608
    
 
609
    memset(&parfmt, 0, sizeof(CS_DATAFMT));
 
610
    strcpy(parfmt.name, "@tax_id");
 
611
    parfmt.namelen= CS_NULLTERM;
 
612
    parfmt.datatype= CS_INT_TYPE;
 
613
    parfmt.maxlength= sizeof(Int4);
 
614
    parfmt.status= CS_INPUTVALUE;
 
615
    parfmt.locale= NULL;
 
616
    if(ct_param(cmd, &parfmt, &tax_id, parfmt.maxlength, CS_UNUSED) != CS_SUCCEED) {
 
617
        ct_cmd_drop(cmd);
 
618
        return 0;
 
619
    }
 
620
 
 
621
    /* send this command to server */
 
622
    if(ct_send(cmd) != CS_SUCCEED) {
 
623
        ct_cmd_drop(cmd);
 
624
        return 0;
 
625
    }
 
626
    
 
627
    while(more_results && (--nnn > 0)) {
 
628
        switch(ct_results(cmd, &res_type)) {
 
629
        case CS_FAIL :
 
630
            if(ct_cancel(NULL, cmd, CS_CANCEL_ALL) != CS_SUCCEED) {
 
631
                ct_close(te_link, CS_FORCE_CLOSE);
 
632
            }
 
633
        case CS_CANCELED :
 
634
            ct_cmd_drop(cmd);
 
635
            return 0;
 
636
 
 
637
        case CS_END_RESULTS :
 
638
            more_results= FALSE;
 
639
            break;
 
640
 
 
641
        case CS_SUCCEED :
 
642
            if(res_type == CS_ROW_RESULT) {
 
643
                /* bind and fetch these data */
 
644
                more_fetch= TRUE;
 
645
                parfmt.datatype= CS_INT_TYPE;
 
646
                parfmt.format= CS_FMT_UNUSED;
 
647
                parfmt.maxlength= 4;
 
648
                parfmt.scale= CS_SRC_VALUE;
 
649
                parfmt.precision= CS_SRC_VALUE;
 
650
                parfmt.count= 1;
 
651
                parfmt.locale= NULL;
 
652
                if(ct_bind(cmd, 1, &parfmt, &tax_id, NULL, NULL) != CS_SUCCEED) break;
 
653
 
 
654
                parfmt.datatype= CS_CHAR_TYPE;
 
655
                parfmt.format= CS_FMT_NULLTERM;
 
656
                parfmt.maxlength= 255;
 
657
                if((ct_bind(cmd, 2, &parfmt, nbuff, NULL, NULL) != CS_SUCCEED) ||
 
658
                   (ct_bind(cmd, 3, &parfmt, ubuff, NULL, &has_unique) != CS_SUCCEED)) break;
 
659
 
 
660
                parfmt.datatype= CS_TINYINT_TYPE;
 
661
                parfmt.format= CS_FMT_UNUSED;
 
662
                parfmt.maxlength= 1;
 
663
                if((ct_bind(cmd, 4, &parfmt, &class_cde, NULL, NULL) != CS_SUCCEED) ||
 
664
                   (ct_bind(cmd, 5, &parfmt, &designator, NULL, NULL) != CS_SUCCEED)) break;
 
665
 
 
666
                while(more_fetch) {
 
667
                    switch(ct_fetch(cmd,CS_UNUSED,CS_UNUSED,CS_UNUSED,&rows_read)) {
 
668
                    case CS_SUCCEED :
 
669
                        if(n == 0) {
 
670
                            *res_name= fnd_name= malloc(sizeof(TaxName));
 
671
                            alloc_room= 1;
 
672
                        }
 
673
                        else if(n >= alloc_room) {
 
674
                            
 
675
                            alloc_room+= (8 > (alloc_room/4))? 8 : (alloc_room/4);
 
676
                            *res_name= realloc(fnd_name, alloc_room*sizeof(TaxName));
 
677
                            if(*res_name == NULL) {
 
678
                                alloc_room= n--;
 
679
                                *res_name= fnd_name;
 
680
                            }
 
681
                            else fnd_name= *res_name;
 
682
                        }
 
683
 
 
684
                        fnd_name[n].tax_id= tax_id;
 
685
                        fnd_name[n].class_cde= class_cde;
 
686
                        fnd_name[n].designator= designator;
 
687
                        fnd_name[n].name_txt= StringSave(nbuff);
 
688
                        fnd_name[n].unique_name= (has_unique < 0)? NULL : StringSave(ubuff);
 
689
                        n++;
 
690
                        continue;
 
691
 
 
692
                    case CS_ROW_FAIL :
 
693
                        break;
 
694
 
 
695
                    case CS_END_DATA :
 
696
                        more_fetch= FALSE;
 
697
                        continue;
 
698
 
 
699
                    default :
 
700
                        /* this is an error */
 
701
                        more_fetch= FALSE;
 
702
                        if(ct_cancel(NULL, cmd, CS_CANCEL_ALL) != CS_SUCCEED) {
 
703
                            ct_close(te_link, CS_FORCE_CLOSE);
 
704
                        }
 
705
                        ct_cmd_drop(cmd);
 
706
                        return FALSE;
 
707
                    }
 
708
                }
 
709
            }
 
710
            continue;
 
711
 
 
712
        default : break;
 
713
        }
 
714
    }
 
715
    ct_cmd_drop(cmd);
 
716
    return n;
 
717
}    
 
718
 
 
719
 
 
720
TXC_NodeDataPtr txc_getOrgData(Int4 tax_id)
 
721
{
 
722
    CS_COMMAND *cmd;
 
723
    CS_DATAFMT parfmt;
 
724
    CS_INT res_type, rows_read;
 
725
    CS_SMALLINT rank_id, gc_id, mgc_id, div_id;
 
726
    Boolean more_results= TRUE;
 
727
    Boolean more_fetch;
 
728
    TXC_NodeDataPtr node_data= NULL;
 
729
    int nnn= 64;
 
730
    char embl_buff[8];
 
731
 
 
732
    if(tax_id <= 0) return 0;
 
733
    
 
734
    if(ct_cmd_alloc(te_link, &cmd) != CS_SUCCEED) return 0;
 
735
 
 
736
 
 
737
    /* build command: tax_getOrgData @tax_id */
 
738
 
 
739
    if(ct_command(cmd, CS_RPC_CMD, "tax_getOrgData", CS_NULLTERM, CS_UNUSED) != CS_SUCCEED) {
 
740
        ct_cmd_drop(cmd);
 
741
        return 0;
 
742
    }
 
743
    
 
744
    memset(&parfmt, 0, sizeof(CS_DATAFMT));
 
745
    strcpy(parfmt.name, "@tax_id");
 
746
    parfmt.namelen= CS_NULLTERM;
 
747
    parfmt.datatype= CS_INT_TYPE;
 
748
    parfmt.maxlength= sizeof(Int4);
 
749
    parfmt.status= CS_INPUTVALUE;
 
750
    parfmt.locale= NULL;
 
751
    if(ct_param(cmd, &parfmt, &tax_id, parfmt.maxlength, CS_UNUSED) != CS_SUCCEED) {
 
752
        ct_cmd_drop(cmd);
 
753
        return 0;
 
754
    }
 
755
 
 
756
    /* send this command to server */
 
757
    if(ct_send(cmd) != CS_SUCCEED) {
 
758
        ct_cmd_drop(cmd);
 
759
        return 0;
 
760
    }
 
761
    
 
762
    while(more_results && (--nnn > 0)) {
 
763
        switch(ct_results(cmd, &res_type)) {
 
764
        case CS_FAIL :
 
765
            if(ct_cancel(NULL, cmd, CS_CANCEL_ALL) != CS_SUCCEED) {
 
766
                ct_close(te_link, CS_FORCE_CLOSE);
 
767
            }
 
768
        case CS_CANCELED :
 
769
            ct_cmd_drop(cmd);
 
770
            return NULL;
 
771
 
 
772
        case CS_END_RESULTS :
 
773
            more_results= FALSE;
 
774
            break;
 
775
 
 
776
        case CS_SUCCEED :
 
777
            if(res_type == CS_ROW_RESULT) {
 
778
                /* bind and fetch these data */
 
779
                if(node_data == NULL) {
 
780
                    if((node_data= MemNew(sizeof(TXC_NodeData))) == NULL) {
 
781
                        ct_cancel(NULL, cmd, CS_CANCEL_ALL);
 
782
                        ct_cmd_drop(cmd);
 
783
                        return NULL;
 
784
                    }
 
785
                    node_data->tax_id= tax_id;
 
786
                }
 
787
                    
 
788
                more_fetch= TRUE;
 
789
                parfmt.datatype= CS_INT_TYPE;
 
790
                parfmt.format= CS_FMT_UNUSED;
 
791
                parfmt.maxlength= 4;
 
792
                parfmt.scale= CS_SRC_VALUE;
 
793
                parfmt.precision= CS_SRC_VALUE;
 
794
                parfmt.count= 1;
 
795
                parfmt.locale= NULL;
 
796
                if((ct_bind(cmd, 1, &parfmt, &node_data->crt_date, NULL, NULL) != CS_SUCCEED) ||
 
797
                   (ct_bind(cmd, 2, &parfmt, &node_data->upd_date, NULL, NULL) != CS_SUCCEED)) break;
 
798
 
 
799
                parfmt.datatype= CS_SMALLINT_TYPE;
 
800
                parfmt.maxlength= 2;
 
801
                if((ct_bind(cmd, 3, &parfmt, &rank_id, NULL, NULL) != CS_SUCCEED) ||
 
802
                   (ct_bind(cmd, 4, &parfmt, &gc_id, NULL, NULL) != CS_SUCCEED) ||
 
803
                   (ct_bind(cmd, 5, &parfmt, &mgc_id, NULL, NULL) != CS_SUCCEED) ||
 
804
                   (ct_bind(cmd, 6, &parfmt, &div_id, NULL, NULL) != CS_SUCCEED) ||
 
805
                   (ct_bind(cmd, 7, &parfmt, &node_data->flags, NULL, NULL) != CS_SUCCEED)) break;
 
806
 
 
807
                parfmt.datatype= CS_CHAR_TYPE;
 
808
                parfmt.format= CS_FMT_NULLTERM;
 
809
                parfmt.maxlength= 8;
 
810
                if(ct_bind(cmd, 8, &parfmt, embl_buff, NULL, NULL) != CS_SUCCEED) break;
 
811
 
 
812
                while(more_fetch) {
 
813
                    switch(ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &rows_read)) {
 
814
                    case CS_SUCCEED :
 
815
                        node_data->rank_id= rank_id;
 
816
                        node_data->gc_id= gc_id;
 
817
                        node_data->mgc_id= mgc_id;
 
818
                        node_data->div_id= div_id;
 
819
                        StringNCpy(node_data->embl_cde, embl_buff, 4);
 
820
                        continue;
 
821
 
 
822
                    case CS_ROW_FAIL :
 
823
                        break;
 
824
 
 
825
                    case CS_END_DATA :
 
826
                        more_fetch= FALSE;
 
827
                        continue;
 
828
 
 
829
                    default :
 
830
                        /* this is an error */
 
831
                        more_fetch= FALSE;
 
832
                        if(ct_cancel(NULL, cmd, CS_CANCEL_ALL) != CS_SUCCEED) {
 
833
                            ct_close(te_link, CS_FORCE_CLOSE);
 
834
                        }
 
835
                        ct_cmd_drop(cmd);
 
836
                        return FALSE;
 
837
                    }
 
838
                }
 
839
            }
 
840
            continue;
 
841
 
 
842
        default : break;
 
843
        }
 
844
    }
 
845
    ct_cmd_drop(cmd);
 
846
    return node_data;
 
847
}    
 
848
 
 
849
 
 
850
Boolean txc_loadNameClasses()
 
851
{
 
852
    CS_COMMAND *cmd;
 
853
    CS_DATAFMT datafmt;
 
854
    CS_SMALLINT class_cde, priority;
 
855
    CS_INT res_type, rows_read;
 
856
    int nnn= 64;
 
857
    Boolean more_results= TRUE;
 
858
    Boolean more_fetch;
 
859
    char class_txt[128];
 
860
    
 
861
    if(ct_cmd_alloc(te_link, &cmd) != CS_SUCCEED) return 0;
 
862
 
 
863
 
 
864
    /* build command: tax_dumpClasses() */
 
865
 
 
866
    if(ct_command(cmd, CS_RPC_CMD, "tax_dumpClasses", CS_NULLTERM, CS_UNUSED) != CS_SUCCEED) {
 
867
        ct_cmd_drop(cmd);
 
868
        return FALSE;
 
869
    }
 
870
    
 
871
    /* send this command to server */
 
872
    if(ct_send(cmd) != CS_SUCCEED) {
 
873
        ct_cmd_drop(cmd);
 
874
        return FALSE;
 
875
    }
 
876
    
 
877
    memset(&datafmt, 0, sizeof(CS_DATAFMT));
 
878
 
 
879
    while(more_results && (--nnn > 0)) {
 
880
        switch(ct_results(cmd, &res_type)) {
 
881
        case CS_FAIL :
 
882
            if(ct_cancel(NULL, cmd, CS_CANCEL_ALL) != CS_SUCCEED) {
 
883
                ct_close(te_link, CS_FORCE_CLOSE);
 
884
            }
 
885
        case CS_CANCELED :
 
886
            ct_cmd_drop(cmd);
 
887
            return FALSE;
 
888
 
 
889
        case CS_END_RESULTS :
 
890
            more_results= FALSE;
 
891
            break;
 
892
 
 
893
        case CS_SUCCEED :
 
894
            if(res_type == CS_ROW_RESULT) {
 
895
                /* bind and fetch these data */
 
896
                more_fetch= TRUE;
 
897
                datafmt.datatype= CS_SMALLINT_TYPE;
 
898
                datafmt.format= CS_FMT_UNUSED;
 
899
                datafmt.maxlength= sizeof(CS_SMALLINT);
 
900
                datafmt.scale= CS_SRC_VALUE;
 
901
                datafmt.precision= CS_SRC_VALUE;
 
902
                datafmt.count= 1;
 
903
                datafmt.locale= NULL;
 
904
                if((ct_bind(cmd, 1, &datafmt, &class_cde, NULL, NULL) != CS_SUCCEED) ||
 
905
                   (ct_bind(cmd, 2, &datafmt, &priority, NULL, NULL) != CS_SUCCEED)) break;
 
906
 
 
907
                datafmt.datatype= CS_CHAR_TYPE;
 
908
                datafmt.format= CS_FMT_NULLTERM;
 
909
                datafmt.maxlength= 128;
 
910
                if(ct_bind(cmd, 3, &datafmt, class_txt, NULL, NULL) != CS_SUCCEED) break;
 
911
 
 
912
                while(more_fetch) {
 
913
                    switch(ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &rows_read)) {
 
914
                    case CS_SUCCEED :
 
915
                        tax_addNameClass(class_cde, class_txt, priority);
 
916
                        continue;
 
917
 
 
918
                    case CS_ROW_FAIL :
 
919
                        break;
 
920
 
 
921
                    case CS_END_DATA :
 
922
                        more_fetch= FALSE;
 
923
                        continue;
 
924
 
 
925
                    default :
 
926
                        /* this is an error */
 
927
                        more_fetch= FALSE;
 
928
                        if(ct_cancel(NULL, cmd, CS_CANCEL_ALL) != CS_SUCCEED) {
 
929
                            ct_close(te_link, CS_FORCE_CLOSE);
 
930
                        }
 
931
                        ct_cmd_drop(cmd);
 
932
                        return FALSE;
 
933
                    }
 
934
                }
 
935
            }
 
936
            continue;
 
937
 
 
938
        default : break;
 
939
        }
 
940
    }
 
941
    ct_cmd_drop(cmd);
 
942
    return TRUE;
 
943
}    
 
944
 
 
945
 
 
946
Boolean txc_loadRanks()
 
947
{
 
948
    CS_COMMAND *cmd;
 
949
    CS_DATAFMT datafmt;
 
950
    CS_SMALLINT rank_id;
 
951
    CS_INT res_type, rows_read;
 
952
    int nnn= 64;
 
953
    Boolean more_results= TRUE;
 
954
    Boolean more_fetch;
 
955
    char rank_txt[128];
 
956
    
 
957
    if(ct_cmd_alloc(te_link, &cmd) != CS_SUCCEED) return 0;
 
958
 
 
959
 
 
960
    /* build command: tax_dumpRanks() */
 
961
 
 
962
    if(ct_command(cmd, CS_RPC_CMD, "tax_dumpRanks", CS_NULLTERM, CS_UNUSED) != CS_SUCCEED) {
 
963
        ct_cmd_drop(cmd);
 
964
        return FALSE;
 
965
    }
 
966
    
 
967
    /* send this command to server */
 
968
    if(ct_send(cmd) != CS_SUCCEED) {
 
969
        ct_cmd_drop(cmd);
 
970
        return FALSE;
 
971
    }
 
972
    
 
973
    memset(&datafmt, 0, sizeof(CS_DATAFMT));
 
974
 
 
975
    while(more_results && (--nnn > 0)) {
 
976
        switch(ct_results(cmd, &res_type)) {
 
977
        case CS_FAIL :
 
978
            if(ct_cancel(NULL, cmd, CS_CANCEL_ALL) != CS_SUCCEED) {
 
979
                ct_close(te_link, CS_FORCE_CLOSE);
 
980
            }
 
981
        case CS_CANCELED :
 
982
            ct_cmd_drop(cmd);
 
983
            return FALSE;
 
984
 
 
985
        case CS_END_RESULTS :
 
986
            more_results= FALSE;
 
987
            break;
 
988
 
 
989
        case CS_SUCCEED :
 
990
            if(res_type == CS_ROW_RESULT) {
 
991
                /* bind and fetch these data */
 
992
                more_fetch= TRUE;
 
993
                datafmt.datatype= CS_SMALLINT_TYPE;
 
994
                datafmt.format= CS_FMT_UNUSED;
 
995
                datafmt.maxlength= sizeof(CS_SMALLINT);
 
996
                datafmt.scale= CS_SRC_VALUE;
 
997
                datafmt.precision= CS_SRC_VALUE;
 
998
                datafmt.count= 1;
 
999
                datafmt.locale= NULL;
 
1000
                if(ct_bind(cmd, 1, &datafmt, &rank_id, NULL, NULL) != CS_SUCCEED) break;
 
1001
 
 
1002
                datafmt.datatype= CS_CHAR_TYPE;
 
1003
                datafmt.format= CS_FMT_NULLTERM;
 
1004
                datafmt.maxlength= 128;
 
1005
                if(ct_bind(cmd, 2, &datafmt, rank_txt, NULL, NULL) != CS_SUCCEED) break;
 
1006
 
 
1007
                while(more_fetch) {
 
1008
                    switch(ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &rows_read)) {
 
1009
                    case CS_SUCCEED :
 
1010
                        tax_addRank(rank_id, rank_txt);
 
1011
                        continue;
 
1012
 
 
1013
                    case CS_ROW_FAIL :
 
1014
                        break;
 
1015
 
 
1016
                    case CS_END_DATA :
 
1017
                        more_fetch= FALSE;
 
1018
                        continue;
 
1019
 
 
1020
                    default :
 
1021
                        /* this is an error */
 
1022
                        more_fetch= FALSE;
 
1023
                        if(ct_cancel(NULL, cmd, CS_CANCEL_ALL) != CS_SUCCEED) {
 
1024
                            ct_close(te_link, CS_FORCE_CLOSE);
 
1025
                        }
 
1026
                        ct_cmd_drop(cmd);
 
1027
                        return FALSE;
 
1028
                    }
 
1029
                }
 
1030
            }
 
1031
            continue;
 
1032
 
 
1033
        default : break;
 
1034
        }
 
1035
    }
 
1036
    ct_cmd_drop(cmd);
 
1037
    return TRUE;
 
1038
}    
 
1039
 
 
1040
 
 
1041
Boolean txc_loadDivisions()
 
1042
{
 
1043
    CS_COMMAND *cmd;
 
1044
    CS_DATAFMT datafmt;
 
1045
    CS_SMALLINT div_id;
 
1046
    CS_INT res_type, rows_read;
 
1047
    int nnn= 64;
 
1048
    Boolean more_results= TRUE;
 
1049
    Boolean more_fetch;
 
1050
    char div_cde[8], div_txt[128];
 
1051
    
 
1052
    if(ct_cmd_alloc(te_link, &cmd) != CS_SUCCEED) return 0;
 
1053
 
 
1054
 
 
1055
    /* build command: tax_dumpDivisions() */
 
1056
 
 
1057
    if(ct_command(cmd, CS_RPC_CMD, "tax_dumpDivisions", CS_NULLTERM, CS_UNUSED) != CS_SUCCEED) {
 
1058
        ct_cmd_drop(cmd);
 
1059
        return FALSE;
 
1060
    }
 
1061
    
 
1062
    /* send this command to server */
 
1063
    if(ct_send(cmd) != CS_SUCCEED) {
 
1064
        ct_cmd_drop(cmd);
 
1065
        return FALSE;
 
1066
    }
 
1067
    
 
1068
    memset(&datafmt, 0, sizeof(CS_DATAFMT));
 
1069
 
 
1070
    while(more_results && (--nnn > 0)) {
 
1071
        switch(ct_results(cmd, &res_type)) {
 
1072
        case CS_FAIL :
 
1073
            if(ct_cancel(NULL, cmd, CS_CANCEL_ALL) != CS_SUCCEED) {
 
1074
                ct_close(te_link, CS_FORCE_CLOSE);
 
1075
            }
 
1076
        case CS_CANCELED :
 
1077
            ct_cmd_drop(cmd);
 
1078
            return FALSE;
 
1079
 
 
1080
        case CS_END_RESULTS : 
 
1081
            more_results= FALSE;
 
1082
            break;
 
1083
 
 
1084
        case CS_SUCCEED :
 
1085
            if(res_type == CS_ROW_RESULT) {
 
1086
                /* bind and fetch these data */
 
1087
                more_fetch= TRUE;
 
1088
                datafmt.datatype= CS_SMALLINT_TYPE;
 
1089
                datafmt.format= CS_FMT_UNUSED;
 
1090
                datafmt.maxlength= sizeof(CS_SMALLINT);
 
1091
                datafmt.scale= CS_SRC_VALUE;
 
1092
                datafmt.precision= CS_SRC_VALUE;
 
1093
                datafmt.count= 1;
 
1094
                datafmt.locale= NULL;
 
1095
                if(ct_bind(cmd, 1, &datafmt, &div_id, NULL, NULL) != CS_SUCCEED) break;
 
1096
 
 
1097
                datafmt.datatype= CS_CHAR_TYPE;
 
1098
                datafmt.format= CS_FMT_NULLTERM;
 
1099
                datafmt.maxlength= 8;
 
1100
                if(ct_bind(cmd, 2, &datafmt, div_cde, NULL, NULL) != CS_SUCCEED) break;
 
1101
 
 
1102
                datafmt.maxlength= 128;
 
1103
                if(ct_bind(cmd, 3, &datafmt, div_txt, NULL, NULL) != CS_SUCCEED) break;         
 
1104
 
 
1105
                while(more_fetch) {
 
1106
                    switch(ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &rows_read)) {
 
1107
                    case CS_SUCCEED :
 
1108
                        tax_addDivision(div_id, div_cde, div_txt);
 
1109
                        continue;
 
1110
 
 
1111
                    case CS_ROW_FAIL :
 
1112
                        break;
 
1113
 
 
1114
                    case CS_END_DATA :
 
1115
                        more_fetch= FALSE;
 
1116
                        continue;
 
1117
 
 
1118
                    default :
 
1119
                        /* this is an error */
 
1120
                        more_fetch= FALSE;
 
1121
                        if(ct_cancel(NULL, cmd, CS_CANCEL_ALL) != CS_SUCCEED) {
 
1122
                            ct_close(te_link, CS_FORCE_CLOSE);
 
1123
                        }
 
1124
                        ct_cmd_drop(cmd);
 
1125
                        return FALSE;
 
1126
                    }
 
1127
                }
 
1128
            }
 
1129
            continue;
 
1130
 
 
1131
        default : break;
 
1132
        }
 
1133
    }
 
1134
    ct_cmd_drop(cmd);
 
1135
    return TRUE;
 
1136
}    
 
1137
 
 
1138
Boolean txc_loadGCs()
 
1139
{
 
1140
    CS_COMMAND *cmd;
 
1141
    CS_DATAFMT datafmt;
 
1142
    CS_SMALLINT gc_id;
 
1143
    CS_INT res_type, rows_read;
 
1144
    int nnn= 64;
 
1145
    Boolean more_results= TRUE;
 
1146
    Boolean more_fetch;
 
1147
    char gc_name[128];
 
1148
    
 
1149
    if(ct_cmd_alloc(te_link, &cmd) != CS_SUCCEED) return 0;
 
1150
 
 
1151
 
 
1152
    /* build command: tax_dumpGCs() */
 
1153
 
 
1154
    if(ct_command(cmd, CS_RPC_CMD, "tax_dumpGCs", CS_NULLTERM, CS_UNUSED) != CS_SUCCEED) {
 
1155
        ct_cmd_drop(cmd);
 
1156
        return FALSE;
 
1157
    }
 
1158
    
 
1159
    /* send this command to server */
 
1160
    if(ct_send(cmd) != CS_SUCCEED) {
 
1161
        ct_cmd_drop(cmd);
 
1162
        return FALSE;
 
1163
    }
 
1164
    
 
1165
    memset(&datafmt, 0, sizeof(CS_DATAFMT));
 
1166
 
 
1167
    while(more_results && (--nnn > 0)) {
 
1168
        switch(ct_results(cmd, &res_type)) {
 
1169
        case CS_FAIL :
 
1170
            if(ct_cancel(NULL, cmd, CS_CANCEL_ALL) != CS_SUCCEED) {
 
1171
                ct_close(te_link, CS_FORCE_CLOSE);
 
1172
            }
 
1173
        case CS_CANCELED :
 
1174
            ct_cmd_drop(cmd);
 
1175
            return FALSE;
 
1176
 
 
1177
        case CS_END_RESULTS :
 
1178
            more_results= FALSE;
 
1179
            break;
 
1180
 
 
1181
        case CS_SUCCEED :
 
1182
            if(res_type == CS_ROW_RESULT) {
 
1183
                /* bind and fetch these data */
 
1184
                more_fetch= TRUE;
 
1185
                datafmt.datatype= CS_SMALLINT_TYPE;
 
1186
                datafmt.format= CS_FMT_UNUSED;
 
1187
                datafmt.maxlength= sizeof(CS_SMALLINT);
 
1188
                datafmt.scale= CS_SRC_VALUE;
 
1189
                datafmt.precision= CS_SRC_VALUE;
 
1190
                datafmt.count= 1;
 
1191
                datafmt.locale= NULL;
 
1192
                if(ct_bind(cmd, 1, &datafmt, &gc_id, NULL, NULL) != CS_SUCCEED) break;
 
1193
 
 
1194
                datafmt.datatype= CS_CHAR_TYPE;
 
1195
                datafmt.format= CS_FMT_NULLTERM;
 
1196
                datafmt.maxlength= 128;
 
1197
                if(ct_bind(cmd, 2, &datafmt, gc_name, NULL, NULL) != CS_SUCCEED) break;
 
1198
 
 
1199
                while(more_fetch) {
 
1200
                    switch(ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &rows_read)) {
 
1201
                    case CS_SUCCEED :
 
1202
                        tax_addGC(gc_id, gc_name);
 
1203
                        continue;
 
1204
 
 
1205
                    case CS_ROW_FAIL :
 
1206
                        break;
 
1207
 
 
1208
                    case CS_END_DATA :
 
1209
                        more_fetch= FALSE;
 
1210
                        continue;
 
1211
 
 
1212
                    default :
 
1213
                        /* this is an error */
 
1214
                        more_fetch= FALSE;
 
1215
                        if(ct_cancel(NULL, cmd, CS_CANCEL_ALL) != CS_SUCCEED) {
 
1216
                            ct_close(te_link, CS_FORCE_CLOSE);
 
1217
                        }
 
1218
                        ct_cmd_drop(cmd);
 
1219
                        return FALSE;
 
1220
                    }
 
1221
                }
 
1222
            }
 
1223
            continue;
 
1224
 
 
1225
        default : break;
 
1226
        }
 
1227
    }
 
1228
    ct_cmd_drop(cmd);
 
1229
    return TRUE;
 
1230
}    
 
1231
 
 
1232
 
 
1233
TXC_TreeNodePtr* txc_getLineage(Int4 lin_id, Int4Ptr lin_len)
 
1234
{
 
1235
    CS_COMMAND *cmd;
 
1236
    CS_DATAFMT parfmt;
 
1237
    CS_INT res_type, rows_read, tax_id, flags;
 
1238
    int nnn= 64;
 
1239
    Boolean more_results= TRUE;
 
1240
    Boolean more_fetch;
 
1241
    Int4 n, l, alloc_room= 48;
 
1242
    char label[512];
 
1243
    TXC_TreeNodePtr* lineage= NULL;
 
1244
    CharPtr c;
 
1245
 
 
1246
    *lin_len= 0;
 
1247
        
 
1248
    if(ct_cmd_alloc(te_link, &cmd) != CS_SUCCEED) return FALSE;
 
1249
 
 
1250
 
 
1251
    /* build command: tax_getLineage @tax_id=lin_id */
 
1252
 
 
1253
    if(ct_command(cmd, CS_RPC_CMD, "tax_getLineage", CS_NULLTERM, CS_UNUSED) != CS_SUCCEED) {
 
1254
        ct_cmd_drop(cmd);
 
1255
        return NULL;
 
1256
    }
 
1257
    
 
1258
    memset(&parfmt, 0, sizeof(CS_DATAFMT));
 
1259
    strcpy(parfmt.name, "@tax_id");
 
1260
    parfmt.namelen= CS_NULLTERM;
 
1261
    parfmt.datatype= CS_INT_TYPE;
 
1262
    parfmt.maxlength= CS_UNUSED;
 
1263
    parfmt.status= CS_INPUTVALUE;
 
1264
    parfmt.locale= NULL;
 
1265
    if(ct_param(cmd, &parfmt, &lin_id, 4, CS_UNUSED) != CS_SUCCEED) {
 
1266
        ct_cmd_drop(cmd);
 
1267
        return NULL;
 
1268
    }
 
1269
 
 
1270
 
 
1271
    /* send this command to server */
 
1272
    if(ct_send(cmd) != CS_SUCCEED) {
 
1273
        ct_cmd_drop(cmd);
 
1274
        return NULL;
 
1275
    }
 
1276
 
 
1277
 
 
1278
    while(more_results && (--nnn > 0)) {
 
1279
        switch(ct_results(cmd, &res_type)) {
 
1280
        case CS_FAIL :
 
1281
            if(ct_cancel(NULL, cmd, CS_CANCEL_ALL) != CS_SUCCEED) {
 
1282
                ct_close(te_link, CS_FORCE_CLOSE);
 
1283
            }
 
1284
        case CS_CANCELED :
 
1285
            ct_cmd_drop(cmd);
 
1286
            return NULL;
 
1287
 
 
1288
        case CS_END_RESULTS :
 
1289
            more_results= FALSE;
 
1290
            break;
 
1291
 
 
1292
        case CS_SUCCEED :
 
1293
            if(res_type == CS_ROW_RESULT) {
 
1294
                /* bind and fetch these data */
 
1295
                more_fetch= TRUE;
 
1296
                parfmt.datatype= CS_INT_TYPE;
 
1297
                parfmt.format= CS_FMT_UNUSED;
 
1298
                parfmt.maxlength= 4;
 
1299
                parfmt.scale= CS_SRC_VALUE;
 
1300
                parfmt.precision= CS_SRC_VALUE;
 
1301
                parfmt.count= 1;
 
1302
                parfmt.locale= NULL;
 
1303
                if((ct_bind(cmd, 1, &parfmt, &tax_id, NULL, NULL) != CS_SUCCEED) ||
 
1304
                   (ct_bind(cmd, 2, &parfmt, &flags, NULL, NULL) != CS_SUCCEED)) {
 
1305
                    break;
 
1306
                }
 
1307
                parfmt.datatype= CS_CHAR_TYPE;
 
1308
                parfmt.format= CS_FMT_NULLTERM;
 
1309
                parfmt.maxlength= 255;
 
1310
                if(ct_bind(cmd, 3, &parfmt, label, NULL, NULL) != CS_SUCCEED) break;
 
1311
 
 
1312
                if(lineage == NULL) {
 
1313
                    lineage= MemNew(alloc_room*sizeof(TXC_TreeNodePtr));
 
1314
                    if(lineage == NULL) {
 
1315
                        ct_cancel(NULL, cmd, CS_CANCEL_ALL);
 
1316
                        ct_cmd_drop(cmd);
 
1317
                        return NULL;
 
1318
                    }
 
1319
                    n= 0;
 
1320
                }
 
1321
 
 
1322
                while(more_fetch) {
 
1323
                    switch(ct_fetch(cmd,CS_UNUSED,CS_UNUSED,CS_UNUSED,&rows_read)) {
 
1324
                    case CS_SUCCEED :
 
1325
                        if(n >= alloc_room) {
 
1326
                            /* need more memory */
 
1327
                            TXC_TreeNodePtr* tmp= MemMore(lineage, (alloc_room + 8)*sizeof(TXC_TreeNodePtr));
 
1328
 
 
1329
                            if(tmp == NULL) continue;
 
1330
                            lineage= tmp;
 
1331
                            alloc_room+= 8;
 
1332
                        }
 
1333
                        l= StringLen(label) + 1;
 
1334
                        if((lineage[n]= MemNew(l + 8)) != NULL) {
 
1335
                            lineage[n]->tax_id= tax_id;
 
1336
                            lineage[n]->flags= flags;
 
1337
                            StringCpy(lineage[n]->node_label, label);
 
1338
                            if((flags & TXC_SUFFIX) != 0) {
 
1339
                                c= StringChr(lineage[n]->node_label, '~');
 
1340
                                if(c != NULL) *c= '\0';
 
1341
                                lineage[n]->flags^= TXC_SUFFIX; /* clear this flag */
 
1342
                            }
 
1343
                            n++;
 
1344
                        }
 
1345
                        continue;
 
1346
 
 
1347
                    case CS_ROW_FAIL :
 
1348
                        break;
 
1349
 
 
1350
                    case CS_END_DATA :
 
1351
                        more_fetch= FALSE;
 
1352
                        continue;
 
1353
 
 
1354
                    default :
 
1355
                        /* this is a error */
 
1356
                        more_fetch= FALSE;
 
1357
                        if(ct_cancel(NULL, cmd, CS_CANCEL_ALL) != CS_SUCCEED) {
 
1358
                            ct_close(te_link, CS_FORCE_CLOSE);
 
1359
                        }
 
1360
                        ct_cmd_drop(cmd);
 
1361
                        return NULL;
 
1362
                    }
 
1363
                }
 
1364
            }
 
1365
            continue;
 
1366
 
 
1367
        default : break;
 
1368
        }
 
1369
    }
 
1370
    ct_cmd_drop(cmd);
 
1371
    *lin_len= n;
 
1372
    return lineage;
 
1373
}    
 
1374
 
 
1375
 
 
1376
TXC_TreeNodePtr* txc_getChildren(Int4 node_id, Int4Ptr nof_children)
 
1377
{
 
1378
    CS_COMMAND *cmd;
 
1379
    CS_DATAFMT parfmt;
 
1380
    CS_INT res_type, rows_read, tax_id, parent_id, flags, last_parent= -1;
 
1381
    CS_SMALLINT levels= 1;
 
1382
    int nnn= 64;
 
1383
    Boolean more_results= TRUE;
 
1384
    Boolean more_fetch;
 
1385
    Int4 n, l, alloc_room= 16;
 
1386
    char label[512];
 
1387
    TXC_TreeNodePtr* child= NULL;
 
1388
    CharPtr c;
 
1389
 
 
1390
    if(node_id < 0) {
 
1391
        node_id= -node_id;
 
1392
        levels= 64;
 
1393
        alloc_room= 64*1024;
 
1394
    }
 
1395
 
 
1396
    *nof_children= 0;
 
1397
        
 
1398
    if(ct_cmd_alloc(te_link, &cmd) != CS_SUCCEED) return NULL;
 
1399
 
 
1400
 
 
1401
    /* build command: tax_getSubtree @root_id=root, @levels= levels */
 
1402
 
 
1403
    if(ct_command(cmd, CS_RPC_CMD, "tax_getSubtree", CS_NULLTERM, CS_UNUSED) != CS_SUCCEED) {
 
1404
        ct_cmd_drop(cmd);
 
1405
        return NULL;
 
1406
    }
 
1407
    
 
1408
    memset(&parfmt, 0, sizeof(CS_DATAFMT));
 
1409
    strcpy(parfmt.name, "@root_id");
 
1410
    parfmt.namelen= CS_NULLTERM;
 
1411
    parfmt.datatype= CS_INT_TYPE;
 
1412
    parfmt.maxlength= CS_UNUSED;
 
1413
    parfmt.status= CS_INPUTVALUE;
 
1414
    parfmt.locale= NULL;
 
1415
    if(ct_param(cmd, &parfmt, &node_id, 4, CS_UNUSED) != CS_SUCCEED) {
 
1416
        ct_cmd_drop(cmd);
 
1417
        return NULL;
 
1418
    }
 
1419
 
 
1420
    strcpy(parfmt.name, "@levels");
 
1421
    parfmt.datatype= CS_SMALLINT_TYPE;
 
1422
    parfmt.locale= NULL;
 
1423
    if(ct_param(cmd, &parfmt, &levels, 2, CS_UNUSED) != CS_SUCCEED) {
 
1424
        ct_cmd_drop(cmd);
 
1425
        return NULL;
 
1426
    }
 
1427
 
 
1428
    /* send this command to server */
 
1429
    if(ct_send(cmd) != CS_SUCCEED) {
 
1430
        ct_cmd_drop(cmd);
 
1431
        return NULL;
 
1432
    }
 
1433
 
 
1434
 
 
1435
    while(more_results && (--nnn > 0)) {
 
1436
        switch(ct_results(cmd, &res_type)) {
 
1437
        case CS_FAIL :
 
1438
            if(ct_cancel(NULL, cmd, CS_CANCEL_ALL) != CS_SUCCEED) {
 
1439
                ct_close(te_link, CS_FORCE_CLOSE);
 
1440
            }
 
1441
        case CS_CANCELED :
 
1442
            ct_cmd_drop(cmd);
 
1443
            return NULL;
 
1444
 
 
1445
        case CS_END_RESULTS :
 
1446
            more_results= FALSE;
 
1447
            break;
 
1448
 
 
1449
        case CS_SUCCEED :
 
1450
            if(res_type == CS_STATUS_RESULT) {
 
1451
                more_fetch= TRUE;
 
1452
                parfmt.datatype= CS_INT_TYPE;
 
1453
                parfmt.format= CS_FMT_UNUSED;
 
1454
                parfmt.maxlength= 4;
 
1455
                parfmt.scale= CS_SRC_VALUE;
 
1456
                parfmt.precision= CS_SRC_VALUE;
 
1457
                parfmt.count= 1;
 
1458
                if(ct_bind(cmd, 1, &parfmt, &tax_id, NULL, NULL) != CS_SUCCEED) break;
 
1459
                while(more_fetch) {
 
1460
                    switch(ct_fetch(cmd,CS_UNUSED,CS_UNUSED,CS_UNUSED,&rows_read)) {
 
1461
                    case CS_SUCCEED :
 
1462
                        continue;
 
1463
 
 
1464
                    case CS_ROW_FAIL :
 
1465
                        break;
 
1466
 
 
1467
                    case CS_END_DATA :
 
1468
                        more_fetch= FALSE;
 
1469
                        continue;
 
1470
 
 
1471
                    default :
 
1472
                        /* this is a error */
 
1473
                        more_fetch= FALSE;
 
1474
                        if(ct_cancel(NULL, cmd, CS_CANCEL_ALL) != CS_SUCCEED) {
 
1475
                            ct_close(te_link, CS_FORCE_CLOSE);
 
1476
                        }
 
1477
                        ct_cmd_drop(cmd);
 
1478
                        return 0;
 
1479
                    }
 
1480
                }
 
1481
            }
 
1482
            else if(res_type == CS_ROW_RESULT) {
 
1483
                /* bind and fetch these data */
 
1484
                more_fetch= TRUE;
 
1485
                parfmt.datatype= CS_INT_TYPE;
 
1486
                parfmt.format= CS_FMT_UNUSED;
 
1487
                parfmt.maxlength= 4;
 
1488
                parfmt.scale= CS_SRC_VALUE;
 
1489
                parfmt.precision= CS_SRC_VALUE;
 
1490
                parfmt.count= 1;
 
1491
                parfmt.locale= NULL;
 
1492
                if((ct_bind(cmd, 1, &parfmt, &parent_id, NULL, NULL) != CS_SUCCEED) ||
 
1493
                   (ct_bind(cmd, 2, &parfmt, &tax_id, NULL, NULL) != CS_SUCCEED) ||
 
1494
                   (ct_bind(cmd, 3, &parfmt, &flags, NULL, NULL) != CS_SUCCEED)) {
 
1495
                    break;
 
1496
                }
 
1497
                parfmt.datatype= CS_CHAR_TYPE;
 
1498
                parfmt.format= CS_FMT_NULLTERM;
 
1499
                parfmt.maxlength= 255;
 
1500
                if(ct_bind(cmd, 4, &parfmt, label, NULL, NULL) != CS_SUCCEED) break;
 
1501
 
 
1502
 
 
1503
                while(more_fetch) {
 
1504
                    switch(ct_fetch(cmd,CS_UNUSED,CS_UNUSED,CS_UNUSED,&rows_read)) {
 
1505
                    case CS_SUCCEED :
 
1506
                        if(((levels == 1) && (parent_id != node_id)) || (tax_id == node_id)) continue;
 
1507
                        
 
1508
                        if(child == NULL) {
 
1509
                            child= MemNew(alloc_room*sizeof(TXC_TreeNodePtr));
 
1510
                            if(child == NULL) {
 
1511
                                ct_cancel(NULL, cmd, CS_CANCEL_ALL);
 
1512
                                ct_cmd_drop(cmd);
 
1513
                                return NULL;
 
1514
                            }
 
1515
                            n= 0;
 
1516
                        }
 
1517
                        else if(n >= alloc_room-1) {
 
1518
                            /* need more memory */
 
1519
                            TXC_TreeNodePtr* tmp= 
 
1520
                                MemMore(child, (alloc_room + 8*levels)*sizeof(TXC_TreeNodePtr));
 
1521
 
 
1522
                            if(tmp == NULL) continue;
 
1523
                            child= tmp;
 
1524
                            alloc_room+= 8*levels;
 
1525
                        }
 
1526
                        if((levels > 1) && (parent_id != last_parent)) {
 
1527
                            last_parent= parent_id;
 
1528
                            child[n]= MemNew(8);
 
1529
                            child[n]->tax_id= parent_id;
 
1530
                            child[n++]->flags= TXC_SUFFIX;
 
1531
                        }
 
1532
                        l= StringLen(label) + 1;
 
1533
                        if((child[n]= MemNew(l + 8)) != NULL) {
 
1534
                            child[n]->tax_id= tax_id;
 
1535
                            child[n]->flags= flags;
 
1536
                            StringCpy(child[n]->node_label, label);
 
1537
                            if((flags & TXC_SUFFIX) != 0) {
 
1538
                                c= StringChr(child[n]->node_label, '~');
 
1539
                                if(c != NULL) *c= '\0';
 
1540
                                child[n]->flags^= TXC_SUFFIX; /* clear this flag */
 
1541
                            }
 
1542
                            n++;
 
1543
                        }
 
1544
                        continue;
 
1545
 
 
1546
                    case CS_ROW_FAIL :
 
1547
                        break;
 
1548
 
 
1549
                    case CS_END_DATA :
 
1550
                        more_fetch= FALSE;
 
1551
                        continue;
 
1552
                    default :
 
1553
                        /* this is a error */
 
1554
                        more_fetch= FALSE;
 
1555
                        if(ct_cancel(NULL, cmd, CS_CANCEL_ALL) != CS_SUCCEED) {
 
1556
                            ct_close(te_link, CS_FORCE_CLOSE);
 
1557
                        }
 
1558
                        ct_cmd_drop(cmd);
 
1559
                        return NULL;
 
1560
                    }
 
1561
                }
 
1562
            }
 
1563
            continue;
 
1564
 
 
1565
        default : break;
 
1566
        }
 
1567
    }
 
1568
    ct_cmd_drop(cmd);
 
1569
    *nof_children= n;
 
1570
    if(n == 0) {
 
1571
        MemFree(child);
 
1572
        child= NULL;
 
1573
    }
 
1574
    return child;
 
1575
}    
 
1576
 
 
1577
 
 
1578
TXC_CitListPtr txc_citGet4Node(Int4 tax_id, Int4Ptr nof_cit)
 
1579
{
 
1580
    CS_COMMAND *cmd;
 
1581
    CS_DATAFMT parfmt;
 
1582
    CS_INT res_type, rows_read;
 
1583
    int nnn= 64;
 
1584
    Boolean more_results= TRUE;
 
1585
    Boolean more_fetch;
 
1586
    char cit_key[132];
 
1587
    CS_INT cit_id;
 
1588
    Int4 n= 0, alloc_room= 0;
 
1589
    TXC_CitListPtr refer= NULL, tmp;
 
1590
 
 
1591
    if(tax_id <= 0) return NULL;
 
1592
    
 
1593
    if(ct_cmd_alloc(te_link, &cmd) != CS_SUCCEED) return NULL;
 
1594
 
 
1595
 
 
1596
    /* build command: tax_citGet4Node @tax_id */
 
1597
 
 
1598
    if(ct_command(cmd, CS_RPC_CMD, "tax_getCit4Node", CS_NULLTERM, CS_UNUSED) != CS_SUCCEED) {
 
1599
        ct_cmd_drop(cmd);
 
1600
        return NULL;
 
1601
    }
 
1602
    
 
1603
    memset(&parfmt, 0, sizeof(CS_DATAFMT));
 
1604
    strcpy(parfmt.name, "@tax_id");
 
1605
    parfmt.namelen= CS_NULLTERM;
 
1606
    parfmt.datatype= CS_INT_TYPE;
 
1607
    parfmt.maxlength= sizeof(Int4);
 
1608
    parfmt.status= CS_INPUTVALUE;
 
1609
    parfmt.locale= NULL;
 
1610
    if(ct_param(cmd, &parfmt, &tax_id, parfmt.maxlength, CS_UNUSED) != CS_SUCCEED) {
 
1611
        ct_cmd_drop(cmd);
 
1612
        return NULL;
 
1613
    }
 
1614
 
 
1615
    /* send this command to server */
 
1616
    if(ct_send(cmd) != CS_SUCCEED) {
 
1617
        ct_cmd_drop(cmd);
 
1618
        return NULL;
 
1619
    }
 
1620
    
 
1621
    while(more_results && (--nnn > 0)) {
 
1622
        switch(ct_results(cmd, &res_type)) {
 
1623
        case CS_FAIL :
 
1624
            if(ct_cancel(NULL, cmd, CS_CANCEL_ALL) != CS_SUCCEED) {
 
1625
                ct_close(te_link, CS_FORCE_CLOSE);
 
1626
            }
 
1627
        case CS_CANCELED :
 
1628
            ct_cmd_drop(cmd);
 
1629
            return NULL;
 
1630
 
 
1631
        case CS_END_RESULTS :
 
1632
            more_results= FALSE;
 
1633
            break;
 
1634
 
 
1635
        case CS_SUCCEED :
 
1636
            if(res_type == CS_ROW_RESULT) {
 
1637
                /* bind and fetch these data */
 
1638
                more_fetch= TRUE;
 
1639
                parfmt.datatype= CS_INT_TYPE;
 
1640
                parfmt.format= CS_FMT_UNUSED;
 
1641
                parfmt.maxlength= 4;
 
1642
                parfmt.scale= CS_SRC_VALUE;
 
1643
                parfmt.precision= CS_SRC_VALUE;
 
1644
                parfmt.count= 1;
 
1645
                parfmt.locale= NULL;
 
1646
                if(ct_bind(cmd, 1, &parfmt, &cit_id, NULL, NULL) != CS_SUCCEED) break;
 
1647
 
 
1648
                parfmt.datatype= CS_CHAR_TYPE;
 
1649
                parfmt.format= CS_FMT_NULLTERM;
 
1650
                parfmt.maxlength= 128;
 
1651
                if(ct_bind(cmd, 2, &parfmt, cit_key, NULL, NULL) != CS_SUCCEED) break;
 
1652
 
 
1653
                while(more_fetch) {
 
1654
                    switch(ct_fetch(cmd,CS_UNUSED,CS_UNUSED,CS_UNUSED,&rows_read)) {
 
1655
                    case CS_SUCCEED :
 
1656
                        if(n == 0) {
 
1657
                            refer= malloc(sizeof(TXC_CitList));
 
1658
                            alloc_room= 1;
 
1659
                        }
 
1660
                        else if(n >= alloc_room) {
 
1661
                            
 
1662
                            alloc_room+= 2;
 
1663
                            tmp= realloc(refer, alloc_room*sizeof(TXC_CitList));
 
1664
                            if(tmp == NULL) {
 
1665
                                alloc_room= n--;
 
1666
                            }
 
1667
                            else refer= tmp;
 
1668
                        }
 
1669
 
 
1670
                        refer[n].cit_id= cit_id;
 
1671
                        refer[n].cit_key= StringSave(cit_key);
 
1672
                        n++;
 
1673
                        continue;
 
1674
 
 
1675
                    case CS_ROW_FAIL :
 
1676
                        break;
 
1677
 
 
1678
                    case CS_END_DATA :
 
1679
                        more_fetch= FALSE;
 
1680
                        continue;
 
1681
 
 
1682
                    default :
 
1683
                        /* this is an error */
 
1684
                        more_fetch= FALSE;
 
1685
                        if(ct_cancel(NULL, cmd, CS_CANCEL_ALL) != CS_SUCCEED) {
 
1686
                            ct_close(te_link, CS_FORCE_CLOSE);
 
1687
                        }
 
1688
                        ct_cmd_drop(cmd);
 
1689
                        return refer;
 
1690
                    }
 
1691
                }
 
1692
            }
 
1693
            continue;
 
1694
        default : break;
 
1695
        }
 
1696
    }
 
1697
    ct_cmd_drop(cmd);
 
1698
 
 
1699
    *nof_cit= n;
 
1700
    return refer;
 
1701
}    
 
1702
 
 
1703
 
 
1704
CharPtr txc_citGetKey(Int4 cit_id)
 
1705
{
 
1706
    CS_COMMAND *cmd;
 
1707
    CS_DATAFMT parfmt;
 
1708
    CS_INT res_type, rows_read;
 
1709
    int nnn= 64;
 
1710
    Boolean more_results= TRUE;
 
1711
    Boolean more_fetch;
 
1712
    char cit_key[132];
 
1713
 
 
1714
    if(cit_id <= 0) return NULL;
 
1715
    
 
1716
    if(ct_cmd_alloc(te_link, &cmd) != CS_SUCCEED) return NULL;
 
1717
 
 
1718
 
 
1719
    /* build command: tax_citGet4Node @tax_id */
 
1720
 
 
1721
    if(ct_command(cmd, CS_RPC_CMD, "tax_getCitKey", CS_NULLTERM, CS_UNUSED) != CS_SUCCEED) {
 
1722
        ct_cmd_drop(cmd);
 
1723
        return NULL;
 
1724
    }
 
1725
    
 
1726
    memset(&parfmt, 0, sizeof(CS_DATAFMT));
 
1727
    strcpy(parfmt.name, "@cit_id");
 
1728
    parfmt.namelen= CS_NULLTERM;
 
1729
    parfmt.datatype= CS_INT_TYPE;
 
1730
    parfmt.maxlength= sizeof(Int4);
 
1731
    parfmt.status= CS_INPUTVALUE;
 
1732
    parfmt.locale= NULL;
 
1733
    if(ct_param(cmd, &parfmt, &cit_id, parfmt.maxlength, CS_UNUSED) != CS_SUCCEED) {
 
1734
        ct_cmd_drop(cmd);
 
1735
        return NULL;
 
1736
    }
 
1737
 
 
1738
    /* send this command to server */
 
1739
    if(ct_send(cmd) != CS_SUCCEED) {
 
1740
        ct_cmd_drop(cmd);
 
1741
        return NULL;
 
1742
    }
 
1743
    
 
1744
    while(more_results && (--nnn > 0)) {
 
1745
        switch(ct_results(cmd, &res_type)) {
 
1746
        case CS_FAIL :
 
1747
            if(ct_cancel(NULL, cmd, CS_CANCEL_ALL) != CS_SUCCEED) {
 
1748
                ct_close(te_link, CS_FORCE_CLOSE);
 
1749
            }
 
1750
        case CS_CANCELED :
 
1751
            ct_cmd_drop(cmd);
 
1752
            return NULL;
 
1753
 
 
1754
        case CS_END_RESULTS :
 
1755
            more_results= FALSE;
 
1756
            break;
 
1757
 
 
1758
        case CS_SUCCEED :
 
1759
            if(res_type == CS_ROW_RESULT) {
 
1760
                /* bind and fetch these data */
 
1761
 
 
1762
                more_fetch= TRUE;
 
1763
                parfmt.datatype= CS_CHAR_TYPE;
 
1764
                parfmt.format= CS_FMT_NULLTERM;
 
1765
                parfmt.maxlength= 128;
 
1766
                parfmt.scale= CS_SRC_VALUE;
 
1767
                parfmt.precision= CS_SRC_VALUE;
 
1768
                parfmt.count= 1;
 
1769
                parfmt.locale= NULL;
 
1770
                if(ct_bind(cmd, 1, &parfmt, cit_key, NULL, NULL) != CS_SUCCEED) break;
 
1771
 
 
1772
                while(more_fetch) {
 
1773
                    switch(ct_fetch(cmd,CS_UNUSED,CS_UNUSED,CS_UNUSED,&rows_read)) {
 
1774
                    case CS_SUCCEED :
 
1775
                        cit_key[131]= '\0';
 
1776
                        continue;
 
1777
 
 
1778
                    case CS_ROW_FAIL :
 
1779
                        break;
 
1780
 
 
1781
                    case CS_END_DATA :
 
1782
                        more_fetch= FALSE;
 
1783
                        continue;
 
1784
 
 
1785
                    default :
 
1786
                        /* this is an error */
 
1787
                        more_fetch= FALSE;
 
1788
                        if(ct_cancel(NULL, cmd, CS_CANCEL_ALL) != CS_SUCCEED) {
 
1789
                            ct_close(te_link, CS_FORCE_CLOSE);
 
1790
                        }
 
1791
                        ct_cmd_drop(cmd);
 
1792
                        return StringSave(cit_key);
 
1793
                    }
 
1794
                }
 
1795
            }
 
1796
            continue;
 
1797
 
 
1798
        default : break;
 
1799
        }
 
1800
    }
 
1801
    ct_cmd_drop(cmd);
 
1802
 
 
1803
    return StringSave(cit_key);
 
1804
}    
 
1805
 
 
1806
TXC_CitListPtr txc_citFind(CharPtr key_str, Int4Ptr nof_cit)
 
1807
{
 
1808
    CS_COMMAND *cmd;
 
1809
    CS_DATAFMT parfmt;
 
1810
    CS_INT res_type, rows_read;
 
1811
    int nnn= 64;
 
1812
    Boolean more_results= TRUE;
 
1813
    Boolean more_fetch;
 
1814
    char cit_key[132];
 
1815
    CS_INT cit_id;
 
1816
    Int4 n= 0, alloc_room= 0;
 
1817
    TXC_CitListPtr refer= NULL, tmp;
 
1818
 
 
1819
    if((key_str == NULL) || (*key_str == '\0')) return NULL;
 
1820
    
 
1821
    if(ct_cmd_alloc(te_link, &cmd) != CS_SUCCEED) return NULL;
 
1822
 
 
1823
 
 
1824
    /* build command: tax_citGet4Node @tax_id */
 
1825
 
 
1826
    if(ct_command(cmd, CS_RPC_CMD, "tax_findCit", CS_NULLTERM, CS_UNUSED) != CS_SUCCEED) {
 
1827
        ct_cmd_drop(cmd);
 
1828
        return NULL;
 
1829
    }
 
1830
    
 
1831
    memset(&parfmt, 0, sizeof(CS_DATAFMT));
 
1832
    strcpy(parfmt.name, "@cit_key");
 
1833
    parfmt.namelen= CS_NULLTERM;
 
1834
    parfmt.datatype= CS_CHAR_TYPE;
 
1835
    parfmt.maxlength= strlen(key_str);
 
1836
    parfmt.status= CS_INPUTVALUE;
 
1837
    parfmt.locale= NULL;
 
1838
    if(ct_param(cmd, &parfmt, key_str, parfmt.maxlength, CS_UNUSED) != CS_SUCCEED) {
 
1839
        ct_cmd_drop(cmd);
 
1840
        return NULL;
 
1841
    }
 
1842
 
 
1843
    /* send this command to the server */
 
1844
    if(ct_send(cmd) != CS_SUCCEED) {
 
1845
        ct_cmd_drop(cmd);
 
1846
        return NULL;
 
1847
    }
 
1848
    
 
1849
    while(more_results && (--nnn > 0)) {
 
1850
        switch(ct_results(cmd, &res_type)) {
 
1851
        case CS_FAIL :
 
1852
            if(ct_cancel(NULL, cmd, CS_CANCEL_ALL) != CS_SUCCEED) {
 
1853
                ct_close(te_link, CS_FORCE_CLOSE);
 
1854
            }
 
1855
        case CS_CANCELED :
 
1856
            ct_cmd_drop(cmd);
 
1857
            return NULL;
 
1858
 
 
1859
        case CS_END_RESULTS :
 
1860
            more_results= FALSE;
 
1861
            break;
 
1862
 
 
1863
        case CS_SUCCEED :
 
1864
            if(res_type == CS_ROW_RESULT) {
 
1865
                /* bind and fetch these data */
 
1866
                more_fetch= TRUE;
 
1867
                parfmt.datatype= CS_INT_TYPE;
 
1868
                parfmt.format= CS_FMT_UNUSED;
 
1869
                parfmt.maxlength= 4;
 
1870
                parfmt.scale= CS_SRC_VALUE;
 
1871
                parfmt.precision= CS_SRC_VALUE;
 
1872
                parfmt.count= 1;
 
1873
                parfmt.locale= NULL;
 
1874
                if(ct_bind(cmd, 1, &parfmt, &cit_id, NULL, NULL) != CS_SUCCEED) break;
 
1875
 
 
1876
                parfmt.datatype= CS_CHAR_TYPE;
 
1877
                parfmt.format= CS_FMT_NULLTERM;
 
1878
                parfmt.maxlength= 128;
 
1879
                if(ct_bind(cmd, 2, &parfmt, cit_key, NULL, NULL) != CS_SUCCEED) break;
 
1880
 
 
1881
                while(more_fetch) {
 
1882
                    switch(ct_fetch(cmd,CS_UNUSED,CS_UNUSED,CS_UNUSED,&rows_read)) {
 
1883
                    case CS_SUCCEED :
 
1884
                        if(n == 0) {
 
1885
                            refer= malloc(sizeof(TXC_CitList));
 
1886
                            alloc_room= 1;
 
1887
                        }
 
1888
                        else if(n >= alloc_room) {
 
1889
                            
 
1890
                            alloc_room+= 2;
 
1891
                            tmp= realloc(refer, alloc_room*sizeof(TXC_CitList));
 
1892
                            if(tmp == NULL) {
 
1893
                                alloc_room= n--;
 
1894
                            }
 
1895
                            else refer= tmp;
 
1896
                        }
 
1897
 
 
1898
                        refer[n].cit_id= cit_id;
 
1899
                        refer[n].cit_key= StringSave(cit_key);
 
1900
                        n++;
 
1901
                        continue;
 
1902
 
 
1903
                    case CS_ROW_FAIL :
 
1904
                        break;
 
1905
 
 
1906
                    case CS_END_DATA :
 
1907
                        more_fetch= FALSE;
 
1908
                        continue;
 
1909
 
 
1910
                    default :
 
1911
                        /* this is an error */
 
1912
                        more_fetch= FALSE;
 
1913
                        if(ct_cancel(NULL, cmd, CS_CANCEL_ALL) != CS_SUCCEED) {
 
1914
                            ct_close(te_link, CS_FORCE_CLOSE);
 
1915
                        }
 
1916
                        ct_cmd_drop(cmd);
 
1917
                        return refer;
 
1918
                    }
 
1919
                }
 
1920
            }
 
1921
            continue;
 
1922
 
 
1923
        default : break;
 
1924
        }
 
1925
    }
 
1926
    ct_cmd_drop(cmd);
 
1927
 
 
1928
    *nof_cit= n;
 
1929
    return refer;
 
1930
}    
 
1931
 
 
1932
_citation* txc_citGetByKey(CharPtr key_str)
 
1933
{
 
1934
    CS_COMMAND *cmd;
 
1935
    CS_DATAFMT parfmt;
 
1936
    CS_INT res_type, rows_read;
 
1937
    int nnn= 64;
 
1938
    Boolean more_results= TRUE;
 
1939
    Boolean more_fetch;
 
1940
    char cit_key[132], url[132], txt[6*256];
 
1941
    CS_INT cit_key_ln, url_ln, txt_ln[6];
 
1942
    CS_SMALLINT url_ind, txt_ind[6];
 
1943
    CS_INT cit_id, flags, medline_id, pubmed_id;
 
1944
    Int4 n, i;
 
1945
    _citation* cit= NULL;
 
1946
 
 
1947
    if((key_str == NULL) || (*key_str == '\0')) return NULL;
 
1948
    
 
1949
    if(ct_cmd_alloc(te_link, &cmd) != CS_SUCCEED) return NULL;
 
1950
 
 
1951
 
 
1952
    /* build command: tax_citGet4Node @tax_id */
 
1953
 
 
1954
    if(ct_command(cmd, CS_RPC_CMD, "tax_citGetByKey", CS_NULLTERM, CS_UNUSED) != CS_SUCCEED) {
 
1955
        ct_cmd_drop(cmd);
 
1956
        return NULL;
 
1957
    }
 
1958
    
 
1959
    memset(&parfmt, 0, sizeof(CS_DATAFMT));
 
1960
    strcpy(parfmt.name, "@cit_key");
 
1961
    parfmt.namelen= CS_NULLTERM;
 
1962
    parfmt.datatype= CS_CHAR_TYPE;
 
1963
    parfmt.maxlength= strlen(key_str);
 
1964
    parfmt.status= CS_INPUTVALUE;
 
1965
    parfmt.locale= NULL;
 
1966
    if(ct_param(cmd, &parfmt, key_str, parfmt.maxlength, CS_UNUSED) != CS_SUCCEED) {
 
1967
        ct_cmd_drop(cmd);
 
1968
        return NULL;
 
1969
    }
 
1970
 
 
1971
    /* send this command to the server */
 
1972
    if(ct_send(cmd) != CS_SUCCEED) {
 
1973
        ct_cmd_drop(cmd);
 
1974
        return NULL;
 
1975
    }
 
1976
    
 
1977
    while(more_results && (--nnn > 0)) {
 
1978
        switch(ct_results(cmd, &res_type)) {
 
1979
        case CS_FAIL :
 
1980
            if(ct_cancel(NULL, cmd, CS_CANCEL_ALL) != CS_SUCCEED) {
 
1981
                ct_close(te_link, CS_FORCE_CLOSE);
 
1982
            }
 
1983
        case CS_CANCELED :
 
1984
            ct_cmd_drop(cmd);
 
1985
            return NULL;
 
1986
 
 
1987
        case CS_END_RESULTS :
 
1988
            more_results= FALSE;
 
1989
            break;
 
1990
 
 
1991
        case CS_SUCCEED :
 
1992
            if(res_type == CS_ROW_RESULT) {
 
1993
                /* bind and fetch these data */
 
1994
                more_fetch= TRUE;
 
1995
                parfmt.datatype= CS_INT_TYPE;
 
1996
                parfmt.format= CS_FMT_UNUSED;
 
1997
                parfmt.maxlength= 4;
 
1998
                parfmt.scale= CS_SRC_VALUE;
 
1999
                parfmt.precision= CS_SRC_VALUE;
 
2000
                parfmt.count= 1;
 
2001
                parfmt.locale= NULL;
 
2002
                if((ct_bind(cmd, 1, &parfmt, &cit_id, NULL, NULL) != CS_SUCCEED) ||
 
2003
                   (ct_bind(cmd, 3, &parfmt, &flags, NULL, NULL) != CS_SUCCEED)  ||
 
2004
                   (ct_bind(cmd, 4, &parfmt, &medline_id, NULL, NULL) != CS_SUCCEED) ||
 
2005
                   (ct_bind(cmd, 5, &parfmt, &pubmed_id, NULL, NULL) != CS_SUCCEED)) break;
 
2006
 
 
2007
                parfmt.datatype= CS_CHAR_TYPE;
 
2008
                parfmt.format= CS_FMT_NULLTERM;
 
2009
                parfmt.maxlength= 128;
 
2010
                if((ct_bind(cmd, 2, &parfmt, cit_key, &cit_key_ln, NULL) != CS_SUCCEED) ||
 
2011
                   (ct_bind(cmd, 6, &parfmt, url, &url_ln, &url_ind) != CS_SUCCEED)) break;
 
2012
 
 
2013
                parfmt.maxlength= 256;
 
2014
                for(n= 0; n < 6; n++) {
 
2015
                    if(ct_bind(cmd, 7+n, &parfmt, txt + 256*n, &txt_ln[n], &txt_ind[n]) != CS_SUCCEED)
 
2016
                        break;
 
2017
                }
 
2018
 
 
2019
                while(more_fetch) {
 
2020
                    switch(ct_fetch(cmd,CS_UNUSED,CS_UNUSED,CS_UNUSED,&rows_read)) {
 
2021
                    case CS_SUCCEED :
 
2022
                        if(cit == NULL) {
 
2023
                            cit= malloc(sizeof(_citation));
 
2024
                        }
 
2025
                        cit->id= cit_id;
 
2026
                        cit->key= StringSave(cit_key);
 
2027
                        cit->flags= flags;
 
2028
                        cit->mu_id= medline_id;
 
2029
                        cit->pm_id= pubmed_id;
 
2030
                        cit->url= ((url_ind == -1) || (url_ln < 1) || (url[0] == '\0'))? NULL :
 
2031
                            StringSave(url);
 
2032
                        
 
2033
                        for(i= n= 0; i < 6; i++) {
 
2034
                            n+= ((txt_ind[i] == -1) || (txt_ln[i] < 1) || (txt[256*i] == '\0'))? 0 : 
 
2035
                                (txt_ln[i] + 1);
 
2036
                        }
 
2037
                        if(n == 0) {
 
2038
                            cit->txt= NULL;
 
2039
                        }
 
2040
                        else {
 
2041
                            cit->txt= MemNew(n);
 
2042
                            cit->txt[0]= '\0';
 
2043
                            for(i= 0; i < 6; i++) {
 
2044
                                if((txt_ind[i] != -1) && (txt_ln[i] > 0) && (txt[256*i] != '\0')) {
 
2045
                                    strcat(cit->txt, &txt[256*i]);
 
2046
                                }
 
2047
                            }
 
2048
                        }
 
2049
                        continue;
 
2050
 
 
2051
                    case CS_ROW_FAIL :
 
2052
                        break;
 
2053
 
 
2054
                    case CS_END_DATA :
 
2055
                        more_fetch= FALSE;
 
2056
                        continue;
 
2057
 
 
2058
                    default :
 
2059
                        /* this is an error */
 
2060
                        more_fetch= FALSE;
 
2061
                        if(ct_cancel(NULL, cmd, CS_CANCEL_ALL) != CS_SUCCEED) {
 
2062
                            ct_close(te_link, CS_FORCE_CLOSE);
 
2063
                        }
 
2064
                        ct_cmd_drop(cmd);
 
2065
                        return cit;
 
2066
                    }
 
2067
                }
 
2068
            }
 
2069
            continue;
 
2070
 
 
2071
        default : break;
 
2072
        }
 
2073
    }
 
2074
    ct_cmd_drop(cmd);
 
2075
 
 
2076
    return cit;
 
2077
}    
 
2078
 
 
2079
void txc_close()
 
2080
{
 
2081
    int i= 0;
 
2082
    CS_RETCODE rc;
 
2083
 
 
2084
    while((rc= ct_close(te_link, CS_UNUSED)) == CS_FAIL) {
 
2085
        ++i;
 
2086
        if(ct_cancel(te_link, NULL, CS_CANCEL_ALL) != CS_SUCCEED) break;
 
2087
        if(i > 2) break;
 
2088
    }
 
2089
    if(rc == CS_FAIL) ct_close(te_link, CS_FORCE_CLOSE);
 
2090
 
 
2091
    if(ct_exit(te_context, CS_UNUSED) != CS_FAIL) {
 
2092
        ct_exit(te_context, CS_FORCE_EXIT);
 
2093
    }
 
2094
}
 
2095
 
 
2096
 
 
2097
/********************************/
 
2098
/* retrive subspecies functions */
 
2099
/********************************/
 
2100
 
 
2101
/* tax_SSget(@tax_id int, @subtype tinyint, @subname varchar(120)) proc */
 
2102
 
 
2103
_subspecPtr tax_SSget(Int4 tax_id, _subspecPtr ssrec)
 
2104
{
 
2105
    CS_COMMAND *cmd;
 
2106
    CS_DATAFMT parfmt;
 
2107
    char rname[132];
 
2108
    CS_INT res_type, rows_read;
 
2109
    CS_INT rname_ln;
 
2110
    int nnn= 64;
 
2111
    Boolean more_results= TRUE;
 
2112
    Boolean more_fetch;
 
2113
 
 
2114
    if(tax_id < 2) return NULL;
 
2115
 
 
2116
    if((ssrec == NULL) || (ssrec->sname == NULL)) return NULL;
 
2117
 
 
2118
    if(ct_cmd_alloc(te_link, &cmd) != CS_SUCCEED) return NULL;
 
2119
 
 
2120
    /* build command: tax_checkUnique @name_txt, @tax_id */
 
2121
 
 
2122
    if(ct_command(cmd, CS_RPC_CMD, "tax_SSget", CS_NULLTERM, CS_UNUSED) != CS_SUCCEED) {
 
2123
        ct_cmd_drop(cmd);
 
2124
        return NULL;
 
2125
    }
 
2126
        
 
2127
    memset(&parfmt, 0, sizeof(CS_DATAFMT));
 
2128
    strcpy(parfmt.name, "@tax_id");
 
2129
    parfmt.namelen= CS_NULLTERM;
 
2130
    parfmt.status= CS_INPUTVALUE;
 
2131
    parfmt.locale= NULL;
 
2132
    parfmt.datatype= CS_INT_TYPE;
 
2133
    parfmt.maxlength= sizeof(CS_INT);
 
2134
    if(ct_param(cmd, &parfmt, &tax_id, parfmt.maxlength, CS_UNUSED) != CS_SUCCEED) {
 
2135
        ct_cmd_drop(cmd);
 
2136
        return NULL;
 
2137
    }
 
2138
 
 
2139
    strcpy(parfmt.name, "@subtype");
 
2140
    parfmt.datatype= CS_TINYINT_TYPE;
 
2141
    parfmt.maxlength= 1;
 
2142
    if(ct_param(cmd, &parfmt, &ssrec->stype, parfmt.maxlength, CS_UNUSED) != CS_SUCCEED) {
 
2143
        ct_cmd_drop(cmd);
 
2144
        return NULL;
 
2145
    }
 
2146
 
 
2147
    strcpy(parfmt.name, "@subname");
 
2148
    strncpy(rname, ssrec->sname, 116);
 
2149
    rname[116]= '\0';
 
2150
    parfmt.datatype= CS_CHAR_TYPE;
 
2151
    parfmt.maxlength= strlen(rname);
 
2152
    if(ct_param(cmd, &parfmt, rname, parfmt.maxlength, CS_UNUSED) != CS_SUCCEED) {
 
2153
        ct_cmd_drop(cmd);
 
2154
        return NULL;
 
2155
    }
 
2156
 
 
2157
    /* send this command to server */
 
2158
    if(ct_send(cmd) != CS_SUCCEED) {
 
2159
        ct_cmd_drop(cmd);
 
2160
        return NULL;
 
2161
    }
 
2162
 
 
2163
    if(ssrec->rname != NULL) ssrec->rname= MemFree(ssrec->rname);
 
2164
        
 
2165
    while(more_results && (--nnn > 0)) {
 
2166
        switch(ct_results(cmd, &res_type)) {
 
2167
        case CS_FAIL :
 
2168
            if(ct_cancel(NULL, cmd, CS_CANCEL_ALL) != CS_SUCCEED) {
 
2169
                ct_close(te_link, CS_FORCE_CLOSE);
 
2170
            }
 
2171
        case CS_CANCELED :
 
2172
            ct_cmd_drop(cmd);
 
2173
            return NULL;
 
2174
 
 
2175
        case CS_END_RESULTS :
 
2176
            more_results= FALSE;
 
2177
            break;
 
2178
 
 
2179
        case CS_SUCCEED :
 
2180
            if(res_type == CS_ROW_RESULT) {
 
2181
                /* bind and fetch these data */
 
2182
                more_fetch= TRUE;
 
2183
                parfmt.datatype= CS_INT_TYPE;
 
2184
                parfmt.format= CS_FMT_UNUSED;
 
2185
                parfmt.maxlength= 4;
 
2186
                parfmt.scale= CS_SRC_VALUE;
 
2187
                parfmt.precision= CS_SRC_VALUE;
 
2188
                parfmt.count= 1;
 
2189
                parfmt.locale= NULL;
 
2190
                if(ct_bind(cmd, 1, &parfmt, &ssrec->r_id, NULL, NULL) != CS_SUCCEED) break;
 
2191
 
 
2192
                parfmt.datatype= CS_TINYINT_TYPE;
 
2193
                parfmt.maxlength= 1;
 
2194
                if(ct_bind(cmd, 2, &parfmt, &ssrec->rtype, NULL, NULL) != CS_SUCCEED) break;
 
2195
                
 
2196
 
 
2197
                parfmt.datatype= CS_CHAR_TYPE;
 
2198
                parfmt.format= CS_FMT_NULLTERM;
 
2199
                parfmt.maxlength= 132;
 
2200
                if(ct_bind(cmd, 3, &parfmt, rname, &rname_ln, NULL) != CS_SUCCEED) break;
 
2201
 
 
2202
                while(more_fetch) {
 
2203
                    switch(ct_fetch(cmd,CS_UNUSED,CS_UNUSED,CS_UNUSED,&rows_read)) {
 
2204
                    case CS_SUCCEED :
 
2205
                        if(rname_ln >= 0) {
 
2206
                            rname[rname_ln]= '\0';
 
2207
                            if(ssrec->rname != NULL) MemFree(ssrec->rname);
 
2208
                            ssrec->rname= StringSave(rname);
 
2209
                        }
 
2210
                        continue;
 
2211
 
 
2212
                    case CS_ROW_FAIL :
 
2213
                        continue;
 
2214
 
 
2215
                    case CS_END_DATA :
 
2216
                        more_fetch= FALSE;
 
2217
                        continue;
 
2218
 
 
2219
                    default :
 
2220
                        /* this is an error */
 
2221
                        more_fetch= FALSE;
 
2222
                        if(ct_cancel(NULL, cmd, CS_CANCEL_ALL) != CS_SUCCEED) {
 
2223
                            ct_close(te_link, CS_FORCE_CLOSE);
 
2224
                        }
 
2225
                        ct_cmd_drop(cmd);
 
2226
                        return NULL;
 
2227
                    }
 
2228
                }
 
2229
            }
 
2230
            continue;
 
2231
 
 
2232
        default : break;
 
2233
        }
 
2234
    }
 
2235
    ct_cmd_drop(cmd);
 
2236
 
 
2237
    return (ssrec->rname == NULL)? NULL : ssrec;   
 
2238
}
 
2239
 
 
2240
tax_OrgModPtr tax_SSgetLegal(Int4 tax_id)
 
2241
{
 
2242
    CS_COMMAND *cmd;
 
2243
    CS_DATAFMT parfmt;
 
2244
    CS_INT res_type, rows_read;
 
2245
    CS_TINYINT subtype;
 
2246
    int nnn= 64;
 
2247
    Boolean more_results= TRUE;
 
2248
    Boolean more_fetch;
 
2249
    char subname[256];
 
2250
    CS_INT subname_ln;
 
2251
    tax_OrgModPtr first= NULL;
 
2252
    tax_OrgModPtr last, tmp;
 
2253
 
 
2254
    if(tax_id < 2) return NULL;
 
2255
    
 
2256
    if(ct_cmd_alloc(te_link, &cmd) != CS_SUCCEED) return NULL;
 
2257
 
 
2258
 
 
2259
    /* call  tax_SSgetLegal (@tax_id int) proc */
 
2260
 
 
2261
    if(ct_command(cmd, CS_RPC_CMD, "tax_SSgetLegal", CS_NULLTERM, CS_UNUSED) != CS_SUCCEED) {
 
2262
        ct_cmd_drop(cmd);
 
2263
        return NULL;
 
2264
    }
 
2265
    
 
2266
    memset(&parfmt, 0, sizeof(CS_DATAFMT));
 
2267
    strcpy(parfmt.name, "@tax_id");
 
2268
    parfmt.namelen= CS_NULLTERM;
 
2269
    parfmt.datatype= CS_INT_TYPE;
 
2270
    parfmt.maxlength= sizeof(Int4);
 
2271
    parfmt.status= CS_INPUTVALUE;
 
2272
    parfmt.locale= NULL;
 
2273
    if(ct_param(cmd, &parfmt, &tax_id, parfmt.maxlength, CS_UNUSED) != CS_SUCCEED) {
 
2274
        ct_cmd_drop(cmd);
 
2275
        return NULL;
 
2276
    }
 
2277
 
 
2278
    /* send this command to the server */
 
2279
    if(ct_send(cmd) != CS_SUCCEED) {
 
2280
        ct_cmd_drop(cmd);
 
2281
        return NULL;
 
2282
    }
 
2283
    
 
2284
    while(more_results && (--nnn > 0)) {
 
2285
        switch(ct_results(cmd, &res_type)) {
 
2286
        case CS_FAIL :
 
2287
            if(ct_cancel(NULL, cmd, CS_CANCEL_ALL) != CS_SUCCEED) {
 
2288
                ct_close(te_link, CS_FORCE_CLOSE);
 
2289
            }
 
2290
        case CS_CANCELED :
 
2291
            ct_cmd_drop(cmd);
 
2292
            return first;
 
2293
 
 
2294
        case CS_END_RESULTS :
 
2295
            more_results= FALSE;
 
2296
            break;
 
2297
 
 
2298
        case CS_SUCCEED :
 
2299
            if(res_type == CS_ROW_RESULT) {
 
2300
                /* bind and fetch these data */
 
2301
                more_fetch= TRUE;
 
2302
                parfmt.datatype= CS_TINYINT_TYPE;
 
2303
                parfmt.format= CS_FMT_UNUSED;
 
2304
                parfmt.maxlength= 1;
 
2305
                parfmt.scale= CS_SRC_VALUE;
 
2306
                parfmt.precision= CS_SRC_VALUE;
 
2307
                parfmt.count= 1;
 
2308
                parfmt.locale= NULL;
 
2309
                if(ct_bind(cmd, 1, &parfmt, &subtype, NULL, NULL) != CS_SUCCEED) break;
 
2310
 
 
2311
 
 
2312
                parfmt.datatype= CS_CHAR_TYPE;
 
2313
                parfmt.format= CS_FMT_NULLTERM;
 
2314
                parfmt.maxlength= 240;
 
2315
                if(ct_bind(cmd, 2, &parfmt, subname, &subname_ln, NULL) != CS_SUCCEED) break;
 
2316
 
 
2317
                while(more_fetch) {
 
2318
                    switch(ct_fetch(cmd,CS_UNUSED,CS_UNUSED,CS_UNUSED,&rows_read)) {
 
2319
                    case CS_SUCCEED :
 
2320
                        if(subname_ln >= 0) subname[subname_ln]= '\0';
 
2321
                        tmp= MemNew(sizeof(tax_OrgMod));
 
2322
                        if(tmp == NULL) continue;
 
2323
                        tmp->next= NULL;
 
2324
                        tmp->subname= StringSave(subname);
 
2325
                        tmp->subtype= subtype;
 
2326
                        if(first == NULL) {
 
2327
                            first= last= tmp;
 
2328
                        }
 
2329
                        else {
 
2330
                            last->next= tmp;
 
2331
                            last= tmp;
 
2332
                        }
 
2333
                        
 
2334
                        continue;
 
2335
 
 
2336
                    case CS_ROW_FAIL :
 
2337
                        continue;
 
2338
 
 
2339
                    case CS_END_DATA :
 
2340
                        more_fetch= FALSE;
 
2341
                        continue;
 
2342
 
 
2343
                    default :
 
2344
                        /* this is an error */
 
2345
                        more_fetch= FALSE;
 
2346
                        if(ct_cancel(NULL, cmd, CS_CANCEL_ALL) != CS_SUCCEED) {
 
2347
                            ct_close(te_link, CS_FORCE_CLOSE);
 
2348
                        }
 
2349
                        ct_cmd_drop(cmd);
 
2350
                        return first;
 
2351
                    }
 
2352
                }
 
2353
            }
 
2354
            continue;
 
2355
 
 
2356
        default : break;
 
2357
        }
 
2358
    }
 
2359
    ct_cmd_drop(cmd);
 
2360
 
 
2361
    return first;
 
2362
}    
 
2363
 
 
2364
/* tax_SSgetNodes(@tax_id int, @subtype tinyint, @subname varchar(120)) proc */
 
2365
 
 
2366
Int4 tax_SSgetNodes(Uint1 stype, CharPtr sname, Uint1 mode, Int4Ptr* ids)
 
2367
{
 
2368
    CS_COMMAND *cmd;
 
2369
    CS_DATAFMT parfmt;
 
2370
    CS_INT res_type, rows_read;
 
2371
    int nnn= 64;
 
2372
    Boolean more_results= TRUE;
 
2373
    Boolean more_fetch;
 
2374
    Int4Ptr p_id;
 
2375
    Int4 nof_nodes= 0, nof_rooms;
 
2376
    CS_INT tax_id;
 
2377
    char tname[120];
 
2378
 
 
2379
    if((sname == NULL) || (*sname == '\0')) return 0;
 
2380
 
 
2381
    if(ct_cmd_alloc(te_link, &cmd) != CS_SUCCEED) return 0;
 
2382
 
 
2383
    /* build command: tax_SSgetNodes @subtype, @subname, @mode */
 
2384
 
 
2385
    if(ct_command(cmd, CS_RPC_CMD, "tax_SSgetNodes", CS_NULLTERM, CS_UNUSED) != CS_SUCCEED) {
 
2386
        ct_cmd_drop(cmd);
 
2387
        return 0;
 
2388
    }
 
2389
        
 
2390
    memset(&parfmt, 0, sizeof(CS_DATAFMT));
 
2391
    strcpy(parfmt.name, "@subtype");
 
2392
    parfmt.namelen= CS_NULLTERM;
 
2393
    parfmt.status= CS_INPUTVALUE;
 
2394
    parfmt.locale= NULL;
 
2395
    parfmt.datatype= CS_TINYINT_TYPE;
 
2396
    parfmt.maxlength= sizeof(CS_TINYINT);
 
2397
    if(ct_param(cmd, &parfmt, &stype, parfmt.maxlength, CS_UNUSED) != CS_SUCCEED) {
 
2398
        ct_cmd_drop(cmd);
 
2399
        return 0;
 
2400
    }
 
2401
 
 
2402
    strcpy(parfmt.name, "@subname");
 
2403
    parfmt.datatype= CS_CHAR_TYPE;
 
2404
    strncpy(tname, sname, 116);
 
2405
    tname[116]= '\0';
 
2406
    parfmt.maxlength= strlen(tname);
 
2407
    if(ct_param(cmd, &parfmt, tname, parfmt.maxlength, CS_UNUSED) != CS_SUCCEED) {
 
2408
        ct_cmd_drop(cmd);
 
2409
        return 0;
 
2410
    }
 
2411
 
 
2412
    strcpy(parfmt.name, "@mode");
 
2413
    parfmt.datatype= CS_TINYINT_TYPE;
 
2414
    parfmt.maxlength= 1;
 
2415
    if(ct_param(cmd, &parfmt, &mode, parfmt.maxlength, CS_UNUSED) != CS_SUCCEED) {
 
2416
        ct_cmd_drop(cmd);
 
2417
        return 0;
 
2418
    }
 
2419
 
 
2420
    /* send this command to server */
 
2421
    if(ct_send(cmd) != CS_SUCCEED) {
 
2422
        ct_cmd_drop(cmd);
 
2423
        return 0;
 
2424
    }
 
2425
 
 
2426
    while(more_results && (--nnn > 0)) {
 
2427
        switch(ct_results(cmd, &res_type)) {
 
2428
        case CS_FAIL :
 
2429
            if(ct_cancel(NULL, cmd, CS_CANCEL_ALL) != CS_SUCCEED) {
 
2430
                ct_close(te_link, CS_FORCE_CLOSE);
 
2431
            }
 
2432
        case CS_CANCELED :
 
2433
            ct_cmd_drop(cmd);
 
2434
            return 0;
 
2435
 
 
2436
        case CS_END_RESULTS :
 
2437
            more_results= FALSE;
 
2438
            break;
 
2439
 
 
2440
        case CS_SUCCEED :
 
2441
            if(res_type == CS_ROW_RESULT) {
 
2442
                /* bind and fetch these data */
 
2443
                more_fetch= TRUE;
 
2444
                parfmt.datatype= CS_INT_TYPE;
 
2445
                parfmt.format= CS_FMT_UNUSED;
 
2446
                parfmt.maxlength= 4;
 
2447
                parfmt.scale= CS_SRC_VALUE;
 
2448
                parfmt.precision= CS_SRC_VALUE;
 
2449
                parfmt.count= 1;
 
2450
                parfmt.locale= NULL;
 
2451
                if(ct_bind(cmd, 1, &parfmt, &tax_id, NULL, NULL) != CS_SUCCEED) break;
 
2452
                p_id= MemNew(4 * sizeof(Int4));
 
2453
                if(p_id != NULL) {
 
2454
                    nof_rooms= 4;
 
2455
                }
 
2456
                else break;
 
2457
 
 
2458
                while(more_fetch) {
 
2459
                    switch(ct_fetch(cmd,CS_UNUSED,CS_UNUSED,CS_UNUSED,&rows_read)) {
 
2460
                    case CS_SUCCEED :
 
2461
                        if(nof_nodes >= nof_rooms) {
 
2462
                            nof_rooms+= 8;
 
2463
                            *ids= p_id;
 
2464
                            p_id= MemMore(p_id, nof_rooms*(sizeof(Int4)));
 
2465
                            if(p_id == NULL) {
 
2466
                                p_id= *ids;
 
2467
                                nof_rooms-= 8;
 
2468
                                continue;
 
2469
                            }
 
2470
                        }
 
2471
                        
 
2472
                        p_id[nof_nodes++]= tax_id;
 
2473
                        continue;
 
2474
 
 
2475
                    case CS_ROW_FAIL :
 
2476
                        continue;
 
2477
 
 
2478
                    case CS_END_DATA :
 
2479
                        more_fetch= FALSE;
 
2480
                        continue;
 
2481
 
 
2482
                    default :
 
2483
                        /* this is an error */
 
2484
                        more_fetch= FALSE;
 
2485
                        if(ct_cancel(NULL, cmd, CS_CANCEL_ALL) != CS_SUCCEED) {
 
2486
                            ct_close(te_link, CS_FORCE_CLOSE);
 
2487
                        }
 
2488
                        ct_cmd_drop(cmd);
 
2489
                        return 0;
 
2490
                    }
 
2491
                }
 
2492
            }
 
2493
            continue;
 
2494
 
 
2495
        default : break;
 
2496
        }
 
2497
    }
 
2498
    ct_cmd_drop(cmd);
 
2499
 
 
2500
    *ids= p_id;
 
2501
    return nof_nodes;   
 
2502
}
 
2503
 
 
2504
Int4 tax_getTaxId4GI(Int4 gi)
 
2505
{
 
2506
    CS_COMMAND *cmd;
 
2507
    CS_DATAFMT parfmt;
 
2508
    CS_INT res_type, rows_read, tax_id= 0;
 
2509
    Boolean more_results= TRUE;
 
2510
    Boolean more_fetch;
 
2511
    int nnn= 64;
 
2512
 
 
2513
    if(ct_cmd_alloc(te_link, &cmd) != CS_SUCCEED) return 0;
 
2514
 
 
2515
    /* build command: tax_getTaxId4GI @gi */
 
2516
 
 
2517
    if(ct_command(cmd, CS_RPC_CMD, "tax_getTaxId4GI", CS_NULLTERM, CS_UNUSED) != CS_SUCCEED) {
 
2518
        ct_cmd_drop(cmd);
 
2519
        return 0;
 
2520
    }
 
2521
        
 
2522
    memset(&parfmt, 0, sizeof(CS_DATAFMT));
 
2523
    strcpy(parfmt.name, "@gi");
 
2524
    parfmt.namelen= CS_NULLTERM;
 
2525
    parfmt.datatype= CS_INT_TYPE;
 
2526
    parfmt.maxlength= sizeof(Int4);
 
2527
    parfmt.status= CS_INPUTVALUE;
 
2528
    parfmt.locale= NULL;
 
2529
    if(ct_param(cmd, &parfmt, &gi, parfmt.maxlength, CS_UNUSED) != CS_SUCCEED) {
 
2530
        ct_cmd_drop(cmd);
 
2531
        return 0;
 
2532
    }
 
2533
 
 
2534
    /* send this command to server */
 
2535
    if(ct_send(cmd) != CS_SUCCEED) {
 
2536
        ct_cmd_drop(cmd);
 
2537
        return 0;
 
2538
    }
 
2539
        
 
2540
    while(more_results && (--nnn > 0)) {
 
2541
        switch(ct_results(cmd, &res_type)) {
 
2542
        case CS_FAIL :
 
2543
            if(ct_cancel(NULL, cmd, CS_CANCEL_ALL) != CS_SUCCEED) {
 
2544
                ct_close(te_link, CS_FORCE_CLOSE);
 
2545
            }
 
2546
        case CS_CANCELED :
 
2547
            ct_cmd_drop(cmd);
 
2548
            return 0;
 
2549
 
 
2550
        case CS_END_RESULTS :
 
2551
            more_results= FALSE;
 
2552
            break;
 
2553
 
 
2554
        case CS_SUCCEED :
 
2555
            if(res_type == CS_STATUS_RESULT) {
 
2556
                more_fetch= TRUE;
 
2557
                parfmt.datatype= CS_INT_TYPE;
 
2558
                parfmt.format= CS_FMT_UNUSED;
 
2559
                parfmt.maxlength= 4;
 
2560
                parfmt.scale= CS_SRC_VALUE;
 
2561
                parfmt.precision= CS_SRC_VALUE;
 
2562
                parfmt.count= 1;
 
2563
                if(ct_bind(cmd, 1, &parfmt, &tax_id, NULL, NULL) != CS_SUCCEED) break;
 
2564
                while(more_fetch) {
 
2565
                    switch(ct_fetch(cmd,CS_UNUSED,CS_UNUSED,CS_UNUSED,&rows_read)) {
 
2566
                    case CS_SUCCEED :
 
2567
                        continue;
 
2568
 
 
2569
                    case CS_ROW_FAIL :
 
2570
                        break;
 
2571
 
 
2572
                    case CS_END_DATA :
 
2573
                        more_fetch= FALSE;
 
2574
                        continue;
 
2575
 
 
2576
                    default :
 
2577
                        /* this is a error */
 
2578
                        more_fetch= FALSE;
 
2579
                        if(ct_cancel(NULL, cmd, CS_CANCEL_ALL) != CS_SUCCEED) {
 
2580
                            ct_close(te_link, CS_FORCE_CLOSE);
 
2581
                        }
 
2582
                        ct_cmd_drop(cmd);
 
2583
                        return 0;
 
2584
                    }
 
2585
                }
 
2586
            }
 
2587
            continue;
 
2588
 
 
2589
        default : break;
 
2590
        }
 
2591
    }
 
2592
    ct_cmd_drop(cmd);
 
2593
    return tax_id;              
 
2594
}