File: DataBaseUtils.cpp

package info (click to toggle)
tango 10.0.2%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 89,484 kB
  • sloc: cpp: 201,245; sh: 1,645; python: 953; java: 800; perl: 467; javascript: 447; xml: 325; makefile: 269; sql: 72; ruby: 24
file content (853 lines) | stat: -rw-r--r-- 24,255 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
//+=============================================================================
//
// file :         DataBaseUtils.cpp
//
// description :  Various utility for the DataBase.
//
// project :      TANGO Device Servers
//
// author(s) :    A.Gotz, P.Verdier, JL Pons
//
// Copyright (C) :      2004,2005,2006,2007,2008,2009,2010,2011,2012,2013
//						European Synchrotron Radiation Facility
//                      BP 220, Grenoble 38043
//                      FRANCE
//
// This file is part of Tango.
//
// Tango is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Tango is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Tango.  If not, see <http://www.gnu.org/licenses/>.
//
//=============================================================================

#include <tango/tango.h>
#include "DataBase.h"
#include "DataBaseClass.h"
#include "DatabaseConnectionHandle.h"
#include "DatabaseConnectionPool.h"
#include "Logging.h"

#include <errmsg.h>

#include <stdio.h>

namespace DataBase_ns {

//+----------------------------------------------------------------------------
//
// method : 		DataBase::replace_wildcard(char *wildcard_c_str)
//
// description : 	utility method to replace all occurrences of
//			wildcards (*) with SQL wildcards % and to escape
//			all occurrences	of '%' and '_' with '\'
//
// in :			string **wildcard_c_str - wildcard C string
//
// out :		void - nothing
//
//-----------------------------------------------------------------------------
std::string DataBase::replace_wildcard(const char *wildcard_c_str)
{
	std::string wildcard(wildcard_c_str);
	std::string::size_type index;

	DEBUG_STREAM << "DataBase::replace_wildcard() wildcard in " << wildcard << std::endl;
// escape %

	index = 0;
	while ((index = wildcard.find('%',index)) != std::string::npos)
	{
		wildcard.insert(index, 1, '\\');
		index = index+2;
	}

// escape _

	index = 0;
	while ((index = wildcard.find('_',index)) != std::string::npos)
	{
		wildcard.insert(index, 1, '\\');
		index = index+2;
	}


// escape "

	index = 0;
	while ((index = wildcard.find('"',index)) != std::string::npos)
	{
		wildcard.insert(index, 1, '\\');
		index = index+2;
	}


// escape '

	index = 0;
	while ((index = wildcard.find('\'',index)) != std::string::npos)
	{
		wildcard.insert(index, 1, '\\');
		index = index+2;
	}

// replace wildcard * with %

	while ((index = wildcard.find('*')) != std::string::npos)
	{
		wildcard.replace(index, 1, 1, '%');
	}

	DEBUG_STREAM << "DataBase::replace_wildcard() wildcard out " << wildcard << std::endl;
	return wildcard;
}

//+----------------------------------------------------------------------------
//
// method : 		DataBase::escape_string(char *string_c_str)
//
// description : 	utility method to escape all occurrences
//			of ' and " with '\'
//
// in :			const char *string_c_str -  C string to be modified.
//
// out :		string - The result string .
//
//-----------------------------------------------------------------------------
std::string DataBase::escape_string(const char *string_c_str)
{
	std::string escaped_string(string_c_str);
	std::string::size_type index;

	DEBUG_STREAM << "DataBase::escape_string() string in : " << escaped_string << std::endl;

	//	escape bakckslash
	index = 0;
	while ((index = escaped_string.find('\\',index)) != std::string::npos)
	{
		//	Check if double backslash already treated by client
		std::string	s = escaped_string.substr(index+1);

			//	Check if another escape sequence treated by client
		if (s.find('\"')==0 || s.find('\'')==0)
			index++;
		else
		{
			escaped_string.insert(index, 1, '\\');
			index += 2;
		}
	}

	//	escape "
	index = 0;
	while ((index = escaped_string.find('"',index)) != std::string::npos)
	{
		if (index==0)	//	Cannot have '\' at -1 !
		{
			escaped_string.insert(index, 1, '\\');
			index += 2;
		}
		else
		{
			//	Check if double quotes already treated by client
			std::string	s = escaped_string.substr(index-1);
			if (s.find('\\')==0)
				index++;
			else
			{
				escaped_string.insert(index, 1, '\\');
				index += 2;
			}
		}
	}


	//	escape '
	index = 0;
	while ((index = escaped_string.find('\'',index)) != std::string::npos)
	{
		if (index==0)	//	Cannot have '\' at -1 !
		{
			escaped_string.insert(index, 1, '\\');
			index += 2;
		}
		else
		{
			//	Check if simple quotes already treated by client
			std::string	s = escaped_string.substr(index-1);
			if (s.find('\\')==0)
				index++;
			else
			{
				escaped_string.insert(index, 1, '\\');
				index += 2;
			}
		}
	}

	DEBUG_STREAM << "DataBase::escaped_string() wildcard out : " << escaped_string << std::endl;
	return escaped_string;
}

//+----------------------------------------------------------------------------
//
// method : 		DataBase::device_name_to_dfm(std::string &device_name,
//			          char **domain, char **family, char **member)
//
// description : 	utility function to return domain, family and member
//			from device name. Assumes device name has (optional)
//			protocol and instance stripped off i.e. conforms
//			to domain/family/member
//
// in :			char *devname - device name
//
// out :		bool - true or false
//
//-----------------------------------------------------------------------------
bool DataBase::device_name_to_dfm(std::string &devname, char domain[], char family[], char member[])
{
	std::string::size_type index, index2;

	DEBUG_STREAM << "DataBase::device_name_to_dfm() device name in " << devname << std::endl;

	index = devname.find('/');
	index2 = devname.find('/', index+1);

	(devname.substr(0,index)).copy(domain,index);
	domain[index] = '\0';

	(devname.substr(index+1,index2-index)).copy(family,index2-index);
	family[index2-index-1] = '\0';

	(devname.substr(index2+1)).copy(member,devname.length()-index2);
	member[devname.length()-index2-1] = '\0';

	DEBUG_STREAM << "DataBase::device_name_to_dfm() domain/family/member out " << domain << "/" << family << "/" << member << std::endl;

	return true;
}

//+----------------------------------------------------------------------------
//
// method : 		DataBase::check_device_name(std::string *device_name)
//
// description : 	utility function to check whether device name conforms
//			to the TANGO naming convention of
//
//			[tango!taco:][//instance/]domain/family/member
//
// in :			string *device_name - device name
//
// out :		bool - true or false
//
//-----------------------------------------------------------------------------
bool DataBase::check_device_name(std::string &device_name_str)
{
	std::string devname(device_name_str);
	std::string::size_type index, index2;

	DEBUG_STREAM << "DataBase::check_device_name(): device_name in " << devname << std::endl;

// check there are no special characters which could be interpreted
// as wildcards or which are otherwise excluded

	if (devname.find('*') != std::string::npos) return false;

// check protocol - "tango:" | "taco:"

	if (devname.substr(0,6) == "tango:")
	{
		devname.erase(0,6);
	}
	else
	{
		if (devname.substr(0,5) == "taco:")
		{
			devname.erase(0,5);
		}
	}

// check instance - "//instance/"

	if (devname.substr(0,2) == "//")
	{
		index = devname.find('/',(std::string::size_type)2);
		if (index == 0 || index == std::string::npos)
		{
			return false;

		}
		devname.erase(0,index+1);
	}

// check name conforms to "D/F/M"

	index = devname.find('/');
	index2 = devname.find('/',index+1);

	if (index == 0 || index == std::string::npos || index2 == std::string::npos ||
	    index2-index <= 0 || devname.length() - index2 <= 0)
	{
		return false;
	}

	device_name_str = devname;

	DEBUG_STREAM << "DataBase::check_device_name(): device_name out " << device_name_str << std::endl;

	return true;
}

//+------------------------------------------------------------------
/**
 *	method:	DataBase::init_timing_stats
 *
 *	description:	method to initialise the timing statistics variables
 *	Returns void
 *
 * @param	none
 * @return	void
 *
 */
//+------------------------------------------------------------------
void DataBase::init_timing_stats()
{
	TimingStatsStruct new_timing_stats = {0.0, 0.0, 0.0, 0.0, 0.0};

	timing_stats_mutex.lock();

	timing_stats_map["DbImportDevice"] = new TimingStatsStruct;
	timing_stats_map["DbExportDevice"] = new TimingStatsStruct;
	timing_stats_map["DbGetHostServerList"] = new TimingStatsStruct;
	timing_stats_map["DbGetHostList"] = new TimingStatsStruct;
	timing_stats_map["DbGetServerList"] = new TimingStatsStruct;
	timing_stats_map["DbGetDevicePropertyList"] = new TimingStatsStruct;
	timing_stats_map["DbGetClassPropertyList"] = new TimingStatsStruct;
	timing_stats_map["DbGetDeviceMemberList"] = new TimingStatsStruct;
	timing_stats_map["DbGetDeviceFamilyList"] = new TimingStatsStruct;
	timing_stats_map["DbGetDeviceDomainList"] = new TimingStatsStruct;
	timing_stats_map["DbGetDeviceProperty"] = new TimingStatsStruct;
	timing_stats_map["DbPutDeviceProperty"] = new TimingStatsStruct;
	timing_stats_map["DbDeleteDeviceProperty"] = new TimingStatsStruct;
	timing_stats_map["DbInfo"] = new TimingStatsStruct;
	timing_stats_map["DbGetDeviceClassList"] = new TimingStatsStruct;
	timing_stats_map["DbGetDeviceAttributeProperty"] = new TimingStatsStruct;
	timing_stats_map["DbPutDeviceAttributeProperty"] = new TimingStatsStruct;
	timing_stats_map["DbGetDeviceAttributeProperty2"] = new TimingStatsStruct;
	timing_stats_map["DbPutDeviceAttributeProperty2"] = new TimingStatsStruct;
	timing_stats_map["DbUnExportServer"] = new TimingStatsStruct;
	timing_stats_map["DbGetDeviceExportedList"] = new TimingStatsStruct;
	timing_stats_map["DbExportEvent"] = new TimingStatsStruct;
	timing_stats_map["DbImportEvent"] = new TimingStatsStruct;
	timing_stats_map["DbGetDataForServerCache"] = new TimingStatsStruct;
	timing_stats_map["DbPutClassProperty"] = new TimingStatsStruct;
	timing_stats_map["DbMySqlSelect"] = new TimingStatsStruct;
	timing_stats_map["DbGetDevicePipeProperty"] = new TimingStatsStruct;
	timing_stats_map["DbPutDevicePipeProperty"] = new TimingStatsStruct;

	timing_stats_size = timing_stats_map.size();
	timing_stats_average = new double[timing_stats_size];
	timing_stats_minimum = new double[timing_stats_size];
	timing_stats_maximum = new double[timing_stats_size];
	timing_stats_calls = new double[timing_stats_size];
	timing_stats_index = new Tango::DevString[timing_stats_size];

	//	now loop over map and initialise remaining variables
	std::map<std::string,TimingStatsStruct*>::iterator iter;
	int i=0;

	for (iter = timing_stats_map.begin(); iter != timing_stats_map.end(); iter++)
	{
		*(iter->second) = new_timing_stats;
		timing_stats_index[i] = (char*)malloc(strlen(iter->first.c_str())+1);
		strcpy(timing_stats_index[i],iter->first.c_str());
		i++;
	}

	timing_stats_mutex.unlock();
}

//+----------------------------------------------------------------------------
//
// method : 		DataBase::check_history_tables()
//
// description : 	Return history id
//
//-----------------------------------------------------------------------------
void DataBase::check_history_tables()
{
	TangoSys_MemStream	sql_query_stream;
	MYSQL_RES *result;

	INFO_STREAM << "DataBase::check_history_tables(): entering" << std::endl;

	sql_query_stream.str("");
	sql_query_stream << "SELECT count(*) FROM property_device_hist";
	DEBUG_STREAM << "DataBase::check_history_tables(): sql_query " << sql_query_stream.str() << std::endl;
	result = query(sql_query_stream.str(),"check_history_tables()");
	mysql_free_result(result);
}

//+----------------------------------------------------------------------------
//
// method : 		DataBase::get_id()
//
// description : 	Return history id
//					In this method, we don't use the classical query()
//					method in order to be sure that the UPDATE and the following
//					mysql_insert_id() are done using the same MySQL connection
//
//-----------------------------------------------------------------------------

Tango::DevULong64 DataBase::get_id(const char *name, DatabaseConnectionHandle &dch)
{
	TangoSys_MemStream sql_query;

	sql_query.str("");
	sql_query << "UPDATE " << name << "_history_id SET id=LAST_INSERT_ID(id+1)";
	std::string tmp_str = sql_query.str();

	if (mysql_real_query(dch.db(), tmp_str.c_str(),tmp_str.length()) != 0)
	{
		TangoSys_OMemStream o;

		WARN_STREAM << "DataBase::get_id() failed to query TANGO database:" << std::endl;
		WARN_STREAM << "  query = " << tmp_str << std::endl;
		WARN_STREAM << " (SQL error=" << mysql_error(dch.db()) << ")" << std::endl;

		o << "Failed to query TANGO database (error=" << mysql_error(dch.db()) << ")" << std::ends;

		Tango::Except::throw_exception(DB_SQLError,o.str(),"DataBase::get_id()");
	}

	my_ulonglong val = mysql_insert_id(dch.db());
	if (val == 0)
	{
		TangoSys_OMemStream o;

		o << "Failed to get history id : " << name;

		Tango::Except::throw_exception(DB_SQLError,o.str(),"DataBase::get_id()");
	}

	return (Tango::DevULong64)val;
}

//+----------------------------------------------------------------------------
//
// method : 		DataBase::simple_query()
//
// description : 	Execute a SQL query , ignore the result.
//
//-----------------------------------------------------------------------------
void DataBase::simple_query(std::string sql_query,const char *method, DatabaseConnectionHandle &dch)
{
//
// Call MySQL
//

	if (mysql_real_query(dch.db(), sql_query.c_str(),sql_query.length()) != 0)
	{
		TangoSys_OMemStream o;
		TangoSys_OMemStream o2;

		WARN_STREAM << "DataBase::" << method << " failed to query TANGO database:" << std::endl;
		WARN_STREAM << "  query = " << sql_query << std::endl;
		WARN_STREAM << " (SQL error=" << mysql_error(dch.db()) << ")" << std::endl;

		o << "Failed to query TANGO database (error=" << mysql_error(dch.db()) << ")";
		o << "\n.The query was: " << sql_query << std::ends;
		o2 << "DataBase::" << method << std::ends;

		Tango::Except::throw_exception(DB_SQLError,o.str(),o2.str());
	}
}

void DataBase::simple_query(std::string sql_query,const char *method)
{
	DatabaseConnectionHandle dch(dcp);

	simple_query(sql_query, method, dch);
}

//+----------------------------------------------------------------------------
//
// method : 		DataBase::query()
//
// description : 	Execute a SQL query and return the result.
//
//-----------------------------------------------------------------------------
MYSQL_RES *DataBase::query(std::string sql_query,const char *method, DatabaseConnectionHandle& dch)
{
	MYSQL_RES *result;

//
// Call MySQL
//

	if (mysql_real_query(dch.db(), sql_query.c_str(),sql_query.length()) != 0)
	{
		TangoSys_OMemStream o;
		TangoSys_OMemStream o2;

		WARN_STREAM << "DataBase::" << method << " failed to query TANGO database:" << std::endl;
		WARN_STREAM << "  query = " << sql_query << std::endl;
		WARN_STREAM << " (SQL error=" << mysql_error(dch.db()) << ")" << std::endl;

		o << "Failed to query TANGO database (error=" << mysql_error(dch.db()) << ")";
		o << "\nThe query was: " << sql_query << std::ends;
		o2 << "DataBase::" << method << std::ends;

		Tango::Except::throw_exception(DB_SQLError,o.str(),o2.str());
	}

	if ((result = mysql_store_result(dch.db())) == NULL)
	{
		TangoSys_OMemStream o;
		TangoSys_OMemStream o2;

		WARN_STREAM << "DataBase:: " << method << " : mysql_store_result() failed  (error=" << mysql_error(dch.db()) << ")" << std::endl;

		o << "mysql_store_result() failed (error=" << mysql_error(dch.db()) << ")";
		o2 << "DataBase::" << method;

		Tango::Except::throw_exception(DB_SQLError,o.str(),o2.str());
	}

	return result;

}

MYSQL_RES *DataBase::query(std::string sql_query,const char *method)
{
	DatabaseConnectionHandle dch(dcp);

	return query(sql_query, method, dch);
}

//+------------------------------------------------------------------
/**
 *	method:	purge_property()
 *
 *	description:	purge a property history table.
 *
 */
//+------------------------------------------------------------------
void DataBase::purge_property(const char *table,const char *field,const char *object,const char *name, DatabaseConnectionHandle &dch) {

  TangoSys_MemStream sql_query;
  MYSQL_RES *result;
  MYSQL_ROW row2;

  sql_query.str("");
  sql_query << "SELECT DISTINCT id,date FROM " << table
            << " WHERE " << field << "=\"" << object << "\" AND name=\"" << name
            << "\" ORDER by date";

  result = query(sql_query.str(),"purge_property()", dch);
  int nb_item = mysql_num_rows(result);

  if(nb_item>historyDepth) {
    // Purge
    int toDelete = nb_item-historyDepth;
    for(int j=0;j<toDelete;j++) {
        row2 = mysql_fetch_row(result);
        sql_query.str("");
        sql_query << "DELETE FROM " << table << " WHERE id='" << row2[0] << "'";
		simple_query(sql_query.str(),"purge_property()", dch);
    }
  }

  mysql_free_result(result);
}

//+------------------------------------------------------------------
/**
 *	method:	purge_attribute_property()
 *
 *	description:	purge an attribute property history table.
 *
 */
//+------------------------------------------------------------------
void DataBase::purge_att_property(const char *table,const char *field,const char *object,const char *attribute,const char *name, DatabaseConnectionHandle& dch) {

  TangoSys_MemStream sql_query;
  MYSQL_RES *result;
  MYSQL_ROW row2;

  //TANGO_LOG << "purge_att_property(" << object << "," << attribute << "," << name << ")" << std::endl;

  sql_query.str("");
  sql_query << "SELECT DISTINCT id,date FROM " << table
            << " WHERE " << field << "=\"" << object << "\" AND name=\"" << name
            << "\" AND attribute=\"" << attribute << "\" ORDER by date";

  result = query(sql_query.str(),"purge_att_property()", dch);
  int nb_item = mysql_num_rows(result);

  if(nb_item>historyDepth) {
    // Purge
    int toDelete = nb_item-historyDepth;
    for(int j=0;j<toDelete;j++) {
		row2 = mysql_fetch_row(result);
        sql_query.str("");
        sql_query << "DELETE FROM " << table << " WHERE id='" << row2[0] << "'";
		simple_query(sql_query.str(),"purge_att_property()", dch);
    }
  }
  mysql_free_result(result);
}

//+------------------------------------------------------------------
/**
 *	method:	purge_pipe_property()
 *
 *	description:	purge an pipe property history table.
 *
 */
//+------------------------------------------------------------------
void DataBase::purge_pipe_property(const char *table,const char *field,const char *object,const char *pipe,const char *name, DatabaseConnectionHandle &dch) {

  TangoSys_MemStream sql_query;
  MYSQL_RES *result;
  MYSQL_ROW row2;

  //TANGO_LOG << "purge_pipe_property(" << object << "," << pipe << "," << name << ")" << std::endl;

  sql_query.str("");
  sql_query << "SELECT DISTINCT id,date FROM " << table
            << " WHERE " << field << "=\"" << object << "\" AND name=\"" << name
            << "\" AND pipe=\"" << pipe << "\" ORDER by date";

  result = query(sql_query.str(),"purge_pipe_property()", dch);
  int nb_item = mysql_num_rows(result);

  if(nb_item>historyDepth) {
    // Purge
    int toDelete = nb_item-historyDepth;
    for(int j=0;j<toDelete;j++) {
		row2 = mysql_fetch_row(result);
        sql_query.str("");
        sql_query << "DELETE FROM " << table << " WHERE id='" << row2[0] << "'";
		simple_query(sql_query.str(),"purge_pipe_property()", dch);
    }
  }
  mysql_free_result(result);
}

//+------------------------------------------------------------------
/**
 *	method:	host_port_from_ior()
 *
 *	description:	Get host and port from a device IOR
 *
 */
//+------------------------------------------------------------------

bool DataBase::host_port_from_ior(const char *iorstr,std::string &h_p)
{
	size_t s = (iorstr ? strlen(iorstr) : 0);

  	if (s < 4)
    	return false;

  	const char *p = iorstr;
  	if (p[0] != 'I' || p[1] != 'O' || p[2] != 'R' || p[3] != ':')
    	return false;

  	s = (s - 4) / 2;  // how many octets are there in the string
  	p += 4;

  	cdrMemoryStream buf((CORBA::ULong)s,0);

  	for (int i=0; i<(int)s; i++)
	{
    	int j = i*2;
    	CORBA::Octet v;

    	if (p[j] >= '0' && p[j] <= '9')
		{
      		v = ((p[j] - '0') << 4);
    	}
    	else if (p[j] >= 'a' && p[j] <= 'f')
		{
      		v = ((p[j] - 'a' + 10) << 4);
    	}
    	else if (p[j] >= 'A' && p[j] <= 'F')
		{
      		v = ((p[j] - 'A' + 10) << 4);
    	}
    	else
      		return false;

    	if (p[j+1] >= '0' && p[j+1] <= '9')
		{
      		v += (p[j+1] - '0');
    	}
    	else if (p[j+1] >= 'a' && p[j+1] <= 'f')
		{
      		v += (p[j+1] - 'a' + 10);
    	}
    	else if (p[j+1] >= 'A' && p[j+1] <= 'F')
		{
      		v += (p[j+1] - 'A' + 10);
    	}
    	else
      		return false;

    	buf.marshalOctet(v);
  	}

  	buf.rewindInputPtr();
  	CORBA::Boolean b = buf.unmarshalBoolean();
  	buf.setByteSwapFlag(b);

  	IOP::IOR ior;

  	ior.type_id = IOP::IOR::unmarshaltype_id(buf);
  	ior.profiles <<= buf;


    if (ior.profiles.length() == 0 && strlen(ior.type_id) == 0)
	{
      	return false;
    }
    else
	{
      	for (unsigned long count=0; count < ior.profiles.length(); count++)
		{
			if (ior.profiles[count].tag == IOP::TAG_INTERNET_IOP)
			{
	  			IIOP::ProfileBody pBody;
	  			IIOP::unmarshalProfile(ior.profiles[count],pBody);

//
// Three possible cases for host name:
// 1 - The host is stored in IOR as IP numbers
// 2 - The host name is stored in IOR as the canonical host name
// 3 - The FQDN is stored in IOR
// We allways try to get the host name as the FQDN
//

				ho = pBody.address.host.in();
				bool host_is_name = false;

				std::string::size_type pos = ho.find('.');
				if (pos == std::string::npos)
					host_is_name = true;
				else
				{
					for (unsigned int loop =0;loop < pos;++loop)
					{
						if (isdigit((int)ho[loop]) == 0)
						{
							host_is_name = true;
							break;
						}
					}
				}

				if (host_is_name == false)
				{
					struct sockaddr_in s;
					char service[20];

					s.sin_family = AF_INET;
					int res;
#ifdef _TG_WINDOWS_
					s.sin_addr.s_addr = inet_addr(ho.c_str());
					if (s.sin_addr.s_addr != INADDR_NONE)
#else
					res = inet_pton(AF_INET,ho.c_str(),&(s.sin_addr.s_addr));
					if (res == 1)
#endif
					{
						res = getnameinfo((const struct sockaddr *)&s,sizeof(s),ho_name,sizeof(ho_name),service,sizeof(service),0);
						if (res == 0)
						{
							h_p = ho_name;
							h_p = h_p + ':';
						}
						else
							h_p = ho + ':';
					}
					else
						h_p = ho + ':';
				}
				else
				{
					if (pos == std::string::npos)
					{
						Tango::DeviceProxy::get_fqdn(ho);
					}

					h_p = ho + ':';
				}

//
// Add port number
//

				std::stringstream ss;
				ss << pBody.address.port;
				h_p = h_p + ss.str();

				break;
			}
		}
	}

	return true;
}


//+------------------------------------------------------------------
/**
 *	method:	AutoLock class ctor and dtor
 *
 *	description:	AutoLock is a small helper class which get a MySQL
 *	              connection from the pool and manages the DB transaction.
 */
//+------------------------------------------------------------------

AutoLock::AutoLock(DataBase *db)
  :the_db(db),
  m_dch(the_db->dcp)
{
	const std::string sql_query{"SET autocommit=0;"}; // or START TRANSACTION
	the_db->simple_query(sql_query,"AutoLock",m_dch);
}

AutoLock::~AutoLock()
{
	const std::string sql_query{"SET autocommit=1;"}; // or COMMIT
	the_db->simple_query(sql_query,"~AutoLock",m_dch);
}

/// Parse a string to return an integer
/// Current implementation does not complain about trailing garbage.
template<>
int DataBase::ParseString<int>(const char * c_str)
{
  int val;
  const int ret = sscanf(c_str, "%d", &val);

  if(ret != 1 || ret == EOF)
  {
    WARN_STREAM << "Could not parse string: " << std::string(c_str) << std::endl;
    Tango::Except::throw_exception(DB_ParseStringError, "Could not parse string to number", "DataBase::ParseString");
  }

  return val;
}

}