Skip to main content

Calling a custom database function in WHERE part of your entity

I have encountered a need to call a custom database function in WHERE part of the entity using entityspaces(2008.x.x) which does not support Raw SQL Injection Everywhere.

I have searched everywhere and posted inquiries in the friendly ES forum but theres no luck.

This task can be done easily using stored procedure but I wanted to use ES for I have other filters, join and grouping, all of this are already in place except for the part where I need to call the function.

Now LastQuery to the rescue, this property contains nothing until you make the query, so the idea is to call the query making sure that only one or zero record will be return then use the value of LastQuery to construct a new SQL Query/Statement with the datase function in it to be use in our entity custom load. I am not sure if this is a good practice but I works for me. See the code below.


' Make sure we only have one record for this load
Dim esqUsers As New UserCollectionQuery("uv")
esqUsers.es.Top = 1
esqUsers.es.PageSize = 10
esqUsers.es.PageNumber = _PageNumber

' this part will give us an entry point for insertion of call to our custom database function it will generate this string "uv.[UserID] IN ('-2')"
esqUsers.Where(esqUsers.UserID.OP(esWhereOperand.In, "-2".Split("!"c)))

' more filters, join and grouping here.

' Make the call so we can generate the LastQuery
esUsers = New UserCollection
esUsers.Load(esqUsers)

' Get last query
Dim strLastQuery As String = esqUsers.es.LastQuery
' Insert the call to the function
strLastQuery = strLastQuery.Replace("uv.[UserID] IN ('-2')", "database function & params")

' Make sure we have the data
strLastQuery = strLastQuery.Replace("TOP 1", "")

' You also need to update some parameters if needed.

' Now we call our custom load
esUsers.CustomLoad(strLastQuery)


The custom load function is very simple, see below:

Public Function CustomLoad(ByVal sSQL As String) As UserCollection
MyBase.Load(esQueryType.Text, sSQL)
Return Me
End Function


HTH




Comments

Popular posts from this blog

Simple Color Picker - a jQuery color picker control

It's been a while since I build this small plugin for jQuery, While browsing through my files I try to see if this plugin still works with the latest version of jQuery which is 1.3.2 and it did. I have updated the sample and change the file names so it is easy to recognize. I also move the downloads to my google site, Download here . Focus on the input box below or click on the box next to the control to show color picker. Basic Sample jQuery(function($) { $("#vtrColorPicker").attachColorPicker(); }); Change Background jQuery(function($) { $("#vtrColorPicker1").attachColorPicker(); $("#vtrColorPicker1").change(function() { $("#content-wrapper").css("background-color",$("#vtrColorPicker1").getValue()); });

dnnAlert and dnnConfirm jQuery Plugin

I like the idea of using jQuery Plugins in DNN for interactive and consistent user interface which is why I digg into using dnnAlert and dnnConfirm plugins, however after a quick look on the documentation I notice the following. For dnnAlert you can't set the title. We are allowed to set the OK button text using okText option but not the dialog title? For dnnConfirm. This is very usefull when you just want a confirmation dialog upon click on your button or link however you can't use it like javascript confirm function or at least I don't know how to use it like below: if(confirm('Question?')){//some code}

A javascript library for formatting and manipulating numbers.

Numeral.js , while less complex, was inspired by and heavily borrowed from  Moment.js  so if you have use momentjs before you should be familiar with this library. Numbers can be formatted to look like currency, percentages and more. How to use: numeral(1000).format('0,0') returns 1,000 numeral(1230974).format('0.0a') returns 1.2m numeral(1000.234).format('$0,0.00') returns $1,000.23 numeral(1230974).format('($ 0.00 a)') returns $ 1.23 m numeral(1).format('0%') returns 100% numeral(0.974878234).format('0.000%') returns 97.488% For more sample and options go to  http://numeraljs.com/ .