You are right. I corrected as you originally suggested.
On Tuesday, 5 June 2012 15:54:17 UTC-5, Alexander Shashkevych wrote:
>
> Thank you for updating the code. Since manual did not specified that
> quotes should be a part of encoder, so I was bit confused with unexpected
> behavior. =)
>
> One remark about your updated code: as I understood from web2py
> documentation, "native type" is a type that natively supported by database?
> If yes, then i'm not sure that some sql dialect has native type "string".
> Shall we rely in such case on "fieldtype.native" which is varying from db
> to db, instead of using "fieldtype.type", which is definitely stated as
> "one of the standard web2py types"?
>
> Or I missed something with native types?
>
> Regards,
> Alexander
>
> вторник, 5 июня 2012 г., 18:58:37 UTC+3 пользователь Massimo Di Pierro
> написал:
>>
>> Good catch. This is a minor change of backward compatibility (the '%' was
>> supposed to be in encoder) but we agreed this is an experimental feature
>> and you are suggesting a good improvement compatible with the example in
>> the manual.
>>
>> I implemented it in trunk with some minor changes:
>>
>> if isinstance(fieldtype, SQLCustomType):
>> value = fieldtype.encoder(obj)
>> if fieldtype.native in ('string','text'):
>> return self.adapt(value)
>> return value
>>
>> On Tuesday, 5 June 2012 01:23:58 UTC-5, Alexander Shashkevych wrote:
>>>
>>> I tried another field name, but result the same. I think dal has
>>> incomplete handling of custom types. I analyzed dal code and found that
>>> constructed query for my example was:
>>>
>>> INSERT INTO posts(content,html,title) VALUES
>>> (\xd0\x94\xd0\xb2\xd0\xb0,\xd0\xa2\xd1\x80\xd0\xb8,\xd0\x9e\xd0\xb4\xd0\xb8\xd0\xbd);
>>>
>>> As you can see values are not enclosed by quotes. This is incorrect as
>>> for me. I rewrote my custom type:
>>>
>>> utext = SQLCustomType(
>>> type="text",
>>> native="text",
>>> encoder = lambda x: "'%s'" % x.replace("'", "''").encode("utf-8"),
>>> decoder = lambda x: x.decode("utf-8")
>>> )
>>>
>>> And it works after such update, but example from the web2py book
>>> wouldn't work also:
>>>
>>> compressed = SQLCustomType(
>>> type ='text',
>>> native='text',
>>> encoder =(lambda x: zlib.compress(x or '')),
>>> decoder = (lambda x: zlib.decompress(x))
>>> )
>>>
>>> Further analysis shown that DAL doesn't check SQLCustomType.type while
>>> constructing query and simply calls SQLCustomType.encoder(). But it should
>>> check the type of SQLCustomType and enclose it with quotes in case of
>>> string/text type. I updated BaseAdapter.represent() with following code:
>>>
>>> ...
>>> if isinstance(fieldtype, SQLCustomType):
>>> if fieldtype.type == "string" or fieldtype.type == "text": # added
>>> return self.adapt(fieldtype.encoder(obj)) # added
>>> return fieldtype.encoder(obj)
>>> ...
>>>
>>> Now my custom type doesn't require code that adds quotes - now this does
>>> DAL. =)
>>>
>>> Could you comment how my fix fits DAL design?
>>>
>>
> вторник, 5 июня 2012 г., 18:58:37 UTC+3 пользователь Massimo Di Pierro
> написал:
>>
>> Good catch. This is a minor change of backward compatibility (the '%' was
>> supposed to be in encoder) but we agreed this is an experimental feature
>> and you are suggesting a good improvement compatible with the example in
>> the manual.
>>
>> I implemented it in trunk with some minor changes:
>>
>> if isinstance(fieldtype, SQLCustomType):
>> value = fieldtype.encoder(obj)
>> if fieldtype.native in ('string','text'):
>> return self.adapt(value)
>> return value
>>
>> On Tuesday, 5 June 2012 01:23:58 UTC-5, Alexander Shashkevych wrote:
>>>
>>> I tried another field name, but result the same. I think dal has
>>> incomplete handling of custom types. I analyzed dal code and found that
>>> constructed query for my example was:
>>>
>>> INSERT INTO posts(content,html,title) VALUES
>>> (\xd0\x94\xd0\xb2\xd0\xb0,\xd0\xa2\xd1\x80\xd0\xb8,\xd0\x9e\xd0\xb4\xd0\xb8\xd0\xbd);
>>>
>>> As you can see values are not enclosed by quotes. This is incorrect as
>>> for me. I rewrote my custom type:
>>>
>>> utext = SQLCustomType(
>>> type="text",
>>> native="text",
>>> encoder = lambda x: "'%s'" % x.replace("'", "''").encode("utf-8"),
>>> decoder = lambda x: x.decode("utf-8")
>>> )
>>>
>>> And it works after such update, but example from the web2py book
>>> wouldn't work also:
>>>
>>> compressed = SQLCustomType(
>>> type ='text',
>>> native='text',
>>> encoder =(lambda x: zlib.compress(x or '')),
>>> decoder = (lambda x: zlib.decompress(x))
>>> )
>>>
>>> Further analysis shown that DAL doesn't check SQLCustomType.type while
>>> constructing query and simply calls SQLCustomType.encoder(). But it should
>>> check the type of SQLCustomType and enclose it with quotes in case of
>>> string/text type. I updated BaseAdapter.represent() with following code:
>>>
>>> ...
>>> if isinstance(fieldtype, SQLCustomType):
>>> if fieldtype.type == "string" or fieldtype.type == "text": # added
>>> return self.adapt(fieldtype.encoder(obj)) # added
>>> return fieldtype.encoder(obj)
>>> ...
>>>
>>> Now my custom type doesn't require code that adds quotes - now this does
>>> DAL. =)
>>>
>>> Could you comment how my fix fits DAL design?
>>>
>>