-
-
Save remzmike/31a56e4bb4e5b1363a4fa0fb8a264baa to your computer and use it in GitHub Desktop.
codified where clauses with alchemysharp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Diagnostics; | |
using AlchemySharp; // https://github.com/FogCreek/AlchemySharp | |
namespace WhereClauseTest | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var d = new string[][] { | |
new string[] { "678", "200" }, | |
new string[] { "404", "784" }, | |
new string[] { "678", "399" }, | |
}; | |
var db = new DB(); | |
var table = db["MyTable"]; | |
// a "row" is the deepest node we care about in the where clause | |
// eg. (NPA = @NPA1 and NXX = @NXX1) | |
// eg. (("MyTable"."NPA" = @p0) and ("MyTable"."NXX" = @p1)) | |
var rows = new List<Expr>(); | |
foreach (string[] pair in d) | |
{ | |
// operator overloading alchemy, using the bitwise ops since they are less common than boolean | |
var row = table["NPA"] == pair[0] & table["NXX"] == pair[1]; | |
rows.Add(row); | |
} | |
// all rows are OR'd together (clumsily (for now)) | |
Expr rowsConcat = rows[0]; // todo: later: don't duplicate first row. | |
foreach (var e in rows) | |
{ | |
rowsConcat |= e; | |
} | |
var parameters = new Parameters(); // will be filled by the next line | |
var sql = rowsConcat.ToSQL(parameters); | |
Console.WriteLine(sql); | |
Console.WriteLine("------------------"); | |
Console.WriteLine(parameters.Serialize()); | |
// Now you can exec the sql, with parameters. | |
/* | |
var sqlParams = parameters.ToSqlParameters(); | |
var ds = SqlHelper.ExecuteDataset(connString, CommandType.Text, sql, sqlParams); | |
*/ | |
} | |
} | |
} | |
// OUTPUT>>> | |
/* | |
((((("MyTable"."NPA" = @p0) and ("MyTable"."NXX" = @p1)) or (("MyTable"."NPA" = @p2) and ("MyTable"."NXX" = @p3))) or (("MyTable"."NPA" = @p4) and ("MyTable"."NXX" = @p5))) or (("MyTable"."NPA" = @p6) and ("MyTable"."NXX" = @p7))) | |
------------------ | |
@p0=678,@p1=200,@p2=678,@p3=200,@p4=404,@p5=784,@p6=678,@p7=399 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment