File: api14.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 (247 lines) | stat: -rw-r--r-- 6,292 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
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
/*
 *	Program type:  API Interface
 *
 *	Description:
 *		This program combines the three programming styles:
 *		static SQL, dynamic SQL, and the API interface.
 *
 *		Employee information is retrieved and printed for some set
 *		of employees.  A predefined set of columns is always printed.
 *		However, the 'where' clause, defining which employees the report
 *		is to be printed for, is unknown.
 *
 *		Dynamic SQL is utilized to construct the select statement.
 *		The 'where' clause is assumed to be passed as a parameter.
 *
 *		Static SQL is used for known SQL statements.
 *
 *		The API interface is used to access the two databases, and
 *		to control most transactions.  The database and transaction
 *		handles are shared between the three interfaces.
 * 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 <stdio.h>
#include "example.h"

#define BUFLEN		1024

char    *sel_str =
    "SELECT full_name, dept_no, salary, job_code, job_grade, job_country \
	 FROM employee";

char	*where_str =
	"WHERE job_code = 'SRep' AND dept_no IN (110, 140, 115, 125, 123, 121)";

/* This macro is used to declare structures representing SQL VARCHAR types */
#define SQL_VARCHAR(len) struct {short vary_length; char vary_string[(len)+1];}

char	Db_name[128];

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



int main(int argc, char** argv)
{
	BASED_ON employee.salary		salary;
	SQL_VARCHAR(5)				job_code;
	BASED_ON employee.job_grade		job_grade;
	SQL_VARCHAR(15)				job_country;
	SQL_VARCHAR(37)				full_name;
	BASED_ON country.currency		currency;
	BASED_ON department.dept_no		dept_no;
	BASED_ON department.department		department;
	char	buf[BUFLEN + 1];
	float	rate;
	void	*trans1 = NULL;			/* transaction handle */
	void	*trans2 = NULL;			/* transaction handle */
	ISC_STATUS_ARRAY status;
	XSQLDA	*sqlda;
        char    empdb2[128];

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


	EXEC SQL
		WHENEVER SQLERROR GO TO Error;

	/*
	 *	Set-up the select query.  The select portion of the query is
	 *	static, while the 'where' clause is determined elsewhere and
	 *	passed as a parameter.
	 */
	 sprintf(buf, "%s %s", sel_str, where_str);


	/*
	 *	Open the employee database.
	 */
	if (isc_attach_database(status, 0, Db_name, &db1, 0, NULL))
		isc_print_status(status);

	/*
	 *	Prepare the select query.
	 */

	sqlda = (XSQLDA *) malloc(XSQLDA_LENGTH(6));
	sqlda->sqln = 6;
	sqlda->sqld = 6;
	sqlda->version = 1;

	EXEC SQL
		SET TRANSACTION USING db1;
	EXEC SQL
		PREPARE q INTO SQL DESCRIPTOR sqlda FROM :buf;
	EXEC SQL
		COMMIT ;
 
	sqlda->sqlvar[0].sqldata = (char *)&full_name;
	sqlda->sqlvar[0].sqltype = SQL_VARYING;

	sqlda->sqlvar[1].sqldata = dept_no;
	sqlda->sqlvar[1].sqltype = SQL_TEXT;

	sqlda->sqlvar[2].sqldata = (char *) &salary;
	sqlda->sqlvar[2].sqltype = SQL_DOUBLE;

	sqlda->sqlvar[3].sqldata = (char *)&job_code;
	sqlda->sqlvar[3].sqltype = SQL_VARYING;

	sqlda->sqlvar[4].sqldata = (char *) &job_grade;
	sqlda->sqlvar[4].sqltype = SQL_SHORT;

	sqlda->sqlvar[5].sqldata = (char *)&job_country;
	sqlda->sqlvar[5].sqltype = SQL_VARYING;


	/*
	 *	Open the second database.
	 */

	EXEC SQL
        	SET DATABASE db2 = "employe2.fdb";

	if (isc_attach_database(status, 0, empdb2, &db2, 0, NULL))
		isc_print_status(status);


	/*
	 *	Select the employees, using the dynamically allocated SQLDA.
	 */
 
	EXEC SQL
		DECLARE emp CURSOR FOR q;
	 
	if (isc_start_transaction(status, &trans1, 1, &db1, 0, NULL))
		isc_print_status(status);

	EXEC SQL
		OPEN TRANSACTION trans1 emp;

	while (SQLCODE == 0)
	{
		EXEC SQL
			FETCH emp USING SQL DESCRIPTOR sqlda;

		if (SQLCODE == 100)
			break;

		/*
		 *	Get the department name, using a static SQL statement.
		 */
		EXEC SQL
			SELECT TRANSACTION trans1 department
			INTO :department
			FROM department
			WHERE dept_no = :dept_no;

		/*
		 *	If the job country is not USA, access the second database
		 *	in order to get the conversion rate between different money
		 *	types.  Even though the conversion rate may fluctuate, all
		 *	salaries will be presented in US dollars for relative comparison.
		 */
		job_country.vary_string[job_country.vary_length] = '\0';
		if (strcmp(job_country.vary_string, "USA"))
		{
			EXEC SQL
				SELECT TRANSACTION trans1 currency
				INTO :currency
				FROM country
				WHERE country = :job_country.vary_string :job_country.vary_length;

			if (isc_start_transaction(status, &trans2, 1, &db2, 0, NULL))
				isc_print_status(status);

			EXEC SQL
				SELECT TRANSACTION trans2 conv_rate
				INTO :rate
				FROM cross_rate
				WHERE from_currency = 'Dollar'
				AND to_currency = :currency;

			if (!SQLCODE)
				salary = salary / rate;

			if (isc_commit_transaction (status, &trans2))
				isc_print_status(status);
		}

		/*
		 *	Print the results.
		 */

		printf("%-20.*s ", full_name.vary_length, full_name.vary_string);
		fflush (stdout);
		printf("%8.2f ", salary);
		fflush (stdout);
		printf("   %-5.*s %d", job_code.vary_length, job_code.vary_string, job_grade);
		fflush (stdout);
		printf("    %-20s\n", department);
		fflush (stdout);
	}
 
	EXEC SQL
		CLOSE emp;
	 
	if (isc_commit_transaction (status, &trans1))
		isc_print_status(status);

	isc_detach_database(status, &db1);
	isc_detach_database(status, &db2);
 
	return(0);


Error:
	printf("\n");
    isc_print_sqlerror(SQLCODE, isc_status);
    return(1);
}