File: api1.c

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 (139 lines) | stat: -rw-r--r-- 4,283 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
/*
 *    Program type:  API Interface
 *
 *    Description:
 *        This program creates a new database, given an SQL statement
 *        string.  The newly created database is accessed after its
 *        creation, and a sample table is added.
 *
 *        The SQLCODE is extracted from the status vector and is used
 *        to check whether the database already exists.
 * 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"
#include <ibase.h>

int pr_error (long *, char *);



static char *create_tbl  = "CREATE TABLE dbinfo (when_created DATE)";
static char *insert_date = "INSERT INTO dbinfo VALUES ('NOW')";

int main (int argc, char** argv)
{
    isc_db_handle   newdb = NULL;          /* database handle */
    isc_tr_handle   trans = NULL;          /* transaction handle */
    ISC_STATUS_ARRAY status;               /* status vector */
    long            sqlcode;               /* SQLCODE  */
    char            create_db[160];        /* 'create database' statement */
    char            new_dbname[128];

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

    /*
     *    Construct a 'create database' statement.
     *    The database name could have been passed as a parameter.
     */
    sprintf(create_db, "CREATE DATABASE '%s'", new_dbname);
    
    /*
     *    Create a new database.
     *    The database handle is zero.
     */
    
    if (isc_dsql_execute_immediate(status, &newdb, &trans, 0, create_db, 1,
                                   NULL))
    {
        /* Extract SQLCODE from the status vector. */
        sqlcode = isc_sqlcode(status);

        /* Print a descriptive message based on the SQLCODE. */
        if (sqlcode == -902)
        {
            printf("\nDatabase already exists.\n");
            printf("Remove %s before running this program.\n\n", new_dbname);
        }

        /* In addition, print a standard error message. */
        if (pr_error(status, "create database"))
            return 1;
    }

    isc_commit_transaction(status, &trans);
    printf("Created database '%s'.\n\n", new_dbname);

    /*
     *    Connect to the new database and create a sample table.
     */

    /* newdb will be set to null on success */ 
    isc_detach_database(status, &newdb);

    if (isc_attach_database(status, 0, new_dbname, &newdb, 0, NULL))
        if (pr_error(status, "attach database"))
            return 1;


    /* Create a sample table. */
    isc_start_transaction(status, &trans, 1, &newdb, 0, NULL);
    if (isc_dsql_execute_immediate(status, &newdb, &trans, 0, create_tbl, 1, NULL))
        if (pr_error(status, "create table"))
            return 1;
    isc_commit_transaction(status, &trans);

    /* Insert 1 row into the new table. */
    isc_start_transaction(status, &trans, 1, &newdb, 0, NULL);
    if (isc_dsql_execute_immediate(status, &newdb, &trans, 0, insert_date, 1, NULL))
        if (pr_error(status, "insert into"))
            return 1;
    isc_commit_transaction(status, &trans);

    printf("Successfully accessed the newly created database.\n\n");

    isc_detach_database(status, &newdb);

    return 0;
}            

/*
 *    Print the status, the SQLCODE, and exit.
 *    Also, indicate which operation the error occured on.
 */
int pr_error (long* status, char* operation)
{
    printf("[\n");
    printf("PROBLEM ON \"%s\".\n", operation);

    isc_print_status(status);

    printf("SQLCODE:%d\n", isc_sqlcode(status));

    printf("]\n");

    return 1;
}