Skip to content

Commit

Permalink
Make reader loop check for DB null values
Browse files Browse the repository at this point in the history
git-svn-id: https://nemerle.googlecode.com/svn/nemerle/trunk@6774 c8a6711f-211a-0410-a8d5-2f220496d6d1
  • Loading branch information
nazgul committed Oct 22, 2006
1 parent 4fc396f commit 2bf1b1a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
30 changes: 22 additions & 8 deletions macros/Data.n
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,10 @@ namespace Nemerle.Data
foreach (myRow :> DataRow in table.Rows){
def col_type = myRow["DataType"].ToString ();
def col_name = myRow["ColumnName"].ToString ();
def type_suff =
if (col_type.StartsWith ("System."))
col_type.Substring (7)
else col_type;

def fetchexpr = Helper.GenerateFetchExpr (Nemerle.Macros.ImplicitCTX ().Manager, col_type, col_num);

// create runtime variables definition according to extracted types
bodyseq = <[ def $(col_name : usesite) =
reader.$("Get" + type_suff : usesite) ($(col_num : int)) ]>
:: bodyseq;
bodyseq = <[ def $(col_name : usesite) = $fetchexpr ]> :: bodyseq;
++col_num;
};

Expand Down Expand Up @@ -367,5 +362,24 @@ namespace Nemerle.Data
});
(fquery.ToString (), tpars, pars_init)
}

public GenerateFetchExpr (mng : ManagerClass, typeName : string, colIdx : int) : Parsetree.PExpr {
def type_suff =
if (typeName.StartsWith ("System."))
typeName.Substring (7)
else typeName;

def retTy =
match (mng.NameTree.LookupExactType (typeName)) {
| Some (t) =>
def rawTy = <[ $(Util.ExprOfQid (typeName)) ]>;
if (t.GetMemType ().CanBeNull) rawTy
else <[ $rawTy ? ]>
| None => Message.FatalError ("DB provider returned unknown type name: " + typeName);
}

<[ if (reader.IsDBNull ($(colIdx : int))) null : $retTy
else reader.$("Get" + type_suff : usesite) ($(colIdx : int)) : $retTy ]>
}
}
}
2 changes: 1 addition & 1 deletion snippets/sql.n
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public class Test
ExecuteReaderLoop (
"SELECT a AS number, b, COUNT(*) AS amount FROM intstr "
"WHERE a = $tt or b = $ty GROUP BY a, b", dbcon, {
Nemerle.IO.printf ("%d %s\n", number, b);
Nemerle.IO.printf ("%d %s\n", number.Value, b);
Console.WriteLine (amount)
});

Expand Down
4 changes: 2 additions & 2 deletions snippets/sql1.n
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public class Test
/// (by connecting to database)
ExecuteReaderLoop ("SELECT * FROM Ludzie WHERE imie = $myparm", dbcon,
{
Nemerle.IO.printf ("Name: %s %s %d\n", imie, nazwisko, wiek)
Nemerle.IO.printf ("Name: %s %s %d\n", imie, nazwisko, wiek.Value)
});

//_ = ExecuteNonQuery ("INSERT INTO intstr VALUES (5, 'beber')", dbcon);
Expand All @@ -179,7 +179,7 @@ public class Test
ExecuteReaderLoop (
"SELECT a AS number, b, COUNT(*) AS amount FROM intstr "
"WHERE a = $tt or b = $ty GROUP BY a, b", dbcon, {
Nemerle.IO.printf ("%d %s\n", number, b);
Nemerle.IO.printf ("%d %s\n", number.Value, b);
Console.WriteLine (amount)
});

Expand Down

0 comments on commit 2bf1b1a

Please sign in to comment.