def send(self, key, value, decode_fn=lambda x: x, safe=False): if not isinstance(key, basestring): raise ValueError("key must be a string") if not isinstance(value, basestring) and value is not None: raise ValueError("value must be string-type (possibly binary) or None") if safe and self.has_key(key): raise ValueError("Already have key %s" % key) # TODO: I should make sure to escape single quotes here decoded_value = decode_fn(value) self._ic._cursor.execute(""" INSERT INTO %s VALUES (%s, %s) """ % (self._name, _py_to_sql_string(key), _py_to_sql_string(decoded_value)))
def send(self, key, value, decode_fn=lambda x: x, safe=False): if not isinstance(key, basestring): raise ValueError("key must be a string") if not isinstance(value, basestring) and value is not None: raise ValueError( "value must be string-type (possibly binary) or None") if safe and key in self: raise ValueError("Already have key %s" % key) # TODO: I should make sure to escape single quotes here decoded_value = decode_fn(value) self._ic._cursor.execute(""" INSERT INTO %s VALUES (%s, %s) """ % (self._name, _py_to_sql_string(key), _py_to_sql_string(decoded_value)))
def has_key(self, key): # TODO: I should make sure to escape single quotes here self._ic._cursor.execute("SELECT COUNT(*) FROM %s WHERE key=%s" % ( self._name, _py_to_sql_string(key))) count = self._ic._cursor.fetchall()[0][0] if count == 0: return False elif count == 1: return True else: raise KeyError("%s is not unique. Blob store in illegal state." % key)
def __getitem__(self, key): if not isinstance(key, basestring): raise ValueError("key must be a string") # TODO: I should make sure to escape single quotes here self._ic._cursor.execute("SELECT value FROM %s WHERE key=%s" % ( self._name, _py_to_sql_string(key))) results = self._ic._cursor.fetchall() if len(results) == 0: raise KeyError("%s not found." % key) if len(results) > 1: raise KeyError("%s is not unique. Blob store in illegal state." % key) return results[0][0]
def has_key(self, key): # TODO: I should make sure to escape single quotes here self._ic._cursor.execute("SELECT COUNT(*) FROM %s WHERE key=%s" % (self._name, _py_to_sql_string(key))) count = self._ic._cursor.fetchall()[0][0] if count == 0: return False elif count == 1: return True else: raise KeyError("%s is not unique. Blob store in illegal state." % key)
def put(self, key, expr, from_, safe=False): if not isinstance(key, basestring): raise ValueError("key must be string") if safe and key in self: raise ValueError("Already have key %s" % key) # TODO: I should make sure to escape single quotes here self._ic._cursor.execute(""" INSERT INTO %s SELECT %s, %s FROM %s """ % (self._name, _py_to_sql_string(key), expr, from_))
def __getitem__(self, key): if not isinstance(key, six.string_types): raise ValueError("key must be a string") # TODO: I should make sure to escape single quotes here self._ic._cursor.execute("SELECT value FROM %s WHERE key=%s" % (self._name, _py_to_sql_string(key))) results = self._ic._cursor.fetchall() if len(results) == 0: raise KeyError("%s not found." % key) if len(results) > 1: raise KeyError("%s is not unique. Blob store in illegal state." % key) return results[0][0]
def distribute_value_to_table(self, key, table_name): """Distributed value assoc with key to all rows in table_name. table_name is the name of a table or view. """ if not isinstance(key, basestring): raise ValueError("key must be string") from_with_side_data = """ %(table_name)s CROSS JOIN %(blob_store)s WHERE %(blob_store)s.key = %(key)s """ % {'table_name': table_name, 'blob_store': self.name, 'key': _py_to_sql_string(key)} return from_with_side_data