~cpick/mongrel2/release

« back to all changes in this revision

Viewing changes to tools/m2sh/src/commands/querying.c

  • Committer: Chris Pick
  • Date: 2013-06-30 16:39:57 UTC
  • mfrom: (1106.1.15)
  • Revision ID: git-v1:ec39967acb6bc9867ed9b9dc3774304ca6b9c294
Merge tag 'v1.8.1' into debian

Hotfix for github issue 148

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 *
 
3
 * Copyright (c) 2010, Zed A. Shaw and Mongrel2 Project Contributors.
 
4
 * All rights reserved.
 
5
 * 
 
6
 * Redistribution and use in source and binary forms, with or without
 
7
 * modification, are permitted provided that the following conditions are
 
8
 * met:
 
9
 * 
 
10
 *     * Redistributions of source code must retain the above copyright
 
11
 *       notice, this list of conditions and the following disclaimer.
 
12
 * 
 
13
 *     * Redistributions in binary form must reproduce the above copyright
 
14
 *       notice, this list of conditions and the following disclaimer in the
 
15
 *       documentation and/or other materials provided with the distribution.
 
16
 * 
 
17
 *     * Neither the name of the Mongrel2 Project, Zed A. Shaw, nor the names
 
18
 *       of its contributors may be used to endorse or promote products
 
19
 *       derived from this software without specific prior written
 
20
 *       permission.
 
21
 * 
 
22
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 
23
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 
24
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 
25
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 
26
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
27
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
28
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 
29
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 
30
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 
31
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
32
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
33
 */
 
34
 
 
35
#include <unistd.h>
 
36
#include <sqlite3.h>
 
37
#include <dbg.h>
 
38
#include <tnetstrings.h>
 
39
#include "../commands.h"
 
40
#include "../query_print.h"
 
41
 
 
42
int Command_servers(Command *cmd)
 
43
{
 
44
    bstring db_file = option(cmd, "db", "config.sqlite");
 
45
 
 
46
    check_file(db_file, "config database", R_OK | W_OK);
 
47
 
 
48
    printf("SERVERS:\n------\n");
 
49
    return simple_query_print(db_file, "SELECT name, default_host, uuid from server");
 
50
 
 
51
error:
 
52
    return -1;
 
53
}
 
54
 
 
55
int Command_hosts(Command *cmd)
 
56
{
 
57
    bstring db_file = option(cmd, "db", "config.sqlite");
 
58
    bstring server = option(cmd, "server", NULL);
 
59
    char *sql = NULL;
 
60
    int rc = 0;
 
61
 
 
62
    check_file(db_file, "config database", R_OK | W_OK);
 
63
    check(server, "You need to give a -server of the server to list hosts from.");
 
64
 
 
65
    sql = sqlite3_mprintf("SELECT id, name from host where server_id = (select id from server where name = %Q)", bdata(server));
 
66
 
 
67
    printf("HOSTS in %s:\n-----\n", bdata(server));
 
68
    rc = simple_query_print(db_file, sql);
 
69
 
 
70
error: // fallthrough
 
71
    if(sql) sqlite3_free(sql);
 
72
    return rc;
 
73
}
 
74
 
 
75
 
 
76
int Command_routes(Command *cmd)
 
77
{
 
78
    bstring db_file = option(cmd, "db", "config.sqlite");
 
79
    bstring server = option(cmd, "server", NULL);
 
80
    bstring host = option(cmd, "host", NULL);
 
81
    bstring host_id = option(cmd, "id", NULL);
 
82
    char *sql = NULL;
 
83
    int rc = 0;
 
84
 
 
85
    check_file(db_file, "config database", R_OK | W_OK);
 
86
 
 
87
    if(host_id) {
 
88
        printf("ROUTES in host id %s\n-----\n", bdata(host_id));
 
89
        sql = sqlite3_mprintf("SELECT path from route where host_id=%q", bdata(host_id));
 
90
    } else {
 
91
        check(server, "Must set the -server name you want or use -id.");
 
92
        check(host, "Must set the -host in that server you want or use -id.");
 
93
 
 
94
        printf("ROUTES in host %s, server %s\n-----\n", bdata(host), bdata(server));
 
95
 
 
96
        sql = sqlite3_mprintf("SELECT route.path from route, host, server where "
 
97
                "host.name = %Q and route.host_id = host.id and server.name = %Q and "
 
98
                "host.server_id = server.id", bdata(host), bdata(server));
 
99
    }
 
100
 
 
101
    rc = simple_query_print(db_file, sql);
 
102
 
 
103
error: //fallthrough
 
104
    if(sql) sqlite3_free(sql);
 
105
    return rc;
 
106
}
 
107
 
 
108