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