File: ex_sql_savepoint.c

package info (click to toggle)
db5.3 5.3.28%2Bdfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 158,500 kB
  • sloc: ansic: 448,411; java: 111,824; tcl: 80,544; sh: 44,264; cs: 33,697; cpp: 21,604; perl: 14,557; xml: 10,799; makefile: 4,077; javascript: 1,998; yacc: 1,003; awk: 965; sql: 801; erlang: 342; python: 216; php: 24; asm: 14
file content (87 lines) | stat: -rw-r--r-- 2,399 bytes parent folder | download | duplicates (8)
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
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 1997, 2013 Oracle and/or its affiliates.  All rights reserved.
 *
 */

#include "ex_sql_utils.h"

/*
 * Transactions created using BEGIN...COMMIT do not nest. For nested
 * transactions, use the SAVEPOINT and RELEASE commands. The "TO SAVEPOINT
 * name" clause of the ROLLBACK command is only applicable to SAVEPOINT
 * transactions.
 *
 * This example shows how to begin/commit/rollback to a savepoint instead of 
 * operating whole transaction.
 */

/* Example body. */
static int
ex_sql_savepoint(db)
	db_handle *db;
{
	const char* disp_result;
	const char* info;
	disp_result = \
		"SELECT rank, region, name from university WHERE rank <= 3;";

	/*
	 * BEGIN a transaction for all following operations.
	 */
	echo_info("Example1. BEGIN and COMMIT a transaction explicitly");
	exec_sql(db, "BEGIN"); 

	/*
	 * Start to execute given sql expression. A savepoint will be started 
	 * in the beginning of each step. We can define multi-savepoints by 
	 * different names.
	 */
	info =  "1. Declare savepoint sp1. Delete the rank 1 record then "
		"release savepoint sp1. We can see the record has been "
		"removed.";
	echo_info(info);
	exec_sql(db, "SAVEPOINT sp1");
	exec_sql(db, disp_result);	/* Display status before deleting. */
	exec_sql(db, "DELETE FROM university WHERE rank = '1';");
	exec_sql(db, "RELEASE sp1");
	exec_sql(db, disp_result);	/* Display status after deleting. */

	info =  "2. Declare savepoint sp2. Then remove the rank 2 record and "
		"rollback. We can see the record was recovered by rollback to "
		"savepoint sp2.";
	echo_info(info);
	exec_sql(db, "SAVEPOINT sp2");
	exec_sql(db, "DELETE FROM university WHERE rank = '2';");
	exec_sql(db, disp_result);	/* Display status before rollback. */
	exec_sql(db, "ROLLBACK TO sp2");
	exec_sql(db, disp_result);	/* Display status after rollback. */

	/* Commit the whole transaction. */
	exec_sql(db, "COMMIT TRANSACTION"); 
	info = "3. Here is the final result. We can see only the savepoint "
	       "sp1 was released.";
	echo_info(info);
	exec_sql(db, disp_result);

	return 0;
}

int
main()
{
	db_handle *db;

	/* Setup environment and preload data. */
	db = setup("./ex_sql_savepoint.db");
	load_table_from_file(db, university_sample_data, 1/* Silent */);

	/* Run example. */
	ex_sql_savepoint(db);

	/* End. */
	cleanup(db);
	return 0;
}