modules/pm/pm_serials.c
/* [<][>][^][v][top][bottom][index][help] */
FUNCTIONS
This source file includes following functions.
- PM_get_minmax_serial
- atlast
- getop
- PM_get_serial_object
1 #include <stdio.h>
2 #include "ud_int.h"
3 #include "protocol_mirror.h"
4
5
6
7 /************************************************************
8 * PM_get_minmax_serial() *
9 * *
10 * Returns the min or max serial number. *
11 * *
12 * Returns: *
13 * min (max=0) or max (max=1) serial number *
14 * -1 in case of an error *
15 * *
16 * Note: *
17 * min serial= MIN(serial_id)+1 *
18 * MIN(serial_id) represents legacy RIPE.CURRENSERIAL *
19 * of the snapshot *
20 * *
21 *************************************************************/
22 long PM_get_minmax_serial(SQ_connection_t *sql_connection, int max)
/* [<][>][^][v][top][bottom][index][help] */
23 {
24 static char query[STR_M];
25 SQ_result_set_t *sql_result;
26 SQ_row_t *sql_row;
27 char *sql_str;
28 long current_serial;
29 char *minmax;
30 int sql_err;
31
32 if(max==1)minmax="max"; else minmax="min";
33
34 sprintf(query, "SELECT %s(serial_id) FROM serials ", minmax);
35
36 //fprintf(stderr, "D:<get_field_str>:query: %s\n", query);
37 sql_err = SQ_execute_query(sql_connection, query, &sql_result);
38
39 if(sql_err) {
40 fprintf(stderr,"ERROR: %s\n", SQ_error(sql_connection));
41 return(-1);
42 }
43
44
45 if ((sql_row = SQ_row_next(sql_result)) != NULL) {
46 sql_str = SQ_get_column_string(sql_result, sql_row, 0);
47
48 /* We must process all the rows of the result,*/
49 /* otherwise we'll have them as part of the next qry */
50 while ( (sql_row = SQ_row_next(sql_result)) != NULL) {
51 fprintf(stderr, "E:<get_field_str> error : Dupl PK[%s]\n", query);
52 if(sql_str)free(sql_str); sql_str=NULL;
53 }
54 }
55 else sql_str=NULL;
56
57 if(sql_result){ SQ_free_result(sql_result); sql_result=NULL; }
58
59 if(sql_str) {
60 current_serial = atol(sql_str);
61 if(max!=1)current_serial++;
62 free(sql_str);
63 }
64 else current_serial=-1;
65
66 return(current_serial);
67
68 }
69
70 /************************************************************
71 * int atlast(long serial_number)
72 * -1 - sql error
73 *
74 ***********************************************************/
75
76 static int atlast(SQ_connection_t *sql_connection, long serial_number)
/* [<][>][^][v][top][bottom][index][help] */
77 {
78 char *sql_str;
79 char str_id[STR_S];
80 int atlast=-1;
81
82
83 sprintf(str_id, "%ld", serial_number);
84 sql_str= get_field_str(sql_connection, "atlast", "serials", "serial_id", str_id, NULL);
85 if(sql_str) {
86 atlast = atoi(sql_str);
87 free(sql_str);
88 }
89
90 return(atlast);
91
92 }
93
94
95 /************************************************************
96 * int getop(long serial_number)
97 * -1 - sql error
98 *
99 * **********************************************************/
100
101 static int getop(SQ_connection_t *sql_connection, long serial_number)
/* [<][>][^][v][top][bottom][index][help] */
102 {
103 char *sql_str;
104 char str_id[STR_S];
105 int op=-1;
106
107
108 sprintf(str_id, "%ld", serial_number);
109 sql_str= get_field_str(sql_connection, "operation", "serials", "serial_id", str_id, NULL);
110 if(sql_str) {
111 op = atoi(sql_str);
112 free(sql_str);
113 }
114
115 return(op);
116
117 }
118
119
120 /************************************************************
121 * char *PM_get_serial_object() *
122 * *
123 * Returns text block corresponding to the requested serial *
124 * *
125 * Returns: *
126 * operation (ADD/DEL) and text object *
127 * NULL in case of an error *
128 * *
129 * Note: *
130 * returned string should be freed by the caller *
131 * *
132 *************************************************************/
133 char *PM_get_serial_object(SQ_connection_t *sql_connection, long serial_number, int *operation)
/* [<][>][^][v][top][bottom][index][help] */
134 {
135 char *table;
136 SQ_result_set_t * sql_result;
137 SQ_row_t *sql_row;
138 char *sql_str;
139 static char query[STR_M];
140 int sql_err;
141
142 switch(atlast(sql_connection, serial_number)){
143
144 case 0: table="history";
145 break;
146 case 1: table="last";
147 break;
148 default: return(NULL);
149
150 }
151
152 sprintf(query, "SELECT %s.object FROM %s, serials "
153 "WHERE serials.serial_id=%ld "
154 "AND serials.object_id=%s.object_id "
155 "AND serials.sequence_id=%s.sequence_id ", table, table, serial_number, table, table);
156
157
158 sql_err = SQ_execute_query(sql_connection, query, &sql_result);
159
160 if(sql_err) {
161 fprintf(stderr,"ERROR: %s\n", SQ_error(sql_connection));
162 return(NULL);
163 }
164
165
166 if ((sql_row = SQ_row_next(sql_result)) != NULL) {
167 sql_str = SQ_get_column_string(sql_result, sql_row, 0);
168
169 /* We must process all the rows of the result,*/
170 /* otherwise we'll have them as part of the next qry */
171 while ( (sql_row = SQ_row_next(sql_result)) != NULL) {
172 fprintf(stderr, "E:<get_field_str> error : Dupl PK[%s]\n", query);
173 if(sql_str)free(sql_str); sql_str=NULL;
174 }
175 }
176 else sql_str=NULL;
177
178 if(sql_result){ SQ_free_result(sql_result); sql_result=NULL; }
179
180 *operation=getop(sql_connection, serial_number);
181
182 return(sql_str);
183
184 }