~digisite-dev/digisite/head

« back to all changes in this revision

Viewing changes to db-easy/src/main.c

  • Committer: Yann Hamon
  • Date: 2009-02-23 11:33:20 UTC
  • Revision ID: yann.hamon@thehumanjourney.net-20090223113320-k9qbb7bnumgjqkp5
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *            main.c
 
3
 *
 
4
 *  Copyright  2009  Benjamin Ducke, Yann Hamon, OA Digital
 
5
 *  <benjamin.ducke@oxfordarch.co.uk>>
 
6
 ****************************************************************************/
 
7
 
 
8
/*
 
9
 * This program is free software; you can redistribute it and/or modify
 
10
 * it under the terms of the GNU General Public License as published by
 
11
 * the Free Software Foundation; either version 2 of the License, or
 
12
 * (at your option) any later version.
 
13
 *
 
14
 * This program is distributed in the hope that it will be useful,
 
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
 * GNU Library General Public License for more details.
 
18
 *
 
19
 * You should have received a copy of the GNU General Public License
 
20
 * along with this program; if not, write to the Free Software
 
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301,  USA
 
22
 */
 
23
 
 
24
 /*
 
25
 
 
26
  Purpose: This just provides a simple testing binary for the db-easy DBMS interface
 
27
    objects. It is not actually meant to be used as a "proper" program. The db-easy
 
28
    stuff consists of functions that are shared by the mobile/desktop and admin GUIs.
 
29
 
 
30
*/
 
31
 
 
32
#include <stdio.h>
 
33
#include <stdlib.h>
 
34
#include <string.h>
 
35
 
 
36
#include "db/db_interface.h"
 
37
#include "db/db_result.h"
 
38
 
 
39
int main (int argc, char *argv[])
 
40
{
 
41
 
 
42
        db_options *opts;
 
43
        db_result *res;
 
44
 
 
45
        int i, n;
 
46
        int error;
 
47
 
 
48
        int *intcol;
 
49
        char **strarr;
 
50
 
 
51
 
 
52
        /* initialize DB drivers */
 
53
        db_init();
 
54
 
 
55
        /* set up options and connect to DBMS */
 
56
        opts = db_options_new ( DB_DRIVER_SQLITE3,
 
57
                                                "/home/benni/Projekte/OADIGITAL/DigitalRecording/db-prototype/site.oax",
 
58
                                                NULL, NULL, NULL );
 
59
        if ( opts == NULL ) {
 
60
                fprintf ( stderr, "DB setup error: %s\n", db_error_str () );
 
61
                exit (-1);
 
62
        }
 
63
 
 
64
        error = db_connect ( opts );
 
65
        if ( error < 0 ) {
 
66
                fprintf ( stderr, "DB connection error: %s\n", db_error_str () );
 
67
                exit (-1);
 
68
        }
 
69
 
 
70
 
 
71
        /* run a query and save into a result struct */
 
72
        res = db_result_new_from_copy ( opts, "sys_users" );
 
73
        if ( res == NULL ) {
 
74
                fprintf ( stderr, "DB query error: %s\n", db_error_str () );
 
75
                exit (-1);
 
76
        }
 
77
        db_result_set_load_mode ( res, DB_LOAD_DISK );
 
78
 
 
79
        //db_result_force_load_data ( res );
 
80
        intcol = db_result_get_integer_array ( res, "key", &n, -1 );
 
81
 
 
82
        strarr = db_result_get_text_array ( res, "alias", &n );
 
83
 
 
84
        fprintf ( stdout, "rec\tkey\talias\n" );
 
85
        fprintf ( stdout, "---------------------------------\n" );
 
86
        for ( i = 0; i < n; i ++ ) {
 
87
                fprintf ( stdout, "%i\t%i\t%s\n", i + 1, intcol[i], strarr[i] );
 
88
        }
 
89
 
 
90
        for ( i = 0; i < n; i ++ ) {
 
91
                if ( strarr[i] != NULL ) {
 
92
                        free ( strarr[i] );
 
93
                }
 
94
        }
 
95
 
 
96
        free ( intcol );
 
97
        free ( strarr );
 
98
 
 
99
        db_result_free ( res );
 
100
 
 
101
        if ( db_error_thrown () ) {
 
102
                fprintf ( stdout, "\nLAST ERROR: %s\n", db_error_str () );
 
103
        }
 
104
 
 
105
        error = db_disconnect ( opts );
 
106
        if ( error < 0 ) {
 
107
                fprintf ( stderr, "DB disconnect error: %s\n", db_error_str () );
 
108
                exit (-1);
 
109
        }
 
110
 
 
111
        if ( db_error_thrown () ) {
 
112
                fprintf ( stdout, "\nLAST ERROR: %s\n", db_error_str () );
 
113
        }
 
114
 
 
115
        db_options_free ( opts );
 
116
 
 
117
        return 0;
 
118
}