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
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
)
34 i
= 0 #for the ',' problem... look like C here...
38 #Boolean must be catch, because we want 0 or 1, not True or False
39 if isinstance(val
, bool):
45 if isinstance(val
, unicode) or isinstance(val
, str):
46 val
= val
.decode('utf8', 'ignore').replace("'", "''")
49 val
= val
.replace("'", "''")
52 props_str
= props_str
+ u
"%s " % prop
53 values_str
= values_str
+ u
"'%s' " % val
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
65 #Create a update query of table with data, and use where data for
67 def create_update_query(self
, table
, data
, where_data
):
68 query
= u
"UPDATE %s set " % (self
.table_prefix
+ table
)
72 i
= 0 #for the , problem...
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
:
79 #Boolean must be catch, because we want 0 or 1, not True or False
80 if isinstance(val
, bool):
85 if isinstance(val
, unicode) or isinstance(val
, str):
86 val
= val
.decode('utf8', 'ignore').replace("'", "''")
89 val
= val
.replace("'", "''")
91 query_folow
+= u
"%s='%s' " % (prop
, val
)
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
:
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):
107 if isinstance(val
, unicode) or isinstance(val
, str):
108 val
= val
.decode('utf8', 'ignore').replace("'", "''")
111 val
= val
.replace("'", "''")
114 where_clause
+= u
"%s='%s' " % (prop
, val
)
116 where_clause
+= u
"and %s='%s' " % (prop
, val
)
118 query
= query
+ query_folow
+ where_clause
124 return self
.db_cursor
.fetchone()