2 * Copyright (c) 2012, Intel Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
37 #include <murphy-db/mql.h>
38 #include <murphy-db/mqi.h>
40 static void db_cmd(char *fmt, ...)
49 n = vsnprintf(buf, sizeof(buf), fmt, ap);
52 if (n < (int)sizeof(buf) && n > 0) {
53 r = mql_exec_string(mql_result_string, buf);
55 if (mql_result_is_success(r))
56 printf("%s\n", mql_result_string_get(r));
58 error = mql_result_error_get_code(r);
59 msg = mql_result_error_get_message(r);
61 printf("DB error %d: %s\n", error, msg ? msg : "unknown error");
69 static void db_exec(mrp_console_t *c, void *user_data, const char *grp,
70 const char *cmd, char *args)
75 MRP_UNUSED(user_data);
79 tx = mqi_begin_transaction();
81 mqi_commit_transaction(tx);
85 void db_source(mrp_console_t *c, void *user_data, int argc, char **argv)
91 MRP_UNUSED(user_data);
94 tx = mqi_begin_transaction();
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]);
100 printf("DB script error %d: %s\n", errno, strerror(errno));
106 mqi_commit_transaction(tx);
108 mqi_rollback_transaction(tx);
109 printf("DB rolled back.\n");
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."
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" \
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"
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),