gSAFE  1.3.8
datalib.cpp
1 /* gSAFE - LIB
2  general Sql dAtabase FrontEnd
3  http://hyperprog.com/gsafe/
4 
5  (C) 2005-2013 Peter Deak (hyper80@gmail.com)
6 
7  License: GPLv2 http://www.gnu.org/licenses/gpl-2.0.html
8 
9  datalib.cpp
10 */
11 
12 #include <string.h>
13 #include <ctype.h>
14 #include <stdlib.h>
15 #include <math.h>
16 
17 #include <QtCore>
18 #include <QtSql>
19 
20 #include "datalib.h"
21 #include "dconsole.h"
22 
23 #define COL_NONSQL 0
24 #define COL_SHOW 1
25 #define COL_KEY 2
26 #define COL_SERIAL 3
27 
28 QString errorLocal="Error";
29 
30 HRefreshAgent* HRefreshAgent::theone = NULL;
31 
32 void setErrorLocal(QString s)
33 {
34  errorLocal = s;
35 }
36 
37 void error(QString s)
38 {
39  dconsole_popup(errorLocal+" !",s);
40 }
41 
42 int getIndexOf(QStringList l,QString s)
43 {
44  int pos=0;
45  for ( QStringList::Iterator it = l.begin(); it != l.end(); ++it )
46  {
47  if(*it == s)
48  return pos;
49  ++pos;
50  }
51  return 0;
52 }
53 
54 QStringList * deep_copy_stringlist(QStringList *target,QStringList *source)
55 {
56  target->clear();
57  QStringList::Iterator i = source->begin();
58  while(i != source->end())
59  target->push_back(*i);
60  return target;
61 }
62 
63 QString convNationalToHtmlCodes(QString input)
64 {
65  QString r = input;
66 /*
67  r.replace("á","&aacute;",Qt::CaseSensitive);
68  r.replace("é","&eacute;",Qt::CaseSensitive);
69  r.replace("í","&iacute;",Qt::CaseSensitive);
70  r.replace("ó","&oacute;",Qt::CaseSensitive);
71  r.replace("ö","&ouml;",Qt::CaseSensitive);
72  r.replace("õ","&otilde;",Qt::CaseSensitive);
73  r.replace("ú","&uacute;",Qt::CaseSensitive);
74  r.replace("ü","&uuml;",Qt::CaseSensitive);
75  r.replace("û","&utilde;",Qt::CaseSensitive);
76 
77  r.replace("Á","&Aacute;",Qt::CaseSensitive);
78  r.replace("É","&Eacute;",Qt::CaseSensitive);
79  r.replace("Í","&Iacute;",Qt::CaseSensitive);
80  r.replace("Ó","&Oacute;",Qt::CaseSensitive);
81  r.replace("Ö","&Ouml;",Qt::CaseSensitive);
82  r.replace("Õ","&Otilde;",Qt::CaseSensitive);
83  r.replace("Ú","&Uacute;",Qt::CaseSensitive);
84  r.replace("Ü","&Uuml;",Qt::CaseSensitive);
85  r.replace("Û","&Utilde;",Qt::CaseSensitive);
86 */
87  r.replace("á","&#0225;",Qt::CaseSensitive);
88  r.replace("é","&#0233;",Qt::CaseSensitive);
89  r.replace("í","&#0237;",Qt::CaseSensitive);
90  r.replace("ó","&#0243;",Qt::CaseSensitive);
91  r.replace("ö","&#0246;",Qt::CaseSensitive);
92  r.replace("õ","&#0337;",Qt::CaseSensitive);
93  r.replace("ú","&#0250;",Qt::CaseSensitive);
94  r.replace("ü","&#0252;",Qt::CaseSensitive);
95  r.replace("û","&#0369;",Qt::CaseSensitive);
96 
97  r.replace("Á","&#0193;",Qt::CaseSensitive);
98  r.replace("É","&#0201;",Qt::CaseSensitive);
99  r.replace("Í","&#0205;",Qt::CaseSensitive);
100  r.replace("Ó","&#0211;",Qt::CaseSensitive);
101  r.replace("Ö","&#0214;",Qt::CaseSensitive);
102  r.replace("Õ","&#0336;",Qt::CaseSensitive);
103  r.replace("Ú","&#0218;",Qt::CaseSensitive);
104  r.replace("Ü","&#0220;",Qt::CaseSensitive);
105  r.replace("Û","&#0368;",Qt::CaseSensitive);
106  return r;
107 }
108 
109 int my_dtoa(double v,char *buffer,int bufflen,int min,int max,int group)
110 {
111  int digitnum;
112  int i,forlength;
113  int length=0; //the currnt filled length of the buffer
114 
115  char digit;
116  char *str = buffer;
117 
118  unsigned long int i_ip,i_fp,idigit_value;
119  double ip,fp;
120 
121  bufflen -= 2; //decrease bufflen value, to avoid decreasing in every if
122 
123  if(isnan(v))
124  {
125  if(bufflen < 4)
126  return 1;
127  strcpy(str,"NaN");
128  return 0;
129  }
130  if(isinf(v))
131  {
132  if(bufflen < 4)
133  return 1;
134  strcpy(str,"Inf");
135  return 0;
136  }
137 
138  //split the number to integer and fractional part.
139  fp = fabs(modf(v,&ip));
140  ip = fabs(ip);
141  if(fp != 0.0)
142  {
143  fp *= pow(10.0,max);
144  fp = floor(fp + 0.5);
145  }
146  i_ip=ip;
147  i_fp=fp;
148 
149  //If the original (rounded) number is negative put the sign to front
150  v *= pow(10.0,max);
151  v = floor(v + 0.5);
152  if (v < 0)
153  {
154  *(str++) = '-';
155  ++length;
156  v = -v;
157  }
158 
159  //Generate integer part (from i_ip)
160  idigit_value = 1;
161  digitnum = 1;
162  while(idigit_value*10 <= i_ip)
163  {
164  idigit_value *= 10;
165  ++digitnum;
166  }
167  forlength=0;
168  while(idigit_value >= 1)
169  {
170  //put grouping space if set
171  if(group && forlength != 0 && digitnum % 3 == 0)
172  {
173  *(str++) = ' ';
174  ++length;
175  if(length >= bufflen)
176  {
177  *(str) = '\0';
178  return 1;
179  }
180  }
181 
182  digit = static_cast<char>((i_ip - i_ip%idigit_value) / idigit_value);
183  i_ip = i_ip%idigit_value;
184 
185  *(str++) = '0' + digit%10;
186  ++length;
187  --digitnum;
188  ++forlength;
189  idigit_value /= 10;
190 
191  if(length >= bufflen)
192  {
193  *(str) = '\0';
194  return 1;
195  }
196  }
197 
198  //Generate fractional part (from i_fp)
199  digitnum=0;
200  if( i_fp > 0 )
201  {
202  *(str++) = '.';
203  ++length;
204 
205  idigit_value = 1;
206  for(i=0;i<max-1;++i)
207  idigit_value *= 10;
208 
209  while (idigit_value >= 1)
210  {
211  if(group && digitnum && digitnum%3 == 0)
212  {
213  *(str++) = ' ';
214  ++length;
215  if(length >= bufflen)
216  {
217  *(str) = '\0';
218  return 1;
219  }
220  }
221 
222  digit = static_cast<char>((i_fp - i_fp%idigit_value) / idigit_value);
223  i_fp = i_fp%idigit_value;
224 
225  *(str++) = '0' + digit%10;
226  ++length;
227  ++digitnum;
228  idigit_value /= 10;
229 
230  if(length >= bufflen)
231  {
232  *(str) = '\0';
233  return 1;
234  }
235 
236  if(digitnum >= min && i_fp == 0)
237  break;
238  }
239  }
240  else
241  { //the original number was an integer, so we fill the minimal fractional part with zeros
242  if(min > 0)
243  {
244  *(str++) = '.';
245  ++length;
246  for(digitnum=0;digitnum<min;)
247  {
248  if(group && digitnum && digitnum%3 == 0)
249  {
250  *(str++) = ' ';
251  ++length;
252  if(length >= bufflen)
253  {
254  *(str) = '\0';
255  return 1;
256  }
257  }
258  *(str++) = '0';
259  ++length;
260  ++digitnum;
261  if(length >= bufflen)
262  {
263  *(str) = '\0';
264  return 1;
265  }
266  }
267  }
268  }
269  *str = '\0';
270  return 0;
271 }
272 
273 QString doubleToQString(double val,int min,int max,int group)
274 {
275 
276  //return QVariant(QString("%1%2").arg(v.toDouble(),0,'f',2).arg(tailstr.isEmpty() ? "" : (" "+tailstr)));
277  //return QVariant(QString("").sprintf("%.2f",v.toDouble()) + (tailstr.isEmpty() ? "" : (" "+tailstr)));
278 
279  QString v;
280  char buffer[128];
281  my_dtoa(val,buffer,128,min,max,group);
282  v = buffer;
283  return v;
284 }
285 
286 double dRound(double val,int mode)
287 {
288  double intpart = floor(val);
289  if(mode == ROUND_FLOOR)
290  return intpart;
291  if( (val-intpart) >= 0.5 )
292  return (intpart + 1.0);
293  return intpart;
294 }
295 
300 {
301  indent_pos = 0;
302  output_stream = NULL;
303  node_stack = new QStack<QString>();
304  codec_name = "";
305  tc = NULL;
306  no_recode = true;
307 }
308 
310 {
311  output_stream = NULL;
312  delete node_stack;
313 }
314 
315 QTextStream * HXmlWriter::getStream(void)
316 {
317  return output_stream;
318 }
319 
320 void HXmlWriter::setStream(QTextStream *oo)
321 {
322  output_stream = oo;
323 }
324 
325 void HXmlWriter::putHead(QString enc,bool nullhead)
326 {
327  QString visible_enc=enc;
328 
329  if(!enc.isEmpty())
330  {
331  if(enc == "CP1250") visible_enc="Windows-1250";
332  if(enc == "UTF-8") visible_enc="UTF-8";
333 
334  if(!nullhead)
335  {
336  (*output_stream) << "<?xml version=\"1.0\" encoding=\"" + visible_enc + "\"?>\r\n";
337  }
338  codec_name = enc;
339  tc = QTextCodec::codecForName(codec_name.toLocal8Bit().constData());
340  no_recode = false;
341  }
342 
343 }
344 
345 void HXmlWriter::eatData(QString d)
346 {
347  if(no_recode)
348  (*output_stream) << d;
349  else
350  (*output_stream) << recodeData(d);
351 }
352 
353 void HXmlWriter::beginNode(QString n,QString parameters)
354 {
355  if(!parameters.isEmpty())
356  parameters.prepend(" ");
357  eatData( QString().fill('\t',indent_pos++) + "<" + n + parameters + ">\r\n" );
358  node_stack->push(n);
359 }
360 
362 {
363  eatData( QString().fill('\t',--indent_pos) + "</" + node_stack->pop() + ">\r\n" );
364 }
365 
366 void HXmlWriter::putCData(QString n,QString data,QString parameters)
367 {
368  if(!parameters.isEmpty())
369  parameters.prepend(" ");
370 
371  eatData( QString().fill('\t',indent_pos) + "<" + n + parameters + ">" + data + "</" + n + ">\r\n");
372 }
373 
374 void HXmlWriter::putEmptyNode(QString n,QString parameters)
375 {
376  if(!parameters.isEmpty())
377  parameters.prepend(" ");
378 
379  eatData( QString().fill('\t',indent_pos) + "<" + n + parameters +"/>\r\n" );
380 }
381 
382 void HXmlWriter::putRawData(QString data)
383 {
384  eatData( QString().fill('\t',indent_pos) + recodeDataNoconvert(data) );
385 }
386 
387 QByteArray HXmlWriter::recodeData(QString d)
388 {
389  return recodeDataNoconvert(d.replace("&","&amp;"));
390 }
391 
392 QByteArray HXmlWriter::recodeDataNoconvert(QString d)
393 {
394  QByteArray rv;
395 
396  if(no_recode)
397  rv = d.toLocal8Bit();
398  else
399  rv = tc->fromUnicode(d);
400  return rv;
401 }
402 
407 {
408  if(theone != NULL)
409  {
410  error("Critical Error: The \"HRefreshAgent\" object must be only one instance!");
411  return;
412  }
413  sdebug("HRefreshAgent initialized...");
414  theone = this;
415 }
416 
417 HRefreshAgent::~HRefreshAgent(void)
418 {
419  theone = NULL;
420 }
421 
422 void HRefreshAgent::notify(QString tblname)
423 {
424  if(theone == NULL)
425  {
426  error("Critical Error: The \"HRefreshAgent\" class is uninitialized!");
427  return;
428  }
429 
430  theone->internalNotify(tblname);
431 }
432 
433 int HRefreshAgent::notifySlot(QString tblname)
434 {
435  theone->internalNotify(tblname);
436  return 0;
437 }
438 
439 void HRefreshAgent::internalNotify(QString tblname)
440 {
441  sdebug("*** HRefreshAgent::internalNotify ***");
442 
443  emit getnotify(tblname);
444 }
445 
448 
449 #ifdef MODULE_REFRESHAGENT_NETSERVER
450 
451 bool HRefreshAgentNetserver::inNotify = false;
452 HRefreshAgentNetserver * HRefreshAgentNetserver::theone = NULL;
453 
455 {
456  tcpServer = NULL;
457  if(theone != NULL)
458  {
459  error("Critical Error: The \"HRefreshAgentNetserver\" object must be only one instance!");
460  return;
461  }
462  sdebug("[HRefreshAgentNetserver] Initializing...");
463  theone = this;
464 
465  //create a tcpserver
466  tcpServer = new QTcpServer(this);
467  if (!tcpServer->listen(QHostAddress::Any,REFRESHAGENT_TCPPORT))
468  {
469  error(QString("Unable to start tcpserver: %1.")
470  .arg(tcpServer->errorString()));
471  }
472 
473  connect(tcpServer,SIGNAL(newConnection()),this,SLOT(newConn()));
474  sdebug(QString("[HRefreshAgentNetserver] Listening on port %1...").arg(tcpServer->serverPort()));
475  laddr = "No ip";
476 }
477 
479 {
480  if(theone->tcpServer == NULL || !theone->tcpServer->isListening())
481  return false;
482  return true;
483 }
484 
486 {
487  if(theone->tcpServer != NULL && theone->tcpServer->isListening())
488  {
489  return theone->laddr;
490  }
491  return QString("Not listening");
492 }
493 
494 int HRefreshAgentNetserver::newConn(void)
495 {
496  sdebug("*** HRefreshAgentNetserver::newConn ***");
497 
498  QTcpSocket *socket;
499  QByteArray block;
500  QTextStream inout(&block,QIODevice::ReadWrite);
501 
502  if(!tcpServer->hasPendingConnections())
503  {
504  sdebug("*** HRefreshAgentNetserver::newConn *** END ERROR");
505  return 1;
506  }
507 
508  socket = tcpServer->nextPendingConnection();
509 
510  laddr = socket->localAddress().toString();
511 
512  inout << "ConnectOk" <<endl;
513  socket->write(block);
514  connect(socket,SIGNAL(readyRead()),this,SLOT(request()));
515  connect(socket,SIGNAL(disconnected()),this,SLOT(clientLeaving()));
516 
517  tcps_list.push_back(socket);
518 
519  sdebug(QString("[HRefreshAgentNetserver] Client connected %1... (have %2 client)")
520  .arg(socket->peerAddress().toString())
521  .arg(tcps_list.size()));
522 
523  sdebug("*** HRefreshAgentNetserver::newConn *** END");
524  return 0;
525 }
526 
527 int HRefreshAgentNetserver::request(void)
528 {
529  int num,sender;
530  QString str;
531  sdebug("*** HRefreshAgentNetserver::request ***");
532  QTcpSocket *socket;
533 
534  QByteArray block;
535  QTextStream inout(&block,QIODevice::ReadWrite);
536 
537  QList<QTcpSocket *>::iterator is;
538 
539  str = "";
540  is = tcps_list.begin();
541  sender = -1;
542  num = 0;
543  while(is != tcps_list.end())
544  {
545  socket = *is;
546  if(socket != NULL && socket->isValid())
547  if(socket->state() == QAbstractSocket::ConnectedState)
548  if(socket->bytesAvailable() != 0)
549  {
550  block = socket->readAll();
551  inout >> str;
552  sdebug(QString("[HRefreshAgentNetserver] Data arrived: \"%1\" from %2 !")
553  .arg(str)
554  .arg(socket->peerAddress().toString()));
555  sender = num;
556  if(!str.isEmpty() && (str == "close" ||
557  str == "quit" ||
558  str == "exit" ||
559  str == "disconnect"))
560  {
561  socket->flush();
562  socket->disconnectFromHost();
563  str = "";
564  return 0;
565  }
566  }
567  ++is;
568  ++num;
569  }
570 
571  if(!str.isEmpty() && str.startsWith("<") && str.endsWith(">") )
572  {
573  int length = str.length();
574  str = str.mid(1,length-2);
575  action(str,sender);
576  }
577 
578  sdebug("*** HRefreshAgentNetserver::request *** END");
579  return 0;
580 }
581 
582 int HRefreshAgentNetserver::action(QString name,int sendernum)
583 {
584  int num;
585  QByteArray block;
586  QTextStream inout(&block,QIODevice::ReadWrite);
587 
588  if(inNotify)
589  return 0;
590  inNotify = true;
591 
592  sdebug("[HRefreshAgentNetserver] NetServer Notify: "+name);
593 
594  inout << "<" << name << ">" << endl;
595  num = 0;
596  QList<QTcpSocket *>::iterator is;
597  is = tcps_list.begin();
598  while(is != tcps_list.end())
599  {
600  if((*is)->state() == QAbstractSocket::ConnectedState)
601  if(num != sendernum)
602  (*is)->write(block);
603  ++is;
604  ++num;
605  }
606  inNotify = false;
607  return 0;
608 }
609 
610 int HRefreshAgentNetserver::clientLeaving(void)
611 {
612  QList<QTcpSocket *>::iterator is;
613  is = tcps_list.begin();
614  while(is != tcps_list.end())
615  {
616  if((*is)->state() == QAbstractSocket::UnconnectedState ||
617  (*is)->state() == QAbstractSocket::ClosingState )
618  {
619  tcps_list.removeAll(*is);
620  sdebug(QString("[HRefreshAgentNetserver] Client leaving... (have %1 client)").arg(tcps_list.size()));
621  return 0;
622  }
623  ++is;
624  }
625  return 0;
626 }
627 
629 {
630  QList<QTcpSocket *>::iterator is;
631 
632  is = tcps_list.begin();
633  while(is != tcps_list.end())
634  {
635  (*is)->close();
636  ++is;
637  }
638  tcps_list.clear();
639 
640  tcpServer->close();
641  delete tcpServer;
642 
643  theone = NULL;
644 }
645 
646 #endif
647 
650 
651 #ifdef MODULE_REFRESHAGENT_NETCLIENT
652 
653 bool HRefreshAgentNetclient::inNotify = false;
654 HRefreshAgentNetclient * HRefreshAgentNetclient::theone = NULL;
655 
657 {
658  if(theone != NULL)
659  {
660  error("Critical Error: The \"HRefreshAgentNetclient\" object must be only one instance!");
661  return;
662  }
663  sdebug("HRefreshAgentNetclient initialized...");
664  theone = this;
665 
666  //code here...
667  socket = new QTcpSocket(this);
668  socket->connectToHost(server_ip,REFRESHAGENT_TCPPORT,QIODevice::ReadWrite);
669 
670  connect(socket,SIGNAL(readyRead()),this,SLOT(request()));
671  connect(socket,SIGNAL(disconnected()),this,SLOT(serverClosing()));
672  connect(HRefreshAgent::getNotifyAgent(),SIGNAL(getnotify(QString)),this,SLOT(action(QString)));
673  c_ok = false;
674 }
675 
677 {
678  if(theone->socket->state() == QAbstractSocket::ConnectedState && theone->c_ok)
679  return true;
680  return false;
681 }
682 
684 {
685  socket->close();
686  delete socket;
687  c_ok = false;
688  theone = NULL;
689 }
690 
691 int HRefreshAgentNetclient::request(void)
692 {
693  QString str;
694 
695  QByteArray block;
696  QTextStream inout(&block,QIODevice::ReadWrite);
697 
698  str = "";
699  if(socket->state() == QAbstractSocket::ConnectedState && socket->bytesAvailable() != 0)
700  {
701  block = socket->readAll();
702  inout >> str;
703  sdebug(QString("[HRefreshAgentNetclient] Data arrived: \"%1\" ").arg(str));
704  if(!c_ok && str == "ConnectOk")
705  {
706  sdebug(QString("[HRefreshAgentNetclient] Connection established."));
707  c_ok = true;
708  str = "";
709  emit connectionEstablished();
710  }
711  }
712 
713  if(c_ok && !str.isEmpty() && str.startsWith("<") && str.endsWith(">"))
714  {
715  int length = str.length();
716  str = str.mid(1,length-2);
717  sdebug(QString("[HRefreshAgentNetclient] HRefreshAgent->notify: \"%1\"").arg(str));
718  inNotify = true;
720  inNotify = false;
721  }
722  return 0;
723 }
724 
725 int HRefreshAgentNetclient::serverClosing(void)
726 {
727  socket->close();
728  c_ok = false;
729  emit connectionLost();
730  sdebug("[HRefreshAgentNetclient] The server closed the connection.");
731  return 0;
732 }
733 
734 int HRefreshAgentNetclient::action(QString name)
735 {
736  QByteArray block;
737  QTextStream inout(&block,QIODevice::ReadWrite);
738 
739  if(!c_ok)
740  return 0;
741 
742  if(inNotify)
743  return 0;
744  inNotify = true;
745 
746  sdebug("[HRefreshAgentNetserver] Send Notify: "+name);
747 
748  inout << "<" << name << ">" << endl;
749  QList<QTcpSocket *>::iterator is;
750 
751  if(socket->state() == QAbstractSocket::ConnectedState)
752  socket->write(block);
753 
754  inNotify = false;
755  return 0;
756 }
757 
758 #endif
759 
763 
765 {
766  whoami = "HBase";
767 }
768 
770 {
771 
772 }
773 
774 QString HBase::getWhoami(void)
775 {
776  return whoami;
777 }
778 
782 
783 HSqlSingleInterface::HSqlSingleInterface(void)
784 {
785 
786 }
787 
788 HSqlSingleInterface_SqliteWin::HSqlSingleInterface_SqliteWin(void)
789 {
790  hsqli_currentModeName = "QtSqlite_Win";
791  hsqli_usequerysize = false;
792  hsqli_booleantypename = "BOOLEAN";
793  hsqli_truevalue = "1";
794  hsqli_falsevalue = "0";
797  hsqli_hkeyconstraint = "PRIMARY KEY AUTOINCREMENT";
804  hsqli_numbertypename = "INTEGER";
805  hsqli_floattypename = "REAL";
806  hsqli_hastimestamptype = false;
807  hsqli_hasdatetype = false;
808  hsqli_timestamptypename = "VARCHAR"; //varchar - can be converted to date by functions
809  hsqli_datetypename = "VARCHAR"; //varchar - can be converted to date by functions
810  hsqli_timestampnowvalue = "datetime(current_timestamp,\'localtime\')";
812  hsqli_appendsqlerrormsg = false;
814 }
815 
816 HSqlSingleInterface_SqliteLin::HSqlSingleInterface_SqliteLin(void)
818 {
819  hsqli_currentModeName = "QtSqlite_Lin";
820  hsqli_usequerysize = false;
821  hsqli_booleantypename = "BOOLEAN";
822  hsqli_truevalue = "1";
823  hsqli_falsevalue = "0";
824  hsqli_hkeytype_varchar_int = false;
825  hsqli_varcharhkeylength = 15;
826  hsqli_hkeyconstraint = "PRIMARY KEY AUTOINCREMENT";
827  hsqli_need_create_sequence_before=false;
828  hsqli_sequence_creation_string="";
829  hsqli_charhash_varcharlength = 5;
830  hsqli_smalltext_varcharlength = 256;
831  hsqli_largetext_sizemustdefine = false;
832  hsqli_largetext_varcharlength = 2048;
833  hsqli_numbertypename = "INTEGER";
834  hsqli_floattypename = "REAL";
835  hsqli_hastimestamptype = false;
836  hsqli_hasdatetype = false;
837  hsqli_timestamptypename = "VARCHAR"; //varchar - can be converted to date by functions
838  hsqli_datetypename = "VARCHAR"; //varchar - can be converted to date by functions
839  hsqli_timestampnowvalue = "datetime(current_timestamp,\'localtime\')";
840  hsqli_sqlchooseconstraint = false;
841  hsqli_appendsqlerrormsg = false;
842  hsqli_hackconvert_stdsqlconcatenation_to_local = 0;
843 }
844 
845 HSqlSingleInterface_PsqlOdbcWin::HSqlSingleInterface_PsqlOdbcWin(void)
847 {
848  hsqli_currentModeName = "PostgreSQL_WinOdbc";
849  hsqli_usequerysize = true;
850  hsqli_booleantypename = "BOOLEAN";
851  hsqli_truevalue = "TRUE";
852  hsqli_falsevalue = "FALSE";
853  hsqli_hkeytype_varchar_int = true;
854  hsqli_varcharhkeylength = 15;
855  hsqli_hkeyconstraint = "PRIMARY KEY DEFAULT(nextval(\'_SEQNAME_\'))";
856  hsqli_need_create_sequence_before=true;
857  hsqli_sequence_creation_string="CREATE SEQUENCE _SEQNAME_ INCREMENT 1 START 1";
858  hsqli_charhash_varcharlength = 5;
859  hsqli_smalltext_varcharlength = 512;
860  hsqli_largetext_sizemustdefine = false;
861  hsqli_largetext_varcharlength = 4096;
862  hsqli_numbertypename = "NUMERIC";
863  hsqli_floattypename = "NUMERIC";
864  hsqli_hastimestamptype = true;
865  hsqli_hasdatetype = true;
866  hsqli_timestamptypename = "TIMESTAMP";
867  hsqli_datetypename = "DATE";
868  hsqli_timestampnowvalue = "now()";
869  hsqli_sqlchooseconstraint = true;
870  hsqli_appendsqlerrormsg = false;
871  hsqli_hackconvert_stdsqlconcatenation_to_local = 0;
872 }
873 
874 HSqlSingleInterface_PostgresqlLin::HSqlSingleInterface_PostgresqlLin(void)
876 {
877  hsqli_currentModeName = "PostgreSQL_LinPsql";
878  hsqli_usequerysize = false;
879  hsqli_booleantypename = "BOOLEAN";
880  hsqli_truevalue = "TRUE";
881  hsqli_falsevalue = "FALSE";
882  hsqli_hkeytype_varchar_int = true;
883  hsqli_varcharhkeylength = 15;
884  hsqli_hkeyconstraint = "PRIMARY KEY DEFAULT(nextval(\'_SEQNAME_\'))";
885  hsqli_need_create_sequence_before=true;
886  hsqli_sequence_creation_string="CREATE SEQUENCE _SEQNAME_ INCREMENT 1 START 1";
887  hsqli_charhash_varcharlength = 5;
888  hsqli_smalltext_varcharlength = 512;
889  hsqli_largetext_sizemustdefine = false;
890  hsqli_largetext_varcharlength = 4096;
891  hsqli_numbertypename = "NUMERIC";
892  hsqli_floattypename = "NUMERIC";
893  hsqli_hastimestamptype = true;
894  hsqli_hasdatetype = true;
895  hsqli_timestamptypename = "TIMESTAMP";
896  hsqli_datetypename = "DATE";
897  hsqli_timestampnowvalue = "now()";
898  hsqli_sqlchooseconstraint = true;
899  hsqli_appendsqlerrormsg = false;
900  hsqli_hackconvert_stdsqlconcatenation_to_local = 0;
901 }
902 
903 HSqlSingleInterface_MysqlOdbcWin::HSqlSingleInterface_MysqlOdbcWin(void)
905 {
906  hsqli_currentModeName = "MYSQL_WinOdbc";
907  hsqli_usequerysize = true;
908  hsqli_booleantypename = "TINYINT(1)";
909  hsqli_truevalue = "1";
910  hsqli_falsevalue = "0";
911  hsqli_hkeytype_varchar_int = false;
912  hsqli_varcharhkeylength = 15;
913  hsqli_hkeyconstraint = "PRIMARY KEY NOT NULL AUTO_INCREMENT";
914  hsqli_need_create_sequence_before=false;
915  hsqli_sequence_creation_string="";
916  hsqli_charhash_varcharlength = 5;
917  hsqli_smalltext_varcharlength = 512;
918  hsqli_largetext_sizemustdefine = true;
919  hsqli_largetext_varcharlength = 4096;
920  hsqli_numbertypename = "INTEGER";
921  hsqli_floattypename = "DOUBLE";
922  hsqli_hastimestamptype = true;
923  hsqli_hasdatetype = true;
924  hsqli_timestamptypename = "TIMESTAMP";
925  hsqli_datetypename = "DATE";
926  hsqli_timestampnowvalue = "now()";
927  hsqli_sqlchooseconstraint = true;
928  hsqli_appendsqlerrormsg = true;
929  hsqli_hackconvert_stdsqlconcatenation_to_local = 1;
930 }
931 
932 HSqlSingleInterface_MysqlLin::HSqlSingleInterface_MysqlLin(void)
934 {
935  hsqli_currentModeName = "MYSQL_LinQt";
936  hsqli_usequerysize = false;
937  hsqli_booleantypename = "TINYINT(1)";
938  hsqli_truevalue = "1";
939  hsqli_falsevalue = "0";
940  hsqli_hkeytype_varchar_int = false;
941  hsqli_varcharhkeylength = 15;
942  hsqli_hkeyconstraint = "PRIMARY KEY NOT NULL AUTO_INCREMENT";
943  hsqli_need_create_sequence_before=false;
944  hsqli_sequence_creation_string="";
945  hsqli_charhash_varcharlength = 5;
946  hsqli_smalltext_varcharlength = 512;
947  hsqli_largetext_sizemustdefine = true;
948  hsqli_largetext_varcharlength = 4096;
949  hsqli_numbertypename = "INTEGER";
950  hsqli_floattypename = "DOUBLE";
951  hsqli_hastimestamptype = true;
952  hsqli_hasdatetype = true;
953  hsqli_timestamptypename = "TIMESTAMP";
954  hsqli_datetypename = "DATE";
955  hsqli_timestampnowvalue = "now()";
956  hsqli_sqlchooseconstraint = true;
957  hsqli_appendsqlerrormsg = true;
958  hsqli_hackconvert_stdsqlconcatenation_to_local = 1;
959 }
960 
961 /* Miscrosoft MSSQL Express scheme. Not tested:
962 HSqlSingleInterface_MssqlWin::HSqlSingleInterface_MssqlWin(void)
963 :HSqlSingleInterface()
964 {
965  hsqli_currentModeName = "MSSQL_WinOdbc";
966  hsqli_usequerysize = false;
967  hsqli_booleantypename = "BIT";
968  hsqli_truevalue = "1";
969  hsqli_falsevalue = "0";
970  hsqli_hkeytype_varchar_int = false;
971  hsqli_varcharhkeylength = 15;
972  hsqli_hkeyconstraint = "INT IDENTITY PRIMARY KEY";
973  hsqli_need_create_sequence_before=false;
974  hsqli_sequence_creation_string="";
975  hsqli_charhash_varcharlength = 5;
976  hsqli_smalltext_varcharlength = 512;
977  hsqli_largetext_sizemustdefine = true;
978  hsqli_largetext_varcharlength = 4096;
979  hsqli_numbertypename = "INT";
980  hsqli_floattypename = "DOUBLE";
981  hsqli_hastimestamptype = true;
982  hsqli_hasdatetype = true;
983  hsqli_timestamptypename = "TIMESTAMP";
984  hsqli_datetypename = "DATE";
985  hsqli_timestampnowvalue = "CURRENT_TIMESTAMP";
986  hsqli_sqlchooseconstraint = false;
987  hsqli_appendsqlerrormsg = true;
988  hsqli_hackconvert_stdsqlconcatenation_to_local = 2;
989 }
990 */
991 
992 bool HSqlInterface::isset = false;
993 int HSqlInterface::currentDefaultInterface = 0;
994 int HSqlInterface::defaultDefaultInterface = -1;
995 HSqlSingleInterface * HSqlInterface::interfaces[MAX_SQL_INTERFACES];
996 
998 {
999  whoami = "HSqlInterface";
1000  if(!isset)
1001  {
1002  error("HSqlInterface: Define a sql interface model with HSqlInterface::setSqlMode() !");
1003  QCoreApplication::quit();
1004  }
1005 
1006  myinterface = currentDefaultInterface;
1007 }
1008 
1010 {
1011 }
1012 
1013 void HSqlInterface::setSqlMode(QString modename)
1014 {
1015  int i,newdefmode;
1016  if(!isset) //first run ever
1017  {
1018  for(i=0;i<MAX_SQL_INTERFACES;++i)
1019  interfaces[i] = NULL;
1020  interfaces[0] = new HSqlSingleInterface_SqliteWin();
1021  interfaces[1] = new HSqlSingleInterface_SqliteLin();
1022  interfaces[2] = new HSqlSingleInterface_PsqlOdbcWin();
1023  interfaces[3] = new HSqlSingleInterface_PostgresqlLin();
1024  interfaces[4] = new HSqlSingleInterface_MysqlOdbcWin();
1025  interfaces[5] = new HSqlSingleInterface_MysqlLin();
1026  isset = true;
1027  }
1028 
1029  newdefmode = -1;
1030  for(i=0;i<MAX_SQL_INTERFACES;++i)
1031  if(interfaces[i] != NULL && interfaces[i]->hsqli_currentModeName == modename)
1032  newdefmode = i;
1033 
1034  if(newdefmode == -1)
1035  {
1036  error("HSqlInterface::setSqlMode : Unknown sql mode!");
1037  QCoreApplication::quit();
1038  return;
1039  }
1040 
1041  if(defaultDefaultInterface == -1)
1042  defaultDefaultInterface = newdefmode;
1043 
1044  currentDefaultInterface = newdefmode;
1045 }
1046 
1048 {
1049  if(defaultDefaultInterface != -1)
1050  currentDefaultInterface = defaultDefaultInterface;
1051 }
1052 
1054 {
1055  return interfaces[currentDefaultInterface];
1056 
1057 }
1058 
1060 {
1061  int i;
1062  for(i=0;i<MAX_SQL_INTERFACES;++i)
1063  if(interfaces[i] != NULL && interfaces[i]->hsqli_currentModeName == interfacename)
1064  return interfaces[i];
1065  return NULL;
1066 }
1067 
1069 {
1070  if(interfaces[myinterface]->hsqli_hackconvert_stdsqlconcatenation_to_local == 0)
1071  return string;
1072 
1073  if(interfaces[myinterface]->hsqli_hackconvert_stdsqlconcatenation_to_local == 1)
1074  {
1075  QString newconc,inner;
1076  inner = string;
1077  inner.replace("||",",");
1078  newconc = "CONCAT("+inner+")";
1079  return newconc;
1080  }
1081  if(interfaces[myinterface]->hsqli_hackconvert_stdsqlconcatenation_to_local == 2)
1082  {
1083  QString newconc;
1084  newconc = string;
1085  newconc.replace("||","+");
1086  return newconc;
1087  }
1088  return "Error, uncnown string concatenation method!";
1089 }
1090 
1091 QString HSqlInterface::sqlConcatenateToLocal(QStringList fields)
1092 {
1093  QString result;
1094  QString sep="";
1095  QStringList::iterator i;
1096 
1097  switch(interfaces[myinterface]->hsqli_hackconvert_stdsqlconcatenation_to_local)
1098  {
1099  case 0: sep = "||"; break;
1100  case 1: sep = ","; break;
1101  case 2: sep = "+"; break;
1102  }
1103 
1104  int fieldnum=0;
1105  for(i=fields.begin();i!=fields.end();++i)
1106  {
1107  result += (fieldnum == 0 ? "" : sep) + *i;
1108  ++fieldnum;
1109  }
1110 
1111  if(interfaces[myinterface]->hsqli_hackconvert_stdsqlconcatenation_to_local == 1)
1112  result = "CONCAT("+result+")";
1113 
1114  return result;
1115 }
1116 
1120 int HSqlHandler::trans = 0;
1121 bool HSqlHandler::globalnew_nodefault = false;
1122 QString HSqlHandler::globalnew_current_database = "";
1123 
1124 void HSqlHandler::setCustomSqlDatabase(QString databaseName,QString sqlinterfacename)
1125 {
1126  globalnew_nodefault = true;
1127  globalnew_current_database = databaseName;
1128  if(!sqlinterfacename.isEmpty())
1129  HSqlInterface::setSqlMode(sqlinterfacename);
1130 }
1131 
1133 {
1134  globalnew_nodefault = false;
1135  globalnew_current_database = "";
1137 }
1138 
1140 {
1141  whoami = "HSqlHandler";
1142 
1143  query_error_occured = false;
1144  nodefault = globalnew_nodefault;
1145  current_database = globalnew_current_database;
1146 }
1147 
1149 {
1150  if(trans != 0) //nem ok
1151  {
1152  emit errorSignal("Error: I have some uncommitted/rollback-ed transaction!");
1153  }
1154 }
1155 
1157 {
1159 
1160  nodefault = other->nodefault;
1161  current_database = other->current_database;
1162 }
1163 
1164 QSqlDatabase HSqlHandler::workDB(void)
1165 {
1166  if(!nodefault)
1167  return QSqlDatabase::database();
1168 
1169  QSqlDatabase db = QSqlDatabase::database(current_database);
1170  if(!db.isValid() || !db.isOpen())
1171  {
1172  query_error_occured = true;
1173  sdebug(QString("Cannot load the given database by name \"%1\"!").arg(current_database));
1174  emit errorSignal(QString("Cannot load the given database by name \"%1\"!").arg(current_database));
1175  return QSqlDatabase::database();
1176  }
1177  return db;
1178 }
1179 
1181 {
1182  return new QSqlQuery(workDB());
1183 }
1184 
1186 {
1187  sdebug(QString("Begin transaction: %1").arg(trans));
1188  trans++;
1189  QSqlDatabase db = workDB();
1190  if(db.transaction()) return; // OK!
1191 
1192  emit errorSignal("Cannot begin the SQL transaction!");
1193 }
1194 
1196 {
1197 
1198  if(trans<=0)
1199  emit errorSignal("BIG Warning: Called commit without begin a transaction!");
1200  trans--;
1201  sdebug(QString("Commit transaction: %1").arg(trans));
1202 
1203  QSqlDatabase db = workDB();
1204  if(db.commit()) return; // OK!
1205 
1206  emit errorSignal("Cannot COMMIT the SQL transaction!");
1207 
1208 }
1209 
1211 {
1212 
1213  if(trans<=0)
1214  emit errorSignal("BIG Warning: Called rollback without begin a transaction!");
1215  trans--;
1216  sdebug(QString("Rollback transaction: %1").arg(trans));
1217 
1218  QSqlDatabase db = workDB();
1219  if(db.rollback()) return; // OK!
1220 
1221  emit errorSignal("Cannot ROLLBACK the SQL transaction!");
1222 }
1223 
1224 
1225 bool HSqlHandler::submit0ResultQuery(QString q,QString err,bool tdisabled)
1226 {
1227  query_error_occured = false;
1228 
1229  if(!tdisabled)
1230  transaction();
1231 
1232  QSqlQuery *qi = allocateQSqlQuery();
1233 
1234  sqldebug(q);
1235  qi->prepare(q);
1236  qi->exec();
1237 
1238  if(qi->lastError().type() != QSqlError::NoError)
1239  {
1240  if(!tdisabled)
1241  rollback();
1242 
1243  QString errortext_new = err;
1244  if(myInterface()->hsqli_appendsqlerrormsg)
1245  {
1246  errortext_new += " (" + qi->lastError().text() + ") Sql was:"+q;
1247  }
1248  delete qi;
1249  emit errorSignal(errortext_new);
1250  query_error_occured = true;
1251  return true;
1252  }
1253  if(!tdisabled)
1254  commit();
1255 
1256  delete qi;
1257  return false;
1258 }
1259 
1260 QVariant HSqlHandler::submit1ResultQuery(QString q,QString err,bool tdisabled)
1261 {
1262  QVariant v;
1263 
1264  query_error_occured = false;
1265  v="";
1266  if(!tdisabled)
1267  transaction();
1268 
1269  QSqlQuery *qi = allocateQSqlQuery();
1270 
1271  sqldebug(q);
1272  qi->prepare(q);
1273  qi->exec();
1274 
1275  if( (myInterface()->hsqli_usequerysize && qi->numRowsAffected() != 1) ||
1276  ! qi->next() ||
1277  qi->lastError().type() != QSqlError::NoError )
1278  {
1279  if(!tdisabled)
1280  rollback();
1281 
1282  QString errortext_new = err;
1283  if(myInterface()->hsqli_appendsqlerrormsg)
1284  {
1285  errortext_new += " (" + qi->lastError().text() + ")";
1286  }
1287  delete qi;
1288  emit errorSignal(errortext_new);
1289  query_error_occured = true;
1290  return v;
1291  }
1292 
1293  v = qi->value(0);
1294 
1295  if(!tdisabled)
1296  commit();
1297 
1298  delete qi;
1299  return v;
1300 }
1301 
1302 HPlainDataMatrix* HSqlHandler::submitNResultQuery(int N,QString q,QString err,bool tdisabled)
1303 {
1304  int i;
1305  QList<QVariant> list;
1306  HPlainDataMatrix* dm=NULL;
1307 
1308  dm = new HPlainDataMatrix(N);
1309 
1310  query_error_occured = false;
1311  if(!tdisabled)
1312  transaction();
1313 
1314  QSqlQuery *qi = allocateQSqlQuery();
1315 
1316  sqldebug(q);
1317  qi->prepare(q);
1318  qi->exec();
1319  if((myInterface()->hsqli_usequerysize && qi->numRowsAffected() < 0) ||
1320  qi->lastError().type() != QSqlError::NoError )
1321  {
1322 
1323  if(!tdisabled)
1324  rollback();
1325 
1326  QString errortext_new = err;
1327  if(myInterface()->hsqli_appendsqlerrormsg)
1328  {
1329  errortext_new += " (" + qi->lastError().text() + ")";
1330  }
1331  delete qi;
1332  emit errorSignal(errortext_new);
1333  query_error_occured = true;
1334  delete dm;
1335  return NULL;
1336  }
1337 
1338  while(qi->next())
1339  {
1340  list.clear();
1341  for(i=0 ; i < qi->record().count() ; ++i)
1342  list.push_back( qi->value(i) );
1343  dm->addRow(list);
1344  }
1345 
1346  if(!tdisabled)
1347  commit();
1348 
1349  delete qi;
1350  return dm;
1351 }
1352 
1355 
1357 {
1358  int i;
1359  whoami = "HPlainDataMatrix";
1360  col_count = col;
1361 
1362  title = "";
1363  exp_title = "";
1364  htmlcss = "";
1365  hheader = new QString[col_count];
1366 
1367  printCellWrap = new bool[col_count];
1368  for(i = 0 ; i < col_count ; ++i)
1369  printCellWrap[i] = false;
1370 
1371  printMaxCellWidth = new int[col_count];
1372  for(i = 0 ; i < col_count ; ++i)
1373  printMaxCellWidth[i] = 0;
1374 
1375  keyfield = -2; //default. The HDispPlainDataMatrix can set the key
1376 }
1377 
1379 {
1380 
1381  delete[] hheader;
1382  while( !data.isEmpty())
1383  {
1384  delete[] data.first();
1385  data.pop_front();
1386  }
1387 }
1388 
1390 {
1391  int i;
1392 
1393  //Clear data fields
1394  while( !data.isEmpty())
1395  {
1396  delete[] data.first();
1397  data.pop_front();
1398  }
1399  data.clear();
1400  //Clear control data
1401  control.clear();
1402 
1403  //Reset iterators
1404  iter = data.begin() ;
1405  iter_ctrl = control.begin();
1406 
1407  //Reset other...
1408  for(i = 0 ; i < col_count ; ++i)
1409  printCellWrap[i] = false;
1410  for(i = 0 ; i < col_count ; ++i)
1411  printMaxCellWidth[i] = 0;
1412 
1413  keyfield = -2;
1414 }
1415 
1417 {
1418  int i;
1419 
1420  title = "";
1421  exp_title = "";
1422  htmlcss = "";
1423 
1424  for(i = 0 ; i < col_count ; ++i)
1425  hheader[i] = "";
1426 
1427  clearData();
1428 
1429 }
1430 
1432 {
1433  return col_count;
1434 }
1435 
1437 {
1438  return data.count();
1439 }
1440 
1441 void HPlainDataMatrix::setHeaderCell(int col,QString strdata)
1442 {
1443  if(col > col_count)
1444  return;
1445  hheader[col] = strdata;
1446 }
1447 
1448 void HPlainDataMatrix::setHeader(QList<QString> strlistdata)
1449 {
1450  int i;
1451  for(i = 0; i < col_count;++i)
1452  {
1453  if(i < strlistdata.count())
1454  hheader[i] = strlistdata.at(i);
1455  else
1456  hheader[i] = "";
1457  }
1458 }
1459 
1460 void HPlainDataMatrix::setHeader(QString d1,QString d2,QString d3
1461  ,QString d4,QString d5,QString d6
1462  ,QString d7,QString d8,QString d9
1463  ,QString d10,QString d11,QString d12
1464  ,QString d13,QString d14,QString d15)
1465 {
1466  int i;
1467  QString s;
1468  for(i = 0; i < col_count;++i)
1469  {
1470  switch(i)
1471  {
1472  case 0: s = d1; break;
1473  case 1: s = d2; break;
1474  case 2: s = d3; break;
1475  case 3: s = d4; break;
1476  case 4: s = d5; break;
1477  case 5: s = d6; break;
1478  case 6: s = d7; break;
1479  case 7: s = d8; break;
1480  case 8: s = d9; break;
1481  case 9: s = d10; break;
1482  case 10: s = d11; break;
1483  case 11: s = d12; break;
1484  case 12: s = d13; break;
1485  case 13: s = d14; break;
1486  case 14: s = d15; break;
1487  default: s = "NOTSUPPORTED"; break;
1488  }
1489  hheader[i] = s;
1490  }
1491 }
1492 
1493 
1494 
1496 {
1497  return hheader[col];
1498 }
1499 
1500 QList<QString> HPlainDataMatrix::getHeader(void)
1501 {
1502  int i;
1503  QList<QString> list;
1504  list.clear();
1505  for(i = 0; i < col_count;++i)
1506  {
1507  list.push_back(hheader[i]);
1508  }
1509  return list;
1510 }
1511 
1512 void HPlainDataMatrix::addRow(QList<QVariant> listdata,QString ctrl)
1513 {
1514  int i;
1515  QVariant *row;
1516 
1517  row = new QVariant[col_count];
1518  for(i = 0; i < col_count;++i)
1519  {
1520  if(i < listdata.count())
1521  row[i] = listdata.at(i);
1522  else
1523  row[i] = QVariant("");
1524  }
1525  data.push_back(row);
1526  control.push_back(ctrl);
1527 }
1528 
1530 {
1531  if(col_count != tail->columnCount())
1532  return;
1533  if(tail->rowCount() > 0)
1534  {
1535  tail->firstRow();
1536  do
1537  addRow( tail->currentRow(),tail->currentRowControl() );
1538  while(tail->nextRow());
1539  }
1540  return;
1541 }
1542 
1543 void HPlainDataMatrix::addRowStr(QList<QString> strlistdata,QString ctrl)
1544 {
1545  int i;
1546  QVariant *row;
1547 
1548  row = new QVariant[col_count];
1549  for(i = 0; i < col_count;++i)
1550  {
1551  if(i < strlistdata.count())
1552  row[i] = QVariant(strlistdata.at(i));
1553  else
1554  row[i] = QVariant("");
1555  }
1556  data.push_back(row);
1557  control.push_back(ctrl);
1558 }
1559 
1560 void HPlainDataMatrix::addRowStrCTRL(QString ctrl,QString d1,QString d2,QString d3
1561  ,QString d4,QString d5,QString d6
1562  ,QString d7,QString d8,QString d9
1563  ,QString d10,QString d11,QString d12
1564  ,QString d13,QString d14,QString d15)
1565 {
1566  int i;
1567  QVariant *row;
1568  QString s;
1569 
1570  row = new QVariant[col_count];
1571  for(i = 0; i < col_count;++i)
1572  {
1573  switch(i)
1574  {
1575  case 0: s = d1; break;
1576  case 1: s = d2; break;
1577  case 2: s = d3; break;
1578  case 3: s = d4; break;
1579  case 4: s = d5; break;
1580  case 5: s = d6; break;
1581  case 6: s = d7; break;
1582  case 7: s = d8; break;
1583  case 8: s = d9; break;
1584  case 9: s = d10; break;
1585  case 10: s = d11; break;
1586  case 11: s = d12; break;
1587  case 12: s = d13; break;
1588  case 13: s = d14; break;
1589  case 14: s = d15; break;
1590  default: s = "NOTSUPPORTED"; break;
1591  }
1592  row[i] = QVariant(s);
1593  }
1594  data.push_back(row);
1595  control.push_back(ctrl);
1596 }
1597 
1598 QVariant HPlainDataMatrix::getCell(int row,int col)
1599 {
1600  if(row < 0 || col < 0)
1601  return QVariant("ERROR");
1602  if(row > data.count() || col > col_count)
1603  return QVariant("ERROR");
1604 
1605  return (data.at(row))[col];
1606 }
1607 
1608 
1609 QString HPlainDataMatrix::getCellStr(int row,int col)
1610 {
1611  if(row < 0 || col < 0)
1612  return "ERROR";
1613  if(row > data.count() || col > col_count)
1614  return "ERROR";
1615 
1616  return (data.at(row))[col].toString();
1617 }
1618 
1619 QString HPlainDataMatrix::getColumn(int col,QString separator)
1620 {
1621  bool first;
1622  QString r;
1623 
1624  r="";
1625  if(data.count() <= 0)
1626  return r;
1627  firstRow();
1628  first = true;
1629  do
1630  {
1631  if(!first)
1632  r.append(separator);
1633  else
1634  first = false;
1635  r.append(currentRowStr()[col]);
1636  }
1637  while(nextRow());
1638  return r;
1639 }
1640 
1641 
1642 void HPlainDataMatrix::setCell(int row,int col,QVariant vdata)
1643 {
1644  if(row < 0 || col < 0)
1645  return;
1646  if(row > data.count() || col > col_count)
1647  return;
1648 
1649  (data.at(row))[col] = vdata;
1650 }
1651 
1652 void HPlainDataMatrix::setRowControl(int row,QString ctrl)
1653 {
1654  if(row < 0 )
1655  return;
1656  if(row > data.count())
1657  return;
1658 
1659  control[row] = ctrl;
1660 }
1661 
1662 void HPlainDataMatrix::setCellStr(int row,int col,QString strdata)
1663 {
1664  if(row < 0 || col < 0)
1665  return;
1666  if(row > data.count() || col > col_count)
1667  return;
1668 
1669  (data.at(row))[col] = QVariant(strdata);
1670 }
1671 
1672 QList<QVariant> HPlainDataMatrix::getRow(int row)
1673 {
1674  int i;
1675  QList<QVariant> list;
1676  list.clear();
1677  QVariant *rowdata=NULL;
1678  rowdata = data.at(row);
1679  if(rowdata != NULL)
1680  for(i = 0; i < col_count;++i)
1681  {
1682  list.push_back(rowdata[i]);
1683  }
1684  return list;
1685 }
1686 
1687 QList<QString> HPlainDataMatrix::getRowStr(int row)
1688 {
1689  int i;
1690  QList<QString> list;
1691  list.clear();
1692  QVariant *rowdata=NULL;
1693  rowdata = data.at(row);
1694  if(rowdata != NULL)
1695  for(i = 0; i < col_count;++i)
1696  {
1697  list.push_back(rowdata[i].toString());
1698  }
1699  return list;
1700 }
1701 
1702 QString HPlainDataMatrix::getRowStr(int row,QString separator)
1703 {
1704  int i;
1705  QString list="";
1706  QVariant *rowdata=NULL;
1707  rowdata = data.at(row);
1708  if(rowdata != NULL)
1709  for(i = 0; i < col_count;++i)
1710  {
1711  list.append(rowdata[i].toString() + separator);
1712  }
1713  return list;
1714 }
1715 
1717 {
1718  iter = data.begin();
1719  iter_ctrl = control.begin();
1720 }
1721 
1723 {
1724  if(iter == data.end())
1725  return false;
1726 
1727  ++iter;
1728  ++iter_ctrl;
1729 
1730  if(iter == data.end())
1731  return false;
1732 
1733  return true;
1734 }
1735 
1737 {
1738  if(iter == data.end() || iter_ctrl == control.end())
1739  return true;
1740  return false;
1741 }
1742 
1744 {
1745  if(iter == data.end() || iter_ctrl == control.end())
1746  return;
1747 
1748  QVariant *to_delete = *iter;
1749  iter = data.erase(iter);
1750  iter_ctrl = control.erase(iter_ctrl);
1751  delete [] to_delete;
1752 
1753 }
1754 
1755 QList<QVariant> HPlainDataMatrix::currentRow(void)
1756 {
1757  int i;
1758  QList<QVariant> list;
1759  list.clear();
1760  QVariant *rowdata=NULL;
1761  rowdata = *iter;
1762  if(rowdata != NULL)
1763  for(i = 0; i < col_count;++i)
1764  {
1765  list.push_back(rowdata[i]);
1766  }
1767  return list;
1768 }
1769 
1771 {
1772  return *iter_ctrl;
1773 }
1774 
1776 {
1777  int i;
1778  QList<QString> list;
1779  list.clear();
1780  QVariant *rowdata=NULL;
1781  rowdata = *iter;
1782  if(rowdata != NULL)
1783  for(i = 0; i < col_count;++i)
1784  {
1785  list.push_back(rowdata[i].toString());
1786  }
1787  return list;
1788 }
1789 
1790 QString HPlainDataMatrix::currentRowStr(QString separator)
1791 {
1792  int i;
1793  QString list="";
1794  QVariant *rowdata=NULL;
1795  rowdata = *iter;
1796  if(rowdata != NULL)
1797  for(i = 0; i < col_count;++i)
1798  {
1799  list.append(rowdata[i].toString() + separator);
1800  }
1801  return list;
1802 }
1803 
1804 void HPlainDataMatrix::replaceTextInColumn(int col,QString find,QString replace,bool parts)
1805 {
1806  QList<QVariant *>::iterator iter;
1807  QVariant *in=NULL;
1808 
1809  iter = data.begin();
1810  while(iter != data.end())
1811  {
1812  in = *iter;
1813  if(parts)
1814  {
1815 
1816  if(in[col].toString().contains(find))
1817  {
1818  QString str;
1819 
1820  str = in[col].toString();
1821  str.replace(find,replace);
1822  in[col].setValue(str);
1823  }
1824  }
1825  else
1826  {
1827  if(in[col].toString() == find)
1828  in[col].setValue(replace);
1829  }
1830  ++iter;
1831  }
1832 }
1833 
1835 {
1836  QString laststr;
1837  QList<QVariant *>::iterator iter;
1838  QVariant *in=NULL;
1839 
1840  laststr = "__NOTHING_WAS_BEFORE__";
1841  iter = data.begin();
1842  while(iter != data.end())
1843  {
1844  in = *iter;
1845  if(in[col].toString() == laststr)
1846  in[col].setValue(QString(""));
1847  else
1848  laststr = in[col].toString();
1849  ++iter;
1850  }
1851 }
1852 
1853 void HPlainDataMatrix::setAppendPrependColumn(int col,QString prepend,QString append)
1854 {
1855  QString str;
1856  QList<QVariant *>::iterator iter;
1857  QVariant *in=NULL;
1858 
1859  iter = data.begin();
1860  while(iter != data.end())
1861  {
1862  in = *iter;
1863  str = in[col].toString();
1864  str.prepend(prepend);
1865  str.append(append);
1866  in[col].setValue(str);
1867  ++iter;
1868  }
1869 
1870 }
1871 
1873 {
1874  int sum;
1875  QList<QVariant *>::iterator iter;
1876  QVariant *in=NULL;
1877 
1878  iter = data.begin();
1879  sum = 0;
1880  while(iter != data.end())
1881  {
1882  in = *iter;
1883  sum += in[col].toInt();
1884 
1885  ++iter;
1886  }
1887  return sum;
1888 }
1889 
1891 {
1892  double sum;
1893  QList<QVariant *>::iterator iter;
1894  QVariant *in=NULL;
1895 
1896  iter = data.begin();
1897  sum = 0;
1898  while(iter != data.end())
1899  {
1900  in = *iter;
1901  sum += in[col].toDouble();
1902  ++iter;
1903  }
1904  return sum;
1905 }
1906 
1907 QString HPlainDataMatrix::concatenateCoulmn(int col,QString separator)
1908 {
1909  QString sum;
1910  QList<QVariant *>::iterator iter;
1911  QVariant *in=NULL;
1912 
1913  iter = data.begin();
1914  sum = "";
1915  while(iter != data.end())
1916  {
1917  in = *iter;
1918  if(!sum.isEmpty())
1919  sum += separator;
1920  sum += in[col].toString();
1921  ++iter;
1922  }
1923  return sum;
1924 }
1925 
1927 {
1928  int i;
1929  QString str,str2;
1930  QList<QVariant *>::iterator iter;
1931  QList<QString>::iterator ctrl_iter;
1932  QVariant *in=NULL;
1933 
1934  //iter = data.begin();
1935 
1936  iter = data.begin();
1937  ctrl_iter = control.begin();
1938  while(iter != data.end())
1939  {
1940  if(ctrl_iter == control.end())
1941  error("HPlainDataMatrix::removeHTMLTags: Control and data count mismatch!");
1942 
1943  in = *iter;
1944  if(force_all || (*ctrl_iter).contains("optionalhtmltags") )
1945  {
1946  for(i=0;i<col_count;++i)
1947  {
1948  str = in[i].toString();
1949 
1950  str2 = str.replace(QRegExp("<[^<>]*>"),"");
1951  sdebug("str:"+str+","+str2);
1952  in[i].setValue(str2);
1953  }
1954  }
1955  ++iter;
1956  ++ctrl_iter;
1957  }
1958 }
1959 
1960 QString HPlainDataMatrix::getContentAsHtml(QString params)
1961 {
1962  int all_width=0,border=0,pad=4,space=1;
1963  QString out;
1964  QString fullwidth;
1965  QTextStream textstream(&out);
1966  HXmlWriter xmlw;
1967  QList<QString> lst;
1968  int i;
1969 
1970  out = "";
1971  xmlw.setStream(&textstream);
1972  xmlw.putHead("",true);
1973  if(params.contains("html"))
1974  xmlw.beginNode("html");
1975  if(params.contains("css") && !htmlcss.isEmpty())
1976  {
1977  xmlw.beginNode("head");
1978  xmlw.putCData("style",htmlcss,"type=\"text/css\"");
1979  xmlw.endNode();
1980  }
1981 
1982  if(params.contains("center"))
1983  xmlw.beginNode("center");
1984  if(!params.contains("notitle"))
1985  xmlw.putCData("h2",getTitle());
1986 
1987  QRegExp reg1("width=(\\d+)");
1988  if(params.contains(reg1))
1989  all_width = (reg1.capturedTexts()[1]).toInt();
1990  QRegExp reg2("border=(\\d+)");
1991  if(params.contains(reg2))
1992  border = (reg2.capturedTexts()[1]).toInt();
1993  QRegExp reg3("pad=(\\d+)");
1994  if(params.contains(reg3))
1995  pad = (reg3.capturedTexts()[1]).toInt();
1996  QRegExp reg4("space=(\\d+)");
1997  if(params.contains(reg4))
1998  space = (reg4.capturedTexts()[1]).toInt();
1999 
2000  fullwidth = "";
2001  if(params.contains("fullwidth"))
2002  fullwidth = " width=\"100%\" ";
2003 
2004  if(!params.contains("notable"))
2005  xmlw.beginNode("table",QString("cellpadding=%1 cellspacing=%2 border=%3 %4")
2006  .arg(pad)
2007  .arg(space)
2008  .arg(border)
2009  .arg(fullwidth));
2010  if(rowCount() > 0)
2011  {
2012  if(!params.contains("noheader"))
2013  {
2014  xmlw.beginNode("tr");
2015  for(i = 0 ; i < col_count ; ++i)
2016  xmlw.putCData("th",
2017  hheader[i],
2018  all_width == 0 ?
2019  "align=\"center\" " :
2020  QString("align=\"center\" width=%1").arg(all_width)); //width=x
2021 
2022  xmlw.endNode();
2023  }
2024 
2025  firstRow();
2026  QString rowparameter;
2027  bool fcc_mode;
2028  bool contains_cellalign;
2029  bool contains_addcellparam;
2030  QString precell,postcell;
2031  QString cellparameters;
2032 
2033 
2034  int expanding_num=0;
2035  int from[15];
2036  int to[15];
2037  int inExpand;
2038 
2039  do
2040  {
2041  expanding_num = 1;
2042  from[0] = -1;
2043  to[0] = -1;
2044 
2045  precell="";
2046  postcell="";
2047  fcc_mode=false;
2048  rowparameter = "";
2049  contains_cellalign = false;
2050  contains_addcellparam = false;
2051  if(currentRowControl().contains("backgroundColor"))
2052  {
2053  QRegExp regc("backgroundColor=(......)");
2054  if(currentRowControl().contains(regc))
2055  {
2056  rowparameter += " bgcolor=\"#"+regc.capturedTexts()[1] + "\"";
2057  }
2058  }
2059  if(currentRowControl().contains("1cellexpandcenter"))
2060  fcc_mode = true;
2061  if(currentRowControl().contains("precell"))
2062  {
2063  QRegExp regc("precell=([^;]+);");
2064  if(currentRowControl().contains(regc))
2065  {
2066  precell = regc.capturedTexts()[1];
2067  }
2068  }
2069  if(currentRowControl().contains("postcell"))
2070  {
2071  QRegExp regc("postcell=([^;]+);");
2072  if(currentRowControl().contains(regc))
2073  {
2074  postcell = regc.capturedTexts()[1];
2075  }
2076  }
2077 
2078  if(currentRowControl().contains("expandCol"))
2079  {
2080  int fpos;
2081  int posinstr=0;
2082  QRegExp regc("expandCol=(\\d+)-(\\d+)");
2083  while((fpos = regc.indexIn(currentRowControl(),posinstr)) != -1)
2084  {
2085  posinstr = fpos + regc.matchedLength();
2086  from[expanding_num] = regc.capturedTexts()[1].toInt();
2087  to[expanding_num] = regc.capturedTexts()[2].toInt();
2088 
2089  ++expanding_num;
2090  if(expanding_num > 11)
2091  {
2092  error("HPlainDataMatrix::getContentAsHtml: Too much expandCol parameter!");
2093  break;
2094  }
2095  }
2096  }
2097 
2098  if(currentRowControl().contains("alignCellRight") || currentRowControl().contains("alignCellCenter"))
2099  contains_cellalign = true;
2100 
2101  if(currentRowControl().contains("addCellParam"))
2102  contains_addcellparam = true;
2103 
2104 
2105  xmlw.beginNode("tr",rowparameter);
2106  lst = currentRowStr();
2107  inExpand=0;
2108  for(i = 0 ; i < col_count ; ++i)
2109  {
2110  int ii;
2111 
2112  if(fcc_mode && i == 0)
2113  {
2114  xmlw.putCData("td",precell+lst[i]+postcell,QString("colspan=%1 align=\"center\"").arg(col_count) );
2115  break;
2116  }
2117 
2118  cellparameters = "";
2119 
2120  if(all_width != 0)
2121  cellparameters += QString(" width=%1").arg(all_width);
2122 
2123  for(ii=0;ii < expanding_num;++ii)
2124  {
2125  if(from[ii] == i && ((to[ii]-from[ii]) > 0))
2126  {
2127  cellparameters += QString(" colspan=%1").arg(1+to[ii]-from[ii]);
2128  inExpand=ii;
2129  }
2130  }
2131  if(from[inExpand] >= 0 && i > from[inExpand] && i <= to[inExpand])
2132  {
2133  continue;
2134  }
2135 
2136  if(contains_cellalign)
2137  {
2138  QRegExp regc1(QString("alignCellRight=%1\\s").arg(i));
2139  if(currentRowControl().contains(regc1))
2140  cellparameters += " align=\"right\"";
2141 
2142  QRegExp regc2(QString("alignCellCenter=%1\\s").arg(i));
2143  if(currentRowControl().contains(regc2))
2144  cellparameters += "align=\"center\"";
2145  }
2146 
2147  if(contains_addcellparam)
2148  {
2149  QRegExp regc(QString("addCellParam=%1:([^;]+);").arg(i));
2150  if(currentRowControl().contains(regc))
2151  cellparameters += " " + regc.capturedTexts()[1];
2152  }
2153 
2154  xmlw.putCData("td",precell+lst[i]+postcell,cellparameters);
2155  }
2156  xmlw.endNode();
2157  }
2158  while(nextRow());
2159  }
2160  if(!params.contains("notable"))
2161  xmlw.endNode();
2162  if(params.contains("center"))
2163  xmlw.endNode();
2164  if(params.contains("html"))
2165  xmlw.endNode();
2166 
2167  return out;
2168 }
2169 
2171 {
2172  emit actionOnRecord(key);
2173  return 0;
2174 }
2175 
2177 {
2178  emit alternateActionOnRecord(key);
2179  return 0;
2180 }
2181 
2184 
2185 HDataField_List::HDataField_List(void)
2186 {
2187  next = NULL;
2188  nextall = NULL;
2189  data = NULL;
2190 }
2191 
2192 HDataField_List::~HDataField_List(void)
2193 {
2194  if(next != NULL)
2195  delete next;
2196  if(data != NULL)
2197  delete data;
2198  data = NULL;
2199  next = NULL;
2200  nextall = NULL;
2201 }
2202 
2203 void HDataField_List::addElement(HDataField *d)
2204 {
2205  if(data == NULL)
2206  { data = d; }
2207  else
2208  {
2209  if(next == NULL)
2210  {
2211  if(nextall != NULL)
2212  {
2213  emit errorSignal(
2214  QString("Cannot insert a new element to the table,"
2215  " because It already has some connected field!\n"
2216  "Insert all own field first, than the connect the tables!"));
2217  return;
2218  }
2219  next=new HDataField_List();
2220  }
2221  next->addElement(d);
2222  nextall=next;
2223  }
2224 }
2225 
2226 void HDataField_List::connectElement(HDataField_List *c)
2227 {
2228  if(nextall != NULL)
2229  {
2230  nextall->connectElement(c);
2231  }
2232  else
2233  {
2234  nextall=c;
2235  }
2236 }
2237 
2238 void HDataField_List::disconnectElement(void)
2239 {
2240  if(next == NULL && nextall == NULL) //end of all
2241  {
2242  return;
2243  }
2244  else if(next == NULL && nextall != NULL) //disconnect
2245  {
2246  nextall = NULL;
2247  return;
2248  }
2249  else
2250  {
2251  next->disconnectElement();
2252  }
2253 }
2254 
2255 void HDataField_List::clearElements(void)
2256 {
2257  if(next != NULL)
2258  delete next;
2259  if(data != NULL)
2260  delete data;
2261  data = NULL;
2262  next = NULL;
2263  nextall = NULL;
2264 }
2265 
2269 
2271 {
2272  whoami = "HTableBase";
2273 
2274  errstr = "";
2275  sqlTable = t;
2276  field = 0;
2277  table = NULL;
2278 
2279  conn_part = "";
2280  imc_thiskey = "";
2281  imc_thatkey = "";
2282  imc_thattable = NULL;
2283 
2284  tabletitle = "";
2285  extrafeatures = false;
2286  gui_showtabletitle = false;
2287  connectedTableBases = new QStack<HTableBase *>();
2288  depend = new QStringList();
2289  firstField();
2290 }
2291 
2293 {
2294  conn_part = "";
2295  imc_thiskey = "";
2296  imc_thatkey = "";
2297  imc_thattable = NULL;
2298  connectedTableBases = new QStack<HTableBase *>();
2299 
2300  table = NULL;
2301  run = NULL;
2302  depend = new QStringList();
2303 
2305 }
2306 
2308 {
2309  HDataField *df;
2310 
2312 
2313  whoami = x->whoami;
2314  tabletitle = x->tabletitle;
2315  field = x->field;
2316  sqlTable = x->sqlTable;
2317 
2320  errstr = x->errstr;
2321 
2322  clearConnections();
2323  freeTable();
2324 
2325  deep_copy_stringlist(depend,x->depend);
2326 
2327  x->firstField();
2328  while((df = x->nextField()))
2329  {
2330  addField(df->get_clone());
2331  }
2332  firstField();
2333 }
2334 
2336 {
2337  sdebug("-->Dieing...HtableBase:"+sqlTable+" __astala vista baby!");
2338  freeTable();
2339  delete connectedTableBases;
2340 }
2341 
2342 HDataField *HTableBase::fieldBySqlName(QString sqln,bool all,QString containerSqlTable)
2343 {
2344  HDataField_List *tmp=table;
2345  while(tmp != NULL)
2346  {
2347  if(tmp->data != NULL && tmp->data->sqlSelectHead() == sqln &&
2348  ( !all || tmp->data->conn_sqlTable == containerSqlTable //connected tables things...
2349  )
2350  )
2351  {
2352  return tmp->data;
2353  }
2354 
2355  if(all)
2356  tmp=tmp->nextall;
2357  else
2358  tmp=tmp->next;
2359  }
2360  return NULL;
2361 }
2362 
2364 {
2365  int ii=0;
2366  HDataField_List *tmp=table;
2367  while(tmp != NULL)
2368  {
2369  if(!tmp->data->isSQLField())
2370  {
2371  if(all)
2372  tmp=tmp->nextall;
2373  else
2374  tmp=tmp->next;
2375  continue;
2376  }
2377 
2378  if(i == ii)
2379  {
2380  return tmp->data;
2381  }
2382  if(all)
2383  tmp=tmp->nextall;
2384  else
2385  tmp=tmp->next;
2386  ++ii;
2387  }
2388  return NULL;
2389 }
2390 
2392 {
2393  HDataField_List *tmp=table;
2394  while(tmp != NULL)
2395  {
2396  if(tmp->data != NULL &&
2397  tmp->data->getWhoami() == "HStatic" &&
2398  (title.isEmpty() || tmp->data->getTitleText() == title))
2399  {
2400  return tmp->data;
2401  }
2402  tmp=tmp->next;
2403  }
2404  return NULL;
2405 
2406 }
2407 
2409 {
2410  if(!n.isEmpty())
2411  depend->push_back(n);
2412 }
2413 
2415 {
2416  HTableBase *tmp;
2417  clearConnections();
2418 
2419  while(!connectedTableBases->isEmpty())
2420  {
2421  tmp = connectedTableBases->pop();
2422  tmp->clearConnections();
2423  delete tmp;
2424  }
2425 
2426 }
2427 
2429 {
2430  HDataField_List *run=NULL;
2431 
2432  if(table != NULL)
2433  table->disconnectElement();
2434 
2435  conn_part = "";
2436 
2437  imc_thiskey = "";
2438  imc_thatkey = "";
2439  imc_thattable = NULL;
2440  connectedTableBases->clear();
2441 
2442  for(run=table;run != NULL;run=run->next)
2443  run->data->connected = false;
2444 
2445 }
2446 
2447 void HTableBase::connectTable(HTableBase *ct,int type,QString connconstraint)
2448 {
2449  HDataField_List *crun=NULL,*krun=NULL;
2450 
2451  crun = table;
2452  while(crun != NULL)
2453  {
2454  if(crun->data != NULL)
2455  if(crun->data->getWhoami() == "HConnect") //if i found connected
2456  if(((HConnect *)(crun->data))->getConnectedTable() == ct->sqlTableName()) //points to the targetted table
2457  {
2458  krun = ct->table;
2459  while(krun != NULL)
2460  {
2461  if(krun->data != NULL)
2462  if(krun->data->getWhoami() == "HKey") //and found a key field in the others
2463  {
2464  //got the connection!
2465  connectTable(ct,crun->data->sqlSelectHead(),
2466  krun->data->sqlSelectHead(),
2467  type,connconstraint );
2468  return;
2469 
2470  }
2471 
2472  krun = krun->next;
2473  }
2474 
2475  }
2476 
2477  crun=crun->next;
2478  }
2479 
2480  //Back again, if the dependency is other side
2481  crun = ct->table;
2482  while(crun != NULL)
2483  {
2484  if(crun->data != NULL)
2485  if(crun->data->getWhoami() == "HConnect") //found a connect field
2486  if(((HConnect *)(crun->data))->getConnectedTable() == sqlTableName()) //points to the targetted table
2487  {
2488  krun = table;
2489  while(krun != NULL)
2490  {
2491  if(krun->data != NULL)
2492  if(krun->data->getWhoami() == "HKey") //and found a key field in the others
2493  {
2494  //got the connection!
2495  connectTable(ct,krun->data->sqlSelectHead(),
2496  crun->data->sqlSelectHead(),
2497  type,connconstraint );
2498  return;
2499 
2500  }
2501 
2502  krun = krun->next;
2503  }
2504  }
2505 
2506  crun=crun->next;
2507  }
2508 
2509  emit errorSignal("Automatic join fail: Can't find all necessary elements/part ");
2510 }
2511 
2512 void HTableBase::connectTable(HTableBase *ct,QString thiskey,QString thatkey,int type,QString connconstraint)
2513 {
2514  HDataField_List *run=NULL;
2515 
2516  sdebug("*** HTableBase::connectTable ***");
2517  if(type == INNER_JOIN || type == LEFT_OUTER_JOIN)
2518  {
2519  conn_part.append(QString(" %1 %2 ON %3.%4 = %5.%6%7 %8")
2520  .arg(QString((type == INNER_JOIN ? "INNER JOIN" : ""))+
2521  QString((type == LEFT_OUTER_JOIN ? "LEFT OUTER JOIN" : "")))
2522  .arg(ct->sqlTableName())
2523  .arg(this->sqlTableName())
2524  .arg(thiskey)
2525  .arg(ct->sqlTableName())
2526  .arg(thatkey)
2527  .arg(connconstraint.isEmpty() ? "" : (" AND " + connconstraint + " "))
2528  .arg(ct->conn_part)
2529  );
2530 
2531 
2532  table->connectElement(ct->table);
2533  connectedTableBases->push(ct);
2534 
2535  //sets IMC data
2536  ct->imc_thattable = this;
2537  ct->imc_thatkey = thiskey;
2538  ct->imc_thiskey = thatkey;
2539 
2540  }
2541  else
2542  {
2543  emit errorSignal(QString("Unknown connection type: %1").arg(type));
2544  return;
2545  }
2546 
2547  for(run=ct->table;run != NULL;run=run->next)
2548  run->data->connected = true;
2549 
2550  sdebug("*** HTableBase::connectTable *** END");
2551 }
2552 
2554 {
2555  if(table != NULL) delete table;
2556  table = NULL;
2557  conn_part = "";
2558 }
2559 
2561 {
2562  if(table == NULL)
2563  {
2564  table = new HDataField_List();
2565  connect( table,SIGNAL(errorSignal(QString )),this,SLOT(errorPassSlot(QString )) );
2566  }
2567  table->addElement(i);
2568  i->conn_sqlTable = sqlTable;
2569  i->cloneSqlSettingsFrom(this);
2570  connect(i,SIGNAL(errorSignal(QString )),this,SLOT(errorPassSlot(QString )));
2571  connect(i,SIGNAL(dataChanged()),SIGNAL(dataChanged()) );
2572 }
2573 
2575 {
2576  return sqlTable;
2577 }
2578 
2579 QString HTableBase::sqlSelect(QString tail,bool all,bool keypart)
2580 {
2581  bool keyfound=false;
2582  int i=0;
2583  QString ret,keyname="",keyvalue="";
2584 
2585  HDataField_List *tmp=table;
2586  ret = "SELECT ";
2587  for(;tmp != NULL;)
2588  {
2589  if(tmp->data->isSQLField()) //it is intrested by sql database
2590  {
2591  if(tmp->data->getWhoami() == "HSmallText" && tmp->data->sqlSelectHead() == "NON_SQL_LIST_SERIAL")
2592  {
2593  if(all)
2594  tmp=tmp->nextall;
2595  else
2596  tmp=tmp->next;
2597  continue;
2598  }
2599 
2600  if(tmp->data->getWhoami() == "HKey" && !keyfound)
2601  {
2602  keyfound = true;
2603  keyname = sqlTable+"."+tmp->data->sqlSelectHead();
2604  keyvalue = tmp->data->getSQLValue();
2605  }
2606  if(i != 0) ret.append(",");
2607 
2608  if(!tmp->data->isEditable() && !tmp->data->getFunction().isEmpty())
2609  {
2610  ret.append(tmp->data->getFunction() + "("+( all ? (tmp->data->conn_sqlTable+".") : "" ) +
2611  tmp->data->sqlSelectHead()+")");
2612  }
2613  else if(!tmp->data->isEditable() && !tmp->data->getSubselect().isEmpty())
2614  {
2615  ret.append("(" + tmp->data->getSubselect() + ")");
2616  }
2617  else
2618  {
2619  ret.append(( all ? (tmp->data->conn_sqlTable+".") : "" ) +
2620  tmp->data->sqlSelectHead());
2621  }
2622  i++;
2623  }
2624  if(all)
2625  tmp=tmp->nextall;
2626  else
2627  tmp=tmp->next;
2628  }
2629 
2630  ret.append(" FROM " + sqlTable + " " + (all ? ( conn_part+ " ") : "") + "WHERE ");
2631 
2632  if(keypart && keyname != "" && keyvalue != "")
2633  {
2634  ret.append(" "+keyname+"="+keyvalue+" ");
2635  }
2636  else
2637  {
2638  ret.append(QString("%1 ").arg(myInterface()->hsqli_truevalue));
2639  }
2640 
2641  ret.append(tail);
2642  return ret;
2643 }
2644 
2646 {
2647  int idx=0;
2648  HDataField_List *tmp=table;
2649  while(tmp != NULL)
2650  {
2651  if(idx == i)
2652  {
2653  if(tmp->data == NULL) return QVariant("");
2654  else return tmp->data->sqlInsertHead();
2655  }
2656  tmp=tmp->next;
2657  ++idx;
2658  }
2659  return QVariant("");
2660 }
2661 
2662 void HTableBase::setColor(int r,int g,int b)
2663 {
2664  HDataField_List *tmp=table;
2665  while(tmp != NULL)
2666  {
2667  if(tmp->data != NULL)
2668  tmp->data->setColor(r,g,b);
2669  tmp=tmp->next;
2670  }
2671 }
2672 
2674 {
2675  HDataField_List *tmp=table;
2676  while(tmp != NULL)
2677  {
2678  if(tmp->data != NULL)
2679  tmp->data->setEditable();
2680  tmp=tmp->next;
2681  }
2682 }
2683 
2685 {
2686  HDataField_List *tmp=table;
2687  while(tmp != NULL)
2688  {
2689  if(tmp->data != NULL)
2690  tmp->data->notSet();
2691  tmp=tmp->next;
2692  }
2693 }
2694 
2696 {
2697  HDataField_List *tmp=table;
2698  while(tmp != NULL)
2699  {
2700  if(tmp->data != NULL)
2701  tmp->data->doSet();
2702  tmp=tmp->next;
2703  }
2704 }
2705 
2706 int HTableBase::fieldCount(bool withconnect)
2707 {
2708  int c = 0;
2709  HDataField_List *tmp=table;
2710  while(tmp != NULL)
2711  {
2712  ++c;
2713 
2714  if(withconnect)
2715  tmp=tmp->nextall;
2716  else
2717  tmp=tmp->next;
2718  }
2719  return c;
2720 }
2721 
2723 {
2724  sdebug("*** HTableBase::isChanged ***");
2725  HDataField_List *tmp=table;
2726  while(tmp != NULL)
2727  {
2728  if(tmp->data != NULL)
2729  if(tmp->data->isSet())
2730  return true;
2731 
2732  if(all)
2733  tmp=tmp->nextall;
2734  else
2735  tmp=tmp->next;
2736  }
2737  sdebug("*** HTableBase::isChanged *** END");
2738  return false;
2739 }
2740 
2741 QString HTableBase::validate(bool all)
2742 {
2743  sdebug("*** HTableBase::validate ***");
2744  QString c,cc;
2745 
2746  c="";
2747  cc="";
2748  HDataField_List *tmp=table;
2749  while(tmp != NULL)
2750  {
2751  if(tmp->data != NULL)
2752  if( !((c = tmp->data->validate()).isEmpty()) )
2753  {
2754 #ifdef VALIDATE_HALT_ON_FIRST_ERROR
2755  return c;
2756 #else
2757  cc += c + "\n";
2758 #endif
2759  }
2760 
2761  if(all)
2762  tmp=tmp->nextall;
2763  else
2764  tmp=tmp->next;
2765 
2766  }
2767  return cc;
2768 }
2769 
2770 
2772 {
2773  HDataField_List *tmp=table;
2774  while(tmp != NULL)
2775  {
2776  if(tmp->data != NULL)
2777  tmp->data->setReadonly();
2778  tmp=tmp->next;
2779  }
2780 }
2781 
2783 {
2784  HDataField_List *tmp=table;
2785  while(tmp != NULL)
2786  {
2787  if(tmp->data != NULL)
2788  tmp->data->setHide();
2789  tmp=tmp->next;
2790  }
2791 }
2792 
2794 {
2795  HDataField_List *tmp=table;
2796  while(tmp != NULL)
2797  {
2798  if(tmp->data != NULL)
2799  tmp->data->setShow();
2800  tmp=tmp->next;
2801  }
2802 }
2803 
2804 QStringList HTableBase::getHeads(int what)
2805 {
2806  QStringList l;
2807 
2808  l.clear();
2809  //1 - title 2 - short title
2810  HDataField_List *tmp=table;
2811  while(tmp != NULL)
2812  {
2813  if(tmp->data != NULL && tmp->data->isShow() && ( what == 1 || tmp->data->isSQLField() ))
2814  {
2815  l.push_back( (what == 1 ? tmp->data->getExplainText() : tmp->data->getTitleText()) );
2816  }
2817  tmp=tmp->nextall;
2818  }
2819  return l;
2820 }
2821 
2823 {
2824  run = NULL;
2825 }
2826 
2827 
2829 {
2830  if(run==NULL) //first
2831  {
2832  run=table;
2833  if(run == NULL) return NULL; //empty
2834  return run->data;
2835  }
2836 
2837  run = run->next;
2838  if(run == NULL)
2839  return NULL;
2840  return run->data;
2841  }
2842 
2844 {
2845  if(run==NULL) //first
2846  {
2847  run=table;
2848  if(run == NULL) return NULL; //empty
2849  return run->data;
2850  }
2851 
2852  run = run->nextall;
2853  if(run == NULL)
2854  return NULL;
2855  return run->data;
2856  }
2857 
2859 {
2860  sdebug("*** HTableBase::dataUpdated ***");
2861  emit dataUpdatedSignal();
2862  sdebug("*** HTableBase::dataUpdated *** END");
2863 }
2864 
2866 {
2867  emit errorSignal(err);
2868  return 0;
2869 }
2870 
2871 QString HTableBase::sqlCreateString(QString switches)
2872 {
2873  bool first;
2874  QString str;
2875  HDataField *f;
2876 
2877  str = "";
2878  if(myInterface()->hsqli_need_create_sequence_before)
2879  {
2880  str += myInterface()->hsqli_sequence_creation_string + "; \n";
2881  }
2882 
2883  str += QString("CREATE TABLE %1 \n (").arg(sqlTable);
2884  first = true;
2885  firstField();
2886  while((f=nextField()) != NULL)
2887  {
2888  if(f->isSQLField())
2889  {
2890  if(first)
2891  first = false;
2892  else
2893  str += ", ";
2894  str += "\n\t " + f->sqlCreateStringPart(switches) + " ";
2895  }
2896  }
2897  str += "\n ); \n";
2898 
2899  if(myInterface()->hsqli_need_create_sequence_before)
2900  str.replace("_SEQNAME_",sqlTable+"_pkey_sequence",Qt::CaseSensitive);
2901 
2902  return str;
2903 }
2904 
2906 {
2907  HDataField *df=NULL;
2908 
2909  firstField();
2910  while((df = nextFieldAll()))
2911  if(df->hasMark(mark))
2912  df->setShow();
2913 }
2914 
2916 {
2917  HDataField *df=NULL;
2918 
2919  firstField();
2920  while((df = nextFieldAll()))
2921  if(df->hasMark(mark))
2922  df->setHide();
2923 
2924 }
2925 
2927 {
2928  HDataField *df=NULL;
2929 
2930  firstField();
2931  while((df = nextFieldAll()))
2932  if(df->hasMark(mark))
2933  df->setEditable();
2934 
2935 }
2936 
2938 {
2939  HDataField *df=NULL;
2940 
2941  firstField();
2942  while((df = nextFieldAll()))
2943  if(df->hasMark(mark))
2944  df->setReadonly();
2945 }
2946 
2947 void HTableBase::setColorAllMarked(QString mark,int r,int g,int b)
2948 {
2949  HDataField *df=NULL;
2950 
2951  firstField();
2952  while((df = nextFieldAll()))
2953  if(df->hasMark(mark))
2954  df->setColor(r,g,b);
2955 }
2956 
2958 {
2959  HDataField *df=NULL;
2960 
2961  firstField();
2962  while((df = nextFieldAll()))
2963  if(df->hasMark(mark))
2964  df->returnToDefault();
2965 }
2966 
2968 {
2969  HDataField *df=NULL;
2970 
2971  firstField();
2972  while((df = nextFieldAll()))
2973  if(df->hasMark(mark))
2974  df->enableControlSlot();
2975 }
2976 
2978 {
2979  HDataField *df=NULL;
2980 
2981  firstField();
2982  while((df = nextFieldAll()))
2983  if(df->hasMark(mark))
2984  df->disableControlSlot();
2985 }
2986 
2987 void HTableBase::blockReInitializeBase(QString hide_marker,QString ro_marker)
2988 {
2989  setShow();
2990  setEditable();
2991  setHideAllMarked(hide_marker);
2992  setReadolnyAllMarked(ro_marker);
2994 }
2995 
2997 {
2998  HDataField *df=NULL;
2999 
3000  firstField();
3001  while((df = nextFieldAll()))
3002  if(df->getWhoami() == "HSqlChoose")
3003  {
3004  ((HSqlChoose *)df)->removeToolButton();
3005  }
3006 }
3007 
3008 void HTableBase::useRole(QString name)
3009 {
3010  HDataField *df=NULL;
3011 
3012  firstField();
3013  while((df = nextFieldAll()))
3014  df->useRole(name);
3015 }
3016 
3017 void HTableBase::delRole(QString name)
3018 {
3019  HDataField *df=NULL;
3020 
3021  firstField();
3022  while((df = nextFieldAll()))
3023  df->delRole(name);
3024 }
3025 
3027 {
3028  HDataField *df=NULL;
3029 
3030  firstField();
3031  while((df = nextFieldAll()))
3032  df->clearRoles();
3033 }
3034 
3035 void HTableBase::sqlSynced(HDataChangeLogger *dclog,bool forcechange,bool forcelog)
3036 {
3037  HDataField *df=NULL;
3038 
3039  firstField();
3040  while((df = nextFieldAll()))
3041  df->sqlSynced(dclog,forcechange,forcelog);
3042 }
3043 
3045 {
3046  HDataField *df=NULL;
3047 
3048  firstField();
3049  while((df = nextFieldAll()))
3050  df->clearSqlSynced();
3051 }
3052 
3054 {
3055  HDataField *df=NULL;
3056 
3057  firstField();
3058  while((df = nextFieldAll()))
3059  if(df->isReallyChanged())
3060  return true;
3061  return false;
3062 }
3063 
3065 {
3066  HDataField *df=NULL;
3067 
3068  firstField();
3069  while((df = nextFieldAll()))
3070  df->resetLogging();
3071 }
3072 
3074 {
3075  HDataField *df=NULL;
3076 
3077  firstField();
3078  while((df = nextFieldAll()))
3079  df->setLogging();
3080 }
3081 
3083 {
3084  HDataField *df=NULL;
3085 
3086  firstField();
3087  while((df = nextFieldAll()))
3088  if(df->hasMark(mark))
3089  df->setLogging();
3090 }
3091 
3095 
3096 HTable::HTable(QString t)
3097 : HTableBase(t)
3098 {
3099  whoami = "HTable";
3100  ro_mask = false;
3101  reread_key = false;
3102  insertedKey = "";
3103  dclog = NULL;
3104 }
3105 
3107 {
3108  sdebug("*** HTable::~HTable ***");
3109  freeTable();
3110  sdebug("*** HTable::~HTable *** END");
3111 }
3112 
3114 : HTableBase("")
3115 {
3116  reread_key = false;
3117  insertedKey = "";
3119 }
3120 
3122 {
3124 
3125  ro_mask = x->ro_mask;
3126  dclog = x->dclog;
3127 }
3128 
3129 void HTable::setSqlFieldValue(QString sqln,QVariant v,bool sdisable)
3130 {
3131  HDataField *f=NULL;
3132 
3133  f = fieldBySqlName(sqln);
3134  if(f == NULL)
3135  return;
3136  f->setValue(v,sdisable);
3137  if(!sdisable)
3138  dataUpdated();
3139 }
3140 
3141 QVariant HTable::getSqlFieldValue(QString sqln)
3142 {
3143  HDataField *f=NULL;
3144 
3145  f = fieldBySqlName(sqln);
3146  if(f == NULL)
3147  return QVariant("");
3148 
3149  return f->getValue();
3150 }
3151 
3152 QString HTable::sqlUpdate(bool all)
3153 {
3154  sdebug("*** HTable::sqlUpdate ***");
3155  bool begin=false,modified=false,first=false;
3156  int i=0;
3157 
3158  QString ret="",part="",keyname="",keyvalue="";
3159 
3160  HDataField_List *tmp=table;
3161  for(;tmp != NULL;tmp=tmp->nextall)
3162  {
3163  if(i==0 && !begin) //before the first field
3164  {
3165  part=QString("UPDATE %1 SET ").arg(tmp->data->conn_sqlTable);
3166  begin = true;
3167  first = true;
3168  modified=false;
3169  }
3170 
3171  if(tmp->data->isSQLField()) //field is intrested in sql database
3172  {
3173  if(tmp->data->getWhoami() == "HKey")
3174  {
3175  keyname = tmp->data->sqlSelectHead();
3176  keyvalue = tmp->data->getSQLValue();
3177  continue;
3178  }
3179  if(tmp->data->getWhoami() == "HConnect")
3180  continue;
3181 
3182  if( (tmp->data->isEditable() && //if the field is editable
3183  tmp->data->isSet() ) | // and the value is set (set status is ture)
3184  (tmp->data->getWhoami() == "HTimestamp" && // Or this is a timestam WHICH HAVE
3185  ((HTimestamp *)tmp->data)->is_checkenabled()) // chech/control function (always have to update)
3186  )
3187  {
3188  /*It's true, that the timastamps always have to be updated, but I don't enabled the SET/MODIFIED status
3189  because if only the timestamp is updated it's unnecessary to update the table.
3190  The update is only necessary if any other field is updated except the timestamp
3191  that case the modifies=true is set. */
3192  if(tmp->data->getWhoami() != "HTimestamp")
3193  modified = true;
3194 
3195  if(!first) //dont need ; on the first element
3196  {
3197  part.append(",");
3198  }
3199  else
3200  {
3201  first = false;
3202  }
3203  part.append(tmp->data->sqlUpdate());
3204  }
3205  i++;
3206  }
3207  if(tmp->next == NULL) //after the last field in the table (without connections)
3208  {
3209  i=0;
3210  if(keyname == "" || keyvalue == "")
3211  {
3212  sdebug("*** HTable::sqlUpdate *** END with empty string");
3213  return QString("");
3214  }
3215 
3216  part.append(" WHERE "+keyname+"="+keyvalue+"; \n");
3217  if(modified)
3218  ret.append(part);
3219  if(!all)
3220  {
3221  sdebug("*** HTable::sqlUpdate *** ("+ret+")");
3222  return ret;
3223  }
3224  begin = false;
3225  }
3226  }
3227  sdebug("*** HTable::sqlUpdate *** ("+ret+")");
3228  return ret;
3229 }
3230 
3231 QVariant HTable::operator [](int i)
3232 {
3233  int idx=0;
3234  HDataField_List *tmp=table;
3235  while(tmp != NULL)
3236  {
3237  if(idx == i)
3238  {
3239  if(tmp->data == NULL) return QVariant("");
3240  else return tmp->data->getValue();
3241  }
3242  tmp=tmp->next;
3243  ++idx;
3244  }
3245  return QVariant("");
3246 }
3247 
3248 
3249 QString HTable::getKey(bool head_val,QString intable)
3250 {
3251  QString in_table;
3252  if(intable.isEmpty())
3253  in_table = sqlTable;
3254  else
3255  in_table = intable;
3256 
3257  HDataField_List *tmp=table;
3258  while(tmp != NULL)
3259  {
3260  if(tmp->data != NULL && tmp->data->getWhoami() == "HKey" && in_table == tmp->data->conn_sqlTable)
3261  {
3262  if(head_val)
3263  {
3264  return tmp->data->sqlSelectHead();
3265  }
3266  else
3267  {
3268  return tmp->data->getValue().toString();
3269  }
3270  }
3271  tmp=tmp->nextall;
3272  }
3273  return QString("");
3274 }
3275 
3276 int HTable::setKey(QString key)
3277 {
3278  HDataField_List *tmp=table;
3279  while(tmp != NULL)
3280  {
3281  if(tmp->data != NULL && tmp->data->getWhoami() == "HKey")
3282  {
3283  tmp->data->setValue(key);
3284  return 0;
3285  }
3286  tmp=tmp->next;
3287  }
3288  return 1;
3289 }
3290 
3291 int HTable::updateWithKey(QString key,bool all,bool update,bool tdisabled)
3292 {
3293  int idx;
3294  HDataField_List *tmp=table;
3295  QString sqlq;
3296 
3297  sdebug("*** HTable::updateWithKey *** ");
3298  if(!key.isEmpty())
3299  if(setKey(key))
3300  return 1;
3301 
3302  /*Have to step through on the data fields and examine if any field have any sql dependency.
3303  for example if an sqlchoose field found it is necessary to read de depended table
3304  because it can contains a new value which is are not in the table on the last read */
3305  HDataField_List *refresh_tmp=table;
3306  while(refresh_tmp != NULL)
3307  {
3308  if(refresh_tmp->data->getWhoami() == "HSqlChoose")
3309  ((HSqlChoose *)refresh_tmp->data)->refreshSqlValues(false,tdisabled);
3310  refresh_tmp=refresh_tmp->next;
3311  }
3312  //That's it.
3313 
3314  if(!tdisabled)
3315  transaction();
3316 
3317  if(all)
3318  sqlq=sqlSelectWkAll();
3319  else
3320  sqlq=sqlSelectWk();
3321 
3322  sqlq.append(";");
3323  QSqlQuery *q = allocateQSqlQuery();
3324  sqldebug(sqlq);
3325  q->prepare(sqlq);
3326  q->exec();
3327 
3328  if((myInterface()->hsqli_usequerysize && q->numRowsAffected() != 1) ||
3329  ! q->next() ||
3330  q->lastError().type() != QSqlError::NoError)
3331  {
3332  if(!tdisabled)
3333  rollback();
3334  errstr=("The SELECT returned with !=1 record! (1)\n>>"+((q->lastError()).text()));
3335  errstr+="\nQuery was: \""+sqlq+"\"\n(Sql name of the table:"+sqlTable+")\n";
3336  delete q;
3337  emit errorSignal(errstr);
3338  return 1;
3339  }
3340 
3341  idx=0;
3342  tmp=table;
3343 
3344  while(tmp != NULL)
3345  {
3346  if(tmp->data != NULL && tmp->data->isSQLField())
3347  {
3348  /* In the following if I examine if it is a refresh of a timestamp field with check controll option.
3349  In that case it's help to prevent the concurent writing. I tells to the node that i refreshed the value of the timestamp now .
3350  If i found a different timestamp during the save i will know that somody updated the record in the database.
3351  That case it have to prevent the store and sign an error */
3352  if(tmp->data->getWhoami() == "HTimestamp" && ((HTimestamp *)tmp->data)->is_checkenabled() )
3353  ((HTimestamp *)tmp->data)->read_from_sql();
3354 
3355  tmp->data->setValue(q->value(idx),true);
3356  tmp->data->notSet();
3357 
3358  idx++;
3359  }
3360  if(all)
3361  tmp=tmp->nextall;
3362  else
3363  tmp=tmp->next;
3364  }
3365  delete q;
3366  if(!tdisabled)
3367  commit();
3368 
3369  emit dataChanged();
3370  if(update)
3371  dataUpdated();
3372 
3373  sqlSynced();
3374  sdebug("*** HTable::updateWithKey *** END");
3375  return 0;
3376 }
3377 
3379 {
3380  sdebug("*** HTable::returnToDefault ***");
3381  HDataField_List *tmp=table;
3382  while(tmp != NULL)
3383  {
3384  if(tmp->data != NULL)
3385  {
3386  QString dv;
3387  tmp->data->notSet();
3388  dv = tmp->data->getDefaultValue();
3389  tmp->data->setDefval(dv);
3390  tmp->data->resetValidateStatus();
3391  }
3392  if(all)
3393  tmp=tmp->nextall;
3394  else
3395  tmp=tmp->next;
3396  }
3397  sdebug("*** HTable::returnToDefault *** END");
3398 
3399  dataUpdated();
3400  return 0;
3401 }
3402 
3403 int HTable::saveRecord(bool all,bool tdisabled)
3404 {
3405  HDataField_List *tmp=table;
3406  QString ts_new="empty";
3407  QString ts_old="empty";
3408 
3409  sdebug("*** HTable::saveRecord *** ");
3410  if(!((errstr=validate(all)).isEmpty()))
3411  {
3412  emit errorSignal(errstr);
3413  sdebug("*** HTable::saveRecord *** ERROR1");
3414  return 1;
3415  }
3416 
3417  if(!tdisabled)
3418  transaction();
3419 
3420  tmp=table;
3421  while(tmp != NULL)
3422  {
3423  if(tmp->data != NULL &&
3424  tmp->data->isSQLField() && // sql related field
3425  tmp->data->getWhoami() == "HTimestamp" && // timestamp AND
3426  ((HTimestamp *)tmp->data)->is_checkenabled() && // have check/controll option AND
3427  ((HTimestamp *)tmp->data)->is_sqlreaded() // the value is already readed
3428  )
3429  {
3430  QString sqlq=QString("SELECT %1 FROM %2 WHERE %3=\'%4\';")
3431  .arg(tmp->data->sqlSelectHead())
3432  .arg(tmp->data->conn_sqlTable)
3433  .arg(getKey(HEAD,tmp->data->conn_sqlTable))
3434  .arg(getKey(VALUE,tmp->data->conn_sqlTable));
3435 
3436  ts_old = tmp->data->getValue().toString(); //queries the stored timestamp valee
3437 
3438  //queryes the current value of the timestamp from the database
3439  QSqlQuery *q = allocateQSqlQuery();
3440  sqldebug(sqlq);
3441  q->prepare(sqlq);
3442  q->exec();
3443  if((myInterface()->hsqli_usequerysize && q->numRowsAffected() != 1 ) ||
3444  ! q->next() ||
3445  q->lastError().type() != QSqlError::NoError)
3446  {
3447 
3448  if(!tdisabled)
3449  rollback();
3450  errstr=("The SELECT returned with !=1 record! (TimeStamp Check- GetTimeStamp)\n>>"+
3451  ((q->lastError()).text()));
3452  delete q;
3453  emit errorSignal(errstr);
3454  sdebug(errstr);
3455  sdebug("*** HTable::saveRecord *** ERROR2");
3456  return 1;
3457  }
3458  ts_new = q->value(0).toString();
3459 
3460  //compare the two value
3461  if(ts_old != ts_new)
3462  {
3463  if(!tdisabled)
3464  rollback();
3465  delete q;
3466 
3467  errstr=QString("The data of the record is out of date in table \"%1 : %2\"!"
3468  "\nUpdate aborted! \n"
3469  "(The record has utomatically re-loaded try again the operation!)")
3470  .arg(tmp->data->conn_sqlTable)
3471  .arg(tmp->data->sqlSelectHead());
3472 
3473  emit errorSignal(errstr);
3474  sdebug(errstr);
3475  updateWithKey("",all);
3476  sdebug("*** HTable::saveRecord *** ERROR3");
3477  return 1;
3478  }
3479  delete q;
3480  }
3481  if(all)
3482  tmp=tmp->nextall;
3483  else
3484  tmp=tmp->next;
3485  }
3486 
3487  //I queried the timestamp and checked. Let's do the update!
3488 
3489  QString sqlu="";
3490 
3491  sqlu = sqlUpdate(all);
3492 
3493  if(sqlu.isEmpty())
3494  {
3495  if(!tdisabled)
3496  rollback();
3497  sdebug("There is no relevant changes:emtpty update generated, exiting.");
3498  return 0;
3499  }
3500 
3501  QSqlQuery *qu = allocateQSqlQuery();
3502 
3503  sqldebug(sqlu);
3504  qu->prepare(sqlu);
3505  qu->exec();
3506 
3507  if(qu->lastError().type() != QSqlError::NoError)
3508  {
3509  if(!tdisabled)
3510  rollback();
3511  errstr=("An error occured during the update! :-(\n>>"+((qu->lastError()).text()));
3512  sdebug("SQLERROR: "+qu->lastError().text());
3513  delete qu;
3514  emit errorSignal(errstr);
3515  sdebug("*** HTable::saveRecord *** ERROR4");
3516  return 1;
3517  }
3518  if(!tdisabled)
3519  commit();
3520 
3521  delete qu;
3522 
3523  //logger begin
3524  if(dclog != NULL)
3525  {
3526  dclog->setTablename(sqlTableName());
3527  dclog->setChangedKey(getKey(false));
3528  }
3529  sqlSynced(dclog);
3530  if(dclog != NULL)
3531  {
3532  dclog->setTablename("EMPTY");
3533  dclog->setChangedKey("");
3534  }
3535  //logger end
3536 
3537  updateWithKey("",all,true,tdisabled);
3538 
3539  //logger write out
3540  if(dclog != NULL)
3541  dclog->writeOut(tdisabled);
3542 
3543  sdebug("*** HTable::saveRecord *** END");
3544  emit dataModifiedSignal();
3545  return 0;
3546 }
3547 
3548 int HTable::insertRecord(bool insertkey,bool disable_tr,bool resetafterinsert)
3549 {
3550  QString sqli="";
3551 
3552  sdebug("*** HTable::insertRecord ***");
3553 
3554  if(!((errstr=validate(false)).isEmpty()))
3555  {
3556  emit errorSignal(errstr);
3557  return 1;
3558  }
3559 
3560  if(!disable_tr)
3561  transaction();
3562 
3563  sqli = sqlInsert(insertkey);
3564  QSqlQuery *qi = allocateQSqlQuery();
3565 
3566  sqldebug(sqli);
3567  qi->prepare(sqli);
3568  qi->exec();
3569 
3570  if(qi->lastError().type() != QSqlError::NoError)
3571  {
3572  if(!disable_tr)
3573  rollback();
3574 
3575  errstr=("An error occured during the insert!\n>>"+((qi->lastError()).text()));
3576  delete qi;
3577  emit errorSignal(errstr);
3578  return 1;
3579  }
3580 
3581  if(reread_key)
3582  {
3583  reread_key = false;
3584 
3585  insertedKey = getKeyValueAfterInsert();
3586  if(query_error_occured)
3587  {
3588  query_error_occured = false;
3589  if(!disable_tr)
3590  rollback();
3591 
3592  insertedKey = "";
3593  emit errorSignal("Error in HTable::getKeyValueAfterInsert !");
3594  delete qi;
3595  return 1;
3596  }
3597  }
3598 
3599  if(!disable_tr)
3600  commit();
3601 
3602  delete qi;
3603  setKey("");
3604  if(resetafterinsert)
3605  {
3606  returnToDefault();
3607  dataUpdated();
3608  emit dataModifiedSignal();
3609  }
3610  sdebug("*** HTable::insertRecord *** END");
3611  return 0;
3612 }
3613 
3614 int HTable::insertRecordAll(bool disable_tr)
3615 {
3616  QString sqli="";
3617 
3618  sdebug("*** HTable::insertRecordAll ***");
3619 
3620  if(!((errstr=validate(true)).isEmpty()))
3621  {
3622  emit errorSignal(errstr);
3623  return 1;
3624  }
3625 
3626  if(!disable_tr)
3627  transaction();
3628 
3629  sqli = sqlInsertAll();
3630 
3631  sqldebug(sqli);
3632 
3633  QSqlQuery *qi = allocateQSqlQuery();
3634  qi->prepare(sqli);
3635  qi->exec();
3636 
3637  if(qi->lastError().type() != QSqlError::NoError)
3638  {
3639  if(!disable_tr)
3640  rollback();
3641 
3642  errstr=("An error occured during the insert!\n>>"+((qi->lastError()).text()));
3643  delete qi;
3644  emit errorSignal(errstr);
3645  return 1;
3646  }
3647 
3648  if(!disable_tr)
3649  commit();
3650 
3651  delete qi;
3652  setKey("");
3653  returnToDefault();
3654  dataUpdated();
3655  emit dataModifiedSignal();
3656  sdebug("*** HTable::insertRecordAll *** END");
3657  return 0;
3658 }
3659 
3660 QString HTable::sqlInsert(bool insertkey)
3661 {
3662  int i=0;
3663  QString ret;
3664 
3665  HDataField_List *tmp=table;
3666  ret = "INSERT INTO "+sqlTable+"(";
3667  for(;tmp != NULL;tmp=tmp->next)
3668  {
3669  if(!insertkey && (tmp->data->getWhoami() == "HKey")) continue;
3670  if(tmp->data->isSQLField()) //field is intrested in sql database
3671  {
3672  if(i != 0) ret.append(",");
3673  ret.append(tmp->data->sqlInsertHead());
3674  i++;
3675  }
3676  }
3677  ret.append(") VALUES(");
3678  tmp=table;
3679  i=0;
3680  for(;tmp != NULL;tmp=tmp->next)
3681  {
3682  if(!insertkey && (tmp->data->getWhoami() == "HKey")) continue;
3683  if(tmp->data->isSQLField()) //field is intrested in sql database
3684  {
3685  if(i != 0) ret.append(",");
3686  ret.append(tmp->data->sqlInsertValue());
3687  i++;
3688  }
3689  }
3690  ret.append(")");
3691  return ret;
3692 }
3693 
3695 {
3696  int i=0;
3697  QString insert;
3698  QString dep;
3699  QString tname;
3700  HDataField_List *run,*mainrun=table;
3701 
3702  QStringList *inserts =new QStringList();
3703  QStringList *dependency =new QStringList();
3704  QStringList *tablenames =new QStringList();
3705  QList<bool> *done =new QList<bool>();
3706 
3707  insert = "";
3708  inserts->clear();
3709  dependency->clear();
3710  tablenames->clear();
3711  while(mainrun != NULL)
3712  {
3713  i=0;
3714  insert = "";
3715  run = mainrun;
3716  if(run->data == NULL)
3717  {
3718  emit errorSignal("Internal error!");
3719  sdebug("ERROR : Internal error!");
3720  return "";
3721  }
3722  tname = run->data->conn_sqlTable;
3723  dep = "NULL";
3724 
3725 
3726  insert.append("INSERT INTO "+run->data->conn_sqlTable+"(");
3727  for(;run != NULL;run=run->next)
3728  {
3729  if(run->data == NULL)
3730  {
3731  emit errorSignal("Internal error!");
3732  sdebug("ERROR : Internal error!");
3733  return "";
3734  }
3735  if(run->data->getWhoami() == "HKey")
3736  {
3737  //The key is generated. We just ignore is
3738  continue;
3739  }
3740 
3741  if(run->data->isSQLField()) //sql related
3742  {
3743  if(i != 0) insert.append(",");
3744  insert.append(run->data->sqlInsertHead());
3745  i++;
3746  }
3747  }
3748  insert.append(") VALUES(");
3749 
3750  i=0;
3751  for(run=mainrun;run != NULL;run=run->next)
3752  {
3753  if(run->data->getWhoami() == "HKey")
3754  {
3755  //The key is generated. We just ignore is
3756  continue;
3757  }
3758  if(run->data->getWhoami() == "HConnect")
3759  {
3760  if(i != 0) insert.append(",");
3761  insert.append(specifyKey(((HConnect *)run->data)->getConnectedTable()));
3762  i++;
3763  dep = ((HConnect *)run->data)->getConnectedTable();
3764  continue;
3765  }
3766 
3767  if(run->data->isSQLField()) //sql related
3768  {
3769  if(i != 0) insert.append(",");
3770  insert.append(run->data->sqlInsertValue());
3771  i++;
3772  }
3773  }
3774 
3775  insert.append(")");
3776 
3777  //Store the current insert with info's.
3778  inserts->push_back(insert);
3779  tablenames->push_back(tname);
3780  dependency->push_back(dep);
3781  done->push_back(false);
3782 
3783  //Step to the next table
3784  while(mainrun->next != NULL)
3785  mainrun=mainrun->next;
3786  mainrun=mainrun->nextall;
3787  }
3788 
3789  insert = "";
3790 
3792  // Dependency check, and sorting //
3793 
3794  int ttl,j;
3795  bool finish=false;
3796 
3797  sdebug("Start dependency analysing...");
3798 
3799  //sdebug("DEP:"+dependency->join(" \n"));
3800  //sdebug("NAMES:"+tablenames->join(" \n"));
3801 
3802  //Collect the independent tables
3803  for(i=0;i<(int)dependency->size();++i)
3804  if( (*dependency)[i] == "NULL")
3805  {
3806  (*done)[i] = true;
3807  insert.append((*inserts)[i] + "; \n");
3808  }
3809 
3810  ttl=done->size()*done->size();
3811  do
3812  {
3813  sdebug(QString("next-round ttl:%1").arg(ttl));
3814  //Look if we have unfinished table
3815  finish = true;
3816  for(i=0;i<(int)done->size();++i)
3817  if(!((*done)[i]))
3818  finish = false;
3819 
3820  //We try todo a new one
3821  if(!finish)
3822  {
3823  for(i=0;i<(int)dependency->size();++i)
3824  if(!((*done)[i])) //Not finished yet
3825  for(j=0;j<(int)dependency->size();++j)
3826  if((*done)[j]) //Finished
3827  if( (*dependency)[i] == (*tablenames)[j] )
3828  {
3829 
3830  (*done)[i] = true;
3831  insert.append((*inserts)[i] + "; \n");
3832  }
3833  }
3834 
3835  if((ttl--) == 0)
3836  {
3837  emit errorSignal("INSERT: dependency error!");
3838  sdebug("ERROR : INSERT: dependency error!");
3839  return "";
3840  }
3841  }
3842  while(!finish);
3843 
3844  delete inserts;
3845  delete dependency;
3846  delete tablenames;
3847  delete done;
3848 
3849  return insert;
3850 }
3851 
3852 QString HTable::getKeyValueAfterInsert(void)
3853 {
3854  sdebug("*** HTable::getKeyValueAfterInsert ***");
3855  QString result,query,keyname="",timestampname="";
3856 
3857  HDataField_List *tmp=table;
3858  for(;tmp != NULL;tmp=tmp->nextall)
3859  {
3860  if(tmp->data->isSQLField()) //sql related
3861  {
3862  if(tmp->data->getWhoami() == "HKey")
3863  keyname = tmp->data->sqlSelectHead();
3864  if(tmp->data->getWhoami() == "HTimestamp" && ((HTimestamp *)tmp->data)->is_checkenabled())
3865  timestampname = tmp->data->sqlSelectHead();
3866  }
3867  }
3868 
3869  if(timestampname.isEmpty())
3870  {
3871  timestampname = keyname;
3872  }
3873 
3874  query = QString("SELECT %1 FROM %2 ORDER BY %4 DESC LIMIT 1")
3875  .arg(keyname)
3876  .arg(sqlTable)
3877  .arg(timestampname);
3878 
3879  result = submit1ResultQuery(query,"Error in HTable::getKeyValueAfterInsert! Cannot get the keyvalue of the inserted record",
3880  true).toString();
3881 
3882  sdebug("*** HTable::getKeyValueAfterInsert *** END");
3883  return result;
3884 }
3885 
3886 
3887 QString HTable::specifyKey(QString tablename)
3888 {
3889  QString ret,keyname,spec,ts;
3890  HDataField_List *run=table;
3891 
3892  while(run != NULL)
3893  {
3894  if(run->data != NULL && run->data->conn_sqlTable == tablename)
3895  break;
3896  run = run->nextall;
3897  }
3898  if(run == NULL)
3899  {
3900  emit errorSignal("Cannot connect tables: Cant find table"+tablename);
3901  return "NULL";
3902  }
3903 
3904  keyname = "";
3905  spec="";
3906  ts = "";
3907 
3908  for(;run != NULL;run = run->next)
3909  {
3910  if(run->data != NULL && run->data->getWhoami() == "HKey")
3911  {
3912  keyname = run->data->sqlSelectHead();
3913  continue;
3914  }
3915  if(run->data != NULL && run->data->getWhoami() == "HTimestamp")
3916  {
3917  ts = run->data->sqlSelectHead();
3918  continue;
3919  }
3920 
3921  if(run->data != NULL && run->data->isSQLField())
3922  {
3923  if(!spec.isEmpty())
3924  spec.append(" AND ");
3925  spec.append(run->data->sqlSelectHead() + "="+run->data->getSQLValue());
3926  }
3927  }
3928 
3929  ret=QString("( SELECT %1 FROM %2 WHERE %3 %4 LIMIT 1)")
3930  .arg(keyname)
3931  .arg(tablename)
3932  .arg(spec)
3933  .arg( (ts.isEmpty() ? "" : (" ORDER BY "+ts+" DESC")) );
3934 
3935  return ret;
3936 }
3937 
3938 void HTable::blockReInitialize(QString hide_marker,QString ro_marker)
3939 {
3940  returnToDefault();
3941  blockReInitializeBase(hide_marker,ro_marker);
3942 }
3943 
3948 {
3949  sdebug("*** HFloatTables::HFloatTables ***");
3950  if(baserec != NULL)
3951  {
3952  baserecord = baserec;
3953  sqlTable = baserecord->sqlTableName();
3954  cloneSqlSettingsFrom(baserec);
3955  }
3956  else
3957  {
3958  emit errorSignal("Error: NULL baserecord passed...");
3959  sdebug("Error in HFloatTables::HFloatTables : NULL baserecord passed...");
3960  baserecord = new HTable("null");
3961  sqlTable = baserecord->sqlTableName();
3962  }
3963 
3964  sts = new QList<int>();
3965  tbls = new QList<HTable *>();
3966 
3967  siter=sts->begin();
3968  titer=tbls->begin();
3969  fiter=true;
3970  sdebug("*** HFloatTables::HFloatTables *** END");
3971 }
3972 
3974 {
3975  HTable *t;
3976  QList<HTable *>::iterator i;
3977  i = tbls->begin();
3978  while(i != tbls->end())
3979  {
3980  t = *(i++);
3981  if(t != NULL)
3982  delete t;
3983  }
3984 
3985  sts->clear();
3986  tbls->clear();
3987  siter=sts->begin();
3988  titer=tbls->begin();
3989  fiter=true;
3990  return 0;
3991 }
3992 
3994 {
3995  sdebug("*** HFloatTables::~HFloatTables ***");
3996  clearMem();
3997  delete sts;
3998  delete tbls;
3999  delete baserecord;
4000  sdebug("*** HFloatTables::~HFloatTables *** END");
4001 }
4002 
4004 {
4005  fiter=true;
4006  siter=sts->begin();
4007  titer=tbls->begin();
4008 }
4009 
4011 {
4012  HTable *t;
4013 
4014  t = NULL;
4015 
4016  if(fiter)
4017  {
4018  siter=sts->begin();
4019  titer=tbls->begin();
4020  fiter = false;
4021  }
4022 
4023  while(titer != tbls->end())
4024  {
4025  if(*siter == FLOATTABLES_STS_ALIVE || *siter == FLOATTABLES_STS_NEW)
4026  if(*titer != NULL)
4027  {
4028  t = *titer;
4029  ++siter;
4030  ++titer;
4031  return t;
4032  }
4033  ++siter;
4034  ++titer;
4035  }
4036  return t;
4037 }
4038 
4040 {
4041  HTable *t;
4042 
4043  if((t = nextRecord()) == NULL)
4044  return "";
4045  return t->getKey(false);
4046 }
4047 
4048 void HFloatTables::setAllField(QString fname,QVariant value)
4049 {
4050  HTable *t;
4051  first();
4052  while((t=nextRecord()) != NULL)
4053  t->fieldBySqlName(fname)->setValue(value);
4054 }
4055 
4057 {
4058  QList<int>::iterator si;
4059  si = sts->begin();
4060  while(si != sts->end())
4061  {
4062  if(*si == FLOATTABLES_STS_ALIVE)
4063  *si = FLOATTABLES_STS_NEW;
4064  ++si;
4065  }
4066 }
4067 
4069 {
4070  return sts->count();
4071 }
4072 
4074 {
4075  int num;
4076 
4077  num=0;
4078 
4079  QList<int>::iterator si;
4080  si = sts->begin();
4081  while(si != sts->end())
4082  {
4083  if(*si == FLOATTABLES_STS_ALIVE || *si == FLOATTABLES_STS_NEW)
4084  ++num;
4085  ++si;
4086  }
4087  return num;
4088 }
4089 
4090 HTable *HFloatTables::getTByField(QString field,QString key)
4091 {
4092  QList<int>::iterator si;
4093  QList<HTable *>::iterator ti;
4094 
4095  ti = tbls->begin();
4096  si = sts->begin();
4097  while(ti != tbls->end())
4098  {
4099  if(*si == FLOATTABLES_STS_ALIVE || *si == FLOATTABLES_STS_NEW)
4100  if( ((*ti)->fieldBySqlName(field))->getValue().toString() == key )
4101  return (*ti);
4102  ++ti;
4103  ++si;
4104  }
4105  return NULL;
4106 }
4107 
4109 {
4110  QList<int>::iterator si;
4111  QList<HTable *>::iterator ti;
4112 
4113  ti = tbls->begin();
4114  si = sts->begin();
4115  while(ti != tbls->end())
4116  {
4117  if(*si == FLOATTABLES_STS_ALIVE || *si == FLOATTABLES_STS_NEW)
4118  if( (*ti)->getKey(false) == key)
4119  return (*ti);
4120  ++ti;
4121  ++si;
4122  }
4123  return NULL;
4124 }
4125 
4127 {
4128  int i;
4129  QList<int>::iterator si;
4130  QList<HTable *>::iterator ti;
4131 
4132  i=0;
4133  ti = tbls->begin();
4134  si = sts->begin();
4135  while(ti != tbls->end())
4136  {
4137  if(*si == FLOATTABLES_STS_ALIVE || *si == FLOATTABLES_STS_NEW)
4138  {
4139  if( i == idx )
4140  return (*ti);
4141  ++i;
4142  }
4143  ++ti;
4144  ++si;
4145  }
4146  return NULL;
4147 }
4148 
4150 {
4151  HTable *newrec;
4152 
4153  newrec=new HTable(baserecord);
4154  newrec->cloneSqlSettingsFrom(this);
4155  tbls->push_back(newrec);
4156  sts->push_back(FLOATTABLES_STS_NEW);
4157 
4158  return newrec;
4159 }
4160 
4162 {
4163  HTable *newrec;
4164 
4165  newrec=new HTable(baserecord);
4166  newrec->cloneSqlSettingsFrom(this);
4167  tbls->push_back(newrec);
4168  sts->push_back(FLOATTABLES_STS_UNDEF);
4169 
4170  return newrec;
4171 }
4172 
4174 {
4175  sdebug("*** HFloatTables::markedRecordToNew ***");
4176  QList<int>::iterator si;
4177  QList<HTable *>::iterator ti;
4178 
4179  ti = tbls->begin();
4180  si = sts->begin();
4181  while(ti != tbls->end())
4182  {
4183  if(*ti == r)
4184  if( *si == FLOATTABLES_STS_UNDEF || *si == FLOATTABLES_STS_ALIVE)
4185  {
4186  *si = FLOATTABLES_STS_NEW;
4187  sdebug("WHOAMI:"+(*ti)->getWhoami());
4188  sdebug("*** HFloatTables::markedRecordToNew *** END.OK");
4189  return;
4190  }
4191  ++ti;
4192  ++si;
4193  }
4194  sdebug("*** HFloatTables::markedRecordToNew *** END.NOTFOUND");
4195 }
4196 
4198 {
4199  sdebug("*** HFloatTables::removeUndef ***");
4200  QList<int>::iterator si;
4201  QList<HTable *>::iterator ti;
4202 
4203  ti = tbls->begin();
4204  si = sts->begin();
4205  while(ti != tbls->end())
4206  {
4207  if(*si == FLOATTABLES_STS_UNDEF)
4208  {
4209  *ti = NULL;
4210  *si = FLOATTABLES_STS_CANCELLED;
4211  }
4212  ++ti;
4213  ++si;
4214  }
4215  sdebug("*** HFloatTables::removeUndef *** END");
4216 }
4217 
4219 {
4220  QList<int>::iterator si;
4221  QList<HTable *>::iterator ti;
4222 
4223  ti = tbls->begin();
4224  si = sts->begin();
4225  while(ti != tbls->end())
4226  {
4227  if(*si == FLOATTABLES_STS_ALIVE || *si == FLOATTABLES_STS_NEW)
4228  {
4229  if( d == *ti )
4230  {
4231  if(*si == FLOATTABLES_STS_ALIVE)
4232  *si = FLOATTABLES_STS_DELETED;
4233  if(*si == FLOATTABLES_STS_NEW)
4234  *si = FLOATTABLES_STS_CANCELLED;
4235  return 0;
4236  }
4237  }
4238  ++ti;
4239  ++si;
4240  }
4241  return 1;
4242 }
4243 
4245 {
4246  return deleteRecord(getTByNum(idx));
4247 }
4248 
4249 int HFloatTables::readDBTable(QString filter,bool tdisabled)
4250 {
4251  sdebug("*** HFloatTables::readDBTable ***");
4252  HTable *t;
4253  QString qs;
4254  QStringList keys;
4255  QSqlQuery *q;
4256 
4257  if(!tdisabled)
4258  transaction();
4259 
4260  qs = QString("SELECT %1 FROM %2 WHERE %3 %4;")
4261  .arg(baserecord->getKey(true))
4262  .arg(sqlTable)
4263  .arg(myInterface()->hsqli_truevalue)
4264  .arg(filter);
4265 
4266  sqldebug(qs);
4267  q = allocateQSqlQuery();
4268  q->prepare(qs);
4269  q->exec(qs);
4270  if(q->lastError().type() != QSqlError::NoError)
4271  {
4272  if(!tdisabled)
4273  rollback();
4274 
4275  delete q;
4276  emit errorSignal("Error: Cannot exec the query in HFloatTables::readDBTable!");
4277  return 1;
4278  }
4279 
4280  keys.clear();
4281  while(q->next())
4282  keys.push_back(q->value(0).toString());
4283 
4284  clearMem();
4285 
4286  QStringList::iterator i;
4287  i = keys.begin();
4288  while(i != keys.end())
4289  {
4290  t = new HTable(baserecord);
4291 
4292  t->setKey(*i);
4293  tbls->push_back(t);
4294  sts->push_back(FLOATTABLES_STS_ALIVE);
4295  if(t->updateWithKeyAll(*i,true,true))
4296  {
4297  if(!tdisabled)
4298  rollback();
4299  delete q;
4300  emit errorSignal("Error: Cannot read a record: "+t->errstr);
4301  return 1;
4302  }
4303  ++i;
4304  }
4305 
4306  if(!tdisabled)
4307  commit();
4308 
4309  sdebug("*** HFloatTables::readDBTable ***END");
4310  return 0;
4311 }
4312 
4313 int HFloatTables::saveDBTable(bool tdisabled)
4314 {
4315  sdebug("*** HFloatTables::saveDBTable ***");
4316  QString k;
4317  QList<int>::iterator si;
4318  QList<HTable *>::iterator ti;
4319 
4320  if(!tdisabled)
4321  transaction();
4322 
4323  ti = tbls->begin();
4324  si = sts->begin();
4325  while(ti != tbls->end())
4326  {
4327  switch(*si)
4328  {
4329  case FLOATTABLES_STS_UNDEF:
4330  break;
4331  case FLOATTABLES_STS_CANCELLED:
4332  break;
4333  case FLOATTABLES_STS_ALIVE:
4334  if((*ti)->isChanged())
4335  if((*ti)->saveRecord(false,true))
4336  {
4337  if(!tdisabled)
4338  rollback();
4339  emit errorSignal("Cannot update a HFloatTable record(Status:ALIVE): "+ (*ti)->errstr );
4340  return 1;
4341  }
4342  break;
4343  case FLOATTABLES_STS_NEW:
4344  (*ti)->rereadNextInsertedKey();
4345  if((*ti)->insertRecord(false,true))
4346  {
4347  if(!tdisabled)
4348  rollback();
4349  emit errorSignal("Cannot insert a HFloatTable record(Status:NEW): "+ (*ti)->errstr );
4350  return 1;
4351  }
4352  k = (*ti)->getInsertedKey();
4353  //READ
4354  if((*ti)->updateWithKeyAll(k,true,true))
4355  {
4356  if(!tdisabled)
4357  rollback();
4358  emit errorSignal("Cannot reread an inserted new record: "+ (*ti)->errstr );
4359  return 1;
4360  }
4361 
4362  *si = FLOATTABLES_STS_ALIVE;
4363  break;
4364  case FLOATTABLES_STS_DELETED:
4365  if(submit0ResultQuery(QString("DELETE FROM %1 WHERE %2=\'%3\';")
4366  .arg(sqlTable)
4367  .arg((*ti)->getKey(true))
4368  .arg((*ti)->getKey(false)),"Err",true))
4369  {
4370  if(!tdisabled)
4371  rollback();
4372  emit errorSignal("Cannot delete erased record!");
4373  return 1;
4374  }
4375  delete *ti;
4376  *ti=NULL;
4377  *si=FLOATTABLES_STS_CANCELLED;
4378  break;
4379 
4380  }
4381  ++ti;
4382  ++si;
4383  }
4384 
4385  if(!tdisabled)
4386  commit();
4387 
4388  sdebug("*** HFloatTables::saveDBTable *** END");
4389  return 0;
4390 }
4391 
4395 
4396 HList::HList(QString t)
4397 : HTableBase(t)
4398 {
4399  sdebug("*** HList::HList ***");
4400  soft_current_key = "";
4401  whoami = "HList";
4402  active_key = "";
4403  keys = new QStringList();
4404  values = new QList<QStringList *> ();
4405 
4406  last_filter = "";
4407  last_all = true;
4408 
4409  if(HRefreshAgent::getNotifyAgent() != NULL)
4410  connect(HRefreshAgent::getNotifyAgent(),SIGNAL(getnotify(QString)),
4411  this,SLOT(captureNotify(QString)));
4412 
4413  query_valiter = values->begin();
4414  query_keyiter = keys->begin();
4415 
4416  sdebug("*** HList::HList *** END");
4417 }
4418 
4420 {
4421  sdebug("*** HList::~HList ***");
4422  freeTable();
4423  if(keys != NULL)
4424  delete keys;
4425  if(values != NULL)
4426  {
4427  for(QList<QStringList *>::iterator it = values->begin();it != values->end();++it)
4428  delete *it;
4429  delete values;
4430  }
4431  sdebug("*** HList::~HList *** END");
4432 }
4433 
4435 : HTableBase("")
4436 {
4437  soft_current_key = "";
4438  whoami = "HList";
4439  active_key = "";
4440  keys = new QStringList();
4441  values = new QList<QStringList *> ();
4442  last_filter = "";
4443  last_all = true;
4444 
4446 
4447  if(HRefreshAgent::getNotifyAgent() != NULL)
4448  connect(HRefreshAgent::getNotifyAgent(),SIGNAL(getnotify(QString)),
4449  this,SLOT(captureNotify(QString)));
4450 }
4451 
4452 
4453 
4455 {
4457  whoami = x->whoami;
4458  clearList();
4459  active_key = x->active_key;
4461  last_filter = x->last_filter;
4462  last_all = x->last_all;
4463 
4464 
4465  for(QList<QStringList *>::iterator it = x->values->begin();it != x->values->end();++it)
4466  { values->push_back(deep_copy_stringlist(new QStringList(),*it)); }
4467 
4468  deep_copy_stringlist(keys,x->keys);
4469  }
4470 
4471 
4472 int HList::captureNotify(QString tblname)
4473 {
4474  sdebug("*** HList::captureNotify ***");
4475  QStringList::iterator i;
4476 
4477  sdebug(QString(" %1 == %2").arg(tblname).arg(sqlTable));
4478  if(tblname == sqlTable)
4479  return reReadList();
4480  i = depend->begin();
4481  while(i != depend->end())
4482  {
4483  if(*i == tblname)
4484  return reReadList();
4485  ++i;
4486  }
4487  return 0;
4488 }
4489 
4491 {
4492  sdebug("*** HList::clearList ***");
4493  for(QList<QStringList *>::iterator it = values->begin();it != values->end();++it)
4494  delete (*it);
4495 
4496  values->clear();
4497  keys->clear();
4498  active_key = "";
4499  sdebug("*** HList::clearList *** END");
4500  return 0;
4501 }
4502 
4504 {
4505  sdebug("*** HList::actLine ***");
4506  emit actionOnRecord(active_key);
4507  sdebug("*** HList::actLine *** END");
4508  return 0;
4509 }
4510 
4512 {
4513  sdebug("*** HList::alternateActLine ***");
4514  emit alternateActionOnRecord(active_key);
4515  sdebug("*** HList::alternateActLine *** END");
4516  return 0;
4517 }
4518 
4519 QString HList::getSqlFieldValue(QString sqln)
4520 {
4521  int idx=0;
4522  HDataField_List *tmp=table;
4523  while(tmp != NULL)
4524  {
4525  if(tmp->data->isSQLField() && tmp->data->isShow())
4526  {
4527  if(tmp->data->sqlSelectHead() == sqln)
4528  {
4529  QStringList *row;
4530 
4531  row = (*values)[getIndexOf(*keys,active_key)];
4532  return (*row)[idx];
4533  }
4534  ++idx;
4535  }
4536  tmp=tmp->nextall;
4537  }
4538  return QString("");
4539 }
4540 
4541 int HList::readList(QString filter,bool all)
4542 {
4543  int serial;
4544  sdebug("*** HList::readList ***");
4545 
4546  last_filter = filter;
4547  last_all = all;
4548 
4549  QStringList *row_values;
4550  QString row_key;
4551 
4552 #ifdef PROGRESS_POSSIBILITY
4553  emit startWorking();
4554 #endif
4555 
4556  for(QList<QStringList *>::iterator it = values->begin();it != values->end();++it)
4557  delete *it;
4558  values->clear();
4559  keys->clear();
4560 
4561  /*Have to step through on the data fields and examine if any field have any sql dependency.
4562  for example if an sqlchoose field found it is necessary to read de depended table
4563  because it can contains a new value which is are not in the table on the last read */
4564  HDataField_List *refresh_tmp=table;
4565  while(refresh_tmp != NULL)
4566  {
4567  if(refresh_tmp->data->getWhoami() == "HSqlChoose")
4568  ((HSqlChoose *)refresh_tmp->data)->refreshSqlValues(false);
4569  refresh_tmp=refresh_tmp->next;
4570  }
4571 
4572  //That's it
4573 #ifdef PROGRESS_POSSIBILITY
4574  emit doWorking();
4575 #endif
4576 
4577  int idx;
4578  HDataField_List *tmp=NULL;
4579  QString sqlq;
4580 
4581  transaction();
4582 
4583  if(all)
4584  sqlq=sqlSelectAll(filter);
4585  else
4586  sqlq=sqlSelect(filter);
4587 
4588  sqlq.append(";");
4589 
4590  //Make the copnnection and send the command and check the status...
4591  QSqlQuery *q = allocateQSqlQuery();
4592  sqldebug(sqlq);
4593  q->prepare(sqlq);
4594  q->exec();
4595 
4596  if(q->lastError().type() != QSqlError::NoError)
4597  {
4598  #ifdef PROGRESS_POSSIBILITY
4599  emit endWorking();
4600  #endif
4601 
4602  rollback();
4603  errstr=("An error occured during the reading of list!\n>>"+((q->lastError()).text()));
4604  delete q;
4605  emit errorSignal(errstr);
4606  sdebug("*** HList::readList *** END");
4607  return 1;
4608  }
4609 
4610  serial = 1;
4611  while(q->next()) //record cycle
4612  {
4613 
4614  #ifdef PROGRESS_POSSIBILITY
4615  emit doWorking();
4616  #endif
4617 
4618  idx=0;
4619  tmp=table;
4620  row_key="";
4621  row_values=new QStringList();
4622  //row_values->clear();
4623  for(;tmp != NULL;) //field cycle
4624  {
4625  if(tmp->data != NULL && tmp->data->isSQLField())
4626  {
4627  if(tmp->data->getWhoami() == "HSmallText" && tmp->data->sqlSelectHead() == "NON_SQL_LIST_SERIAL")
4628  {
4629  row_values->push_back( QString("%1").arg(serial++) );
4630  if(all)
4631  tmp=tmp->nextall;
4632  else
4633  tmp=tmp->next;
4634  continue;
4635  }
4636 
4637  if(tmp->data->getWhoami() == "HKey" && row_key.isEmpty())
4638  {
4639  row_key = q->value(idx).toString();
4640  }
4641  if(tmp->data->isShow())
4642  {
4643  row_values->push_back( tmp->data->dbValueToDispValue(q->value(idx)).toString() );
4644  }
4645 
4646  idx++;
4647  }
4648 
4649  if(all)
4650  tmp=tmp->nextall;
4651  else
4652  tmp=tmp->next;
4653  }
4654 
4655  values->push_back(row_values);
4656  keys->push_back(row_key);
4657  row_values = NULL;
4658  }
4659  sdebug(QString("Fetched %1 values %2 keys.")
4660  .arg(values->size())
4661  .arg(keys->size()));
4662 
4663  delete q;
4664  commit();
4665 
4666  #ifdef PROGRESS_POSSIBILITY
4667  emit doWorking();
4668  #endif
4669 
4670  dataUpdated();
4671 
4672  #ifdef PROGRESS_POSSIBILITY
4673  emit endWorking();
4674  #endif
4675 
4676  sdebug("*** HList::readList *** END");
4677  return 0;
4678 }
4679 
4680 QString HList::operator [](int i)
4681 {
4682  int idx=0;
4683  HDataField_List *tmp=table;
4684  while(tmp != NULL)
4685  {
4686  if(tmp->data->isSQLField() && tmp->data->isShow())
4687  {
4688  if(idx == i)
4689  {
4690  if(tmp->data == NULL) return QString("");
4691  else return tmp->data->getTitleText();
4692  }
4693  ++idx;
4694  }
4695  tmp=tmp->nextall;
4696  }
4697  return QString("");
4698 
4699 }
4700 
4701 QString HList::activeKey(void)
4702 {
4703  return active_key;
4704 }
4705 
4706 void HList::setActiveKey(QString k)
4707 {
4708  sdebug("*** HList::setActiveKey to "+k);
4709  active_key = k;
4710 }
4711 
4713 {
4714  query_valiter = values->begin();
4715  query_keyiter = keys->begin();
4716 }
4717 
4719 {
4720  ++query_valiter;
4721  ++query_keyiter;
4722  if(query_valiter == values->end() ||
4723  query_keyiter == keys->end())
4724  return false;
4725  return true;
4726 }
4727 
4729 {
4730  if(queryNextRecord())
4731  return queryCurrKey();
4732  return QString();
4733 
4734 }
4735 
4737 {
4738  if(queryNextRecord())
4739  return queryCurrValues();
4740  return NULL;
4741 
4742 }
4743 
4745 {
4746  if(query_keyiter != keys->end())
4747  return (*query_keyiter);
4748  return QString();
4749 }
4750 
4752 {
4753  if(query_valiter != values->end())
4754  return (*query_valiter);
4755  return NULL;
4756 }
4757 
4758 
4762 
4763 HFloatTablesAsHList::HFloatTablesAsHList(HFloatTables *memt,HList *templ,bool clone_struct_from_memt)
4764 : HList(memt->sqlTableName())
4765 {
4766  sdebug("*** HFloatTablesAsHList::HFloatTablesAsHList ***");
4767 
4768  mem = memt;
4769  cloneSqlSettingsFrom(mem);
4770 
4771  if(templ != NULL)
4772  deepcopy_from_hlist(templ);
4773  if(templ == NULL && clone_struct_from_memt)
4774  {
4776  whoami = "HList";
4777  }
4778 
4779  sdebug("*** HFloatTablesAsHList::HFloatTablesAsHList *** END");
4780 }
4781 
4783 {
4784  sdebug("*** HFloatTablesAsHList::~HFloatTablesAsHList ***");
4785 
4786  sdebug("*** HFloatTablesAsHList::~HFloatTablesAsHList *** END");
4787 
4788 }
4789 
4791 {
4792  int idx;
4793  sdebug("active_key:"+active_key);
4794  sdebug("soft_current_key:"+soft_current_key);
4795 
4796  idx = getIndexOf(*keys,soft_current_key);
4797  return mem->getTByNum(idx);
4798 }
4799 
4801 {
4802  QList<HTable *>::iterator ti;
4803  QList<int>::iterator si;
4804 
4805  int serial;
4806 
4807  QStringList *row_values;
4808 
4809  //deleting
4810  for(QList<QStringList *>::iterator it = values->begin();it != values->end();++it)
4811  delete *it;
4812  values->clear();
4813  keys->clear();
4814 
4815  int idx;
4816  HDataField_List *tmp=NULL;
4817 
4818  serial = 1;
4819  ti = mem->tbls->begin();
4820  si = mem->sts->begin();
4821 
4822  while(ti != mem->tbls->end())
4823  {
4824  if(*si == FLOATTABLES_STS_ALIVE || *si == FLOATTABLES_STS_NEW )
4825  {
4826  idx=0;
4827  tmp=table;
4828  row_values=new QStringList();
4829  for(;tmp != NULL;) //field cycle
4830  {
4831  if(tmp->data != NULL && tmp->data->isSQLField())
4832  {
4833  if(tmp->data->getWhoami() == "HSmallText" && tmp->data->sqlSelectHead() == "NON_SQL_LIST_SERIAL")
4834  {
4835  row_values->push_back( QString("%1").arg(serial) );
4836  tmp=tmp->next;
4837  continue;
4838  }
4839 
4840  if(tmp->data->isShow())
4841  {
4842  row_values->push_back(
4843  tmp->data->dbValueToDispValue(
4844  ((*ti)->fieldBySqlName(tmp->data->sqlSelectHead()))->getValue()
4845  ).toString()
4846  );
4847  }
4848 
4849  idx++;
4850  }
4851  tmp=tmp->next;
4852  }
4853 
4854 
4855  keys->push_back(QString("temp_%1").arg(serial));
4856  values->push_back(row_values);
4857 
4858  row_values = NULL;
4859  ++serial;
4860  }
4861  ++ti;
4862  ++si;
4863  }
4864 
4865  /*
4866  sdebug(QString("NUM k.%1 v.%2").arg(keys->size()).arg(values->size()));
4867  QStringList::iterator i1,i3;
4868  QList<QStringList *>::iterator i2;
4869  i1=keys->begin();
4870  for(;i1 != keys->end();i1++)
4871  sdebug("KEY: "+ *i1);
4872 
4873  i2 = values->begin();
4874  for(;i2 != values->end();i2++)
4875  {
4876  i3 = (*i2)->begin();
4877  for(;i3 != (*i2)->end();i3++)
4878  sdebug("VAL:"+*i3);
4879  }
4880  */
4881 
4882  if(updSignal)
4883  dataUpdated();
4884 
4885  sdebug(QString("Converted %1 values %2 keys.").arg(values->size()).arg(keys->size()));
4886 }
4887 
4888 QString HFloatTablesAsHList::getSqlFieldValue(QString sqln)
4889 {
4890  Q_UNUSED(sqln);
4891  // !-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!
4892  // This function disabled here
4893  // There can be keys in the memory which are non existing in the sql/HFloatTable. So we can't search of its!
4894  // !-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!
4895  return "";
4896 }
4897 
4898 
4900 {
4901  // !-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!
4902  // This function disabled here
4903  // You should erase the data in HFloatTable!
4904  // !-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!
4906  return 0;
4907 }
4908 
4909 int HFloatTablesAsHList::readList(QString filter,bool all)
4910 {
4911  last_filter = filter;
4912  last_all = all;
4913 
4914 #ifdef PROGRESS_POSSIBILITY
4915  emit startWorking();
4916 #endif
4917 
4918  mem->readDBTable(filter);
4919 
4921 
4922 #ifdef PROGRESS_POSSIBILITY
4923  emit endWorking();
4924 #endif
4925 
4926  return 0;
4927 }
4928 
4933 {
4934  name = "";
4935  mod = "";
4936  val = "";
4937 }
4938 
4939 HDataFieldRole::HDataFieldRole(QString name, QString mod, QString val)
4940 {
4941  this->name = name;
4942  this->mod = mod;
4943  this->val = val;
4944 }
4945 
4946 HDataFieldRole::~HDataFieldRole(void)
4947 {
4948 }
4949 
4951 {
4952  if(mod.contains("hide;",Qt::CaseInsensitive))
4953  mod.replace("hide;","",Qt::CaseInsensitive);
4954  if(!mod.contains("show;",Qt::CaseInsensitive))
4955  mod.prepend("show;");
4956 }
4957 
4959 {
4960  if(mod.contains("show;",Qt::CaseInsensitive))
4961  mod.replace("show;","",Qt::CaseInsensitive);
4962  if(!mod.contains("hide;",Qt::CaseInsensitive))
4963  mod.prepend("hide;");
4964 }
4965 
4967 {
4968  if(mod.contains("readonly;",Qt::CaseInsensitive))
4969  mod.replace("readonly;","",Qt::CaseInsensitive);
4970  if(!mod.contains("editable;",Qt::CaseInsensitive))
4971  mod.prepend("editable;");
4972 }
4974 {
4975  if(mod.contains("editable;",Qt::CaseInsensitive))
4976  mod.replace("editable;","",Qt::CaseInsensitive);
4977  if(!mod.contains("readonly;",Qt::CaseInsensitive))
4978  mod.prepend("readonly;");
4979 }
4980 
4982 {
4983  if(mod.contains("guidisabled;",Qt::CaseInsensitive))
4984  mod.replace("guidisabled;","",Qt::CaseInsensitive);
4985  if(!mod.contains("guienabled;",Qt::CaseInsensitive))
4986  mod.prepend("guienabled;");
4987 }
4988 
4990 {
4991  if(mod.contains("guienabled;",Qt::CaseInsensitive))
4992  mod.replace("guienabled;","",Qt::CaseInsensitive);
4993  if(!mod.contains("guidisabled;",Qt::CaseInsensitive))
4994  mod.prepend("guidisabled;");
4995 }
4996 
4998 {
4999  if(mod.contains("setval;",Qt::CaseInsensitive))
5000  mod.replace("setval;","",Qt::CaseInsensitive);
5001  if(!mod.contains("reset;",Qt::CaseInsensitive))
5002  mod.prepend("reset;");
5003 }
5004 
5006 {
5007  if(mod.contains("reset;",Qt::CaseInsensitive))
5008  mod.replace("reset;","",Qt::CaseInsensitive);
5009  if(!mod.contains("setval;",Qt::CaseInsensitive))
5010  mod.prepend("setval;");
5011 }
5012 
5014 {
5015  if(mod.contains("show;",Qt::CaseInsensitive))
5016  return true;
5017  return false;
5018 }
5019 
5021 {
5022  if(mod.contains("hide;",Qt::CaseInsensitive))
5023  return true;
5024  return false;
5025 }
5026 
5028 {
5029  if(mod.contains("editable;",Qt::CaseInsensitive))
5030  return true;
5031  return false;
5032 }
5033 
5035 {
5036  if(mod.contains("readonly;",Qt::CaseInsensitive))
5037  return true;
5038  return false;
5039 }
5040 
5042 {
5043  if(mod.contains("guienabled;",Qt::CaseInsensitive))
5044  return true;
5045  return false;
5046 }
5047 
5049 {
5050  if(mod.contains("guidisabled;",Qt::CaseInsensitive))
5051  return true;
5052  return false;
5053 }
5054 
5056 {
5057  if(mod.contains("reset;",Qt::CaseInsensitive))
5058  return true;
5059  return false;
5060 }
5061 
5063 {
5064  if(mod.contains("setval;",Qt::CaseInsensitive))
5065  return true;
5066  return false;
5067 }
5068 
5072 
5073 HDataField::HDataField(QString _sqlColumn,QString _explain,QString tit,
5074  QString _tailstr,bool _editable)
5075 {
5076  whoami="HDataField";
5077  set = 0;
5078  sqlColumn = _sqlColumn;
5079  explain = _explain;
5080  tailstr = _tailstr;
5081  title = tit;
5082  editable = _editable;
5083  color_r = 180;
5084  color_g = 180;
5085  color_b = 180;
5086  sql = true;
5087  show = true;
5088  guienabled= true;
5089  connected = false;
5090  meldwithnext = false;
5091  nomiddlestretch = false;
5092  editboxmaxwidth = 0;
5093  conn_sqlTable = "";
5094  defval = "";
5095  valid = "";
5096  notvalid.clear();
5097 
5098  lastvalidate_result = false;
5099  permanentvalidate_result = false;
5100  manual_validate_error = "";
5101 
5102  spec_disp_sort = "";
5103 
5104  wcellw_print = 0;
5105  wcell_print = false;
5106 
5107  function = "";
5108  derived = "";
5109 
5110  markers.clear();
5111  roles.clear();
5112 
5113  logged = false;
5114  sync_valueInSql = false;
5115  valueInSql = "NOT_VALID";
5116 }
5117 
5119 {
5121 }
5122 
5124 {
5126 
5127  whoami = x->whoami;
5128  set = x->set;
5129  sqlColumn = x->sqlColumn;
5130  explain = x->explain;
5131  tailstr = x->tailstr;
5132  title = x->title;
5133  editable = x->editable;
5134  color_r = x->color_r;
5135  color_g = x->color_g;
5136  color_b = x->color_b;
5137  sql = x->sql;
5138  show = x->show;
5139  guienabled= x->guienabled;
5140 
5141  defval = x->defval;
5142  valid = x->valid;
5143  notvalid = x->notvalid;
5144 
5145  wcellw_print = x->wcellw_print;
5146  wcell_print = x->wcell_print;
5147 
5151 
5152  function = x->function;
5153 
5154  derived = x->derived;
5155 
5157 
5158  markers = x->markers;
5159  lastvalidate_result = x->lastvalidate_result;
5160  permanentvalidate_result = x->permanentvalidate_result;
5161  manual_validate_error = x->manual_validate_error;
5162 
5163  //The connection data is not copied
5164  connected = false;
5165  conn_sqlTable = "";
5166 
5167  roles.clear();
5168  roles = QList<HDataFieldRole>(roles);
5169 
5170  logged = x->logged;
5172  valueInSql = x->valueInSql;
5173 }
5174 
5176 {
5177  return new HDataField(this);
5178 }
5179 
5181 {
5182  //empty
5183 }
5184 
5186 {
5187  valid = v;
5188 }
5189 
5190 void HDataField::setNotValidValues(QStringList nvl)
5191 {
5192  notvalid.clear();
5193  notvalid = nvl;
5194 }
5195 
5197 {
5198  notvalid.push_back(v);
5199 }
5200 
5202 {
5203  notvalid.clear();
5204 }
5205 
5207 {
5208  lastvalidate_result = true;
5209  permanentvalidate_result = lastvalidate_result;
5210  manual_validate_error = message;
5211 }
5212 
5213 
5214 void HDataField::setFunction(QString func)
5215 {
5216  if(acceptFunction())
5217  {
5218  sdebug("HDataField::setFunction - Function accepted");
5219  function = func;
5220  }
5221  else
5222  {
5223  sdebug(QString("Warning: HDataField::setFunction - Function definitions rejected! (Cannot set function on \"%1\")")
5224  .arg(getWhoami()));
5225  }
5226 }
5227 
5228 void HDataField::setExplainText(QString expl)
5229 {
5230  explain = expl;
5232 }
5233 
5234 void HDataField::setTitleText(QString titl)
5235 {
5236  title = titl;
5238 }
5239 
5240 void HDataField::setTailText(QString tail)
5241 {
5242  tailstr = tail;
5244 }
5245 
5246 void HDataField::setSubselect(QString subs)
5247 {
5248  if(acceptSubselect())
5249  {
5250  sdebug("HDataField::setSubselect - Subselect accepted");
5251  derived = subs;
5252  }
5253  else
5254  {
5255  sdebug(QString("Warning: HDataField::setSubselect - Subselect definitions rejected! (Cannot set subselect on \"%1\")")
5256  .arg(getWhoami()));
5257  }
5258 }
5259 
5261 {
5262  QString d;
5263  QRegExp re(valid);
5264 
5265  d = "";
5266  if(!valid.isEmpty())
5267  {
5268  //Regex check
5269  if( !re.isValid() )
5270  d = "Regular Expression is invalid!";
5271 
5272  if( ! re.exactMatch(getValue().toString()) )
5273  d = ("ERROR: The \""+explain+"\" field contains not valid data: \""+getValue().toString()+"\" ! ");
5274  }
5275 
5276  //Notvalid values check
5277  if(d.isEmpty())
5278  {
5279  QStringList::iterator iter=notvalid.begin();
5280  while(iter != notvalid.end())
5281  {
5282  if(getValue().toString() == *iter )
5283  d = "ERROR: The \""+explain+"\" field contains a NOTVALID data: \""+getValue().toString()+"\" ! ";
5284  ++iter;
5285  }
5286  }
5287 
5288  if(d.isEmpty() && !manual_validate_error.isEmpty())
5289  {
5290  d = manual_validate_error;
5291  }
5292  manual_validate_error = "";
5293 
5294  if(d.isEmpty())
5295  {
5296  d = lowerValidate();
5297  }
5298 
5299  if(d.isEmpty())
5300  lastvalidate_result = false;
5301  else
5302  lastvalidate_result = true;
5303 
5304  permanentvalidate_result = lastvalidate_result;
5305  return d;
5306 }
5307 
5308 
5310 {
5311  return v; //default action (will be redefined in chield classes)
5312 }
5313 
5315 {
5316  QString dv;
5317  notSet();
5318  dv = getDefaultValue();
5319  setDefval(dv);
5321 
5322  emit dataChanged();
5323  return 0;
5324 }
5325 
5326 QVariant HDataField::defvalParser(QString s)
5327 {
5328  QString sqlq;
5329  QVariant v;
5330 
5331  defval = s; //save the default value
5332  QRegExp rx;
5333  rx.setPattern("^DEFVAL_SQL\\((.*)\\)$");
5334  if(rx.indexIn(s) == -1 || rx.cap(1).isEmpty()) //no match
5335  {
5336  v=s;
5337  return v;
5338  }
5339 
5340  sqlq = rx.cap(1);
5341  transaction();
5342  QSqlQuery *q = allocateQSqlQuery();
5343  sqldebug(sqlq);
5344  q->prepare(sqlq);
5345  q->exec();
5346 
5347  if((myInterface()->hsqli_usequerysize && q->numRowsAffected() != 1 ) ||
5348  ! q->next() ||
5349  q->lastError().type() != QSqlError::NoError)
5350  {
5351  QString errstr;
5352  rollback();
5353  errstr=("The SELECT returned with !=1 record! (1)\n>>"+((q->lastError()).text()));
5354  delete q;
5355  v="";
5356  emit errorSignal(errstr);
5357  return v;
5358  }
5359 
5360 
5361  v=q->value(0);
5362  commit();
5363  delete q;
5364  value_changed();
5365  return v;
5366 }
5367 
5369  {
5371  }
5372 
5373 void HDataField::setData(QString _sqlColumn,QString _explain,QString tit,
5374  QString _tailstr,bool _editable)
5375 {
5376  sqlColumn = _sqlColumn;
5377  title = tit;
5378  explain = _explain;
5379  tailstr = _tailstr;
5380  editable = _editable;
5381 }
5382 
5383 void HDataField::setColor(int r,int g,int b)
5384 {
5385  color_r = r;
5386  color_g = g;
5387  color_b = b;
5388 }
5389 
5390 int HDataField::setValue(QVariant v,bool sdisabled)
5391 {
5392  Q_UNUSED(v);
5393  //I don't have any type yet...
5394  if(!sdisabled)
5395  emit dataChanged();
5396  set = 1;
5397  value_changed();
5398  return -1;
5399 }
5400 
5401 int HDataField::setDefval(QVariant v)
5402 {
5403  Q_UNUSED(v);
5404  //I don't have any type yet...
5405  return -1;
5406 }
5407 
5409 {
5410  return QString("");
5411 }
5412 
5414 {
5415  return QString("");
5416 }
5417 
5418 QVariant HDataField::getValue (void)
5419 {
5420  return QVariant(0);
5421 }
5422 
5424 {
5425  return QString("%1").arg(sqlColumn);
5426 }
5427 
5429 {
5430  return QString("NULL");
5431 }
5432 
5434 {
5435  return QString("%1=NULL").arg(sqlColumn);
5436 }
5437 
5439 {
5440  return QString("%1").arg(sqlColumn);
5441 }
5442 
5444 {
5445  editable = true;
5446 }
5447 
5449 {
5450  editable = false;
5451 }
5452 
5453 QString HDataField::sqlCreateStringPart(QString switches)
5454 {
5455  Q_UNUSED(switches);
5456 
5457  return sqlColumn + "\t TYPE";
5458 }
5459 
5460 void HDataField::addMark(QString mark)
5461 {
5462  if(!mark.isEmpty())
5463  markers.push_back(mark);
5464 }
5465 
5466 void HDataField::addMark(QStringList pmarkers)
5467 {
5468  if(!pmarkers.isEmpty())
5469  markers += pmarkers;
5470 }
5471 
5472 bool HDataField::hasMark(QString mark)
5473 {
5474  if(markers.contains(mark) > 0)
5475  return true;
5476  return false;
5477 }
5478 
5480 {
5481  markers.clear();
5482 }
5483 
5484 QStringList HDataField::allMarker(void)
5485 {
5486  return markers;
5487 }
5488 
5490 {
5491  roles.push_back(role);
5492 }
5493 
5494 void HDataField::delRole(QString name)
5495 {
5496  QList<HDataFieldRole>::iterator i = roles.begin();
5497  while(i != roles.end())
5498  {
5499  if(i->name == name)
5500  {
5501  roles.erase(i);
5502  delRole(name); //del other occurences...
5503  return;
5504  }
5505  ++i;
5506  }
5507 }
5508 
5510 {
5511  roles.clear();
5512 }
5513 
5514 void HDataField::useRole(QString name)
5515 {
5516  sdebug("*** HDataField::useRole ***");
5517  HDataFieldRole role;
5518  if(name.isEmpty())
5519  return;
5520 
5521  QList<HDataFieldRole>::iterator i = roles.begin();
5522  while(i != roles.end())
5523  {
5524  if(i->name == name)
5525  {
5526  role = *i;
5527 
5528  if(role.hasShowMod())
5529  setShow();
5530  if(role.hasHideMod())
5531  setHide();
5532 
5533  if(role.hasEditableMod())
5534  setEditable();
5535  if(role.hasReadonlyMod())
5536  setReadonly();
5537 
5538  if(role.hasGuienabledMod())
5539  {
5540  setGuienabled();
5542  }
5543  if(role.hasGuidisabledMod())
5544  {
5545  setGuidisabled();
5547  }
5548 
5549  if(role.hasResetMod())
5550  returnToDefault();
5551  if(role.hasSetvalMod())
5552  {
5553  notSet();
5554  setDefval(role.val);
5556  emit dataChanged();
5557  }
5558 
5559  return;
5560  }
5561  ++i;
5562  }
5563 }
5564 
5565 void HDataField::sqlSynced(HDataChangeLogger *dclog,bool forcechange,bool forcelog)
5566 {
5567  if( dclog != NULL && (sync_valueInSql || forcechange) && (logged || forcelog) )
5569  {
5570  /*Log*/
5571  sdebug(QString("DEBUG DataChangeLog: %1 field: \"%2\" to \"%3\" ---")
5572  .arg(sqlInsertHead())
5573  .arg(valueInSql)
5574  .arg(getSQLValueAsString()));
5575 
5576  dclog->addChange(sqlInsertHead(),valueInSql,getSQLValueAsString());
5577  }
5578 
5579  sync_valueInSql = true;
5581 }
5582 
5584 {
5585  sync_valueInSql = false;
5586  valueInSql = "CLEARED";
5587 }
5588 
5590 {
5591 
5593  return true;
5594  return false;
5595 }
5596 
5600 
5601 HKey::HKey(QString sqlc,QString ex,QString tit,QString def)
5602  : HDataField(sqlc,ex,tit,"",false)
5603 {
5604  whoami="HKey";
5605  defval = def;
5606  set = 0;
5607  setDefval(def);
5608  editable = false;
5609  sql = true;
5610 }
5611 
5613 {
5614  //empty
5615 }
5616 
5618 : HDataField(t)
5619 {
5620  deepcopy_from(t);
5621 }
5622 
5624 {
5625  value = x->value;
5626 }
5627 
5629 {
5630  return new HKey(this);
5631 }
5632 
5633 int HKey::setValue(QVariant v,bool sdisabled)
5634 {
5635  set = 1;
5636  value = v.toString();
5637  if(!sdisabled)
5638  emit dataChanged();
5639  value_changed();
5640  return 0;
5641 }
5642 
5643 int HKey::setDefval(QVariant v)
5644 {
5645  if(!set) //if the value was set I dont set the default.
5646  value = defvalParser( v.toString() ).toString();
5647 
5648  return 0;
5649 }
5650 
5651 QVariant HKey::getValue (void)
5652 {
5653  return QVariant(value);
5654 }
5655 
5656 QString HKey::getSQLValue (void)
5657 {
5658  if(!myInterface()->hsqli_hkeytype_varchar_int)
5659  return QString("%1").arg(value);
5660 
5661  return QString("\'%1\'").arg(value);
5662 }
5663 
5665 {
5666  return QString("%1").arg(value);
5667 }
5668 
5669 QString HKey::sqlInsertHead(void)
5670 {
5671  return QString("%1").arg(sqlColumn);
5672 }
5673 
5675 {
5676  bool ok;
5677  QString r;
5678  if(myInterface()->hsqli_hkeytype_varchar_int)
5679  return QString("\'%1\'").arg(value);
5680  r = QString("%1").arg(value.toInt(&ok));
5681  if(!ok)
5682  return QString::null;
5683  return r;
5684 
5685 }
5686 
5687 QString HKey::sqlUpdate(void)
5688 {
5689  return QString::null; //We don't modify the key. (autogenerated)
5690 }
5691 
5692 QString HKey::sqlSelectHead(void)
5693 {
5694  return QString("%1").arg(sqlColumn);
5695 }
5696 
5697 QString HKey::sqlCreateStringPart(QString switches)
5698 {
5699  Q_UNUSED(switches);
5700 
5701  QString type;
5702 
5703  if(myInterface()->hsqli_hkeytype_varchar_int)
5704  type = QString("VARCHAR(%1)").arg(myInterface()->hsqli_varcharhkeylength);
5705  else
5707 
5708  return (QString("%1\t %2 %3")
5709  .arg(sqlColumn)
5710  .arg(type)
5711  .arg(myInterface()->hsqli_hkeyconstraint));
5712 }
5713 
5717 
5718 HConnect::HConnect(QString sqlc,QString connected_tablep)
5719  : HDataField(sqlc,"","","",true)
5720 {
5721  whoami="HConnect";
5722  connected_table = connected_tablep;
5723  set = 0;
5724  setDefval("");
5725  editable = false; //Not editable
5726  show = false; //Hided
5727  sql = true; //It's an sql field
5728 }
5729 
5731 {
5732  //empty
5733 }
5734 
5736 :HDataField(t)
5737 {
5738  deepcopy_from(t);
5739 }
5740 
5742 {
5743  connected_table = x->connected_table;
5744  value = x->value;
5745 }
5746 
5748 {
5749  return new HConnect(this);
5750 }
5751 
5752 int HConnect::setValue (QVariant v,bool sdisabled)
5753 {
5754  set = 1;
5755  value = v.toString();
5756  if(!sdisabled)
5757  emit dataChanged();
5758  value_changed();
5759  return 0;
5760 }
5761 
5762 int HConnect::setDefval(QVariant v)
5763 {
5764  Q_UNUSED(v);
5765 
5766  if(!set) //if the value was set I dont set the default.
5767  value = "";
5768  return 0;
5769 }
5770 
5771 QVariant HConnect::getValue (void)
5772 {
5773  return QVariant(value);
5774 }
5775 
5777 {
5778  return QString("\'%1\'").arg(value);
5779 }
5780 
5782 {
5783  return QString("%1").arg(value);
5784 }
5785 
5787 {
5788  return QString("%1").arg(sqlColumn);
5789 }
5790 
5792 {
5793  if(value.isEmpty())
5794  return QString("NULL");
5795  return QString("\'%1\'").arg(value);
5796 }
5797 
5798 QString HConnect::sqlUpdate(void)
5799 {
5800  return QString::null;
5801 }
5802 
5804 {
5805  return QString("%1").arg(sqlColumn);
5806 }
5807 
5808 QString HConnect::sqlCreateStringPart(QString switches)
5809 {
5810  Q_UNUSED(switches);
5811  QString type;
5812 
5813  if(myInterface()->hsqli_hkeytype_varchar_int)
5814  type = QString("VARCHAR(%1)").arg(myInterface()->hsqli_varcharhkeylength);
5815  else
5817 
5818  return (QString("%1\t %2")
5819  .arg(sqlColumn)
5820  .arg(type));
5821 }
5825 
5826 HSmallText::HSmallText(QString sqlc,QString ex,QString tit,QString t,QString def)
5827  : HDataField(sqlc,ex,tit,t,true)
5828 {
5829  whoami="HSmallText";
5830  defval = def;
5831  set = 0;
5832  setDefval(def);
5833  editable = true;
5834  sql = true;
5835  initial_selected = false;
5836 }
5837 
5839 {
5840  //empty
5841 }
5842 
5844 :HDataField(t)
5845 {
5846  deepcopy_from(t);
5847 }
5848 
5850 {
5851  value = x->value;
5852  initial_selected = x->initial_selected;
5853 }
5854 
5856 {
5857  return new HSmallText(this);
5858 }
5859 
5860 int HSmallText::setValue (QVariant v,bool sdisabled)
5861 {
5862  set = 1;
5863  value = v.toString();
5864  if(!sdisabled)
5865  emit dataChanged();
5866  value_changed();
5867  return 0;
5868 }
5869 
5871 {
5872  return QVariant( v.toString() + (tailstr.isEmpty() ? "" : (" " + tailstr)) );
5873 }
5874 
5875 int HSmallText::setDefval(QVariant v)
5876 {
5877  if(!set) //if the value was set I dont set the default.
5878  value = defvalParser( v.toString() ).toString();
5879  return 0;
5880 }
5881 
5882 QVariant HSmallText::getValue (void)
5883 {
5884  return QVariant(value);
5885 }
5886 
5888 {
5889  return QString("\'%1\'").arg(value);
5890 }
5891 
5893 {
5894  return QString("%1").arg(value);
5895 }
5896 
5898 {
5899  return QString("%1").arg(sqlColumn);
5900 }
5901 
5903 {
5904  return QString("\'%1\'").arg(value);
5905 }
5906 
5908 {
5909  return QString("%1=\'%2\'")
5910  .arg(sqlColumn)
5911  .arg(value);
5912 }
5913 
5915 {
5916  return QString("%1").arg(sqlColumn);
5917 }
5918 
5919 QString HSmallText::sqlCreateStringPart(QString switches)
5920 {
5921  Q_UNUSED(switches);
5922  return sqlColumn + QString("\t VARCHAR(%1)").arg(myInterface()->hsqli_smalltext_varcharlength);
5923 }
5924 
5928 
5929 HLargeText::HLargeText(QString sqlc,QString ex,QString tit,QString def)
5930  : HDataField(sqlc,ex,tit,"",true)
5931 {
5932  whoami="HLargeText";
5933  defval = def;
5934  set = 0;
5935  setDefval(def);
5936  editable = true;
5937  sql = true;
5938 }
5939 
5941 {
5942  //empty
5943 }
5944 
5946 :HDataField(t)
5947 {
5948  deepcopy_from(t);
5949 }
5950 
5952 {
5953  value = x->value;
5954 }
5955 
5957 {
5958  return new HLargeText(this);
5959 }
5960 
5961 int HLargeText::setValue (QVariant v,bool sdisabled)
5962 {
5963  set = 1;
5964  value = v.toString();
5965  if(!sdisabled)
5966  emit dataChanged();
5967  value_changed();
5968  return 0;
5969 }
5970 
5971 int HLargeText::setDefval(QVariant v)
5972 {
5973  if(!set) //if the value was set I dont set the default.
5974  value = defvalParser( v.toString() ).toString();
5975  return 0;
5976 }
5977 
5978 QVariant HLargeText::getValue (void)
5979 {
5980  return QVariant(value);
5981 }
5982 
5984 {
5985  return QString("\'%1\'").arg(value);
5986 }
5987 
5989 {
5990  return QString("%1").arg(value);
5991 }
5992 
5994 {
5995  return QString("%1")
5996  .arg(sqlColumn);
5997 }
5998 
6000 {
6001  return QString("\'%1\'")
6002  .arg(value);
6003 }
6004 
6006 {
6007  return QString("%1=\'%2\'")
6008  .arg(sqlColumn)
6009  .arg(value);
6010 }
6011 
6013 {
6014  return QString("%1").arg(sqlColumn);
6015 }
6016 
6017 QString HLargeText::sqlCreateStringPart(QString switches)
6018 {
6019  Q_UNUSED(switches);
6020 
6021  if(myInterface()->hsqli_largetext_sizemustdefine)
6022  return sqlColumn + QString("\t VARCHAR(%1)").arg(myInterface()->hsqli_largetext_varcharlength);
6023 
6024  return sqlColumn + "\t VARCHAR";
6025 }
6029 
6030 HCharHash::HCharHash(QString sqlc,QString ex,QString tit,QString t,QString def,
6031  QStringList k,QStringList v)
6032  : HDataField(sqlc,ex,tit,t,true)
6033 {
6034  whoami="HCharHash";
6035  defval = def;
6036  set = 0;
6037  setDefval(def);
6038  keys = k;
6039  values = v;
6040  editable = true;
6041  sql = true;
6042 }
6043 
6045 {
6046  //empty
6047 }
6048 
6049 
6051 :HDataField(t)
6052 {
6053  deepcopy_from(t);
6054 }
6055 
6057 {
6058  value = x->value;
6059  keys = x->keys;
6060  values = x->values;
6061 }
6062 
6064 {
6065  return new HCharHash(this);
6066 }
6067 
6068 QVariant HCharHash::dbValueToDispValue(QVariant v)
6069 {
6070  return QVariant(values[getIndexOf(keys,v.toString())]);
6071 }
6072 
6073 QString HCharHash::getKeyOfValue(QString s)
6074 {
6075  return keys[getIndexOf(values,s)];
6076 }
6077 
6078 QString HCharHash::getValueOfKey(QString s)
6079 {
6080  return values[getIndexOf(keys,s)];
6081 }
6082 
6083 int HCharHash::setValue(QVariant v,bool sdisabled)
6084 {
6085  set = 1;
6086  value = v.toString();
6087  if(!sdisabled)
6088  emit dataChanged();
6089  value_changed();
6090  return 0;
6091 }
6092 
6093 int HCharHash::setDefval(QVariant v)
6094 {
6095  if(!set) //if the value was set I dont set the default.
6096  value = defvalParser( v.toString() ).toString();
6097  return 0;
6098 }
6099 
6100 QVariant HCharHash::getValue (void)
6101 {
6102  return QVariant(value);
6103 }
6104 
6106 {
6107  return QString("\'%1\'").arg(value);
6108 }
6109 
6111 {
6112  return QString("%1").arg(value);
6113 }
6114 
6116 {
6117  return QString("%1").arg(sqlColumn);
6118 }
6119 
6121 {
6122  return QString("\'%1\'").arg(value);
6123 }
6124 
6126 {
6127  return QString("%1=\'%2\'")
6128  .arg(sqlColumn)
6129  .arg(value);
6130 }
6131 
6133 {
6134  return QString("%1")
6135  .arg(sqlColumn);
6136 }
6137 
6138 QString HCharHash::sqlCreateStringPart(QString switches)
6139 {
6140  Q_UNUSED(switches);
6141 
6142  return sqlColumn + QString("\t VARCHAR(%1)").arg(myInterface()->hsqli_charhash_varcharlength);
6143 }
6144 
6148 
6149 HNumHash::HNumHash(QString sqlc,QString ex,QString tit,QString t,QString def,
6150  QStringList k,QStringList v)
6151  : HDataField(sqlc,ex,tit,t,true)
6152 {
6153  whoami="HNumHash";
6154  defval = def;
6155  set = 0;
6156  setDefval(def);
6157  keys = k;
6158  values = v;
6159  editable = true;
6160  sql = true;
6161 }
6162 
6164 {
6165  //empty
6166 }
6167 
6169 :HDataField(t)
6170 {
6171  deepcopy_from(t);
6172 }
6173 
6175 {
6176  value = x->value;
6177  keys = x->keys;
6178  values = x->values;
6179 }
6180 
6182 {
6183  return new HNumHash(this);
6184 }
6185 
6186 QVariant HNumHash::dbValueToDispValue(QVariant v)
6187 {
6188  return QVariant(values[getIndexOf(keys,v.toString())]);
6189 }
6190 
6191 QString HNumHash::getKeyOfValue(QString s)
6192 {
6193  return keys[getIndexOf(values,s)];
6194 }
6195 
6196 QString HNumHash::getValueOfKey(QString s)
6197 {
6198  return values[getIndexOf(keys,s)];
6199 }
6200 
6201 int HNumHash::setValue (QVariant v,bool sdisabled)
6202 {
6203  set = 1;
6204  value = (int)(v.toString().toDouble());
6205  if(!sdisabled)
6206  emit dataChanged();
6207  value_changed();
6208  return 0;
6209 }
6210 
6211 int HNumHash::setDefval(QVariant v)
6212 {
6213  if(!set) //if the value was set I dont set the default.
6214  value = defvalParser( v.toString() ).toInt();
6215  return 0;
6216 }
6217 
6218 QVariant HNumHash::getValue (void)
6219 {
6220  return QVariant(value);
6221 }
6222 
6224 {
6225  return QString("%1").arg(value);
6226 }
6227 
6229 {
6230  return QString("%1").arg(value);
6231 }
6232 
6234 {
6235  return QString("%1").arg(sqlColumn);
6236 }
6237 
6239 {
6240  return QString("%1").arg(value);
6241 }
6242 
6243 QString HNumHash::sqlUpdate(void)
6244 {
6245  return QString("%1=%2")
6246  .arg(sqlColumn)
6247  .arg(value);
6248 }
6249 
6251 {
6252  return QString("%1")
6253  .arg(sqlColumn);
6254 }
6255 
6256 QString HNumHash::sqlCreateStringPart(QString switches)
6257 {
6258  Q_UNUSED(switches);
6259 
6260  if(myInterface()->hsqli_numbertypename == "NUMERIC")
6261  return sqlColumn + "\t NUMERIC(5,0)";
6262 
6263  return sqlColumn + QString("\t %1").arg(myInterface()->hsqli_numbertypename);
6264 }
6268 
6269 HNumber::HNumber(QString sqlc,QString ex,QString tit,QString t,QString def)
6270  : HDataField(sqlc,ex,tit,t,true)
6271 {
6272  whoami="HNumber";
6273  defval = def;
6274  set = 0;
6275  setDefval(def);
6276  editable = true;
6277  sql = true;
6278  minimum = -10000000;
6279  maximum = 10000000;
6280 }
6281 
6283 {
6284  //empty
6285 }
6286 
6288 :HDataField(t)
6289 {
6290  deepcopy_from(t);
6291 }
6292 
6294 {
6295  value = x->value;
6296  minimum = x->minimum;
6297  maximum = x->maximum;
6298 }
6299 
6301 {
6302  return new HNumber(this);
6303 }
6304 
6305 QVariant HNumber::dbValueToDispValue(QVariant v)
6306 {
6307  return QVariant(QString("%1%2").arg(v.toInt()).arg(tailstr.isEmpty() ? "" : (" "+tailstr)));
6308 }
6309 
6311 {
6312  if(value < minimum)
6313  return QString("%1:Value lower than minimum!").arg(sqlColumn);
6314  if(value > maximum)
6315  return QString("%1:Value higher than maximum!").arg(sqlColumn);
6316  return QString("");
6317 }
6318 
6319 int HNumber::setValue (QVariant v,bool sdisabled)
6320 {
6321  set = 1;
6322 
6323  value = v.toInt();
6324 
6325  if(!sdisabled)
6326  emit dataChanged();
6327  value_changed();
6328  return 0;
6329 }
6330 
6331 int HNumber::setDefval(QVariant v)
6332 {
6333  if(!set) //if the value was set I dont set the default.
6334  value = defvalParser( v.toString() ).toInt();
6335  return 0;
6336 }
6337 
6338 QVariant HNumber::getValue (void)
6339 {
6340  return QVariant(value);
6341 }
6342 
6343 QString HNumber::getSQLValue (void)
6344 {
6345  return QString("%1").arg(value);
6346 }
6347 
6349 {
6350  return QString("%1").arg(value);
6351 }
6352 
6354 {
6355  return QString("%1").arg(sqlColumn);
6356 }
6357 
6359 {
6360  return QString("%1").arg(value);
6361 }
6362 
6363 QString HNumber::sqlUpdate(void)
6364 {
6365  return QString("%1=%2").arg(sqlColumn).arg(value);
6366 }
6367 
6369 {
6370  return QString("%1").arg(sqlColumn);
6371 }
6372 
6373 QString HNumber::sqlCreateStringPart(QString switches)
6374 {
6375  Q_UNUSED(switches);
6376 
6377  if(myInterface()->hsqli_numbertypename == "NUMERIC")
6378  {
6379  int i =(int)( ::log10((double)maximum));
6380  return sqlColumn + QString("\t NUMERIC(%1,0)").arg(i+1);
6381  }
6382 
6383  return sqlColumn + QString("\t %1").arg(myInterface()->hsqli_numbertypename);
6384 }
6385 
6389 HStatic::HStatic(QString ex,QString tit,QString def)
6390  : HDataField("",ex,tit,"",false)
6391 {
6392  whoami="HStatic";
6393  defval = def;
6394  set = 0;
6395  setDefval(def);
6396  editable = false;
6397  sql = false;
6398 }
6399 
6401 {
6402  //empty
6403 }
6404 
6406 : HDataField(t)
6407 {
6408  deepcopy_from(t);
6409 }
6410 
6412 {
6413  value = x->value;
6414 }
6415 
6417 {
6418  return new HStatic(this);
6419 }
6420 
6421 int HStatic::setValue (QVariant v,bool sdisabled)
6422 {
6423  value = v.toString();
6424  if(!sdisabled)
6425  emit dataChanged();
6426  return 0;
6427 }
6428 
6429 QVariant HStatic::getValue (void)
6430 {
6431  return QVariant(value);
6432 }
6433 
6434 QString HStatic::getSQLValue (void)
6435 {
6436  return QString("");
6437 }
6438 
6440 {
6441  return QString("NULL");
6442 }
6443 
6444 int HStatic::setDefval(QVariant v)
6445 {
6446  value = defvalParser( v.toString() ).toString();
6447  return 0;
6448 }
6449 
6451 {
6452  return QString("");
6453 }
6454 
6456 {
6457  return QString("");
6458 }
6459 
6460 QString HStatic::sqlUpdate(void)
6461 {
6462  return QString("");
6463 }
6464 
6466 {
6467  return QString("");
6468 }
6469 
6470 QString HStatic::sqlCreateStringPart(QString switches)
6471 {
6472  Q_UNUSED(switches);
6473 
6474  return "";
6475 }
6479 
6480 HCheck::HCheck(QString sqlc,QString ex,QString tit,QString t,QString def,QString ts,QString fs)
6481  : HDataField(sqlc,ex,tit,t,true)
6482 {
6483  whoami="HCheck";
6484  defval = def;
6485  set = 0;
6486  setDefval(def);
6487  editable = true;
6488  sql = true;
6489  trues = ts;
6490  falses = fs;
6491 }
6492 
6494 {
6495  //empty
6496 }
6497 
6499 :HDataField(t)
6500 {
6501  deepcopy_from(t);
6502 }
6503 
6505 {
6506  value = x->value;
6507  trues = x->trues;
6508  falses= x->falses;
6509 }
6510 
6512 {
6513  return new HCheck(this);
6514 }
6515 
6516 QVariant HCheck::dbValueToDispValue(QVariant v)
6517 {
6518  return QVariant( v.toBool() ? trues : falses );
6519 }
6520 
6521 int HCheck::setValue (QVariant v,bool sdisabled)
6522 {
6523  set = 1;
6524  value = v.toBool();
6525  if(!sdisabled)
6526  emit dataChanged();
6527  value_changed();
6528  return 0;
6529 
6530 }
6531 
6532 int HCheck::setDefval(QVariant v)
6533 {
6534  if(!set) //if the value was set I dont set the default.
6535  value = defvalParser( v.toString() ).toBool();
6536  return 0;
6537 }
6538 
6539 QVariant HCheck::getValue (void)
6540 {
6541  return QVariant(value);
6542 }
6543 
6544 QString HCheck::getSQLValue (void)
6545 {
6546  return QString(value ? myInterface()->hsqli_truevalue : myInterface()->hsqli_falsevalue);
6547 }
6548 
6550 {
6551  return QString(value ? "TRUE" : "FALSE");
6552 }
6553 
6555 {
6556  return QString("%1").arg(sqlColumn);
6557 }
6558 
6560 {
6561  return QString(value ? myInterface()->hsqli_truevalue : myInterface()->hsqli_falsevalue);
6562 }
6563 
6564 QString HCheck::sqlUpdate(void)
6565 {
6566  return QString("%1=%2").arg(sqlColumn).arg(value ? myInterface()->hsqli_truevalue : myInterface()->hsqli_falsevalue);
6567 }
6568 
6570 {
6571  return QString("%1").arg(sqlColumn);
6572 }
6573 
6574 QString HCheck::sqlCreateStringPart(QString switches)
6575 {
6576  Q_UNUSED(switches);
6577 
6578  return sqlColumn + "\t " + myInterface()->hsqli_booleantypename;
6579 }
6583 
6584 HFloating::HFloating(QString sqlc,QString ex,QString tit,QString t,QString def)
6585  : HDataField(sqlc,ex,tit,t,true)
6586 {
6587  whoami="HFloating";
6588  defval = def;
6589  set = 0;
6590  setDefval(def);
6591  editable = true;
6592  sql = true;
6593  minimum = -10000000;
6594  maximum = 10000000;
6595  dMminDf = 0;
6596  dMmaxDf = 4;
6597  dMgroup = 0;
6598 }
6599 
6601 {
6602  //empty
6603 }
6604 
6606 :HDataField(t)
6607 {
6608  deepcopy_from(t);
6609 }
6610 
6612 {
6613  value = x->value;
6614  minimum = x->minimum;
6615  maximum = x->maximum;
6616  dMminDf = x->dMminDf;
6617  dMmaxDf = x->dMmaxDf;
6618  dMgroup = x->dMgroup;
6619 }
6620 
6622 {
6623  return new HFloating(this);
6624 }
6625 
6626 QVariant HFloating::dbValueToDispValue(QVariant v)
6627 {
6628  return QVariant( doubleToQString(v.toDouble(),dMminDf,dMmaxDf,dMgroup)
6629  +
6630  (tailstr.isEmpty() ? "" : (" "+tailstr))
6631  );
6632 
6633 }
6634 
6636 {
6637  return doubleToQString(value,dMminDf,dMmaxDf,dMgroup);
6638 }
6639 
6641 {
6642  if(value < minimum)
6643  return QString("%1: Value lower than minimum!").arg(sqlColumn);
6644  if(value > maximum)
6645  return QString("%1: Value higher than maximum!").arg(sqlColumn);
6646  return QString("");
6647 }
6648 
6649 int HFloating::setValue (QVariant v,bool sdisabled)
6650 {
6651  set = 1;
6652 
6653  value = v.toDouble();
6654 
6655  if(!sdisabled)
6656  emit dataChanged();
6657  value_changed();
6658  return 0;
6659 }
6660 
6661 int HFloating::setDefval(QVariant v)
6662 {
6663  if(!set) //if the value was set I dont set the default.
6664  value = defvalParser( v.toString() ).toDouble();
6665  return 0;
6666 }
6667 
6668 QVariant HFloating::getValue (void)
6669 {
6670  return QVariant(value);
6671 }
6672 
6674 {
6675  return QString("%1").arg(value,0,'f');
6676 }
6677 
6679 {
6680  return QString("%1").arg(value,0,'f');
6681 }
6682 
6684 {
6685  return QString("%1").arg(sqlColumn);
6686 }
6687 
6689 {
6690  return QString("%1").arg(value,0,'f');
6691 }
6692 
6694 {
6695  return QString("%1=%2").arg(sqlColumn).arg(value,0,'f');
6696 }
6697 
6699 {
6700  return QString("%1").arg(sqlColumn);
6701 }
6702 
6703 QString HFloating::sqlCreateStringPart(QString switches)
6704 {
6705  Q_UNUSED(switches);
6706 
6707  if(myInterface()->hsqli_floattypename == "NUMERIC")
6708  {
6709  int i =(int)( ::log10(maximum));
6710  return sqlColumn + QString("\t NUMERIC(%1,3)").arg(i+4);
6711  }
6712 
6713  return sqlColumn + QString("\t %1").arg(myInterface()->hsqli_floattypename);
6714 }
6715 
6719 
6720 HDate::HDate(QString sqlc,QString ex,QString tit,QString def,bool u_a,bool unk)
6721  : HDataField(sqlc,ex,tit,"",true)
6722 {
6723  whoami="HDate";
6724  defval = def;
6725  set = 0;
6726 
6727  u_alive = u_a;
6728  unknown = unk;
6729 
6730  if(def.toUpper() == "NOW" )
6731  value = QDate::currentDate();
6732  else if(u_a && def.toUpper() == "UNKNOWN")
6733  {
6734  unknown = true;
6735  value = QDate(2000,1,1);
6736  }
6737  else if(u_a && def.toUpper() == "UNKNOWN_AND_NOW")
6738  {
6739  unknown = true;
6740  value = QDate::currentDate();
6741  }
6742  else
6743  {
6744  QString parsedDv;
6745  parsedDv = defvalParser(def).toString();
6746  if(parsedDv.isEmpty())
6747  value = QDate(2000,1,1);
6748  else
6749  value = QDate::fromString(parsedDv,Qt::ISODate);
6750  }
6751 
6752  editable = true;
6753  sql = true;
6754 }
6755 
6757 {
6758  //empty
6759 }
6760 
6762 :HDataField(t)
6763 {
6764  deepcopy_from(t);
6765 }
6766 
6768 {
6769  value = x->value;
6770  u_alive = x->u_alive;
6771  unknown = x->unknown;
6772 }
6773 
6775 {
6776  return new HDate(this);
6777 }
6778 
6780 {
6781  if(!unknown)
6782  set = 1;
6783 
6784  unknown=true;
6785 }
6787 {
6788  if(unknown)
6789  set = 1;
6790 
6791  if(value.toString().isEmpty() ||
6792  value.toString() == "NULL" ||
6793  value.isNull() ||
6794  !value.isValid() )
6795  {
6796  value = QDate(2000,1,1);
6797  }
6798 
6799  unknown=false;
6800 }
6801 
6802 int HDate::setValue (QVariant v,bool sdisabled)
6803 {
6804  set = 1;
6805  if(v.toString().isEmpty() ||
6806  v.toString() == "NULL" )
6807  {
6808  if(u_alive)
6809  unknown = true;
6810  else
6811  value = QDate(2000,1,1);
6812  return 0;
6813  }
6814  unknown = false;
6815 
6816  if(myInterface()->hsqli_hasdatetype)
6817  value = v.toDate();
6818  else
6819  {
6820  value = QDate::fromString(v.toString(),"yyyy-MM-dd"); //"yyyy-M-d"
6821  if(!value.isValid())
6822  value = QDate::fromString(v.toString(),"yyyy-M-d");
6823  }
6824 
6825  if(!sdisabled)
6826  emit dataChanged();
6827  value_changed();
6828  return 0;
6829 }
6830 
6831 int HDate::setDefval(QVariant v)
6832 {
6833  if(!set) //if the value was set I dont set the default.
6834  {
6835 
6836  if(v.toString().toUpper() == "NOW" )
6837  {
6838  unknown = false;
6839  value = QDate::currentDate();
6840  }
6841  else if(u_alive && v.toString().toUpper() == "UNKNOWN" )
6842  {
6843  unknown = true;
6844  value = QDate(2000,1,1);
6845  }
6846  else if(u_alive && v.toString().toUpper() == "UNKNOWN_AND_NOW")
6847  {
6848  unknown = true;
6849  value = QDate::currentDate();
6850  }
6851  else
6852  {
6853  value = QDate::fromString(
6854  defvalParser(v.toString()).toString(),Qt::ISODate
6855  );
6856  }
6857  }
6858  return 0;
6859 }
6860 
6861 QVariant HDate::getValue (void)
6862 {
6863  if(u_alive && unknown)
6864  {
6865  return QVariant("");
6866  }
6867  if(value.toString().isEmpty() ||
6868  value.toString() == "NULL" ||
6869  value.isNull() ||
6870  !value.isValid() )
6871  {
6872  return QVariant(QDate(2000,1,1));
6873  }
6874  return QVariant(value);
6875 }
6876 
6877 QVariant HDate::getKnownValue(void)
6878 {
6879  if(value.toString().isEmpty() ||
6880  value.toString() == "NULL" ||
6881  value.isNull() ||
6882  !value.isValid() )
6883  {
6884  return QVariant(QDate(2000,1,1));
6885  }
6886  return QVariant(value);
6887 }
6888 
6889 QString HDate::getSQLValue (void)
6890 {
6891  if(u_alive && unknown)
6892  return QString("NULL");
6893 
6894  if(!myInterface()->hsqli_hasdatetype)
6895  return QString("\'%1\'")
6896  .arg(value.toString("yyyy-MM-dd"));
6897 
6898  return QString("\'%1-%2-%3\'")
6899  .arg(value.year())
6900  .arg(value.month())
6901  .arg(value.day());
6902 }
6903 
6905 {
6906  return QString("%1")
6907  .arg(value.toString("yyyy-MM-dd"));