File: dyn4.e

package info (click to toggle)
firebird3.0 3.0.13.ds7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 42,632 kB
  • sloc: ansic: 374,403; cpp: 319,973; sql: 14,691; pascal: 14,532; yacc: 7,557; fortran: 5,645; sh: 5,336; makefile: 1,041; perl: 194; sed: 83; awk: 76; xml: 19; csh: 15
file content (161 lines) | stat: -rw-r--r-- 3,641 bytes parent folder | download | duplicates (16)
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
/*
 *  Program type:   Embedded Dynamic SQL
 *
 *  Description:
 *		This program updates departments' budgets, given
 *		the department and the new budget information parameters.
 *
 *		An input SQLDA is allocated for the update query
 *		with parameter markers.
 * 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 "example.h"
#include <stdlib.h>
#include <string.h>

int get_input (char *dept_no, double *percent);

static char *Dept_data[] =
	{"622", "100", "116", "900", 0};

static double Percent_data[] =
	{0.05,  1.00,  0.075,  0.10, 0};

int	Input_ptr = 0;
char Db_name[128];

EXEC SQL
	SET DATABASE empdb = "employee.fdb" RUNTIME :Db_name;

char *upd_str =
	"UPDATE department SET budget = ? * budget + budget WHERE dept_no = ?";


int main(int argc, char** argv)
{
	BASED_ON department.dept_no	dept_no;
	double	percent_inc;
	short	nullind = 0;
	XSQLDA	*sqlda;

        if (argc > 1)
                strcpy(Db_name, argv[1]);
        else
                strcpy(Db_name, "employee.fdb");


	EXEC SQL
		WHENEVER SQLERROR GO TO Error;

	EXEC SQL
		CONNECT empdb;

	EXEC SQL
		SET TRANSACTION USING empdb;

	/* Allocate an input SQLDA.  There are two unknown parameters. */
	sqlda = (XSQLDA *) malloc(XSQLDA_LENGTH(2));
	sqlda->sqln = 2;
	sqlda->sqld = 2;
	sqlda->version = 1;

	/* Prepare the query. */
	EXEC SQL
		PREPARE q FROM :upd_str;

	/* Prepare the input sqlda, only data and indicator to set */

	EXEC SQL 
		DESCRIBE INPUT q USING SQL DESCRIPTOR sqlda;

	sqlda->sqlvar[0].sqldata = (char *) &percent_inc;
	sqlda->sqlvar[0].sqlind = &nullind;

	sqlda->sqlvar[1].sqldata = dept_no;
	/* FOrce the type to char instead of varchar */
	sqlda->sqlvar[1].sqltype = SQL_TEXT +1;
	sqlda->sqlvar[1].sqlind = &nullind;

	/* Expect an error, trap it */
	EXEC SQL WHENEVER SQLERROR CONTINUE;

	/*
	 *	Get the next department-percent increase input pair.
	 */
	while (get_input(dept_no, &percent_inc))
	{
		printf("\nIncreasing budget for department:  %s  by %5.2f percent.\n",
				dept_no, percent_inc);


		/* Update the budget. */
		EXEC SQL
			EXECUTE q USING SQL DESCRIPTOR sqlda;

		/* Don't save the update, if the new budget exceeds 
		** the limit. Detect an integrity violation 
		*/
		if (SQLCODE == -625) 
		{
			printf("\tExceeded budget limit -- not updated.\n");
			continue;
		}
		/* Undo all changes, in case of an error. */
		else if (SQLCODE)
			goto Error;

		/* Save each department's update independently. */
		EXEC SQL
			COMMIT RETAIN;
	}

	EXEC SQL
		COMMIT RELEASE;
	return (0);
Error:
	isc_print_status(gds__status);
	printf("SQLCODE=%d\n", SQLCODE);

	EXEC SQL
		ROLLBACK RELEASE;

	EXEC SQL
		DISCONNECT empdb;

	free( sqlda);
	return(1);
}


/*
 *	Get the department and percent parameters.
 */
int get_input(char* dept_no, double* percent)
{
	if (Dept_data[Input_ptr] == 0)
		return 0;

	strcpy(dept_no, Dept_data[Input_ptr]);

	if ((*percent = Percent_data[Input_ptr++]) == 0)
		return 0;

	return(1);
}