Skip to content

NH-3426 - Wrong result when converting Guid to string #1151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nhibernate-bot opened this issue Oct 12, 2017 · 3 comments
Closed

NH-3426 - Wrong result when converting Guid to string #1151

nhibernate-bot opened this issue Oct 12, 2017 · 3 comments

Comments

@nhibernate-bot
Copy link
Collaborator

nhibernate-bot commented Oct 12, 2017

Oskar Berggren created an issue — 18th March 2013, 9:06:30:

Having a sqlite database where a column contains guids stored as blobs.

In the select clause of a linq query, ToString() is applied on the Guid property of the model object. NHibernate will forward this to the SQL as

     select
        cast(Id as char) as col_2_0_ 

Unfortunately, the resulting strings won't contain the intended standard string representations of GUID, but instead the raw bytes directly interpreted as characters, which is something completely different.

@lillo42
Copy link
Contributor

lillo42 commented Sep 23, 2018

Hi,

I found how to fix this bug, We need change query to:

select entity0_.Id as col_0_0_ from Entity entity0_

I'm thinking to create a new function "strguid":

RegisterFunction("strguid", new SQLFunctionTemplate(NHibernateUtil.String, "?1"));

What do you think? Is it a good solution?

@fredericDelaporte
Copy link
Member

I do not currently understand what is exactly the trouble, so even less what could be a good solution.
I think this issue first need a test case demonstrating the trouble.

@hazzik hazzik added p: Lowest and removed p: Minor labels Sep 24, 2018
@hazzik hazzik changed the title NH-3426 - Wrong result on sqlite when converting Guid to string. NH-3426 - Wrong result when converting Guid to string Sep 24, 2018
fredericDelaporte pushed a commit that referenced this issue Nov 14, 2018
Fixes #1151 

Co-authored-by: Alexander Zaytsev <[email protected]>
@fredericDelaporte fredericDelaporte added this to the 5.2 milestone Nov 14, 2018
@oskarb
Copy link
Member

oskarb commented Apr 9, 2019

Actually, this SQL:

select
        cast(Id as char) as col_2_0_ 

worked fine assuming that BinaryGuid=False was given in the connection string.

The new SQL from NH 5.2:

    select
        substr(hex(entity0_.Id),
        7,
        2) || substr(hex(entity0_.Id),
        5,
        2) || substr(hex(entity0_.Id),
        3,
        2) || substr(hex(entity0_.Id),
        1,
        2) || '-' || substr(hex(entity0_.Id),
        11,
        2) || substr(hex(entity0_.Id),
        9,
        2) || '-' || substr(hex(entity0_.Id),
        15,
        2) || substr(hex(entity0_.Id),
        13,
        2) || '-' || substr(hex(entity0_.Id),
        17,
        4) || '-' || substr(hex(entity0_.Id),
        21)  as col_0_0_ 
    from
        Entity entity0_

while keeping BinaryGuid=False in the connection string, the new SQL generates something miserable:
Expected: equivalent to < "2a3208ff-4587-4582-91f6-aa2a00b67c3e", "f1c32b3f-1de1-4620-8055-aa2a00b67c38" >
But was: < "32336132-3830-6666-2D34-3538372D343538322D393166362D616132613030623637633365", "33633166-6232-6633-2D31-6465312D343632302D383035352D616132613030623637633338" >

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants