1 | /***************************************
2 |
3 | Functions for handling serials
4 |
5 | Status: NOT REVUED, NOT TESTED
6 |
7 | Author(s): Andrei Robachevsky
8 |
9 | ******************/ /******************
10 | Modification History:
11 | andrei (08/02/2000) Created.
12 | ******************/ /******************
13 | Copyright (c) 2000 RIPE NCC
14 |
15 | All Rights Reserved
16 |
17 | Permission to use, copy, modify, and distribute this software and its
18 | documentation for any purpose and without fee is hereby granted,
19 | provided that the above copyright notice appear in all copies and that
20 | both that copyright notice and this permission notice appear in
21 | supporting documentation, and that the name of the author not be
22 | used in advertising or publicity pertaining to distribution of the
23 | software without specific, written prior permission.
24 |
25 | THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
26 | ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
27 | AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
28 | DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
29 | AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
30 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
31 | ***************************************/
32 | #include "ud.h"
33 | #include "ud_int.h"
34 |
35 | /************************************************************
36 | * create_serial() *
37 | * *
38 | * Creates a serial record for given transaction *
39 | * For updates creates 2 serial records (DEL+ADD) *
40 | * *
41 | * Important fields of transaction are: *
42 | * tr->action TR_CREATE/TR_UPDATE/TR_DELETE *
43 | * tr->object_id should be filled in *
44 | * tr->sequence_id should be set to current *
45 | * *
46 | * Returns: *
47 | * currnt serial number. *
48 | * -1 in case of an error *
49 | * *
50 | *************************************************************/
51 |
52 | long create_serial(Transaction_t *tr)
53 | {
54 | GString *query;
55 | long current_serial=0;
56 | int sql_err;
57 | int operation;
58 |
59 | if ((query = g_string_sized_new(STR_XL)) == NULL){
60 | fprintf(stderr, "E: cannot allocate gstring\n");
61 | tr->succeeded=0;
62 | tr->error |= ERROR_U_MEM;
63 | return(ERROR_U_MEM);
64 | }
65 |
66 | /* fprintf(stderr, "creating serial\n"); */
67 | /* if the transaction failed store it in transaction table */
68 | if(tr->succeeded==0){
69 | if(ACT_DELETE(tr->action))operation=OP_DEL; else operation=OP_ADD;
70 |
71 | g_string_sprintf(query, "INSERT serials SET "
72 | "object_id=0, sequence_id=0, "
73 | "atlast=2, "
74 | "operation=%d ", operation);
75 |
76 | sql_err = SQ_execute_query(tr->sql_connection, query->str, (SQ_result_set_t **)NULL);
77 | if (sql_err) {
78 | fprintf(stderr, "E: <create_serial>: cannot insert %s\n", query->str);
79 | current_serial=-1;
80 | }
81 | else {
82 | current_serial=mysql_insert_id(tr->sql_connection);
83 | g_string_sprintf(query, "INSERT transaction SET "
84 | "serial_id=%ld, "
85 | "object='%s' ", current_serial, tr->object->object->str);
86 | /* make a record in transaction table */
87 | sql_err = SQ_execute_query(tr->sql_connection, query->str, (SQ_result_set_t **)NULL);
88 | if (sql_err) {
89 | fprintf(stderr, "E: <create_serial>: cannot save transactiom %s\n", query->str);
90 | current_serial=-1;
91 | }
92 | }
93 | g_string_free(query, TRUE);
94 | return(current_serial);
95 | }
96 |
97 |
98 | /* if the transaction has succeeded */
99 |
100 | /* If this is an update or delete */
101 | if(!ACT_CREATE(tr->action)) {
102 | /* set the atlast field of the latest record for this object to 0 */
103 | /* because it is moved to history */
104 | g_string_sprintf(query, "UPDATE serials SET atlast=0 "
105 | "WHERE object_id=%ld "
106 | "AND sequence_id=%ld ", tr->object_id, tr->sequence_id-1);
107 |
108 | sql_err = SQ_execute_query(tr->sql_connection, query->str, (SQ_result_set_t **)NULL);
109 | if (sql_err) { // we can have empty updates, but not errors
110 | fprintf(stderr, "E: <create_serial>: cannot update %s\n", query->str);
111 | current_serial=-1;
112 | }
113 | }
114 | /* if this a DEL */
115 | if(ACT_DELETE(tr->action)) {
116 | /* generate DEL serial */
117 | g_string_sprintf(query, "INSERT serials SET "
118 | "object_id=%ld, "
119 | "sequence_id=%ld, "
120 | "atlast=0, "
121 | "operation=%d ", tr->object_id, tr->sequence_id-1, OP_DEL);
122 |
123 | sql_err = SQ_execute_query(tr->sql_connection, query->str, (SQ_result_set_t **)NULL);
124 | if (sql_err) {
125 | fprintf(stderr, "E: <create_serial>: cannot insert %s\n",query->str);
126 | current_serial=-1;
127 | }
128 |
129 | if(current_serial!=-1)current_serial=mysql_insert_id(tr->sql_connection);
130 |
131 | }
132 | else { /* otherwise this is an ADD */
133 |
134 | /* now insert creation serial */
135 | g_string_sprintf(query, "INSERT serials SET "
136 | "object_id=%ld, "
137 | "sequence_id=%ld, "
138 | "atlast=1, "
139 | "operation=%d ", tr->object_id, tr->sequence_id, OP_ADD);
140 |
141 | sql_err = SQ_execute_query(tr->sql_connection, query->str, (SQ_result_set_t **)NULL);
142 | if (sql_err) {
143 | fprintf(stderr, "E: <create_serial>: cannot insert %s\n",query->str);
144 | current_serial=-1;
145 | }
146 |
147 | if(current_serial!=-1)current_serial=mysql_insert_id(tr->sql_connection);
148 | }
149 |
150 | g_string_free(query, TRUE);
151 | return(current_serial);
152 | }
153 |