mysql_update.cpp

00001 #include <iostream>
00002 #include "clssdf.h"
00003 
00004 using namespace std;
00005 
00006 namespace class_def {
00007 
00008   void class_definition::print_mysql_update( std::ostream& os) const {
00009 
00010     os << "int " << _name << "::mysql_update(  void* _mysql) const {" << endl;
00011 
00012     if (!_all_functional_attributes.empty()) {
00013 
00014       os << "   MYSQL      *mysql =  (MYSQL*) _mysql;" << endl << endl;
00015 
00016       os << "   if (mysql == 0) {" << endl;
00017       os << "      return -101;" << endl;
00018       os << "   }" << endl << endl;
00019 
00020       os << "   if (mysql_ping( mysql)) {" << endl;
00021       os << "      return -102;" << endl;
00022       os << "   }" << endl << endl;
00023 
00024       os << "   ostringstream oss;" << endl;
00025 
00026       os << "   oss << \"update " << mysql_case_adjusted_name( _name) << " set\" << endl" << endl;
00027 
00028       std::vector<attribute*>::const_iterator pa = _all_functional_attributes.begin(), pa_next;
00029       bool _is_string;
00030 
00031       for (;;) {
00032         os << "       << \"" << mysql_case_adjusted_name( (*pa)->get_name());
00033       
00034         _is_string = is_string( (*pa)->get_type().c_str());
00035 
00036         pa_next = pa;
00037         ++pa_next;
00038         if (pa_next == _all_functional_attributes.end()) {
00039           if (_is_string) {
00040             os << " = \\\"\" << " << (*pa)->storage_format_of_name() << " << \"\\\"\" << endl" << endl;
00041           } else {
00042             os << " = \" << " << (*pa)->storage_format_of_name() << " << endl" << endl;
00043           }
00044           break;
00045         } else {
00046           if (_is_string) {
00047             os << " = \\\"\" << " << (*pa)->storage_format_of_name() << " << \"\\\",\" << endl" << endl;
00048           } else {
00049             os << " = \" << " << (*pa)->storage_format_of_name() << " << \",\" << endl" << endl;
00050           }
00051         }
00052         pa = pa_next;
00053       }
00054       os << "       << \" where  \"" << endl;
00055 
00056       pa = _all_key_attributes.begin();
00057 
00058       for (;;) {
00059         os << "       << \"" << mysql_case_adjusted_name( (*pa)->get_name());
00060       
00061         _is_string = is_string( (*pa)->get_type().c_str());
00062 
00063         pa_next = pa;
00064         ++pa_next;
00065         if (pa_next == _all_key_attributes.end()) {
00066           if (_is_string) {
00067             os << " = \\\"\" << " << (*pa)->storage_format_of_name() << " << \"\\\"\" << endl;" << endl;
00068           } else {
00069             os << " = \" << " << (*pa)->storage_format_of_name() << " << endl;" << endl;
00070           }
00071           break;
00072         } else {
00073           if (_is_string) {
00074             os << " = \\\"\" << " << (*pa)->storage_format_of_name() << " << \"\\\" and \" << endl" << endl;
00075           } else {
00076             os << " = \" << " << (*pa)->storage_format_of_name() << " << \" and \" << endl" << endl;
00077           }
00078         }
00079         pa = pa_next;
00080       }
00081 
00082       os << "   int erno = mysql_query( mysql, oss.str().c_str());" << endl;
00083       os << "   if (erno) {" << endl;
00084       os << "      erno = mysql_errno( mysql);" << endl;
00085       os << "      CERR << \"query = {\" << oss.str() << \"}\" << endl" << endl;
00086       os << "           << \"error = {\" << mysql_error( mysql) << \"}\" << endl" 
00087          << endl;
00088       os << "           << \"erno  = \" << erno << endl;" << endl;
00089       os << "      return -erno;" << endl;
00090       os << "   }" << endl;
00091     }
00092     os << "   return 0;" << endl;
00093     os << "}" << endl << endl;
00094   }
00095 }

Generated on Tue Jul 14 12:22:31 2009 for cstpp by  doxygen 1.5.1