Hands-on CFQL


If you are familiar with CodeFluent Entities it is possible that you have already used the CFQL. The acronym CFQL stands for CodeFluent Query Language. In a nutshell, the CodeFluent Query Language is a specific language that specifically eases stored procedures creation, custom methods and rule definitions.

As you know, according to the database that you use you will have to write either PL-SQL or T-SQL depending if you are using a SQL Server or an Oracle database. CodeFluent Query Language is platform independent and is therefore used to declare platform independent queries which will then be translated by producers in platform specific code.

Therefore, we are going to take a look at it, to see how CFQL works.

To illustrate this article, we will use the “SampleAdvertising” model provided by CodeFluent Entities. We will also create a new “ClassLibrary” project which will contain the business code generated by CodeFluent Entities. We will also add a new “Console Application” project to our solution to test the custom methods we are about to create.

1

First we will add our producers, in this case we will need two of these, the Business Object Model producers which allows us to generate our business code, and the SQL producer which will generate our database, tables, stored procedures, views…

For the Business Object Model producer, we will set its targeted project to our class library project.

2

Regarding the SQL Server producer, we will create a new folder named “SQL Scripts” in our class library project which will contain all the files generated by this producer.

3

Now that we have added our producers, we can start creating our custom methods using the CodeFluent Query Language. We will start by creating all our methods, then we will use some of them in our console application.

For instance, we will create custom methods for our “Customer” entity. To do so, in the modeler, click on the “Customer” entity and hit “Add method” from the ribbon.

4

First, we will create a custom “Load” method, “LoadByCountry”, which will enable us to load our customers depending on their country. Therefore, add the following code to create our custom method:

5

As you can see, the syntax of the CodeFluent Query Language is really close from the SQL language.

We will now create a “LoadOne” method named “LoadOneByName” which will return a single line of data, in this case a customer based on its name. Since the generated method manipulates a single line of data, in the generated Business Object Model, the method is generated in the entity class and not the collection class as for classic load methods.

6

Then we will create a “Load Raw” method named “LoadTodaysCustomers”. Raw methods are methods in which a developer can code whatever he wants: platform specific code (e.g. CSharp, SQL, etc.), CodeFluent Query Language (CFQL), or a mix of both. In this example, we will create a method which will return today new customers. As you can see in the screenshot below, creating a raw method allows to target specific platform and language, in our case it will be T-SQL.

7

Now, we will create a “LoadOne Raw” method named “LoadLastFiveMinCreatedCustomer” which will return a single row of a customer created within the last five minutes.

8

Once this method created, we will create a custom search method to find our customers based on their city. We will name it “SearchByCity”.

9

Then we will create a new method to count our customers based on their city according to the screenshot below, name it “CountByCity”.

10

Finally, we will create a custom method named “DeleteByCountry” to delete our customers based on their country.

11

Now that all the methods we wanted have been created, let’s build our solution to generate our database and our business code (right click on our CodeFluent Entity project > build).

Once the build is complete and successful, open the file “Program.cs” of our console application project and add the following code to get all our customers from the database.

foreach (Customer cust in CustomerCollection.LoadAll())
{
    Console.WriteLine(cust.Name + " " + cust.Country + " " + cust.City);
}

Then, we will start by testing our “LoadByCountry” custom method

CustomerCollection custByCountry = null;
Console.Write("LoadByCountry, enter a country: ");
string countryInput = Console.ReadLine();
if (countryInput != null && countryInput.Trim() != String.Empty)
{
    custByCountry = CustomerCollection.LoadByCountry(countryInput);
}
if (custByCountry != null)
{
    foreach (Customer cust in custByCountry)
    {
        Console.WriteLine(cust.Name + " " + cust.Country + " " + cust.City);
    }
}

As you can see on the screenshot below, our custom method returns our customers based on their country.

12

Now, we will test our “LoadOneByName” custom method. Insert the following code into the “Program.cs” file.

Console.WriteLine("\r\n");
Console.Write("LoadOneByName, enter a name: ");
string nameInput = Console.ReadLine();
Customer singleCustByName = null;
if (nameInput != null && nameInput.Trim() != String.Empty)
{
    singleCustByName = Customer.LoadOneByName(nameInput);
}
if (singleCustByName != null)
{
    Console.WriteLine(singleCustByName.Name + " " + singleCustByName.Country + " " + singleCustByName.City);
}

You can see the result in the screenshot below.

13

We can take a look to our raw method “LoadTodaysCustomers”. In order to test this method add the following code to the “Program.cs” file.

CustomerCollection custFromToday = null;
Console.WriteLine("\r\n");
Console.WriteLine("LoadTodaysCustomers:");
custFromToday = CustomerCollection.LoadTodaysCustomers();
if (custFromToday != null)
{
    foreach (var cust in custFromToday)
    {
        Console.WriteLine(cust.Name + " " + cust.Country + " " + cust.City);
    }
}

As you can see in the following screenshot, our method returns all our customers since they were all generated today.

14

Then, we can test our “LoadLastFiveMinCreatedCustomer” method, to do so, insert the following code into the “Program.cs” file.

Console.WriteLine("\r\n");
Console.WriteLine("LoadLastFiveMinCreatedCustomer:");
Customer lastModifiedCust = Customer.LoadLastFiveMinCreatedCustomer();
if (lastModifiedCust != null)
{
    Console.WriteLine(lastModifiedCust.Name + " " + lastModifiedCust.Country + " " + lastModifiedCust.City);
}

We can verify the result thanks to the two following screenshots.

15

16

We will now test our “SearchByCity” method, to do so, add the following code to the “Program.cs” file.

Console.Write("SearchByCity, enter a city: ");
string cityInput = Console.ReadLine();
CustomerCollection searchByCity = CustomerCollection.SearchByCity(cityInput);
foreach (Customer cust in searchByCity)
{
    Console.WriteLine(cust.Name + " " + cust.Country + " " + cust.City);
}

And this is what you should get.

17

Then we will test our “CountByCity” method which allow us to count our customers based on their city.

Console.Write("CountByCity, enter a city: ");
string cityCountInput = Console.ReadLine();
Console.WriteLine("Number of customer in " + cityCountInput + ": " + CustomerCollection.CountByCity(cityCountInput));

You can see the result in the following screenshot.

18

Finally, we can test our “DeleteByCountry” custom method and here is the code to test it.

Console.Write("DeleteByCountry, enter a country: ");
string deleteCountryInput = Console.ReadLine();
Console.WriteLine("You are about to delete all customers from " + deleteCountryInput + ". Are you sure? (y/n)");
string deleteConfirm = Console.ReadLine();
if(deleteConfirm.ToLower() == "y")
{
    CustomerCollection.DeleteByCountry(deleteCountryInput);
}

You can call again the “CustomerCollection.LoadAll” method to verify that customers related to the country you have specified in the previous method.

19

Cheers,

Aymeric ROLAND.

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s