~drizzle-trunk/drizzle/development

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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 *
 *  Copyright (C) 2009 Sun Microsystems
 *
 *  Authors:
 *
 *    Jay Pipes <joinfu@sun.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/**
 * @file
 *
 * Implementation of various routines that can be used to convert
 * Command messages to other formats, including SQL statements.
 */

#include "drizzled/message/command_transform.h"
#include "drizzled/message/replication.pb.h"

#include <string>
#include <bitset>

namespace drizzled
{
namespace message
{

void transformCommand2Sql(const Command &source,
                          std::string *destination,
                          enum CommandTransformSqlVariant sql_variant)
{
  
  char quoted_identifier= '`';
  if (sql_variant == ANSI)
    quoted_identifier= '"';

  switch (source.type())
  {
  case Command::START_TRANSACTION:
    destination->assign("START TRANSACTION");
    break;
  case Command::COMMIT:
    destination->assign("COMMIT");
    break;
  case Command::ROLLBACK:
    destination->assign("ROLLBACK");
    break;
  case Command::INSERT:
    {
      destination->assign("INSERT INTO ");
      destination->push_back(quoted_identifier);
      destination->append(source.schema());
      destination->push_back(quoted_identifier);
      destination->push_back('.');
      destination->push_back(quoted_identifier);
      destination->append(source.table());
      destination->push_back(quoted_identifier);
      destination->append(" (");

      const message::InsertRecord &record= source.insert_record();

      int32_t num_fields= record.insert_field_size();

      int32_t x;
      for (x= 0; x < num_fields; x++)
      {
        if (x != 0)
          destination->push_back(',');

        const message::Table::Field f= record.insert_field(x);

        destination->push_back(quoted_identifier);
        destination->append(f.name());
        destination->push_back(quoted_identifier);
      }

      destination->append(") VALUES ");

      /* 
       * There may be an INSERT VALUES (),() type statement.  We know the
       * number of records is equal to the field_values array size divided
       * by the number of fields.
       *
       * So, we do an inner and an outer loop.  Outer loop is on the number
       * of records and the inner loop on the number of fields.  In this way, 
       * we know that record.field_values(outer_loop * num_fields) + inner_loop))
       * always gives us our correct field value.
       */
      int32_t num_records= (record.insert_value_size() / num_fields);
      int32_t y;
      for (x= 0; x < num_records; x++)
      {
        if (x != 0)
          destination->push_back(',');

        destination->push_back('(');
        for (y= 0; y < num_fields; y++)
        {
          if (y != 0)
            destination->push_back(',');

          destination->push_back('\'');
          destination->append(record.insert_value((x * num_fields) + y));
          destination->push_back('\'');
        }
        destination->push_back(')');
      }
    }
    break;
  case Command::UPDATE:
    {
      const message::UpdateRecord &record= source.update_record();
      int32_t num_update_fields= record.update_field_size();
      int32_t x;

      destination->assign("UPDATE ");
      destination->push_back(quoted_identifier);
      destination->append(source.schema());
      destination->push_back(quoted_identifier);
      destination->push_back('.');
      destination->push_back(quoted_identifier);
      destination->append(source.table());
      destination->push_back(quoted_identifier);
      destination->append(" SET ");

      for (x= 0;x < num_update_fields; x++)
      {
        Table::Field f= record.update_field(x);
        
        if (x != 0)
          destination->push_back(',');

        destination->push_back(quoted_identifier);
        destination->append(f.name());
        destination->push_back(quoted_identifier);
        destination->append("='");
        destination->append(record.after_value(x));
        destination->push_back('\'');
      }

      int32_t num_where_fields= record.where_field_size();
      /* 
       * Make sure we catch anywhere we're not aligning the fields with
       * the field_values arrays...
       */
      assert(num_where_fields == record.where_value_size());

      destination->append(" WHERE ");
      for (x= 0;x < num_where_fields; x++)
      {
        if (x != 0)
          destination->append(" AND "); /* Always AND condition with a multi-column PK */

        const Table::Field f= record.where_field(x);

        /* Always equality conditions */
        destination->push_back(quoted_identifier);
        destination->append(f.name());
        destination->push_back(quoted_identifier);
        destination->append("='");
        destination->append(record.where_value(x));
        destination->push_back('\'');
      }
    }
    break;
  case Command::DELETE:
    {
      const message::DeleteRecord &record= source.delete_record();
      int32_t num_where_fields= record.where_field_size();
      int32_t x;

      destination->assign("DELETE FROM ");
      destination->push_back(quoted_identifier);
      destination->append(source.schema());
      destination->push_back(quoted_identifier);
      destination->push_back('.');
      destination->push_back(quoted_identifier);
      destination->append(source.table());
      destination->push_back(quoted_identifier);
      
      destination->append(" WHERE ");
      for (x= 0; x < num_where_fields; x++)
      {
        if (x != 0)
          destination->append(" AND "); /* Always AND condition with a multi-column PK */

        const Table::Field f= record.where_field(x);

        /* Always equality conditions */
        destination->push_back(quoted_identifier);
        destination->append(f.name());
        destination->push_back(quoted_identifier);
        destination->append(" = '");
        destination->append(record.where_value(x));
        destination->push_back('\'');
      }
    }
    break;
  case Command::RAW_SQL:
    destination->assign(source.sql());
    break;
  }
}

} /* end namespace drizzled::message */
} /* end namespace drizzled */