Skip to content
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

Cannot insert boolean using oracle #877

Closed
DVoavi-Arison opened this issue Nov 10, 2017 · 6 comments
Closed

Cannot insert boolean using oracle #877

DVoavi-Arison opened this issue Nov 10, 2017 · 6 comments

Comments

@DVoavi-Arison
Copy link

DVoavi-Arison commented Nov 10, 2017

sqlQuery variable

insert into SomeTable (
  SomeBoolValue // number(1, 0)
) values (
  :SomeBoolValue
);

Dapper call

Class SomeTable {
  bool SomeBoolValue
}
...
var values = new SomeTable{SomeBoolValue = false};
this.conn.ExecuteAsync(sqlQuery, values );

That is causing an exception.

I tried to create a BoolTypeHandler but it was not even called by dapper

         {
               ...
               SqlMapper.AddTypeHandlerImpl(typeof(bool), new BoolTypeHandler(), true);
              ...
         }

        public class BoolTypeHandler : SqlMapper.TypeHandler<bool>
        {
            public override void SetValue(IDbDataParameter parameter, bool value)
            {
                parameter.Value = value ? (byte)1 : (byte)0;
            }

            public override bool Parse(object value)
            {
                return bool.Parse((string)value); // This is just an example
            }
        }
@ghost
Copy link

ghost commented Nov 14, 2017

@dvoaviarisonld I've had this exact same issue. This is the same root cause as #458 but there isn't a released version that includes an equivalent change yet.

For now you will need to build it yourself from source. I would recommend checking out the code for the version you have installed and applying one of the TypeHandler fixes yourself.

@rhubley
Copy link

rhubley commented Nov 15, 2017

Have you tried casting it to an Int? For enums I wish to store as String instead of int I will cast them as string in the values collection, I don't see why that wouldn't work here.

Class SomeTable {
  bool SomeBoolValue
}
...
var values = new SomeTable{SomeBoolValue = false};
this.conn.ExecuteAsync(sqlQuery, new { SomeBoolValue = (int)values.SomeBoolValue});

@ben-at-sparq
Copy link

ben-at-sparq commented May 31, 2018

Only solution I've found to this problem is creating a function in the database;

create or replace function bool_to_num(value in boolean) return number
is
begin

    if (value is null) then
        return null;
    elsif (value) then
        return 1;
    else
        return 0;
    end if;

end;
/

You can then continue to used named parameters in your SQL;

insert into table (column) values (bool_to_num(:value))

@daconglee
Copy link

I use dapper 1.60,oracle 11g

        SqlMapper.RemoveTypeMap(typeof(bool));
        SqlMapper.AddTypeHandler(typeof(bool), new BoolTypeHandler());

TypeHandlerTests.cs

@ben-at-sparq
Copy link

I use dapper 1.60,oracle 11g

        SqlMapper.RemoveTypeMap(typeof(bool));
        SqlMapper.AddTypeHandler(typeof(bool), new BoolTypeHandler());

TypeHandlerTests.cs

Thanks for that, worked a treat!

@NickCraver
Copy link
Member

For anyone hitting this today, check out Dapper.Oracle which has boolean type handlers built-in to save a bit of time: https://github.com/DIPSAS/Dapper.Oracle/

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