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   | static char query[STR_M];
55   | long current_serial=0;
56   | int sql_err;
57   | 
58   | /* fprintf(stderr, "creating serial\n"); */
59   | 
60   |   if(!ACT_CREATE(tr->action)) { 
61   |     /* set the atlast field of the latest record for this object to 0 */
62   |     /* because it is moved to history */
63   |     sprintf(query, "UPDATE serials SET atlast=0 "
64   |                    "WHERE object_id=%ld "
65   |                    "AND sequence_id=%ld ", tr->object_id, tr->sequence_id-1);
66   |     
67   |     sql_err = SQ_execute_query(tr->sql_connection, query, (SQ_result_set_t **)NULL);
68   |     if (sql_err) { // we can have empty updates, but not errors
69   |         fprintf(stderr, "E: <create_serial>: cannot update %s\n", query);
70   |         current_serial=-1;
71   |     }
72   |     
73   |     
74   |     /* generate DEL serial */
75   |     sprintf(query, "INSERT serials SET "
76   |                    "object_id=%ld, "
77   |                    "sequence_id=%ld, "
78   |                    "atlast=0, "
79   |                    "operation=%d ", tr->object_id, tr->sequence_id-1, OP_DEL);
80   |     
81   |     sql_err = SQ_execute_query(tr->sql_connection, query, (SQ_result_set_t **)NULL);
82   |     if (sql_err) {
83   |         fprintf(stderr, "E: <create_serial>: cannot insert %s\n",query);
84   |         current_serial=-1;
85   |     }    
86   |     
87   |     if(current_serial!=-1)current_serial=mysql_insert_id(tr->sql_connection);
88   |     
89   |     /* there is nothing more to do when OP_DEL */
90   |     if(ACT_DELETE(tr->action)) return(current_serial);
91   |   }
92   | 
93   |   /* now insert creation serial */
94   |   sprintf(query, "INSERT serials SET "
95   |                  "object_id=%ld, "
96   |                  "sequence_id=%ld, "
97   |                  "atlast=1, "
98   |                  "operation=%d ", tr->object_id, tr->sequence_id, OP_ADD);
99   |     
100  |   sql_err = SQ_execute_query(tr->sql_connection, query, (SQ_result_set_t **)NULL);
101  |   if (sql_err) {
102  |         fprintf(stderr, "E: <create_serial>: cannot insert %s\n",query);
103  |         current_serial=-1;
104  |   }    
105  |     
106  |   if(current_serial!=-1)current_serial=mysql_insert_id(tr->sql_connection);
107  |         
108  | return(current_serial);
109  | }
110  |