def update(self, data): """update is your basic update method""" data = to_ascii(data) if data is None: return None original_data = {} updated_data = {} for i in data: if i.startswith("_") and i != "_table" and i != "_id": original_data[i] = data[i] else: updated_data[i] = data[i] to_change = {} for i in updated_data: if i != "_table" and i != "_id": if updated_data[i] != original_data["_%s" % i]: to_change[i] = updated_data[i] sql = "UPDATE \"%s\" SET" % data["_table"] if not to_change: return None for i in to_change: sql += " %s=?," % i sql = sql.strip(",") sql += " WHERE id = ?;" params = to_change.values() params.append(data["_id"]) self.raw_sql(sql, params)
def insert(self, table_name, data): """insert is your basic insertion method""" data = to_ascii(data) if data is None: return None sql = "INSERT INTO %s" % table_name sql += "(id, %s) VALUES" % ', '.join(data.keys()) sql += "(NULL, " sql += ', '.join(['?'] * len(data.values())) sql = "%s);" % sql params = data.values() self.raw_sql(sql, params)
def delete(self, data): """delete is your basic deletion method""" data = to_ascii(data) if data is None: return None if type(data) == dict: sql = "DELETE FROM \"%s\" WHERE id = ?;" % data["_table"] self.raw_sql(sql, [data["_id"]]) return elif type(data) == list: tables_and_ids = {} for i in data: table = i["_table"] if table not in tables_and_ids: tables_and_ids[table] = [] tables_and_ids[table].append(i["_id"]) for k, j in tables_and_ids.iteritems(): sql = "DELETE FROM \"%s\" WHERE id IN (%s);" % ( k, ', '.join(['?']*len(j)) ) self.raw_sql(sql, j)
def delete(self, data): """delete is your basic deletion method""" data = to_ascii(data) if data is None: return None if isinstance(data, dict): sql = "DELETE FROM \"%s\" WHERE id = ?;" % data["_table"] self.raw_sql(sql, [data["_id"]]) return elif isinstance(data, (list, tuple)): tables_and_ids = {} for i in data: table = i["_table"] if table not in tables_and_ids: tables_and_ids[table] = [] tables_and_ids[table].append(i["_id"]) for k, j in tables_and_ids.iteritems(): sql = "DELETE FROM \"%s\" WHERE id IN (%s);" % ( k, ', '.join(['?']*len(j)) ) self.raw_sql(sql, j)