First commit
[anna.git] / source / dbms.mysql / OracleTranslator.cpp
1 // ANNA - Anna is Not 'N' Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // https://bitbucket.org/testillano/anna
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
11 //     * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 //     * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
16 // distribution.
17 //     * Neither the name of Google Inc. nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 #include <ctype.h>
38
39 #include <anna/config/defines.hpp>
40
41 #include <anna/dbms.mysql/OracleTranslator.hpp>
42
43 using namespace std;
44 using namespace anna;
45 using namespace anna::dbms;
46
47 mysql::OracleTranslator mysql::OracleTranslator::st_this;
48
49 /*
50  * Pone las sentencias SQL escritas para Oracle en el formato que necesita el
51  * MySQL.
52  *
53  * La sentencia Oracle podría ser algo así como:
54  * insert into foo (a, b, c) values (:x, :y, :zzzz)
55  * update goo set xx=&value where yy=:zzz
56  *
57  * Y Debería quedar algo así:
58  * insert into foo (a, b, c) values (?,?,?)
59  * update goo set xx=? where yy=?
60  */
61 const char* mysql::OracleTranslator::apply(const char* statement)
62 throw(RuntimeException) {
63   bool makeit = false;
64
65   if(anna_strchr(statement, ':') != NULL)
66     makeit = true;
67   else if(anna_strchr(statement, '&') != NULL)
68     makeit = true;
69
70   if(makeit == false)
71     return statement;
72
73   allocate(statement);
74   enum { Copying, Filtering };
75   int mode(Copying);
76   char* w = a_buffer;
77   char character;
78
79   while((character = *statement) != 0) {
80     switch(mode) {
81     case Copying:
82
83       if(character == ':' || character == '&') {
84         *w ++ = '?';
85         mode = Filtering;
86       } else
87         *w ++ = character;
88
89       break;
90     case Filtering:
91
92       if(character == ',' || character == ')' || isspace(character) || iscntrl(character)) {
93         *w ++ = character;
94         mode = Copying;
95       }
96
97       break;
98     }
99
100     statement ++;
101   }
102
103   *w = 0;
104   return a_buffer;
105 }
106
107 void mysql::OracleTranslator::allocate(const char* statement)
108 throw() {
109   const int size = anna_strlen(statement);
110
111   if(size > a_size) {
112     if(a_size > 0) {
113       delete a_buffer;
114       a_buffer = NULL;
115     }
116
117     a_buffer = new char [a_size = size  + 1];
118   }
119
120   a_buffer [0] = 0;
121 }
122