Hmm, looks like I spoke too soon. Testing against a SQLite database the
hybrid attribute approach works fine but I'm having some trouble with SQL
Server. Basically, given the structure that Michael laid out, the following
query:

    model = TestModel(
        flags=1
    )
    session.add(model)
    session.commit()
    result = session.query(TestModel).filter(TestModel.flag_one ==
True).first()

Resullts in this exception:

ProgrammingError: (ProgrammingError) (102, "Incorrect syntax near
'='.DB-Lib error message 102, severity 15:
General SQL Server error: Check messages from the SQL Server
") 'SELECT TOP 1 testmodel.id AS testmodel_id, testmodel.flags AS
testmodel_flags
FROM testmodel
WHERE ((testmodel.flags & %(flags_1)s) > %(param_1)s) = 1' {'flags_1': 1,
'param_1': 0}

So it looks like the equality comparison is coercing True to 1, I can't
figure out which hook I need to use to change this. I've tried to use
coerce_compared_value with no effect.

Alex


On Mon, Mar 31, 2014 at 8:49 PM, Alex <quixop...@googlemail.com> wrote:

> The hybrid property and custom comparator approach works like a charm, I
> had an inkling that hybrid propertyies were the correct approach but hadn't
> really thought about subclassing hybrid_property, this has helped me
> understand the custom types architecture a lot, thank very much.
>
> Alex
>
>
> On Mon, Mar 31, 2014 at 4:13 PM, Jonathan Vanasco 
> <jonat...@findmeon.com>wrote:
>
>> I'm interested in what you find. I know TypeDecorator is the right
>> solution, I was looking at that for this exact same situation a few weeks
>> ago (
>> https://groups.google.com/forum/#!searchin/sqlalchemy/vanasco%7Csort:date/sqlalchemy/sQtOYxSUiqI/5ns2vWMFaGAJ)
>>
>> I have a similar situation.  I wrote a generic bitwise wrapper class that
>> I was ashamed of, but I guess I should eventually release it.  i can toss
>> it on github under the MIT if you'd like.
>>
>> The way I have my bitwise stuff working is this:
>>
>> * I create an class that inherits from my `BitwiseSet` class.  That
>> subclass stores a mapping of the bitwise values, the parent class has
>> functions for common bitwise operations ( add, remove, has_any, has_all --
>> both by string and integer ).   The parent class has an 'encode' and
>> 'decode' function, which returns an int or list of elements (as int or
>> string ).
>>
>>     class BitwiseClassFieldA(BitwiseSet):
>>         set_ = {
>>                'flag_a' << 1,
>>                'flag_b' << 2,
>>                'flag_c' << 3,
>>         }
>>
>> * I have a property on each of my sqlalchemy objects that works something
>> like this...
>>
>>     class Foo(SqlAlchemyObject):
>>          bitwise_field_a = sa.Column( sa.Integer, default=0 )
>>
>>          @property
>>          def bitwise_manager_field_a(self):
>>                 if self._bitwise_manager_field_a is None:
>>                       self. bitwise_manager_field_a =
>> BitwiseClassFieldA(self.bitwise_field_a)
>>                 return self.bitwise_manager_field_a
>>          _ bitwise_manager_field_a = None
>>
>> * when i do saves , i call the manager's `encode` function
>>
>>       instance.bitwise_field_a = instance.bitwise_manager_field_a.encode()
>>
>>
>> anyways, when I was trying to get a TypeDecorator working, I was focused
>> on the Integer field having a decorator and managing my object -- not on
>> defining many column attributes like you are.  i think that might be an
>> easier avenue, because you want to affect the column itself for these
>> comparisons.
>>
>> --
>> You received this message because you are subscribed to a topic in the
>> Google Groups "sqlalchemy" group.
>> To unsubscribe from this topic, visit
>> https://groups.google.com/d/topic/sqlalchemy/Mu9m1dVU1Gw/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to
>> sqlalchemy+unsubscr...@googlegroups.com.
>> To post to this group, send email to sqlalchemy@googlegroups.com.
>> Visit this group at http://groups.google.com/group/sqlalchemy.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to