Ngugi1 / sqlite-net

Automatically exported from code.google.com/p/sqlite-net
0 stars 0 forks source link

Support nullable types #19

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I had trouble getting int? to work in class. SqlType barfs. I think you need to 
unpack the inner type from a Nullable<> generic, the following seems to work:

  var clrType = p.ColumnType;
  if (clrType.IsGenericType && clrType.GetGenericTypeDefinition() == typeof(Nullable<>))
      clrType = clrType.GetGenericArguments()[0];
  if (clrType == typeof(Boolean) || ...

Original issue reported on code.google.com by olau%iol...@gtempaccount.com on 8 Jul 2010 at 6:58

GoogleCodeExporter commented 9 years ago
You also need to modify ReadCol (bug found by Anders Rune Jensen), adding the 
same two lines to unpack the generic nullable (the actual "null" case is 
handled in the if just before):

if (type == SQLite3.ColType.Null) {
    return null;
} else {
    if (clrType.IsGenericType && clrType.GetGenericTypeDefinition() == typeof(Nullable<>))
        clrType = clrType.GetGenericArguments()[0];

    if (clrType == typeof(String)) {
        ...

Original comment by olau%iol...@gtempaccount.com on 9 Jul 2010 at 12:04

GoogleCodeExporter commented 9 years ago
The latter comment is wrong - I didn't understand the logic completely. 
Actually, the "type" we get here is only computed once for all rows in a 
multiple-row result (up in ExecuteQuery), hence if the type is nullable, we 
can't rely on it since it might be SQLite3.ColType.Null in the first row and 
Integer in the second.

What we need instead of my latter comment is either a revamp of this logic, or 
the following little piece of code that unpacks the generic type immediately 
and resets the SQLite3 column type:

object ReadCol (IntPtr stmt, int index, SQLite3.ColType type, Type clrType)
{
  if (clrType.IsGenericType && clrType.GetGenericTypeDefinition() == typeof(Nullable<>))
  {
    type = SQLite3.ColumnType(stmt, index); // don't trust the predefined type
    clrType = clrType.GetGenericArguments()[0];
  }

  if (type == SQLite3.ColType.Null)
  {
    return null;
  } else {
    if (clrType == typeof(String)) {

I'm surprised sqlite-net doesn't support nullable types out of the box, they're 
handy for foreign keys. Maybe you're just using 0 instead of null?

Original comment by olau%iol...@gtempaccount.com on 10 Aug 2010 at 1:21

GoogleCodeExporter commented 9 years ago
Actually, as it turns out, my previous comment was fine, it's the logic that's 
broken. It won't work for string columns with null values either. So comment 1 
still applies. I'll open a separate issue.

Original comment by olau%iol...@gtempaccount.com on 13 Aug 2010 at 5:06

GoogleCodeExporter commented 9 years ago
Ah, I can see you fixed it now in SVN. So no need to open another issue. :)

Original comment by olau%iol...@gtempaccount.com on 13 Aug 2010 at 5:14

GoogleCodeExporter commented 9 years ago

Original comment by frank.al...@gmail.com on 14 Nov 2010 at 12:31

GoogleCodeExporter commented 9 years ago
Sorry yes I fixed this a while back :-)

Original comment by elite.da...@gmail.com on 19 Nov 2011 at 4:35