gam-resource-manager: adjust to updated proxied call callback signature.
[profile/ivi/murphy.git] / src / core / console-db.c
1 /*
2  * Copyright (c) 2012, Intel Corporation
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *   * Redistributions of source code must retain the above copyright notice,
9  *     this list of conditions and the following disclaimer.
10  *   * Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  *   * Neither the name of Intel Corporation nor the names of its contributors
14  *     may be used to endorse or promote products derived from this software
15  *     without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30
31 /*
32  * DB commands
33  */
34
35 #include <stdarg.h>
36
37 #include <murphy-db/mql.h>
38 #include <murphy-db/mqi.h>
39
40 static void db_cmd(char *fmt, ...)
41 {
42     mql_result_t *r;
43     char          buf[1024];
44     va_list       ap;
45     int           n, error;
46     const char   *msg;
47
48     va_start(ap, fmt);
49     n = vsnprintf(buf, sizeof(buf), fmt, ap);
50     va_end(ap);
51
52     if (n < (int)sizeof(buf) && n > 0) {
53         r = mql_exec_string(mql_result_string, buf);
54
55         if (mql_result_is_success(r))
56             printf("%s\n", mql_result_string_get(r));
57         else {
58             error = mql_result_error_get_code(r);
59             msg   = mql_result_error_get_message(r);
60
61             printf("DB error %d: %s\n", error, msg ? msg : "unknown error");
62         }
63
64         mql_result_free(r);
65     }
66 }
67
68
69 static void db_exec(mrp_console_t *c, void *user_data, const char *grp,
70                     const char *cmd, char *args)
71 {
72     mqi_handle_t tx;
73
74     MRP_UNUSED(c);
75     MRP_UNUSED(user_data);
76     MRP_UNUSED(grp);
77     MRP_UNUSED(cmd);
78
79     tx = mqi_begin_transaction();
80     db_cmd(args);
81     mqi_commit_transaction(tx);
82 }
83
84
85 void db_source(mrp_console_t *c, void *user_data, int argc, char **argv)
86 {
87     mqi_handle_t tx;
88     int          i, success;
89
90     MRP_UNUSED(c);
91     MRP_UNUSED(user_data);
92
93     success = TRUE;
94     tx      = mqi_begin_transaction();
95
96     for (i = 2; i < argc && success; i++) {
97         if (mql_exec_file(argv[i]) == 0)
98             printf("DB script '%s' OK\n", argv[i]);
99         else {
100             printf("DB script error %d: %s\n", errno, strerror(errno));
101             success = FALSE;
102         }
103     }
104
105     if (success)
106         mqi_commit_transaction(tx);
107     else {
108         mqi_rollback_transaction(tx);
109         printf("DB rolled back.\n");
110     }
111 }
112
113
114 #define DB_GROUP_DESCRIPTION                                                \
115     "Database commands provide means to manipulate the Murphy database\n"   \
116     "from the console. Commands are provided for listing, describing,\n"    \
117     "and removing tables as well as for issuing arbitrary high-level\n"     \
118     "MQL commands. Note that these commands are intended for debugging\n" \
119     "and debugging purposes. Extra care should to be taken when directly\n" \
120     "manipulating the database."
121
122 #define DBEXEC_SYNTAX      "<DB command>"
123 #define DBEXEC_SUMMARY     "execute the given database MQL command"
124 #define DBEXEC_DESCRIPTION "Executes the given MQL command and prints the\n" \
125     "result.\n"
126
127 #define DBSRC_SYNTAX      "source <file>"
128 #define DBSRC_SUMMARY     "evaluate the MQL script in the given <file>"
129 #define DBSRC_DESCRIPTION "Read and evaluate the contents of <file>.\n"
130
131
132 MRP_CORE_CONSOLE_GROUP(db_group, "db", DB_GROUP_DESCRIPTION, NULL, {
133         MRP_TOKENIZED_CMD("source", db_source, FALSE,
134                           DBSRC_SYNTAX, DBSRC_SUMMARY, DBSRC_DESCRIPTION),
135         MRP_RAWINPUT_CMD("eval", db_exec,
136                          MRP_CONSOLE_CATCHALL | MRP_CONSOLE_SELECTABLE,
137                          DBEXEC_SYNTAX, DBEXEC_SUMMARY, DBEXEC_DESCRIPTION),
138 });