-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlite3.py
1587 lines (1376 loc) · 63.8 KB
/
sqlite3.py
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
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import print_function
import os
import logging
import functools
from concurrent.futures import ThreadPoolExecutor
import tango
from tango.databaseds.db_errors import *
th_exc = tango.Except.throw_exception
Executor = ThreadPoolExecutor(1)
def get_create_db_statements():
this_dir = os.path.dirname(__file__)
statements = []
with open(os.path.join(this_dir, "create_db_tables.sql")) as f:
lines = f.readlines()
# strip comments
lines = (line for line in lines if not line.startswith('#'))
lines = (line for line in lines if not line.lower().strip().startswith('key'))
lines = (line for line in lines if not line.lower().strip().startswith('key'))
lines = "".join(lines)
lines = lines.replace("ENGINE=MyISAM", "")
statements += lines.split(";")
with open(os.path.join(this_dir, "create_db.sql")) as f:
lines = f.readlines()
# strip comments
lines = (line for line in lines if not line.lower().startswith('#'))
lines = (line for line in lines if not line.lower().startswith('create database'))
lines = (line for line in lines if not line.lower().startswith('use'))
lines = (line for line in lines if not line.lower().startswith('source'))
lines = "".join(lines)
statements += lines.split(";")
return statements
def replace_wildcard(text):
# escape '%' with '\'
text = text.replace("%", "\\%")
# escape '_' with '\'
text = text.replace("_", "\\_")
# escape '"' with '\'
text = text.replace('"', '\\"')
# escape ''' with '\'
text = text.replace("'", "\\'")
# replace '*' with '%'
text = text.replace("*", "%")
return text
def use_cursor(f):
@functools.wraps(f)
def wrap(*args, **kwargs):
self = args[0]
has_cursor = 'cursor' in kwargs
cursor = kwargs.pop('cursor', None)
if not has_cursor:
cursor = Executor.submit(self.get_cursor).result()
self.cursor = cursor
try:
ret = Executor.submit(f, *args, **kwargs).result()
if not has_cursor:
Executor.submit(cursor.connection.commit).result()
return ret
finally:
if not has_cursor:
Executor.submit(cursor.close).result()
del self.cursor
return wrap
class SqlDatabase(object):
DB_API_NAME = 'sqlite3' # Default implementation
def __init__(self, name, db_name="tango_database.db", history_depth=10, fire_to_starter=True):
self._db_api = None
self._db_conn = None
self.name = name
self.dev_name = 'sys/database/' + name
self.db_name = db_name
self.history_depth = history_depth
self.fire_to_starter = fire_to_starter
self._logger = logging.getLogger(self.__class__.__name__)
self._debug = self._logger.debug
self._info = self._logger.info
self._warn = self._logger.warn
self._error = self._logger.error
self._critical = self._logger.critical
self.initialize()
def close_db(self):
if self._db_conn is not None:
self._db_conn.commit()
self._db_conn.close()
self._db_api = None
self._db_conn = None
def get_db_api(self):
if self._db_api is None:
self._db_api = __import__(self.DB_API_NAME)
return self._db_api
@property
def db_api(self):
return self.get_db_api()
@property
def db_conn(self):
if self._db_conn is None:
self._db_conn = self.db_api.connect(self.db_name)
return self._db_conn
def get_cursor(self):
return self.db_conn.cursor()
def initialize(self):
self._info("Initializing database %r (%s)...", self.db_name, os.path.isfile(self.db_name))
if not os.path.isfile(self.db_name):
self.create_db()
else:
# trigger connection
self._trigger_connection()
@use_cursor
def _trigger_connection(self):
return self.db_conn
@use_cursor
def create_db(self):
self._info("Creating database...")
statements = get_create_db_statements()
cursor = self.cursor
for statement in statements:
cursor.execute(statement)
@use_cursor
def get_id(self, name):
cursor = self.cursor
name += '"_history_id'
_id = cursor.execute('SELECT id FROM ?', (name,)).fetchone()[0] + 1
cursor.execute('UPDATE ? SET id=?', (name, _id))
return _id
@use_cursor
def purge_att_property(self, table, field, obj, attr, name):
cursor = self.cursor
cursor.execute(\
'SELECT DISTINCT id FROM ? WHERE ? = ? AND name = ? AND ' \
'attribute = ? ORDER BY date', (table, field, obj, name, attr))
rows = cursor.fetchall()
to_del = len(rows) - self.history_depth
if to_del > 0:
for row in rows[:to_del]:
cursor.execute('DELETE FROM ? WHERE id=?', (table, row[0]))
@use_cursor
def purge_property(self, table, field, obj, name):
cursor = self.cursor
cursor.execute(\
'SELECT DISTINCT id FROM ? WHERE ? = ? AND name = ? ' \
'ORDER BY date', (table, field, obj, name))
rows = cursor.fetchall()
to_del = len(rows) - self.history_depth
if to_del > 0:
for row in rows[:to_del]:
cursor.execute('DELETE FROM ? WHERE id=?', (table, row[0]))
@use_cursor
def get_device_host(self, name):
cursor = self.cursor
name = replace_wildcard(name)
cursor.execute('SELECT host FROM device WHERE name LIKE ?', (name,))
row = cursor.fetchone()
if row is None:
raise Exception("No host for device '" + name + "'")
else:
return row[0]
def send_starter_cmd(self, starter_dev_names):
for name in starter_dev_names:
pos = name.find('.')
if pos != -1:
name = name[0:pos]
dev = tango.DeviceProxy(name)
dev.UpdateServersInfo()
# TANGO API
def get_stored_procedure_release(self):
return 'release 1.8'
@use_cursor
def add_device(self, server_name, dev_info, klass_name, alias=None):
self._info("delete_attribute_alias(server_name=%s, dev_info=%s, klass_name=%s, alias=%s)",
server_name, dev_info, klass_name, alias)
dev_name, (domain, family, member) = dev_info
cursor = self.cursor
# first delete the tuple (device,name) from the device table
cursor.execute('DELETE FROM device WHERE name LIKE ?', (dev_name,))
# then insert the new value for this tuple
cursor.execute(\
'INSERT INTO device (name, alias, domain, family, member, exported, ' \
'ior, host, server, pid, class, version, started, stopped) ' \
'VALUES (?, ?, ?, ?, ?, 0, "nada", "nada", ?, 0, ?, "0", NULL, NULL)',
(dev_name, alias, domain, family, member, server_name, klass_name))
# Check if a DServer device entry for the process already exists
cursor.execute('SELECT name FROM device WHERE server LIKE ? AND class LIKE "DServer"', (server_name,))
if cursor.fetchone() is None:
dev_name = "dserver/" + server_name
domain, family, member = dev_name.split("/", 2)
cursor.execute(\
'INSERT INTO device (name, domain, family, member, exported, ior, ' \
'host, server, pid, class, version, started, stopped) ' \
'VALUES (?, ?, ?, ?, 0, "nada", "nada", ?, 0, "DServer", "0", NULL, NULL)',
(dev_name, domain, family, member, server_name))
@use_cursor
def delete_attribute_alias(self, alias):
self._info("delete_attribute_alias(alias=%s)", alias)
self.cursor.execute('DELETE FROM attribute_alias WHERE alias=?', (alias,))
@use_cursor
def delete_class_attribute(self, klass_name, attr_name):
self.cursor.execute(\
'DELETE FROM property_attribute_class WHERE class LIKE ? AND ' \
'attribute LIKE ?', (klass_name, attr_name))
@use_cursor
def delete_class_attribute_property(self, klass_name, attr_name, prop_name):
cursor = self.cursor
# Is there something to delete ?
cursor.execute(\
'SELECT count(*) FROM property_attribute_class WHERE class = ? ' \
'AND attribute = ? AND name = ?', (klass_name, attr_name, prop_name))
if cursor.fetchone()[0] > 0:
# then delete property from the property_attribute_class table
cursor.execute(\
'DELETE FROM property_attribute_class WHERE class = ? AND ' \
'attribute = ? and name = ?', (klass_name, attr_name, prop_name))
# mark this property as deleted
hist_id = self.get_id('class_attribute', cursor=cursor)
cursor.execute(\
'INSERT INTO property_attribute_class_hist (class, attribute, ' \
'name, id, count, value) VALUES ' \
'(?, ?, ?, ?, "0", "DELETED")',
(klass_name, attr_name, prop_name, hist_id))
self.purge_att_property("property_attribute_class_hist", "class",
klass_name, attr_name, prop_name, cursor=cursor)
@use_cursor
def delete_class_property(self, klass_name, prop_name):
cursor = self.cursor
prop_name = replace_wildcard(prop_name)
# Is there something to delete ?
cursor.execute(\
'SELECT DISTINCT name FROM property_class WHERE class=? AND ' \
'name LIKE ?', (klass_name, prop_name))
for row in cursor.fetchall():
# delete the tuple (device,name,count) from the property table
name = row[0]
cursor.execute(\
'DELETE FROM property_class WHERE class=? AND name=?',
(klass_name, name))
# Mark this property as deleted
hist_id = self.get_id("class", cursor=cursor)
cursor.execute(\
'INSERT INTO property_class_hist (class, name, id, count, value) ' \
'VALUES (?, ?, ?, "0", "DELETED")',
(klass_name, name, hist_id))
self.purge_property("property_class_hist", "class", klass_name,
name, cursor=cursor)
@use_cursor
def delete_device(self, dev_name):
self._info("delete_device(dev_name=%s)", dev_name)
cursor = self.cursor
dev_name = replace_wildcard(dev_name)
# delete the device from the device table
cursor.execute('DELETE FROM device WHERE name LIKE ?', (dev_name,))
# delete device from the property_device table
cursor.execute('DELETE FROM property_device WHERE device LIKE ?', (dev_name,))
# delete device from the property_attribute_device table
cursor.execute('DELETE FROM property_attribute_device WHERE device LIKE ?', (dev_name,))
@use_cursor
def delete_device_alias(self, dev_alias):
self._info("delete_device_alias(dev_alias=%s)", dev_alias)
self.cursor.execute('UPDATE device SET alias=NULL WHERE alias=?', (dev_alias,))
@use_cursor
def delete_device_attribute(self, dev_name, attr_name):
dev_name = replace_wildcard(dev_name)
self.cursor.execute(\
'DELETE FROM property_attribute_device WHERE device LIKE ? AND ' \
'attribute LIKE ?', (dev_name, attr_name))
@use_cursor
def delete_device_attribute_property(self, dev_name, attr_name, prop_name):
cursor = self.cursor
# Is there something to delete ?
cursor.execute(\
'SELECT count(*) FROM property_attribute_device WHERE device = ?' \
'AND attribute = ? AND name = ?', (dev_name, attr_name, prop_name))
if cursor.fetchone()[0] > 0:
# delete property from the property_attribute_device table
cursor.execute(\
'DELETE FROM property_attribute_device WHERE device = ? AND '
'attribute = ? AND name = ?', (dev_name, attr_name, prop_name))
# Mark this property as deleted
hist_id = self.get_id("device_attribute", cursor=cursor)
cursor.execute(\
'INSERT INTO property_attribute_device_hist ' \
'(device, attribute, name, id, count, value) VALUES ' \
'(?, ?, ?, ?, "0", "DELETED")', (dev_name, attr_name, prop_name, hist_id))
self.purge_att_property("property_attribute_device_hist", "device",
dev_name, attr_name, prop_name, cursor=cursor)
@use_cursor
def delete_device_property(self, dev_name, prop_name):
cursor = self.cursor
prop_name = replace_wildcard(prop_name)
# Is there something to delete ?
cursor.execute(\
'SELECT DISTINCT name FROM property_device WHERE device=? AND ' \
'name LIKE ?', (dev_name, prop_name))
for row in cursor.fetchall():
# delete the tuple (device,name,count) from the property table
cursor.execute(\
'DELETE FROM property_device WHERE device=? AND name LIKE ?',
(dev_name, prop_name))
# Mark this property as deleted
hist_id = self.get_id("device", cursor=cursor)
cursor.execute(\
'INSERT INTO property_device_hist (device, id, name, count, value) ' \
'VALUES (?, ?, ?, "0", "DELETED")', (dev_name, hist_id, row[0]))
self.purge_property("property_device_hist", "device", dev_name, row[0])
@use_cursor
def delete_property(self, obj_name, prop_name):
cursor = self.cursor
prop_name = replace_wildcard(prop_name)
# Is there something to delete ?
cursor.execute(\
'SELECT DISTINCT name FROM property WHERE object=? AND ' \
'name LIKE ?', (obj_name, prop_name))
for row in cursor.fetchall():
# delete the tuple (object,name,count) from the property table
cursor.execute(\
'DELETE FROM property_device WHERE device=? AND name LIKE ?',
(obj_name, prop_name))
# Mark this property as deleted
hist_id = self.get_id("object", cursor=cursor)
cursor.execute(\
'INSERT INTO property_hist (object, name, id, count, value) ' \
'VALUES (?, ?, ?, "0", "DELETED")', (obj_name, row[0], hist_id))
self.purge_property("property_hist", "object", obj_name, row[0])
@use_cursor
def delete_server(self, server_instance):
cursor = self.cursor
server_instance = replace_wildcard(server_instance)
previous_host = None
# get host where running
if self.fire_to_starter:
adm_dev_name = "dserver/" + server_instance
previous_host = self.get_device_host(adm_dev_name)
# then delete the device from the device table
cursor.execute('DELETE FROM device WHERE server LIKE ?', (server_instance,))
# Update host's starter to update controlled servers list
if self.fire_to_starter and previous_host:
self.send_starter_cmd(previous_host)
pass
@use_cursor
def delete_server_info(self, server_instance):
self.cursor.execute('DELETE FROM server WHERE name=?', (server_instance,))
@use_cursor
def export_device(self, dev_name, IOR, host, pid, version):
self._info("export_device(dev_name=%s, host=%s, pid=%s, version=%s)",
dev_name, host, pid, version)
self._info("export_device(IOR=%s)", IOR)
cursor = self.cursor
do_fire = False
previous_host = None
if self.fire_to_starter:
if dev_name[0:8] == "dserver/":
# Get database server name
tango_util = tango.Util.instance()
db_serv = tango_util.get_ds_name()
adm_dev_name = "dserver/" + db_serv.lower()
if dev_name != adm_dev_name and dev_name[0:16] != "dserver/starter/":
do_fire = True
previous_host = self.get_device_host(dev_name)
cursor.execute('SELECT server FROM device WHERE name LIKE ?', (dev_name,))
row = cursor.fetchone()
if row is None:
th_exc(DB_DeviceNotDefined,
"device " + dev_name + " not defined in the database !",
"DataBase::ExportDevice()")
server = row[0]
# update the new value for this tuple
cursor.execute(\
'UPDATE device SET exported=1, ior=?, host=?, pid=?, version=?, ' \
'started=datetime("now") WHERE name LIKE ?',
(IOR, host, pid, version, dev_name))
# update host name in server table
cursor.execute('UPDATE server SET host=? WHERE name LIKE ?', (host, server))
if do_fire:
hosts = []
hosts.append(host)
if previous_host != "" and previous_host != "nada" and previous_host != host:
hosts.append(previous_host)
self.send_starter_cmd(hosts)
@use_cursor
def export_event(self, event, IOR, host, pid, version):
cursor = self.cursor
cursor.execute(\
'INSERT event (name,exported,ior,host,server,pid,version,started) ' \
'VALUES (?, 1, ?, ?, ?, ?, ?, datetime("now")',
(event, IOR, host, event, pid, version))
@use_cursor
def get_alias_device(self, dev_alias):
cursor = self.cursor
cursor.execute('SELECT name FROM device WHERE alias LIKE ?',
(dev_alias,))
row = cursor.fetchone()
if row is None:
th_exc(DB_DeviceNotDefined,
"No device found for alias '" + dev_alias + "'",
"DataBase::GetAliasDevice()")
return row[0]
@use_cursor
def get_attribute_alias(self, attr_alias):
cursor = self.cursor
cursor.execute('SELECT name from attribute_alias WHERE alias LIKE ?',
(attr_alias,))
row = cursor.fetchone()
if row is None:
th_exc(DB_SQLError,
"No attribute found for alias '" + attr_alias + "'",
"DataBase::GetAttributeAlias()")
return row[0]
@use_cursor
def get_attribute_alias_list(self, attr_alias):
cursor = self.cursor
cursor.execute('SELECT DISTINCT alias FROM attribute_alias WHERE alias LIKE ? ORDER BY attribute',
(attr_alias,))
return [ row[0] for row in cursor.fetchall() ]
@use_cursor
def get_class_attribute_list(self, class_name, wildcard):
cursor = self.cursor
cursor.execute('SELECT DISTINCT attribute FROM property_attribute_class WHERE class=? and attribute like ?',
(class_name, wildcard))
return [ row[0] for row in cursor.fetchall() ]
@use_cursor
def get_class_attribute_property(self, class_name, attributes):
cursor = self.cursor
stmt = 'SELECT name,value FROM property_attribute_class WHERE class=? AND attribute LIKE ?'
result = [class_name, str(len(attributes))]
for attribute in attributes:
cursor.execute(stmt, (class_name, attribute))
rows = cursor.fetchall()
result.append(attribute)
result.append(str(len(rows)))
for row in rows:
result.append(row[0])
result.append(row[1])
return result
@use_cursor
def get_class_attribute_property2(self, class_name, attributes):
cursor = self.cursor
stmt = 'SELECT name,value FROM property_attribute_class WHERE class=? AND attribute LIKE ? ORDER BY name,count'
result = [class_name, str(len(attributes))]
for attribute in attributes:
cursor.execute(stmt, (class_name, attribute))
rows = cursor.fetchall()
result.append(attribute)
j = 0
new_prop = True
nb_props = 0
prop_size = 0
prop_names = []
prop_sizes = []
prop_values = []
for row in rows:
prop_values.append(row[1])
if j == 0:
old_name = row[0]
else:
name = row[0]
if name != old_name:
new_prop = True
old_name = name
else:
new_prop = False
j = j + 1
if new_prop == True:
nb_props = nb_props + 1
prop_names.append(row[0])
if prop_size != 0:
prop_sizes.append(prop_size)
prop_size = 1
else:
prop_size = prop_size + 1
result.append(str(nb_props))
j = 0
k = 0
for name in prop_names:
result.append(name)
result.append(prop_sizes[j])
for i in range(0, prop_sizes[j]):
result.append(prop_values[k])
k = k + 1
j = j + 1
return result
@use_cursor
def get_class_attribute_property_hist(self, class_name, attribute, prop_name):
cursor = self.cursor
stmt = 'SELECT DISTINCT id FROM property_attribute_class_hist WHERE class=? AND attribute LIKE ? AND name LIKE ? ORDER by date ASC'
result = []
cursor.execute(stmt, (class_name, attribute, prop_name))
for row in cursor.fetchall():
idr = row[0]
stmt = 'SELECT DATE_FORMAT(date,\'%Y-%m-%d %H:%i:%s\'),value,attribute,name,count FROM property_attribute_class_hist WHERE id =? AND class =?'
cursor.execute(stmt, (idr, class_name))
rows = cursor.fetchall()
result.append(rows[2])
result.append(rows[3])
result.append(rows[0])
result.append(str(rows[4]))
for value in rows[1]:
result.append(value)
return result
@use_cursor
def get_class_for_device(self, dev_name):
cursor = self.cursor
cursor.execute('SELECT DISTINCT class FROM device WHERE name=?', (dev_name,))
row = cursor.fetchone()
if row is None:
th_exc(DB_IncorrectArguments, "Class not found for " + dev_name,
"Database.GetClassForDevice")
return row
@use_cursor
def get_class_inheritance_for_device(self, dev_name):
cursor = self.cursor
class_name = self.get_class_for_device(dev_name, cursor=cursor)
props = self.get_class_property(class_name, "InheritedFrom", cursor=cursor)
return [class_name] + props[4:]
@use_cursor
def get_class_list(self, server):
cursor = self.cursor
cursor.execute('SELECT DISTINCT class FROM device WHERE class LIKE ? ORDER BY class', (server,))
return [ row[0] for row in cursor.fetchall() ]
@use_cursor
def get_class_property(self, class_name, properties):
cursor = self.cursor
stmt = 'SELECT count,value FROM property_class WHERE class=? AND name LIKE ? ORDER BY count'
result.append(class_name)
result.append(len(properties))
for prop_name in properties:
cursor.execute(stmt, (class_name, prop_name))
rows = cursor.fetchall()
result.append(prop_name)
result.append(str(len(rows)))
for row in rows:
result.append(row[1])
return result
@use_cursor
def get_class_property_hist(self, class_name, prop_name):
cursor = self.cursor
stmt = 'SELECT DISTINCT id FROM property_class_hist WHERE class=? AND AND name LIKE ? ORDER by date ASC'
result = []
cursor.execute(stmt, (class_name, prop_name))
for row in cursor.fetchall():
idr = row[0]
stmt = 'SELECT DATE_FORMAT(date,\'%Y-%m-%d %H:%i:%s\'),value,name,count FROM property_class_hist WHERE id =? AND class =?'
cursor.execute(stmt, (idr, class_name))
rows = cursor.fetchall()
result.append(rows[2])
result.append(rows[0])
result.append(str(rows[3]))
for value in rows[1]:
result.append(value)
return result
@use_cursor
def get_class_property_list(self, class_name):
cursor = self.cursor
cursor.execute('SELECT DISTINCT name FROM property_class WHERE class LIKE ? order by NAME',
(class_name,))
return [ row[0] for row in cursor.fetchall() ]
@use_cursor
def get_device_alias(self, dev_name):
cursor = self.cursor
cursor.execute('SELECT DISTINCT alias FROM device WHERE name LIKE ?',
(dev_name,))
row = cursor.fetchone()
if row is None:
th_exc(DB_DeviceNotDefined,
"No alias found for device '" + dev_name + "'",
"DataBase::GetDeviceAlias()")
return row[0]
@use_cursor
def get_device_alias_list(self, alias):
cursor = self.cursor
cursor.execute('SELECT DISTINCT alias FROM device WHERE alias LIKE ? ORDER BY alias',
(alias,))
return [ row[0] for row in cursor.fetchall() ]
@use_cursor
def get_device_attribute_list(self, dev_name, attribute):
cursor = self.cursor
cursor.execute('SELECT DISTINCT attribute FROM property_attribute_device WHERE device=? AND attribute LIKE ? ORDER BY attribute',
(dev_name, attribute,))
return [ row[0] for row in cursor.fetchall() ]
@use_cursor
def get_device_attribute_property(self, dev_name, attributes):
cursor = self.cursor
stmt = 'SELECT name,value FROM property_attribute_device WHERE device=? AND attribute LIKE ?'
result = [dev_name, str(len(attributes))]
for attribute in attributes:
cursor.execute(stmt, (dev_name, attribute))
rows = cursor.fetchall()
result.append(attribute)
result.append(str(len(rows)))
for row in rows:
result.append(row[0])
result.append(row[1])
return result
@use_cursor
def get_device_attribute_property2(self, dev_name, attributes):
cursor = self.cursor
stmt = 'SELECT name,value FROM property_attribute_device WHERE device=? AND attribute LIKE ? ORDER BY name,count'
result = [dev_name, str(len(attributes))]
for attribute in attributes:
cursor.execute(stmt, (dev_name, attribute))
rows = cursor.fetchall()
result.append(attribute)
j = 0
new_prop = True
nb_props = 0
prop_size = 0
prop_names = []
prop_sizes = []
prop_values = []
for row in rows:
prop_values.append(row[1])
if j == 0:
old_name = row[0]
else:
name = row[0]
if name != old_name:
new_prop = True
old_name = name
else:
new_prop = False
j = j + 1
if new_prop == True:
nb_props = nb_props + 1
prop_names.append(row[0])
if prop_size != 0:
prop_sizes.append(prop_size)
prop_size = 1
else:
prop_size = prop_size + 1
result.append(str(nb_props))
j = 0
k = 0
for name in prop_names:
result.append(name)
result.append(prop_sizes[j])
for i in range(0, prop_sizes[j]):
result.append(prop_values[k])
k = k + 1
j = j + 1
return result
@use_cursor
def get_device_attribute_property_hist(self, dev_name, attribute, prop_name):
cursor = self.cursor
stmt = 'SELECT DISTINCT id FROM property_attribute_device_hist WHERE device=? AND attribute LIKE ? AND name LIKE ? ORDER by date ASC'
result = []
cursor.execute(stmt, (dev_name, attribute, prop_name))
for row in cursor.fetchall():
idr = row[0]
stmt = 'SELECT DATE_FORMAT(date,\'%Y-%m-%d %H:%i:%s\'),value,attribute,name,count FROM property_attribute_device_hist WHERE id =? AND device =? ORDER BY count ASC'
cursor.execute(stmt, (idr, class_name))
rows = cursor.fetchall()
result.append(rows[2])
result.append(rows[3])
result.append(rows[0])
result.append(str(rows[4]))
for value in rows[1]:
result.append(value)
return result
@use_cursor
def get_device_class_list(self, server_name):
cursor = self.cursor
result = []
cursor.execute('SELECT name,class FROM device WHERE server =? ORDER BY name',
(server_name,))
for row in cursor.fetchall():
result.append(row[0])
result.append(row[1])
return result
@use_cursor
def get_device_domain_list(self, wildcard):
cursor = self.cursor
cursor.execute('SELECT DISTINCT domain FROM device WHERE name LIKE ? OR alias LIKE ? ORDER BY domain',
(wildcard,wildcard))
return [ row[0] for row in cursor.fetchall() ]
@use_cursor
def get_device_exported_list(self, wildcard):
cursor = self.cursor
cursor.execute('SELECT DISTINCT name FROM device WHERE (name LIKE ? OR alias LIKE ?) AND exported=1 ORDER BY name',
(wildcard,wildcard))
return [ row[0] for row in cursor.fetchall() ]
@use_cursor
def get_device_family_list(self, wildcard):
cursor = self.cursor
cursor.execute('SELECT DISTINCT family FROM device WHERE name LIKE ? OR alias LIKE ? ORDER BY family',
(wildcard,wildcard))
return [ row[0] for row in cursor.fetchall() ]
@use_cursor
def get_device_info(self, dev_name):
cursor = self.cursor
cursor.execute('SELECT exported,ior,version,pid,server,host,started,stopped,class FROM device WHERE name =? or alias =?',
(dev_name,dev_name))
result_long = []
result_str = []
for row in cursor.fetchall():
if ((row[4] == None) or (row[5] == None)):
th_exc(DB_SQLError,
"Wrong info in database for device '" + dev_name + "'",
"DataBase::GetDeviceInfo()")
result_str.append(dev_name)
if raw[1] != None:
result_str.append(str(raw[1]))
else:
result_str.append("")
result_str.append(str(raw[2]))
result_str.append(str(raw[4]))
result_str.append(str(raw[5]))
for i in range(0,2):
cursor.execute('SELECT DATE_FORMAT(?,\'%D-%M-%Y at %H:%i:%s\')', raw[6 + i])
tmp_date = cursor.fetchone()
if tmp_date == None:
result_str.append("?")
else:
result_str.append(str(tmp_date))
for i in range(0,2):
if raw[i] != None:
result_long.append(raw[i])
result = (result_long, result_str)
return result
@use_cursor
def get_device_list(self,server_name, class_name ):
cursor = self.cursor
cursor.execute('SELECT DISTINCT name FROM device WHERE server LIKE ? AND class LIKE ? ORDER BY name',
(server_name, class_name))
return [ row[0] for row in cursor.fetchall() ]
@use_cursor
def get_device_wide_list(self, wildcard):
cursor = self.cursor
cursor.execute('SELECT DISTINCT name FROM device WHERE name LIKE ? ORDER BY name',
(wildcard,))
return [ row[0] for row in cursor.fetchall() ]
@use_cursor
def get_device_member_list(self, wildcard):
cursor = self.cursor
cursor.execute('SELECT DISTINCT member FROM device WHERE name LIKE ? ORDER BY member',
(wildcard,))
return [ row[0] for row in cursor.fetchall() ]
@use_cursor
def get_device_property(self, dev_name, properties):
cursor = self.cursor
stmt = 'SELECT count,value,name FROM property_device WHERE device = ? AND name LIKE ? ORDER BY count'
result = []
result.append(dev_name)
result.append(str(len(properties)))
for prop in properties:
result.append(prop)
tmp_name = replace_wildcard(prop)
cursor.execute(stmt, (dev_name, tmp_name))
rows = cursor.fetchall()
result.append(attribute)
result.append(str(len(rows)))
for row in rows:
result.append(row[1])
return result
@use_cursor
def get_device_property_hist(self, device_name, prop_name):
cursor = self.cursor
stmt = 'SELECT DISTINCT id FROM property_device_hist WHERE device=? AND name LIKE ? ORDER by date ASC'
result = []
tmp_name = replace_wildcard(prop_name);
cursor.execute(stmt, (class_name, device_name, tmp_name))
stmt = 'SELECT DATE_FORMAT(date,\'%Y-%m-%d %H:%i:%s\'),value,name,count FROM property_device_hist WHERE id =? AND device =? ORDER BY count ASC'
for row in cursor.fetchall():
idr = row[0]
cursor.execute(stmt, (idr, device_name))
rows = cursor.fetchall()
result.append(rows[2])
result.append(rows[0])
result.append(str(rows[3]))
for value in rows[1]:
result.append(value)
return result
@use_cursor
def get_device_server_class_list(self, server_name):
cursor = self.cursor
cursor.execute('SELECT DISTINCT class FROM device WHERE server LIKE ? ORDER BY class',
(sever_name,))
return [ row[0] for row in cursor.fetchall() ]
@use_cursor
def get_exported_device_list_for_class(self, class_name):
cursor = self.cursor
cursor.execute('SELECT DISTINCT name FROM device WHERE class LIKE ? AND exported=1 ORDER BY name',
(class_name,))
return [ row[0] for row in cursor.fetchall() ]
@use_cursor
def get_host_list(self, host_name):
cursor = self.cursor
cursor.execute('SELECT DISTINCT host FROM device WHERE host LIKE ? ORDER BY host',
(host_name,))
return [ row[0] for row in cursor.fetchall() ]
@use_cursor
def get_host_server_list(self, host_name):
cursor = self.cursor
cursor.execute('SELECT DISTINCT server FROM device WHERE host LIKE ? ORDER BY server',
(host_name,))
return [ row[0] for row in cursor.fetchall() ]
def get_host_servers_info(self, host_name):
servers = self.get_host_server_list(host_name)
result = []
for server in servers:
result.append(server)
info = self.get_server_info(server)
result.append(info[2])
result.append(info[3])
return result
def get_instance_name_list(self, server_name):
server_name = server_name + "\*"
server_list = self.get_server_list(server_name)
result = []
for server in server_list:
names = server.split("/")
result.append(names[1])
return result
@use_cursor
def get_object_list(self, name):
cursor = self.cursor
cursor.execute('SELECT DISTINCT object FROM property WHERE object LIKE ? ORDER BY object',
(name,))
return [ row[0] for row in cursor.fetchall() ]
@use_cursor
def get_property(self, object_name, properties):
cursor = self.cursor
result = []
result.append(object_name)
result.append(str(len(properties)))
stmt = 'SELECT count,value,name FROM property WHERE object LIKE ? AND name LIKE ? ORDER BY count'
for prop_name in properties:
result.append(prop_name)
prop_name = replace_wildcard(prop_name)
cursor.execute(stmt, (object_name,prop_name))
rows = cursor.fetchall()
n_rows = len(rows)
result.append(str(n_rows))
if n_rows:
for row in rows:
result.append(row[1])
else:
result.append(" ")
return result
@use_cursor
def get_property_hist(self, object_name, prop_name):
cursor = self.cursor
result = []
stmt = 'SELECT DISTINCT id FROM property_hist WHERE object=? AND name LIKE ? ORDER by date'
prop_name = replace_wildcard(prop_name)
cursor.execute(stmt, (object_name, prop_name))
stmt = 'SELECT DATE_FORMAT(date,\'%Y-%m-%d %H:%i:%s\'),value,name,count FROM property_hist WHERE id =? AND object =?'
for row in cursor.fetchall():
idr = row[0]
cursor.execute(stmt, (idr, object_name))
rows = cursor.fetchall()
count = len(rows)