Add : service without host will be just droped, like Nagios.
[shinken.git] / shinken / db.py
blob658a99db6fb5520505cf9b0c0c7fdd7be70e4121
1 #!/usr/bin/env python
2 #Copyright (C) 2009-2010 :
3 # Gabes Jean, naparuba@gmail.com
4 # Gerhard Lausser, Gerhard.Lausser@consol.de
5 # Gregory Starck, g.starck@gmail.com
6 # Hartmut Goebel, h.goebel@goebel-consult.de
8 #This file is part of Shinken.
10 #Shinken is free software: you can redistribute it and/or modify
11 #it under the terms of the GNU Affero General Public License as published by
12 #the Free Software Foundation, either version 3 of the License, or
13 #(at your option) any later version.
15 #Shinken is distributed in the hope that it will be useful,
16 #but WITHOUT ANY WARRANTY; without even the implied warranty of
17 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 #GNU Affero General Public License for more details.
20 #You should have received a copy of the GNU Affero General Public License
21 #along with Shinken. If not, see <http://www.gnu.org/licenses/>.
24 #DB is a generic class for SQL Database
26 class DB(object):
27 def __init__(self, table_prefix = ''):
28 self.table_prefix = table_prefix
31 #Create a INSERT query in table with all data of data (a dict)
32 def create_insert_query(self, table, data):
33 query = u"INSERT INTO %s " % (self.table_prefix + table)
34 props_str = u' ('
35 values_str = u' ('
36 i = 0 #for the ',' problem... look like C here...
37 for prop in data:
38 i += 1
39 val = data[prop]
40 #Boolean must be catch, because we want 0 or 1, not True or False
41 if isinstance(val, bool):
42 if val:
43 val = 1
44 else:
45 val = 0
47 if isinstance(val, unicode) or isinstance(val, str):
48 val = val.decode('utf8', 'ignore').replace("'", "''")
49 else:
50 val = str(val)
51 val = val.replace("'", "''")
53 if i == 1:
54 props_str = props_str + u"%s " % prop
55 values_str = values_str + u"'%s' " % val
56 else:
57 props_str = props_str + u", %s " % prop
58 values_str = values_str + u", '%s' " % val
60 #Ok we've got data, let's finish the query
61 props_str = props_str + u' )'
62 values_str = values_str + u' )'
63 query = query + props_str + u' VALUES' + values_str
64 return query
67 #Create a update query of table with data, and use where data for
68 #the WHERE clause
69 def create_update_query(self, table, data, where_data):
70 query = u"UPDATE %s set " % (self.table_prefix + table)
72 #First data manage
73 query_folow = ''
74 i = 0 #for the , problem...
75 for prop in data:
76 #Do not need to update a property that is in where
77 #it is even dangerous, will raise a warning
78 if prop not in where_data:
79 i += 1
80 val = data[prop]
81 #Boolean must be catch, because we want 0 or 1, not True or False
82 if isinstance(val, bool):
83 if val:
84 val = 1
85 else:
86 val = 0
87 if isinstance(val, unicode) or isinstance(val, str):
88 val = val.decode('utf8', 'ignore').replace("'", "''")
89 else:
90 val = str(val)
91 val = val.replace("'", "''")
92 if i == 1:
93 query_folow += u"%s='%s' " % (prop, val)
94 else:
95 query_folow += u", %s='%s' " % (prop, val)
97 #Ok for data, now WHERE, same things
98 where_clause = u" WHERE "
99 i = 0 # For the 'and' problem
100 for prop in where_data:
101 i += 1
102 val = where_data[prop]
103 #Boolean must be catch, because we want 0 or 1, not True or False
104 if isinstance(val, bool):
105 if val:
106 val = 1
107 else:
108 val = 0
109 if isinstance(val, unicode) or isinstance(val, str):
110 val = val.decode('utf8', 'ignore').replace("'", "''")
111 else:
112 val = str(val)
113 val = val.replace("'", "''")
115 if i == 1:
116 where_clause += u"%s='%s' " % (prop, val)
117 else:
118 where_clause += u"and %s='%s' " % (prop, val)
120 query = query + query_folow + where_clause
121 return query
124 #Just get an entry
125 def fetchone(self):
126 return self.db_cursor.fetchone()