libsample.cpp

Go to the documentation of this file.
00001 /*
00002  *       libsample.cpp
00003  *       Generated by sql2class by (C) AH 2000
00004  *       Sun May 30 12:33:30 2004
00005  */
00006 
00007 /*
00008 Copyright (C) 2001  Anders Hedstrom (grymse@alhem.net)
00009 
00010 This program is free software; you can redistribute it and/or
00011 modify it under the terms of the GNU General Public License
00012 as published by the Free Software Foundation; either version 2
00013 of the License, or (at your option) any later version.
00014 
00015 This program is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00023 */
00024 
00025 #include <stdio.h>
00026 #include <stdlib.h>
00027 #include <string.h>
00028 #include <mysql/mysql.h>
00029 #include <libsql++.h>
00030 #include "libsample.h"
00031 
00032 
00037         Sampletable::Sampletable(Database *db)
00038         {
00039                 database = db;
00040                 new_object = 1;
00041                 clear();
00042         }
00043         Sampletable::Sampletable(Database *db,const std::string& sql)
00044         {
00045                 database = db;
00046                 new_object = 1;
00047                 spawn(sql);
00048         }
00049         Sampletable::Sampletable(Database *db,Query *qd)
00050         {
00051                 database = db;
00052                 new_object = 0;
00053                 spawn(qd);
00054         }
00055         Sampletable::Sampletable(Database& db,long i_num) : database(&db),new_object(1)
00056         {
00057                 Query q(database);
00058                 std::string sql = "select * from sampletable where ";
00059                 {
00060                         char slask[100];
00061                         sprintf(slask,"num='%ld'",i_num);
00062                         sql += slask;
00063                 }
00064                 spawn(sql);
00065         }
00066         Sampletable::~Sampletable()
00067         {
00068         }
00069 
00070         void Sampletable::select(const std::string& sql)
00071         {
00072                 spawn(sql);
00073         }
00074 
00075         long Sampletable::insert()
00076         {
00077                 Query q(database);
00078                 std::string sql;
00079 
00080                 sql = "insert into sampletable(num,name)";
00081                 {
00082                         char slask[100];
00083                         sprintf(slask," values(%ld",this -> num);
00084                         sql += slask;
00085                 }
00086                 sql += ", '" + q.safestr(this -> name) + "'";
00087                 sql += ")";
00088                 q.execute(sql);
00089                 new_object = 0;
00090                 return num = q.insert_id();
00091         }
00092 
00093         void Sampletable::update()
00094         {
00095                 update(this -> num);
00096         }
00097 
00098         void Sampletable::update(long i_num)
00099         {
00100                 Query q(database);
00101                 std::string sql;
00102                 sql += "update sampletable set name='" + q.safestr(this -> name) + "'";
00103                 {
00104                         char slask[200];
00105                         sprintf(slask," where num='%ld'",i_num);
00106                         sql += slask;
00107                 }
00108                 q.execute(sql);
00109         }
00110 
00111         void Sampletable::save()
00112         {
00113                 if (new_object)
00114                         insert();
00115                 else
00116                         update();
00117         }
00118 
00119         void Sampletable::erase()
00120         {
00121                 if (!new_object)
00122                 {
00123                         std::string sql = "delete from sampletable where";
00124                         Query q(database);
00125                         {
00126                                 char slask[200];
00127                                 sprintf(slask," num='%ld'",this -> num);
00128                                 sql += slask;
00129                         }
00130                         q.execute(sql);
00131                 }
00132         }
00133 
00134         std::string Sampletable::xml()
00135         {
00136                 Query q(database);
00137                 std::string dest;
00138                 char slask[200];
00139                 dest = "<SAMPLETABLE>";
00140                 sprintf(slask,"<NUM>%ld</NUM>",num);
00141                 dest += slask;
00142                 dest += "<NAME>" + q.xmlsafestr(name) + "</NAME>";
00143                 dest += "</SAMPLETABLE>";
00144                 return dest;
00145         }
00146 
00147         std::string Sampletable::xml(const std::string& tag,const std::string& xvalx)
00148         {
00149                 Query q(database);
00150                 std::string dest;
00151                 char slask[200];
00152                 dest = "<SAMPLETABLE " + tag + "=\"" + xvalx + "\">";
00153                 sprintf(slask,"<NUM>%ld</NUM>",num);
00154                 dest += slask;
00155                 dest += "<NAME>" + q.xmlsafestr(name) + "</NAME>";
00156                 dest += "</SAMPLETABLE>";
00157                 return dest;
00158         }
00159 
00160         int Sampletable::num_cols()
00161         {
00162                 return 2;
00163         }
00164 
00165         void Sampletable::clear()
00166         {
00167                 num = 0;
00168         }
00169         void Sampletable::spawn(const std::string& sql)
00170         {
00171                 Query q(database);
00172                 std::string temp;
00173 
00174                 if (!strncasecmp(sql.c_str(),"select * ",9))
00175                 {
00176                         temp = "select num,name " + sql.substr(9);
00177                 } else
00178                         temp = sql;
00179                 q.get_result(temp);
00180                 if (q.fetch_row())
00181                 {
00182                         num = q.getval(0);                                    // 0 - num integer
00183                         name = q.getstr(1);                                   // 1 - name varchar(255)
00184                         new_object = 0;
00185                 } else
00186                         clear();
00187                 q.free_result();
00188         }
00189 
00190         void Sampletable::spawn(Query *qd)
00191         {
00192                 num = qd -> getval(0);                                    // 0 - num integer
00193                 name = qd -> getstr(1);                                   // 1 - name varchar(255)
00194         }
00195 
00196 // End of implementation of class 'Sampletable'
00197 

Generated for dbdesigner by doxygen 1.3.6