File: hk_actionqueryutility.cpp

package info (click to toggle)
hk-classes 0.8.2-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 10,872 kB
  • ctags: 16,819
  • sloc: cpp: 90,990; ansic: 78,016; sh: 8,385; python: 3,793; makefile: 292
file content (146 lines) | stat: -rw-r--r-- 3,826 bytes parent folder | download | duplicates (2)
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
140
141
142
143
144
145
146
#include "hk_actionquery.h"
#include "hk_utilities.h"

static char doc[]="hk_actionquery executes a data manipulation query (DML)";
static char args_doc[]="QUERY";
static struct argp_option options[] =
{
    DEFAULTPARAMETER,
    {"sql",'s',"SQL",0,"sql",0},
    {0,0,0,0,0,0}

};

class queryargumentclass :public argumentclass
{
    public:
        hk_string sql,query;
};

static error_t parse_options (int key,char* arg, struct argp_state* state)
{
    queryargumentclass* arguments=(queryargumentclass*)state->input;
    assert(arguments);
    switch (key)
    {

        case 's'    :
        {
            arguments->sql=arg;
            break;

        }
        case ARGP_KEY_ARG:
        {
            if (state->arg_num!=0)
            {
                cerr <<"argnum!=0 !!!"<<endl;
                argp_usage(state);
            }

            arguments->query=arg;
            break;
        }
        default :  return parse_generaloptions(key,arg,state);
    }
    return 0;
}


static struct argp argstruct={options,parse_options,args_doc,doc,0,0,0};

void verify_arguments(queryargumentclass& arguments)
{

/*cerr <<"driver:"<<arguments.driver<<endl;
cerr <<"database:"<<arguments.database<<endl;
cerr <<"query:"<<arguments.query<<endl;
cerr <<"sql:"<<arguments.sql<<endl;
cerr <<"port:"<<arguments.port<<endl;
cerr <<"user:"<<arguments.user<<endl;
cerr <<"password:"<<arguments.password<<endl;
cerr <<"host:"<<arguments.host<<endl;
cerr <<"filter:"<<arguments.filter<<endl;
*/
    if (arguments.query.size()==0 && arguments.sql.size()==0)
    {
        cerr << "No query selected"<<endl;
        exit(1);
    }
    if (arguments.query.size()>0 && arguments.sql.size()>0)
    {
        cerr << "Either select a query or a SQL statement, never both"<<endl;
        exit(1);
    }
    if (!verify_generalarguments(arguments))exit(1);

}


int main(int argc, char** argv)
{
    umask(0077);
    hk_drivermanager d;
    queryargumentclass arguments;
    argp_parse(&argstruct,argc,argv,0,0, &arguments);
    verify_arguments(arguments);
    hk_connection * c=d.new_connection(arguments.driver,hk_connection::noninteractive);
    if (!c)
    {
        hk_class::show_warningmessage(hk_class::hk_translate("Error creating connection"));
        exit(1);
    }
    if (arguments.user.size())c->set_user(arguments.user);
    if (arguments.password.size()) c->set_password(arguments.password);
    if (arguments.host.size()) c->set_host(arguments.host);
    if (arguments.port.size())
    {
        c->set_tcp_port(localestring2uint(arguments.port));
    }
    if (!c->connect(hk_connection::noninteractive))
    {
        exit(1);
    }
    if (!c->database_exists(arguments.database))
    {
        hk_class::show_warningmessage(hk_class::hk_translate("No such database"));
        exit(1);
    }

    hk_database* db=c->new_database(arguments.database);

    hk_actionquery* q=db->new_actionquery();
    if (!q)
    {
        hk_class::show_warningmessage(hk_class::hk_translate("query could not be created"));
        exit(1);
    }

    if (arguments.query.size())
    {
        xmlNodePtr query;
        if (is_localfile(arguments.query))
        {
            xmlDocPtr dp=xmlParseFile(arguments.query.c_str());
            query=xmlDocGetRootElement(dp);
        }
        else
            query=db->xmlload(arguments.query,ft_query);

        if (!query)
        {
            hk_class::show_warningmessage(hk_class::hk_translate("Queryfile couldn't be loaded or is empty"));
            exit(1);
        }
        hk_class::get_tagvalue(query,"SQL",arguments.sql);
    }

    if (arguments.sql.size()) q->set_sql(arguments.sql.c_str(),arguments.sql.size());

    if (!q->execute())
    {
        hk_class::show_warningmessage(hk_class::hk_translate("Query couldn't be executed"));
        exit(1);
    }

}