def remove_meta(self, cur, key, value=None): """Removes a metadata (key, value) pair or all pairs that match on the key alone. """ where = "WHERE key = %s" % sql_literal(key) if value: where += " AND value = %s" % sql_literal(value) cur.execute(""" DELETE FROM %(schema)s.%(table)s %(where)s ; """ % dict(schema=self._SCHEMA_NAME, table=self._TABLE_NAME, where=where))
def add_meta(self, cur, key, value): """Adds a metadata (key, value) pair. """ cur.execute(""" INSERT INTO %(schema)s.%(table)s (key, value) VALUES (%(key)s, %(value)s) ; """ % dict(schema=self._SCHEMA_NAME, table=self._TABLE_NAME, key=sql_literal(key), value=sql_literal(value)))
def add_meta(self, cur, key, value): """Adds a metadata (key, value) pair. """ cur.execute(""" INSERT INTO %(schema)s.%(table)s (key, value) VALUES (%(key)s, %(value)s) ; """ % dict(schema=self._SCHEMA_NAME, table=self._TABLE_NAME, key=sql_literal(key), value=sql_literal(value) ) )
def _destroy_catalog(self, conn, ignored_cur, catalog): """Destroys a catalog. Do not call this method directly. NOTE: This code looks b0rken; notice reference to _dbc which is never defined """ cur = None try: conn.set_isolation_level( psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) # first, attempt to disconnect clients cur = conn.cursor() cur.execute(""" SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = %(dbname)s AND pid <> pg_backend_pid() ;""" % dict(dbname=sql_literal(catalog.descriptor[self._KEY_DBNAME]))) #TODO: note that a client could reconnect ...now... and prevent the drop # then, drop database cur.execute("DROP DATABASE " + sql_identifier(catalog.descriptor[self._KEY_DBNAME])) cur.close() except psycopg2.Error, ev: msg = str(ev) idx = msg.find("\n") # DETAIL starts after the first line feed if idx > -1: msg = msg[0:idx] raise RuntimeError(msg)
def _destroy_catalog(self, conn, ignored_cur, catalog): """Destroys a catalog. Do not call this method directly. NOTE: This code looks b0rken; notice reference to _dbc which is never defined """ cur = None try: conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) # first, attempt to disconnect clients cur = conn.cursor() cur.execute(""" SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = %(dbname)s AND pid <> pg_backend_pid() ;""" % dict(dbname=sql_literal(catalog.descriptor[self._KEY_DBNAME]))) #TODO: note that a client could reconnect ...now... and prevent the drop # then, drop database cur.execute("DROP DATABASE " + sql_identifier(catalog.descriptor[self._KEY_DBNAME])) cur.close() except psycopg2.Error, ev: msg = str(ev) idx = msg.find("\n") # DETAIL starts after the first line feed if idx > -1: msg = msg[0:idx] raise RuntimeError(msg)
def set_meta(self, cur, key, value): """Sets a metadata (key, value) pair. """ cur.execute(""" DELETE FROM %(schema)s.%(table)s WHERE key=%(key)s ; INSERT INTO %(schema)s.%(table)s (key, value) VALUES (%(key)s, %(value)s) ; """ % dict(schema=self._SCHEMA_NAME, table=self._TABLE_NAME, key=sql_literal(key), value=sql_literal(value)))
def remove_meta(self, cur, key, value=None): """Removes a metadata (key, value) pair or all pairs that match on the key alone. """ where = "WHERE key = %s" % sql_literal(key) if value: where += " AND value = %s" % sql_literal(value) cur.execute(""" DELETE FROM %(schema)s.%(table)s %(where)s ; """ % dict(schema=self._SCHEMA_NAME, table=self._TABLE_NAME, where=where ) )
def set_meta(self, cur, key, value): """Sets a metadata (key, value) pair. """ cur.execute(""" DELETE FROM %(schema)s.%(table)s WHERE key=%(key)s ; INSERT INTO %(schema)s.%(table)s (key, value) VALUES (%(key)s, %(value)s) ; """ % dict(schema=self._SCHEMA_NAME, table=self._TABLE_NAME, key=sql_literal(key), value=sql_literal(value) ) )
def get_meta(self, cur, key=None, value=None): """Gets metadata fields, optionally filtered by attribute key or by key and value pair, to test existence of specific pair. """ where = '' if key: where = "WHERE key = %s" % sql_literal(key) if value: if hasattr(value, '__iter__'): where += " AND value IN (%s)" % (','.join( [sql_literal(v) for v in value])) else: where += " AND value = %s" % sql_literal(value) cur.execute(""" SELECT * FROM %(schema)s.%(table)s %(where)s ;""" % dict(schema=self._SCHEMA_NAME, table=self._TABLE_NAME, where=where)) for k, v in cur: yield dict(k=k, v=v)
def get_meta(self, cur, key=None, value=None): """Gets metadata fields, optionally filtered by attribute key or by key and value pair, to test existence of specific pair. """ where = '' if key: where = "WHERE key = %s" % sql_literal(key) if value: if hasattr(value, '__iter__'): where += " AND value IN (%s)" % ( ','.join([sql_literal(v) for v in value])) else: where += " AND value = %s" % sql_literal(value) cur.execute(""" SELECT * FROM %(schema)s.%(table)s %(where)s ;""" % dict(schema=self._SCHEMA_NAME, table=self._TABLE_NAME, where=where) ) for k, v in cur: yield dict(k=k, v=v)