Getting a log of the SQL SubSonic is using - subsonic

Linq2SQL has the great Log property to see what the actual SQL statements that it is generating. Does SubSonic 2.2 have something similar to this?

http://www.e-webdevelopers.com/268/view-the-sql-generated-by-subsonic/
SqlQuery sq = new Select()
.From(Item.Schema)
.InnerJoin(ItemStatus.IstIDColumn, Item.ItmStatusColumn)
.InnerJoin(ItemCategory.ItcItemIDColumn, Item.ItmIDColumn)
.WhereExpression("ItmIsEnabled").IsEqualTo(true)
.AndExpression("ItmName").Like("%" + findThis + "%")
.Or(Item.ItmShortDescriptionColumn).Like("%" + findThis + "%")
.Or(Item.ItmItemCodeColumn).Like("%" + findThis + "%")
.Or(Item.ItmLongDescriptionColumn).Like("%" + findThis + "%")
.Paged(pageIndex, PageSize)
.OrderAsc("itmName");
Response.Write(sq.ToString());
Not tested as I'm not infront of my dev box. Hope that helps.

SubSonic 2.2 ActiveRecord has some events you can override, like AfterValidate() and BeforeCommit(). You could use one of those to log the Sql, but you would have to modify your templates so that code ended up in all your classes.
Or just hit up SubSonic\DataProviders\DataService.cs in your local SubSonic source and see if it will work to add your logging events to all of the .Execute* methods.

It is not possible

Related

Prepared statement for updating a map column in camel-cassandraql is failing

I got this exception - "no viable alternative at input '?'", i feel it is because of "+" query statement.
private static final String CQL_BEAN = "cql:bean:cassandraCluster";
String updataQuery = "UPDATE user_preference SET preference = preference + ? WHERE user_id = ? AND tenant_id = ? IF EXISTS";
.to(CQL_BEAN + "/" + cassandraProperties.getKeyspaceName() + "?cql=" + this.updataQuery + "&prepareStatements=false")
Update: it could be because you are using prepareStatement=false - it looks like that in this case it won't substitute placeholders... Although I'm not expert in this integration.
....
What do you want to achieve with this syntax? Update only records that were inserted previously?
Usually LWTs are used only in very limited number of situations as they require coordination between nodes in the cluster, and seriously degrade performance. More details on LWTs you can find in documentation.

Pagination in share Point 2013 using rest API

I have one requirement to implement pagination in sharePoint 2013 using rest api.
I am currently using below query string for pagination:
var querystring="?$skiptoken=" + encodeURIComponent('Paged=TRUE&p_SortBehavior=0&p_ID=' + (startItemId-1) + '&$top=' + itemsCount);
It is working fine. But one of my client asking they want to display items and pagination based on order by start date.
I have tried with below querystring:
var querystring="?$skiptoken=" + encodeURIComponent('Paged=TRUE&p_SortBehavior=0&p_ID=' + (startItemId-1) + '&$top=' + itemsCount)+ "&$orderby=startdate ";
If I will give like this its not working. If i will give ID column instead of startdate its working fine.
Can any one suggest how to use startdate column in querystring while using skiptoken?
Thanks in advance
Modify your query like below. This should help.
var querystring="?$skiptoken=" + encodeURIComponent('Paged=TRUE&p_SortBehavior=0&p_ID=' + (startItemId-1) + '&$top=' + itemsCount + '&$orderby=startdate')
Thanks,
Danny
Please remember to mark as answer if it helps or vote if useful.

Subsonic:Selfjoin query need

I want to construct the query which is going to be used in .net. Below you can see the sql query, any one can give me the equivalent subsonic query
SELECT DISTINCT
a2.AccountID AS BID,
a2.AccountName AS Brand
FROM
Account a
INNER JOIN Account a2 ON a.ParentID = a2.AccountID
WHERE
a.AccountTypeID = 6
ORDER BY
Brand
Please help me.
SubSonic 2 or 3?
With SubSonic you always have a nice backdoor.
It's called InlineQuery in 2.x and CodingHorror in 3.x
e.g:
var result = DB.Query().ExecuteReader("SELECT DISTINCT
a2.AccountID AS BID,
a2.AccountName AS Brand
FROM Account a
INNER JOIN Account a2 ON a.ParentID = a2.AccountID
WHERE a.AccountTypeID = ?accounttypeid
ORDER BY Brand", 6);
If you want to stay with the fluent interface because of the syntax checking and the sql conversion. Here is another approach I could think of (SubSonic 2.2)
DataTable result = DB.Select(
"a1." + Account.Columns.AccountId + " as BID",
"a2." + Account.Columns.AccountName + " as Brand")
.From(Account.Schema.QualifiedName + " a1")
.InnerJoin(Account.Schema.QualifiedName + " a2",
"a2." + Account.Columns.account_id,
"a1", "a1." + Account.Columns.parent_id)
.Where("a1." + Account.Columns.AccountTypeId).IsEqualTo(6)
.OrderAsc("a2." + Account.Columns.AccountName)
.ExecuteDataSet().Tables[0];
But I never did this and I haven't verified it. But maybe it works.

SubSonic Paging Syntax Help

I am struggling to get a subsonic select query to work, I am writing a paging method and tried the following
Select ns = new Select(maincolumns.ToArray());
ns.PageSize = 10; ** Error Here **
ns.PageIndex = 1; ** And Error Here **
ns.Where("IsLive").IsEqualTo(true);
ns.And("Title").Like("%" + SearchTerm + "%");
ns.OrderAsc("RentalExVat");
return ns.ExecuteDataSet().Tables[0];
Now it doesn't recognise ns.PageSize Or ns.PageIndex, the rest of the query works fine?? I see I need to use the new 'Query' tool to be able to use these two, but I can't figure out the Query syntax??
any syntax help appreciated
SubSonic's query syntax is 'fluent', so in your code sample, the Where clause is not be applied to your query. This snippet may work better:
Select ns = new Select(maincolumns.ToArray());
ns = ns.Where("IsLive").IsEqualTo(true)
.And("Title").Like("%" + SearchTerm + "%")
.OrderAsc("RentalExVat")
.Paged(1, 10); // paging is set here
return ns.ExecuteDataSet().Tables[0];
Also, make sure your 'SearchTerm' has been SQL escaped (or use an alternate calling pattern) or you are vulnerable to SQL injection.

Calling an SQL function from a Subsonic.Select

I asked the following question on the subsonic forum, but only seemed to get one response, so I thought I'd post up here as well to see if anyone could shed some more light on the problem...
I wish to create the following SQL statement through SubSonic using the Select tool (or Query tool) .. it uses a custom function called "SPLIT()":
SELECT * FROM VwPropertyList
WHERE VwPropertyList.idCreatedBy = 123
AND VwPropertyList.idCounty = 45
AND 29 IN (SELECT Item FROM SPLIT(DistrictGroupList, ','))
(the last part of this SQL uses the SPLIT function)
My subsonic equivalent looks like the following...
Dim mySelect As New SubSonic.Select
mySelect.From(VwPropertyList.Schema)
mySelect.Where(VwPropertyList.Columns.IdCreatedBy).IsEqualTo(123)
mySelect.And(VwPropertyList.Columns.IdCounty).IsEqualTo(45)
mySelect.And(29).In(New SubSonic.Select("Item").From("SPLIT("
&
VwPropertyList.Columns.DistrictGroupList
& ", ',')"))
This doesn't work though due to the last part .. how can I add "AND 29 IN (SELECT Item FROM SPLIT(DistrictGroupList, ','))" into my Subsonic.Select ?
The response I got from the subsonic forum suggested I do away with Subsonic.Select and replace with hard-coded InlineQuery() statements .. like:
Dim SQL as String = "Select " &
VwPropertyList.Columns.Item SQL = SQL
& " From " &
VwPropertyList.Schema.TableName SQL =
SQL & " Where " &
VwPropertyList.Columns.IdCreatedBy & "
= #CreatedBy " SQL = SQL & " And " & VwPropertyList.Columns.IdCounty & " =
#County " SQL = SQL & " And
#DistrictGroup IN (Select Item From
SPLIT(DistrictGroupList,',')"
Items =
SubSonic.InlineQuery().ExecuteTypedList(Of
MyItem)(SQL, 123,45,29)
I would prefer to use SubSonic.Select if possible though so that I can avail of the paging functionality etc.
Any ideas?
You could do John's suggestion or you could write the SQL using our InlineQuery - which allows you to write raw SQL and pass in params:
var qry=new InlineQuery("SELECT * FROM table WHERE column=#param",value)
You could try to use the original query object (pre 2.1) like so (untested, from memory):
Query q = new Query(VwPropertyList.Schema.TableName);
q.WHERE("29 IN (SELECT Item FROM SPLIT(DistrictGroupList, ','))");
// pass q.ExecuteReader() to the Load() method of your view.
I would suggest that you use the original Query object as you are looking to get paging. Inline Query does not have any methods that allow paging.
If you absolutely wanted to use Subsonic.Select you could mesh the two ideas and run an Inline Query to get the list of values and then use a Regular Subsonic.Select and pass the retrieved values to the select case but then you would be making two trips to the db.
On a side note I prefer reading Subsonic.Select statements written using the fluent interface that it is namely
SubSonic.Select.AllColumnsFrom()
.Where(VwPropertyList.Columns.IdCreatedBy).IsEqualTo(123)
.And(VwPropertyList.Columns.IdCounty).IsEqualTo(45)
.ExecuteAsCollection();
thanks for the responses.
I ended up doing InlineQuery and just re-wrote the paging code that's normally produced by Subsonic.Select ... not the best solution but it seems to work.
It would be good if I could have done something like this though:
Dim s As New SubSonic.Select
s.From(VwPropertyList.Schema)
sWhere(VwPropertyList.Columns.IdCreatedBy).IsEqualTo(123)
sAnd(VwPropertyList.Columns.IdCounty).IsEqualTo(45)
s.And(29).In(New
InlineQuery("(SELECT Item FROM
SPLIT(DistrictGroupList, ','))"))

Resources