class UUID(TypeDecorator): """ A memory-efficient MySQL UUID-type. """ impl = VARBINARY(16) def result_processor(self, dialect, coltype): return lambda value: self.process_result_value(value, dialect) def bind_processor(self, dialect): """ Simple override to avoid pre-processing before process_bind_param. This is for when the Python type can't be easily coerced into the `impl` type.""" return lambda value: self.process_bind_param(value, dialect) def process_bind_param(self, value, dialect): """ Emit the param in hex notation. """ if isinstance(value, str) and value: value = uuid.UUID(value) return value.bytes if value else value def process_result_value(self, value, dialect): if value: assert len(value) == 16, "Expected 16 bytes, got %d" % len(value) return uuid.UUID(bytes=value) else: return None
class UUID(UnboundTypeDecorator): """ A memory-efficient MySQL UUID-type. """ impl = VARBINARY(16) def process_bind_param(self, value, dialect): """ Emit the param in hex notation. """ value = uuid.UUID(str(value)) return value if value is None else value.bytes def process_result_value(self, value, dialect): if value is None: return value else: assert len(value) == 16, "Expected 16 bytes, got %d" % len(value) return uuid.UUID(bytes=value)
def test_sqla_utf8_chain(): """Test insert and select of a UTF-8 string.""" from sqlalchemy import Table, Column from sqlalchemy.types import CHAR, VARBINARY from sqlalchemy.ext.declarative import declarative_base from invenio.ext.sqlalchemy import db print("Test UTF-8 support... \t", end="") tmptable = "test__invenio__utf8" beta_in_utf8 = "β" # Greek beta in UTF-8 is 0xCEB2 Base = declarative_base() table = Table(tmptable, Base.metadata, Column('x', CHAR(1)), Column('y', VARBINARY(2))) if table.exists(bind=db.engine): table.drop(bind=db.engine) table.create(bind=db.engine) db.engine.execute(table.insert(), x=beta_in_utf8, y=beta_in_utf8) result = db.engine.execute(table.select().where( table.c.x == beta_in_utf8).where(table.c.y == beta_in_utf8)) assert result.rowcount == 1 for row in result: # Database is configured to always return Unicode. assert row['x'] == beta_in_utf8.decode('utf-8') assert row['y'] == beta_in_utf8 table.drop(bind=db.engine) print(" [OK]")
def function_handler(params, within_column_clause): return cast(cast(params[0], VARBINARY('max')), Geometry)
def load_dialect_impl(self, dialect): if dialect.name == 'default': impl = sqlite.NUMERIC(self.length) else: impl = VARBINARY(self.length) return dialect.type_descriptor(impl)
class User(Base): __tablename__ = "users" username = Column(VARCHAR(32), primary_key=True) email = Column(VARCHAR(64), unique=True, nullable=False) password = Column(VARBINARY(32), nullable=False)
def load_dialect_impl(self, dialect): if dialect.name == "postgresql": return dialect.type_descriptor(POSTGRES_UUID(as_uuid=True)) return dialect.type_descriptor(VARBINARY(16))