2
* Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
4
* This program is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU General Public License as
6
* published by the Free Software Foundation; version 2 of the
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23
#include "mysql_sql_inserts_loader.h"
24
#include "mysql_sql_parser_utils.h"
25
#include <boost/foreach.hpp>
31
#define NULL_STATE_KEEPER Null_state_keeper _nsk(this);
34
Mysql_sql_inserts_loader::Mysql_sql_inserts_loader(grt::GRT *grt)
37
Mysql_sql_parser_base(grt)
43
void Mysql_sql_inserts_loader::load(const std::string &sql, const std::string &schema_name)
47
_schema_name= schema_name;
48
_process_sql_statement= boost::bind(&Mysql_sql_inserts_loader::process_sql_statement, this, _1);
50
Mysql_sql_parser_fe sql_parser_fe(_grtm->get_grt());
51
sql_parser_fe.ignore_dml= false;
52
Mysql_sql_parser_base::parse_sql_script(sql_parser_fe, sql.c_str());
56
int Mysql_sql_inserts_loader::process_sql_statement(const SqlAstNode *tree)
60
if (const SqlAstNode *item= tree->subitem(sql::_statement, sql::_insert))
61
process_insert_statement(item);
64
return 0; // error count
68
Mysql_sql_inserts_loader::Parse_result
69
Mysql_sql_inserts_loader::process_insert_statement(const SqlAstNode *tree)
71
std::string schema_name= _schema_name;
72
std::string table_name;
74
Strings fields_values;
75
std::vector<bool> null_fields;
78
const SqlAstNode *insert_field_spec= tree->subitem(sql::_insert_field_spec);
79
if (insert_field_spec)
81
// schema & table name
83
const SqlAstNode *table_ident= tree->subitem(sql::_insert2, sql::_insert_table, sql::_table_ident);
84
process_obj_full_name_item(table_ident, schema_name, table_name);
89
const SqlAstNode *fields= insert_field_spec->subitem(sql::_fields);
92
BOOST_FOREACH (const SqlAstNode *item, *fields->subitems())
93
if (item->name_equals(sql::_insert_ident))
94
fields_names.push_back(item->restore_sql_text(_sql_statement));
100
fields_values.reserve(fields_names.size());
101
null_fields.reserve(fields_names.size());
103
const SqlAstNode *insert_values= insert_field_spec->subitem(sql::_insert_values, sql::_values_list);
104
BOOST_FOREACH (const SqlAstNode *item, *insert_values->subitems())
106
if (item->name_equals(sql::_no_braces))
108
fields_values.clear();
111
const SqlAstNode *values= item->subitem(sql::_opt_values, sql::_values);
112
BOOST_FOREACH (const SqlAstNode *item, *values->subitems())
114
if (item->name_equals(sql::_expr_or_default))
117
bool is_field_null= false;
118
if (const SqlAstNode *item1= item->subitem(sql::_expr))
119
if (1 == item1->subitems()->size())
120
if (const SqlAstNode *item2= item1->subitem(sql::_bool_pri))
121
if (1 == item2->subitems()->size())
122
if (const SqlAstNode *item3= item2->subitem(sql::_predicate))
123
if (1 == item3->subitems()->size())
124
if (const SqlAstNode *item4= item3->subitem(sql::_bit_expr))
125
if (1 == item4->subitems()->size())
126
if (const SqlAstNode *item5= item4->subitem(sql::_simple_expr))
127
if (1 == item5->subitems()->size())
128
if (const SqlAstNode *item6= item5->subitem(sql::_literal))
129
if (1 == item6->subitems()->size())
130
if (/*const SqlAstNode *item7=*/ item6->subitem(sql::_NULL_SYM))
135
value= item->restore_sql_text(_sql_statement);
136
if (1 < value.size())
142
value= value.substr(1, value.size()-2);
145
static const std::string func_call_seq= "\\func ";
146
if (value[0] == '\\')
148
if ((value.size() > func_call_seq.size()) && (value.compare(0, func_call_seq.size(), func_call_seq) == 0))
155
bool is_expression= false;
156
for (std::string::iterator i= value.begin(), i_end= value.end(); i != i_end; ++i)
158
if (!std::isdigit(*i) && (*i != '.') && (*i != ','))
166
value= func_call_seq + value;
174
fields_values.push_back(value);
175
null_fields.push_back(is_field_null);
179
const std::pair<std::string, std::string> schema_table= make_pair(schema_name, table_name);
180
_process_insert(sql_statement(), schema_table, fields_names, fields_values, null_fields);