#include #include #include "nsql.h" #include "tlmpsql.h" PUBLIC NSQL_FIELD_NAMES::NSQL_FIELD_NAMES (int _nbf, const char *_fields[]) { nb = _nbf; fields = _fields; } /* Return the number of fields in the description */ PUBLIC int NSQL_FIELD_NAMES::getnb() const { return nb; } /* Return the name of a SQL field */ PUBLIC const char *NSQL_FIELD_NAMES::getfieldname(int no) const { return fields[no]; } PUBLIC NSQL_FIELD::NSQL_FIELD() { name = NULL; } PUBLIC NSQL_FIELD::NSQL_FIELD(const char *_name) { name = strdup(_name); flags = 0; } PUBLIC NSQL_FIELD::NSQL_FIELD(const char *_name, unsigned _flags) { name = strdup(_name); flags = _flags; } PUBLIC NSQL_FIELD::~NSQL_FIELD() { free (name); } PUBLIC const char *NSQL_FIELD::getsqlname() const { return name; } PUBLIC bool NSQL_FIELD::tstflag (unsigned one_flag) { return (flags & one_flag) != 0; } PUBLIC void NSQL_FIELD::setflag (unsigned one_flag) { flags |= one_flag; } PUBLIC void NSQL_FIELD::clrflag (unsigned one_flag) { flags &= ~one_flag; } /* Reset the field, forget the value. */ PUBLIC void NSQL_FIELD::clear() { rstmodified(); setfrom (""); } /* This class is never used directly. You must derive from it and declare explicit SQL field names */ PROTECTED NSQL_RECORD::NSQL_RECORD() { nbfield = 0; } PUBLIC void NSQL_RECORD::rstmodified() { for (int i=0; irstmodified(); } PUBLIC void NSQL_RECORD::clear() { for (int i=0; iclear(); } /* Update the database using "set" field */ PUBLIC int NSQL_RECORD::updatev( NSQL &ns, const char *table, const char *where, va_list list) { int ret = 0; NSQL_ENCODE enc; SSTRING req; for (int i=0; iwas_modified()){ if (req.is_empty()){ req.setfromf ("update %s set ",table); }else{ req.append (","); } const char *s = enc.enc(f); if (f->tstflag (NSQL_IS_PASSWORD)){ req.appendf ("%s=password('%s')",f->getsqlname(),s); }else{ req.appendf ("%s='%s'",f->getsqlname(),s); } } } if (req.is_filled()){ char tmp[10000]; vsnprintf (tmp,sizeof(tmp)-1,where,list); req.append (" where "); req.append (tmp); ret = sql_action (ns,req.get()); } return ret; } PUBLIC int NSQL_RECORD::update( NSQL &ns, const char *table, const char *where, ...) { va_list list; va_start (list,where); int ret = updatev (ns,table,where,list); va_end (list); return ret; } PUBLIC int NSQL_RECORD::update( const char *table, const char *where, ...) { va_list list; va_start (list,where); int ret = updatev (*query_getdefaultdb(),table,where,list); va_end (list); return ret; } /* Insert the record in the database */ PUBLIC int NSQL_RECORD::insert(NSQL &ns, const char *table) { SSTRING req; req.setfromf ("insert into %s (",table); bool firstfield = true; for (int i=0; igetsqlname()); firstfield = false; } req.append (") values ("); NSQL_ENCODE enc; firstfield = true; for (int i=0; itstflag(NSQL_IS_PASSWORD)){ req.appendf ("password('%s')",enc.enc(f)); }else{ req.appendf ("'%s'",enc.enc(f)); } } req.append (")"); return sql_action (ns,req.get()); } PUBLIC int NSQL_RECORD::insert (const char *table) { return insert (*query_getdefaultdb(),table); } /* Load several fields from a table */ PUBLIC void NSQL_RECORD::set ( NSQL_FIELD_NAMES &fields, const char *row[]) // values { int nbf = fields.getnb(); for (int i=0; igetsqlname(),name)==0){ f->setfrom (row[i]); break; } } } } /* Empty every field variable */ PUBLIC void NSQL_RECORD::setempty() { for (int i=0; isetempty(); } } #ifdef TEST class RECORDA: public NSQL_RECORD{ public: NSQL_FIELD f1; NSQL_FIELD f2; public: RECORDA () : f1("f1"),f2("f2") { tb[0] = &f1; tb[1] = &f2; nbfield = 2; } }; #endif