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
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
)
36 i
= 0 #for the ',' problem... look like C here...
40 #Boolean must be catch, because we want 0 or 1, not True or False
41 if isinstance(val
, bool):
47 if isinstance(val
, unicode) or isinstance(val
, str):
48 val
= val
.decode('utf8', 'ignore').replace("'", "''")
51 val
= val
.replace("'", "''")
54 props_str
= props_str
+ u
"%s " % prop
55 values_str
= values_str
+ u
"'%s' " % val
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
67 #Create a update query of table with data, and use where data for
69 def create_update_query(self
, table
, data
, where_data
):
70 query
= u
"UPDATE %s set " % (self
.table_prefix
+ table
)
74 i
= 0 #for the , problem...
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
:
81 #Boolean must be catch, because we want 0 or 1, not True or False
82 if isinstance(val
, bool):
87 if isinstance(val
, unicode) or isinstance(val
, str):
88 val
= val
.decode('utf8', 'ignore').replace("'", "''")
91 val
= val
.replace("'", "''")
93 query_folow
+= u
"%s='%s' " % (prop
, val
)
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
:
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):
109 if isinstance(val
, unicode) or isinstance(val
, str):
110 val
= val
.decode('utf8', 'ignore').replace("'", "''")
113 val
= val
.replace("'", "''")
116 where_clause
+= u
"%s='%s' " % (prop
, val
)
118 where_clause
+= u
"and %s='%s' " % (prop
, val
)
120 query
= query
+ query_folow
+ where_clause
126 return self
.db_cursor
.fetchone()