SQLSerialize.cc

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2004-2006 Intel Corporation
00003  * 
00004  *    Licensed under the Apache License, Version 2.0 (the "License");
00005  *    you may not use this file except in compliance with the License.
00006  *    You may obtain a copy of the License at
00007  * 
00008  *        http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  *    Unless required by applicable law or agreed to in writing, software
00011  *    distributed under the License is distributed on an "AS IS" BASIS,
00012  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *    See the License for the specific language governing permissions and
00014  *    limitations under the License.
00015  */
00016 
00017 #include "SQLSerialize.h"
00018 #include "SQLImplementation.h"
00019 
00020 #include "debug/DebugUtils.h"
00021 #include "debug/Log.h"
00022 #include "util/StringUtils.h"
00023 
00024 namespace oasys {
00025 
00026 /******************************************************************************
00027  *
00028  * SQLQuery
00029  *
00030  *****************************************************************************/
00031 
00035 SQLQuery::SQLQuery(action_t type, const char* table_name,
00036                    SQLImplementation* impl, const char* initial_query)
00037     
00038     : SerializeAction(type, Serialize::CONTEXT_LOCAL),
00039       table_name_(table_name),
00040       sql_impl_(impl),
00041       query_(256, initial_query)
00042 {
00043 }
00044 
00045 /******************************************************************************
00046  *
00047  * SQLInsert
00048  *
00049  *****************************************************************************/
00050 
00054 SQLInsert::SQLInsert(const char* table_name, SQLImplementation* impl)
00055     : SQLQuery(Serialize::MARSHAL, table_name, impl)
00056 {
00057 }
00058 
00059 
00060 // Virtual functions inherited from SerializeAction
00061 
00065 void 
00066 SQLInsert::begin_action() 
00067 {
00068     query_.appendf("INSERT INTO %s  VALUES(",table_name_);
00069 }
00070 
00075 void 
00076 SQLInsert::end_action() 
00077 {
00078     if (query_.data()[query_.length() - 1] == ',') {
00079         query_.data()[query_.length() - 1] = ')';
00080     }
00081 }
00082 
00083 
00084 void 
00085 SQLInsert::process(const char* name, u_int32_t* i)
00086 {
00087     (void)name;
00088     query_.appendf("%u,", *i);
00089 }
00090 
00091 void 
00092 SQLInsert::process(const char* name, u_int16_t* i)
00093 {
00094     (void)name;
00095     query_.appendf("%u,", *i);
00096 }
00097 
00098 void 
00099 SQLInsert::process(const char* name, u_int8_t* i)
00100 {
00101     (void)name;
00102     query_.appendf("%u,", *i);
00103 }
00104 
00105 void 
00106 SQLInsert::process(const char* name, int32_t* i)
00107 {
00108     (void)name;
00109 #ifdef __CYGWIN__
00110     query_.appendf("%ld,", *i);
00111 #else
00112     query_.appendf("%d,", *i);
00113 #endif
00114 }
00115 
00116 void 
00117 SQLInsert::process(const char* name, int16_t* i)
00118 {
00119     (void)name;
00120     query_.appendf("%d,", *i);
00121 }
00122 
00123 void 
00124 SQLInsert::process(const char* name, int8_t* i)
00125 {
00126     (void)name;
00127     query_.appendf("%d,", *i);
00128 }
00129 
00130 void 
00131 SQLInsert::process(const char* name, bool* b)
00132 {
00133     (void)name;
00134     if (*b) {
00135         query_.append("'TRUE',");
00136     } else {
00137         query_.append("'FALSE',");
00138     }
00139 }
00140 
00141 
00142 void 
00143 SQLInsert::process(const char* name, std::string* s) 
00144 {
00145     (void)name;
00146     query_.appendf("'%s',", sql_impl_->escape_string(s->c_str()));
00147 }
00148 
00149 void 
00150 SQLInsert::process(const char* name, u_char* bp, size_t len)
00151 {
00152     (void)name;
00153     query_.appendf("'%s',", sql_impl_->escape_binary(bp, len));
00154 }
00155 
00156 void 
00157 SQLInsert::process(const char* name, u_char** bp, size_t* lenp, int flags)
00158 {
00159     (void)name;
00160     (void)bp;
00161     (void)lenp;
00162     (void)flags;
00163     NOTIMPLEMENTED;
00164 }
00165 
00166 /******************************************************************************
00167  *
00168  * SQLUpdate
00169  *
00170  *****************************************************************************/
00171 
00175 SQLUpdate::SQLUpdate(const char* table_name, SQLImplementation* impl)
00176     : SQLQuery(Serialize::MARSHAL, table_name, impl)
00177 {
00178 }
00179 
00180 
00181 // Virtual functions inherited from SerializeAction
00182 void 
00183 SQLUpdate::begin_action() 
00184 {
00185     query_.appendf("UPDATE %s SET ",table_name_);
00186 }
00187 
00188 void 
00189 SQLUpdate::end_action() 
00190 {
00191     if (query_.data()[query_.length() - 2] == ',') {
00192         query_.data()[query_.length() - 2] =  ' ';
00193     }
00194 }
00195 
00196 void 
00197 SQLUpdate::process(const char* name, u_int32_t* i)
00198 {
00199     (void)name;
00200     query_.appendf("%s = %u, ", name, *i);
00201 }
00202 
00203 void 
00204 SQLUpdate::process(const char* name, u_int16_t* i)
00205 {
00206     (void)name;
00207     query_.appendf("%s = %u, ", name, *i);
00208 }
00209 
00210 void 
00211 SQLUpdate::process(const char* name, u_int8_t* i)
00212 {
00213     (void)name;
00214     query_.appendf("%s = %u, ", name, *i);
00215 }
00216 
00217 void 
00218 SQLUpdate::process(const char* name, int32_t* i)
00219 {
00220     (void)name;
00221 #ifdef __CYGWIN__
00222     query_.appendf("%s = %ld, ", name, *i);
00223 #else
00224     query_.appendf("%s = %d, ", name, *i);
00225 #endif
00226 }
00227 
00228 void 
00229 SQLUpdate::process(const char* name, int16_t* i)
00230 {
00231     (void)name;
00232     query_.appendf("%s = %d, ", name, *i);
00233 }
00234 
00235 void 
00236 SQLUpdate::process(const char* name, int8_t* i)
00237 {
00238     (void)name;
00239     query_.appendf("%s = %d, ", name, *i);
00240 }
00241 
00242 void 
00243 SQLUpdate::process(const char* name, bool* b)
00244 {
00245     (void)name;
00246     if (*b) {
00247         query_.appendf("%s = 'TRUE', ", name);
00248     } else {
00249         query_.appendf("%s = 'FALSE', ", name);
00250     }
00251 }
00252 
00253 
00254 void 
00255 SQLUpdate::process(const char* name, std::string* s) 
00256 {
00257     (void)name;
00258     query_.appendf("%s = '%s', ", name, sql_impl_->escape_string(s->c_str()));
00259 }
00260 
00261 void 
00262 SQLUpdate::process(const char* name, u_char* bp, size_t len)
00263 {
00264     (void)name;
00265     query_.appendf("%s = '%s', ", name, sql_impl_->escape_binary(bp, len));
00266 }
00267 
00268 void 
00269 SQLUpdate::process(const char* name, u_char** bp, size_t* lenp, int flags)
00270 {
00271     (void)name;
00272     (void)bp;
00273     (void)lenp;
00274     (void)flags;
00275     NOTIMPLEMENTED;
00276 }
00277 
00278 /******************************************************************************
00279  *
00280  * SQLTableFormat
00281  *
00282  *****************************************************************************/
00283 
00288 SQLTableFormat::SQLTableFormat(const char* table_name,
00289                                SQLImplementation* impl)
00290     : SQLQuery(Serialize::INFO, table_name, impl)
00291 {
00292 }
00293 
00294 
00295 // Virtual functions inherited from SerializeAction
00296 
00297 void 
00298 SQLTableFormat::begin_action() 
00299 {
00300     query_.appendf("CREATE TABLE  %s  (",table_name_);
00301 }
00302 
00303 void 
00304 SQLTableFormat::end_action() 
00305 {
00306     if (query_.data()[query_.length() - 1] == ',') {
00307         query_.data()[query_.length() - 1] =  ')';
00308     }
00309 }
00310 
00311 
00312 void
00313 SQLTableFormat::process(const char* name,  SerializableObject* object) 
00314 {
00315     int old_len = column_prefix_.length();
00316 
00317     column_prefix_.appendf("%s__", name);
00318     object->serialize(this);
00319     column_prefix_.trim(column_prefix_.length() - old_len);
00320 }
00321 
00322 void 
00323 SQLTableFormat::append(const char* name, const char* type)
00324 {
00325     query_.appendf("%.*s%s %s,",
00326                    (int)column_prefix_.length(), column_prefix_.data(),
00327                    name, type);
00328 }
00329 
00330 // Virtual functions inherited from SerializeAction
00331 void 
00332 SQLTableFormat::process(const char* name, u_int32_t* i)
00333 {
00334     (void)i;
00335     append(name, "BIGINT");
00336 }
00337 
00338 void 
00339 SQLTableFormat::process(const char* name, u_int16_t* i)
00340 {
00341     (void)i;
00342     append(name, "INTEGER");
00343 }
00344 
00345 void
00346 SQLTableFormat::process(const char* name, u_int8_t* i) {
00347 
00348     (void)i;
00349     append(name, "SMALLINT");
00350 }
00351  
00352 
00353 // Mysql and Postgres do not have common implementation of bool
00354 // XXX/demmer fix this
00355 void 
00356 SQLTableFormat::process(const char* name, bool* b)
00357 {
00358     (void)b;
00359 //    append(name, "BOOLEAN");
00360     append(name, "TEXT");
00361 }
00362 
00363 void 
00364 SQLTableFormat::process(const char* name, std::string* s) 
00365 {
00366     (void)s;
00367     append(name, "TEXT");
00368 }
00369 
00370 void 
00371 SQLTableFormat::process(const char* name, u_char* bp, size_t len)
00372 {
00373     (void)bp;
00374     (void)len;
00375     append(name,sql_impl_->binary_datatype());
00376 }
00377     
00378 void 
00379 SQLTableFormat::process(const char* name, u_char** bp, size_t* lenp, int flags)
00380 {
00381     (void)bp;
00382     (void)lenp;
00383     (void)flags;
00384 
00385     if (flags & Serialize::NULL_TERMINATED) {
00386         NOTIMPLEMENTED;
00387     }
00388 
00389     append(name,sql_impl_->binary_datatype());
00390 }
00391 
00392 /******************************************************************************
00393  *
00394  * SQLExtract
00395  *
00396  *****************************************************************************/
00397 
00402 SQLExtract::SQLExtract(SQLImplementation* impl)
00403     : SerializeAction(Serialize::UNMARSHAL, 
00404                       Serialize::CONTEXT_LOCAL)
00405 {
00406     field_ = 0;
00407     sql_impl_ = impl;
00408 }
00409 
00410 const char* 
00411 SQLExtract::next_field()
00412 {
00413     return sql_impl_->get_value(0, field_++);
00414 }
00415 
00416 void
00417 SQLExtract::process(const char* name, u_int32_t* i)
00418 {
00419     (void)name;
00420     const char* buf = next_field();
00421     if (buf == NULL) return;
00422     
00423     *i = atoi(buf) ; 
00424 
00425     if (log_) logf(log_, LOG_DEBUG, "<=int32(%d)", *i);
00426 }
00427 
00428 void 
00429 SQLExtract::process(const char* name, u_int16_t* i)
00430 {
00431     (void)name;
00432     const char* buf = next_field();
00433     if (buf == NULL) return;
00434 
00435     *i = atoi(buf) ; 
00436     
00437     if (log_) logf(log_, LOG_DEBUG, "<=int16(%d)", *i);
00438 }
00439 
00440 void 
00441 SQLExtract::process(const char* name, u_int8_t* i)
00442 {
00443     (void)name;
00444     const char* buf = next_field();
00445     if (buf == NULL) return;
00446     
00447     *i = buf[0];
00448     
00449     if (log_) logf(log_, LOG_DEBUG, "<=int8(%d)", *i);
00450 }
00451 
00452 void 
00453 SQLExtract::process(const char* name, bool* b)
00454 {
00455     (void)name;
00456     const char* buf = next_field();
00457 
00458     if (buf == NULL) return;
00459 
00460     switch(buf[0]) {
00461     case 'T':
00462     case 't':
00463     case '1':
00464     case '\1':
00465         *b = true;
00466         break;
00467     case 'F':
00468     case 'f':
00469     case '0':
00470     case '\0':
00471         *b = false;
00472         break;
00473     default:
00474         logf("/sql", LOG_ERR, "unexpected value '%s' for boolean column", buf);
00475         signal_error();
00476         return;
00477     }
00478     
00479     if (log_) logf(log_, LOG_DEBUG, "<=bool(%c)", *b ? 'T' : 'F');
00480 }
00481 
00482 void 
00483 SQLExtract::process(const char* name, std::string* s)
00484 {
00485     (void)name;
00486     const char* buf = next_field();
00487     if (buf == NULL) return;
00488     
00489     s->assign(buf);
00490     
00491     size_t len = s->length();
00492     
00493     if (log_) logf(log_, LOG_DEBUG, "<=string(%zu: '%.*s')",
00494                    len, (int)(len < 32 ? len : 32), s->data());
00495 }
00496 
00497 void 
00498 SQLExtract::process(const char* name, u_char* bp, size_t len)
00499 {
00500     (void)name;
00501     const char* buf = next_field();
00502     if (buf == NULL) return;
00503  
00504     const u_char* v = sql_impl_->unescape_binary((const u_char*)buf);
00505 
00506     memcpy(bp, v, len);
00507     if (log_) {
00508         std::string s;
00509         hex2str(&s, bp, len < 16 ? len : 16);
00510         logf(log_, LOG_DEBUG, "<=bufc(%zu: '%.*s')",
00511              len, (int)s.length(), s.data());
00512     }
00513 
00514 }
00515 
00516 
00517 void 
00518 SQLExtract::process(const char* name, u_char** bp, size_t* lenp, int flags)
00519 {
00520     (void)name;
00521     (void)bp;
00522     (void)lenp;
00523     (void)flags;
00524     NOTIMPLEMENTED;
00525 }
00526 
00527 } // namespace oasys

Generated on Thu Jun 7 16:56:52 2007 for DTN Reference Implementation by  doxygen 1.5.1