1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
/*
* Program type: API Interface
*
* Description:
* This program has several active transactions:
*
* Sales order records are entered continuously (transaction 1).
*
* If it is discovered during transaction 1, that the customer
* placing the order is new, then a new customer record must be
* added (transaction 2).
*
* If the customer record uses a country that does not exist
* in the 'country' table, a new country record is added (transaction 3).
*
* Transaction 2 can be committed after the country is added.
* Transaction 1 can be committed after the customer record is added.
*
* Transactions 1, 2, and 3 can be undone individually, if the user
* decides not to save the sales, customer, or country changes.
* If transaction 3 is undone, transactions 1 and 2 must be undone.
* If transaction 2 is undone, transaction 1 must be undone.
*
* In addition, several independent transactions, selecting the
* customer number and the country records, take place during
* the three update transactions.
* The contents of this file are subject to the Interbase Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy
* of the License at http://www.Inprise.com/IPL.html
*
* Software distributed under the License is distributed on an
* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
* or implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code was created by Inprise Corporation
* and its predecessors. Portions created by Inprise Corporation are
* Copyright (C) Inprise Corporation.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*/
#include <stdlib.h>
#include <string.h>
#include <ibase.h>
#include <stdio.h>
#include "example.h"
#define BUFLEN 512
char* more_orders (void);
int do_trans (void);
int cleanup (void);
char *customer = "Maritime Museum";
char *country = "Cayman Islands";
char *currency = "CmnDlr";
isc_db_handle db = NULL;
isc_tr_handle sales_trans = NULL,
cust_trans = NULL,
cntry_trans = NULL,
trans = NULL;
ISC_STATUS_ARRAY status;
static char *Sales[] = {"V88005", 0};
int Inp_ptr = 0;
char *trans_str = "SET TRANSACTION ISOLATION LEVEL READ COMMITTED";
int main (int argc, char** argv)
{
char empdb[128];
if (argc > 1)
strcpy(empdb, argv[1]);
else
strcpy(empdb, "employee.fdb");
/* Zero the transaction handles. */
if (isc_attach_database(status, 0, empdb, &db, 0, NULL))
{
ERREXIT(status, 1)
}
/* Do the updates */
do_trans();
if (trans)
isc_rollback_transaction(status, &trans);
if (cust_trans)
isc_rollback_transaction(status, &cust_trans);
if (cntry_trans)
isc_rollback_transaction(status, &cntry_trans);
if (sales_trans)
isc_rollback_transaction(status, &sales_trans);
/* Remove them again */
cleanup();
if (trans)
isc_rollback_transaction(status, &trans);
isc_detach_database(status, &db);
return 0;
}
/*
* Function does all the work.
*/
int do_trans (void)
{
long cust_no;
char sales_str[BUFLEN + 1];
char cust_str[BUFLEN + 1];
char cntry_str[BUFLEN + 1];
char sel_str[BUFLEN + 1];
XSQLDA *sqlda1, *sqlda2, *sqlda3;
isc_stmt_handle stmt0 = NULL,
stmt1 = NULL,
stmt2 = NULL;
short flag0 = 0, flag1 = 0;
long fetch_stat;
long sqlcode;
/* Prepare a query for fetching data. Make it read committed, so you
* can see your own updates.
*/
if (isc_dsql_execute_immediate(status, &db, &trans, 0, trans_str,
1, NULL))
{
ERREXIT(status, 1)
}
sprintf(sel_str, "SELECT cust_no FROM customer WHERE customer = '%s'",
customer);
if (isc_dsql_allocate_statement(status, &db, &stmt0))
{
ERREXIT(status, 1)
}
sqlda1 = (XSQLDA *) malloc(XSQLDA_LENGTH(1));
sqlda1->sqln = 1;
sqlda1->version = 1;
if (isc_dsql_prepare(status, &trans, &stmt0, 0, sel_str, 1, sqlda1))
{
ERREXIT(status, 1)
}
sqlda1->sqlvar[0].sqldata = (char *) &cust_no;
sqlda1->sqlvar[0].sqltype = SQL_LONG + 1;
sqlda1->sqlvar[0].sqlind = &flag0;
/* Prepare a query for checking if a country exists. */
sprintf(sel_str, "SELECT country FROM country WHERE country = '%s'",
country);
sqlda2 = (XSQLDA *) malloc(XSQLDA_LENGTH(1));
sqlda2->sqln = 1;
sqlda2->version = 1;
if (isc_dsql_allocate_statement(status, &db, &stmt2))
{
ERREXIT(status, 1)
}
if (isc_dsql_prepare(status, &trans, &stmt2, 0, sel_str, 1, sqlda2))
{
ERREXIT(status, 1)
}
sqlda2->sqlvar[0].sqldata = (char *) country;
sqlda2->sqlvar[0].sqltype = SQL_TEXT + 1;
sqlda2->sqlvar[0].sqlind = &flag1;
/*
* Start transaction 1 -- add a sales order.
* for a customer.
*/
cust_no = 9999;
/* This transaction is also read committed so it can see the results of
* other transactions
*/
if (isc_dsql_execute_immediate(status, &db, &sales_trans, 0, trans_str,
1, NULL))
{
ERREXIT(status, 1)
}
sprintf(sales_str, "INSERT INTO sales (po_number, cust_no, \
order_status, total_value) VALUES ('V88005', ?, \
'new', 2000)");
if (isc_dsql_allocate_statement(status, &db, &stmt1))
{
ERREXIT(status, 1)
}
if (isc_dsql_prepare(status, &trans, &stmt1, 0, sales_str, 1, NULL))
{
ERREXIT(status, 1)
}
/* Insert parameter (cust_no) used for sales insert */
sqlda3 = (XSQLDA *) malloc(XSQLDA_LENGTH(1));
sqlda3->sqln = 1;
sqlda3->version = 1;
isc_dsql_describe_bind(status, &stmt1, 1, sqlda3);
sqlda3->sqlvar[0].sqldata = (char *) &cust_no;;
sqlda3->sqlvar[0].sqlind = &flag0;
isc_dsql_execute(status, &sales_trans, &stmt1, 1, sqlda3);
sqlcode = isc_sqlcode(status);
if (sqlcode == -530)
{
/* Integrity constraint indicates missing primary key*/
printf ("No customer number %ld -- adding new customer \n", cust_no);
/*
* This a new customer.
* Start transaction 2 -- add a customer record.
*/
if (isc_start_transaction(status, &cust_trans, 1, &db, 0, NULL))
{
ERREXIT(status, 1)
}
sprintf(cust_str, "INSERT INTO customer (customer, country) \
VALUES ('%s', '%s')", customer, country);
printf("Adding a customer record for %s\n", customer );
/* Does the customer country exist in the validation table?
* Do a lookup this time instead of waiting for the constraint
* violation. Because trans is read committed, it will see
* updates on other transactions.
*/
if (isc_dsql_execute(status, &trans, &stmt2, 1, NULL))
{
ERREXIT(status, 1)
}
fetch_stat = isc_dsql_fetch(status, &stmt2, 1, sqlda2);
/*
* Country was not found in the validation table.
* Start transaction 3 -- add a country record.
*/
if (fetch_stat == 100L)
{
printf("Missing country record, adding %s\n", country);
if (isc_start_transaction(status, &cntry_trans, 1, &db, 0, NULL))
{
ERREXIT (status, 1)
}
sprintf(cntry_str, "INSERT INTO country VALUES ('%s', '%s')",
country, currency);
if (isc_dsql_execute_immediate(status, &db, &cntry_trans, 0,
cntry_str, 1, NULL))
{
ERREXIT(status, 1)
}
/* This must be committed to be visible */
isc_commit_transaction(status, &cntry_trans);
}
/*
* End transaction 2.
* Add the customer record, now with a reference.
*/
if (isc_dsql_execute_immediate(status, &db, &cust_trans, 0, cust_str,
1, NULL))
{
ERREXIT(status, 1)
}
/* Commit to make this reference visible */
if (isc_commit_transaction(status, &cust_trans))
{
ERREXIT(status, 1)
}
/* Lookup the new cust_no for this record */
if (isc_dsql_execute(status, &trans, &stmt0, 1, NULL))
{
ERREXIT(status, 1)
}
if (!isc_dsql_fetch(status, &stmt0, 1, sqlda1))
printf("New customer number: %ld\n", cust_no);
/* Then try to add the sales record again */
if (isc_dsql_execute(status, &sales_trans, &stmt1, 1, sqlda3))
{
ERREXIT(status, 1)
}
}
if (isc_commit_transaction(status, &sales_trans))
{
ERREXIT (status, 1)
}
printf("Added sales record for V88055\n");
isc_commit_transaction(status, &trans);
isc_dsql_free_statement(status, &stmt0, DSQL_close);
isc_dsql_free_statement(status, &stmt1, DSQL_close);
isc_dsql_free_statement(status, &stmt2, DSQL_close);
free(sqlda1);
free(sqlda2);
free(sqlda3);
return 0;
}
/* Cleanup removes all updates that might have been done
* Make sure to cleanup in reverse order to avoid primary
* key violations
*/
int cleanup (void)
{
char del_str[100];
printf ("Cleaning up...\n");
if (isc_start_transaction(status, &trans, 1, &db, 0, NULL))
{
ERREXIT(status, 1)
}
strcpy(del_str, "DELETE FROM SALES WHERE PO_NUMBER = \"V88005\"");
if (isc_dsql_execute_immediate(status, &db, &trans, 0, del_str, 1, NULL))
{
ERREXIT(status, 1)
}
strcpy (del_str, "DELETE FROM CUSTOMER WHERE COUNTRY LIKE \"Cayman%\"");
if (isc_dsql_execute_immediate(status, &db, &trans, 0, del_str, 1, NULL))
{
ERREXIT (status, 1)
}
strcpy (del_str, "DELETE FROM COUNTRY WHERE COUNTRY LIKE \"Cayman%\"");
if (isc_dsql_execute_immediate(status, &db, &trans, 0, del_str, 1, NULL))
{
ERREXIT(status, 1)
}
if (isc_commit_transaction(status, &trans))
{
ERREXIT(status, 1)
}
return 0;
}
/*
* Return the order number for the next sales order to be entered.
*/
char* more_orders (void)
{
return Sales[Inp_ptr++];
}
|