Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def sql_insert(self, table="", uptype="INSERT", sqldict={}, filters={}):
- self.states['Activity'] = 1
- fil=""
- if len(filters) > 0:
- fil += "where "
- for key, value in filters.items():
- fil += "%s = '%s' and " %(key, value)
- fil = fil[0:-4]
- if uptype == "INSERT":
- cols = [None] * len(sqldict)
- row = [None] * len(sqldict)
- i = 0
- for key, value in sqldict.items():
- cols[i] = key
- if value != "NULL" and value != None:
- row[i] = "'%s'" %self.sf.safe_var(str(value))
- else:
- row[i] = "NULL"
- i+=1
- columns = ''', '''.join(cols)
- data = ', '.join(row)
- query = '''INSERT INTO %s ( %s ) VALUES ( %s ) %s;''' % (table, columns, data, fil)
- elif uptype == "UPDATE":
- updates = ""
- for key, value in sqldict.items():
- if value != "NULL" and value != None:
- updates += "%s = '%s', " %(key, self.sf.safe_var(str(value)))
- else:
- updates += "%s = NULL, " %key
- if updates != "":
- updates = updates[0:-2]
- query = '''UPDATE %s SET %s %s;''' % (table, updates, fil)
- elif uptype == "DELETE":
- query = '''DELETE FROM %s %s;''' % (table, fil)
- return query
- class SafeVars:
- def __init__(self):
- self.strtype = type("test")
- self.inttype = type(0)
- self.datetype = type(datetime.now())
- def safe_var(self, userinput):
- if type(userinput) == self.strtype:
- output = self.safe_string(userinput)
- elif type(userinput) == self.inttype:
- output = self.safe_int(userinput)
- elif type(userinput) == self.datetype:
- output = self.safe_date(userinput)
- else:
- output = userinput
- return output
- def decode_vars(self, userinput):
- if type(userinput) == self.strtype:
- userinput = userinput.encode().decode('unicode-escape')
- #userinput = userinput.encode().decode('unicode-escape')
- output = userinput.encode("latin1").decode("UTF-8")
- elif type(userinput) == self.inttype:
- output = self.safe_int(userinput)
- elif type(userinput) == self.datetype:
- output = self.safe_date(userinput)
- else:
- output = userinput
- return output
- def safe_string(self, userinput):
- output = str(userinput)
- output = output.encode('UTF-8')
- output = str(output).replace("\\\\", "")[2:-1].replace("\\x", "\\\\x")
- output = output.replace("\\'", "'")
- output = output.replace('"', '\\\\\\"')
- output = output.replace("'", "\\\\\\'")
- return output
- def safe_int(self, userinput):
- output = userinput
- return output
- def safe_date(self, userinput):
- return userinput
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement