Power Query - The dOOdad way

All your dOOdad needs ...

Power Query - The dOOdad way

Postby mike.griffin on Sun Apr 04, 2004 1:59 am

VB.NET and C# dOOdads are in sync and up-to-date. Both now have TearOff Where parameters and string properties. Below is an example showing how to build a sophisticated query. The \"AND\" conjuction is the default conjuction between parameters, however you can say Query.Load(\"OR\") and override it, same holds true for GenerateSQL(\"OR\"). You can set the conjuction on individual parameters as well, and group clauses with parenthesis. The query below uses most of the advanced query techniques, including a TearOff WhereParameter. I most warn you however, the query below really doesn't make much sense, but it does show you how to tackle your query needs.

Several people have asked for a sample query that is more advanced in nature, here is such an example.
Code: Select all
public void ThePowerQuery()
{
   // Before we start, the default conjuction for all dynamic queries is \"AND\",
   // You can override this when you call Load(Where) or GenerateSQL().

   // The query we are going to generate doesn' really make sense, it's the
   // AND's, OR's, and grouping that is important here.

   //=========================================================================
   // SELECT * FROM [Employees]
   // WHERE [LastName] LIKE 'A%' AND [LastName] IS NOT NULL
   //    OR ( [City] LIKE 'Ind%' OR [HireDate] BETWEEN '1/1/95' AND '4/4/04' )
   //=========================================================================


   //=========================================================================
   // SELECT * FROM [Employees]
   //=========================================================================
   // The default is all fields or *, however, this can be overridden by
   // using emps.Query.AddResultColumn()
   Employees emps = new Employees();

   //=========================================================================
   // WHERE
   //=========================================================================
   // As soon as you access a WhereParameter via the traditional way
   // or via a TearOff you've initiated the creation of a WHERE clause


   //=========================================================================
   // [LastName] LIKE 'A%' AND [LastName] IS NOT NULL
   //=========================================================================
   emps.Where.LastName.Value = \"A%\";
   emps.Where.LastName.Operator = WhereParameter.Operand.Like;

   // Here we need to use LastName again, but we already used are intrinsic
   // WhereParameter, let's create a tear off, notice how we access it a
   // little differently, each time you access a TeafOff it is created and
   // add to the WhereParameter collection
   WhereParameter wp = emps.Where.TearOff.LastName;
   wp.Operator = WhereParameter.Operand.IsNotNull;

   //=========================================================================
   // OR (
   //=========================================================================
   emps.Query.AddConjunction(WhereParameter.Conj.Or);
   emps.Query.OpenParenthesis();

   //=========================================================================
   // [City] LIKE 'Ind%'
   //=========================================================================
   emps.Where.City.Conjuction = WhereParameter.Conj.And;
   emps.Where.City.Value = \"Ind%\";
   emps.Where.City.Operator = WhereParameter.Operand.Like;

   //=========================================================================
   // OR [HireDate] BETWEEN '1/1/95' AND '4/4/04'
   //=========================================================================
   // Notice how we override the Query level default conjuction here, we set
   // the HireData to \"Or\", when you override the conjuction the it precedes
   // the WhereParameter it is attached to.
   emps.Where.HireDate.Conjuction = WhereParameter.Conj.Or;
   emps.Where.HireDate.Operator = WhereParameter.Operand.Between;
   emps.Where.HireDate.BetweenBeginValue = \"1/1/95\";
   emps.Where.HireDate.BetweenEndValue = \"4/4/04\";

   //=========================================================================
   // )
   //=========================================================================
   emps.Query.CloseParenthesis();

   // Let's look at the SQL without executing it
   string query = emps.Query.GenerateSQL();

   // We could have executed it this way
   // emps.Query.Load();

   //=========================================================================
   // In reality, your query will look like this as all data is passed in via
   // Paramters
   //=========================================================================
   // SELECT * FROM [Employees]
   // WHERE [LastName] LIKE @LastName1 AND [LastName] IS NOT NULL
   // OR ( [City] LIKE @City3  OR [HireDate] BETWEEN @HireDate4 AND @HireDate5 )
   //=========================================================================
}
Last edited by mike.griffin on Tue Feb 15, 2005 3:04 pm, edited 1 time in total.
User avatar
mike.griffin
Site Admin
 
Posts: 3290
Joined: Sat Apr 03, 2004 6:10 am
Location: Indianapolis, IN

VB Version

Postby sbc on Tue Apr 27, 2004 8:47 am

Here is a VB version. Not tested though but should work:
Code: Select all
Public Sub ThePowerQuery()
   ' Before we start, the default conjuction for all dynamic queries is \"AND\",
   ' You can override this when you call Load(Where) or GenerateSQL().

   ' The query we are going to generate doesn' really make sense, it's the
   ' AND's, OR's, and grouping that is important here.

   '=========================================================================
   ' SELECT * FROM [Employees]
   ' WHERE [LastName] LIKE 'A%' AND [LastName] IS NOT NULL
   '    OR ( [City] LIKE 'Ind%' OR [HireDate] BETWEEN '1/1/95' AND '4/4/04' )
   '=========================================================================


   '=========================================================================
   ' SELECT * FROM [Employees]
   '=========================================================================
   ' The default is all fields or *, however, this can be overridden by
   ' using emps.Query.AddResultColumn()
   Dim emps As Employees = New Employees

   '=========================================================================
   ' WHERE
   '=========================================================================
   ' As soon as you access a WhereParameter via the traditional way
   ' or via a TearOff you've initiated the creation of a WHERE clause


   '=========================================================================
   ' [LastName] LIKE 'A%' AND [LastName] IS NOT NULL
   '=========================================================================
   emps.Where.LastName.Value = \"A%\"
   emps.Where.LastName.Operator = WhereParameter.Operand.Like

   ' Here we need to use LastName again, but we already used are intrinsic
   ' WhereParameter, let's create a tear off, notice how we access it a
   ' little differently, each time you access a TeafOff it is created and
   ' add to the WhereParameter collection
   Dim wp As WhereParameter = emps.Where.TearOff.LastName
   wp.Operator = WhereParameter.Operand.IsNotNull

   '=========================================================================
   ' OR (
   '=========================================================================
   emps.Query.AddConjunction WhereParameter.Conj.Or
   emps.Query.OpenParenthesis()

   '=========================================================================
   ' [City] LIKE 'Ind%'
   '=========================================================================
   emps.Where.City.Conjuction = WhereParameter.Conj.And
   emps.Where.City.Value = \"Ind%\"
   emps.Where.City.Operator = WhereParameter.Operand.Like

   '=========================================================================
   ' OR [HireDate] BETWEEN '1/1/95' AND '4/4/04'
   '=========================================================================
   ' Notice how we override the Query level default conjuction here, we set
   ' the HireData to \"Or\", when you override the conjuction the it precedes
   ' the WhereParameter it is attached to.
   emps.Where.HireDate.Conjuction = WhereParameter.Conj.Or
   emps.Where.HireDate.Operator = WhereParameter.Operand.Between
   emps.Where.HireDate.BetweenBeginValue = \"1/1/95\"
   emps.Where.HireDate.BetweenEndValue = \"4/4/04\"

   '=========================================================================
   ' )
   '=========================================================================
   emps.Query.CloseParenthesis()

   ' Let's look at the SQL without executing it
   Dim query As String = emps.Query.GenerateSQL()

   ' We could have executed it this way
   ' emps.Query.Load()

   '=========================================================================
   ' In reality, your query will look like this as all data is passed in via
   ' Paramters
   '=========================================================================
   ' SELECT * FROM [Employees]
   ' WHERE [LastName] LIKE @LastName1 AND [LastName] IS NOT NULL
   ' OR ( [City] LIKE @City3  OR [HireDate] BETWEEN @HireDate4 AND @HireDate5 )
   '=========================================================================
End Sub
User avatar
sbc
Expert
 
Posts: 446
Joined: Sat Apr 03, 2004 6:10 am
Location: North East Lincolnshire, England

Postby sbc on Mon Oct 11, 2004 11:03 am

Here is a way of generating the correct SQL for when you are not sure how many where parameters there are going to be. | is the deliminator.
Code: Select all
string FilterOut = \"City 1|City 2|City 3\"
string[] filters = FilterOut.Split('|');
emps.Query.AddConjunction(WhereParameter.Conj.And);
emps.Query.OpenParenthesis();
emps.Where.City.Value = \"%\" + filters[0] + \"%\";
emps.Where.City.Operator = WhereParameter.Operand.NotLike;
if (filters.Length > 1) {
   WhereParameter wp = emps.Where.TearOff.City;
   for (int i = 1; i < filters.Length; i++) {
      wp = emps.Where.TearOff.City;
      wp.Value = \"%\" + filters[i] + \"%\";
      wp.Operator = WhereParameter.Operand.NotLike;
      if (i != filters.Length) {
         wp.Conjuction = WhereParameter.Conj.And;
      }
   }
}
emps.Query.CloseParenthesis();

This would generate:
Code: Select all
AND (City NOT LIKE '%City 1%' AND City NOT LIKE '%City 2%' AND City NOT LIKE '%City 3%')
User avatar
sbc
Expert
 
Posts: 446
Joined: Sat Apr 03, 2004 6:10 am
Location: North East Lincolnshire, England

Postby vipinjosea on Tue Dec 27, 2005 1:58 am

nice but what about joining (left,outer ,inner,right) is there is any simple way other than views ? urgent please reply soon ,thanks in advance
vipinjosea
Lurker
 
Posts: 2
Joined: Mon Dec 26, 2005 5:37 am

Postby mike.griffin on Tue Dec 27, 2005 2:15 am

No, dOOdads doesn't do dynamic joins. Views are the best way, why do you want to avoid them, you can build dOOdads off of views. Maybe this will help? You could write your own join and use #6 ?

See http://www.mygenerationsoftware.com/php ... .php?t=894
User avatar
mike.griffin
Site Admin
 
Posts: 3290
Joined: Sat Apr 03, 2004 6:10 am
Location: Indianapolis, IN


Return to dOOdads - MyGeneration's .NET Architecture

Who is online

Users browsing this forum: No registered users and 0 guests