Question

I've written the following query for a resource scheduling calendar app. The query is used to find any events already scheduled on the calendar that would conflict with the event of interest:

        string qry = "SELECT * FROM " + EventX.Schema.TableName + 
            " WHERE " +
                " ( " +
                    " ( " + 
                        EventX.Columns.StartTime + " >= '" + startTime + 
                        "' AND " + EventX.Columns.StartTime + " < '" + endTime + 
                    "' ) " + 
                    " OR " +
                    " ( " + 
                        EventX.Columns.EndTime + " > '" + startTime + 
                        "' AND " + EventX.Columns.EndTime + " <= '" + endTime + 
                    "' ) " + 
                " ) " + 
                " AND " +
                " ( " + 
                    EventX.Columns.EventID + " <> " + eventId + 
                    " AND " + EventX.Columns.StatusID + " IN " + 
                    " ( " + 
                        (int) EventStatus.Approved + " , " + 
                        (int) EventStatus.Completed + 
                    " ) " + 
                " ) "; 

        QueryCommand qc = new QueryCommand(qry, EventX.Schema.Provider.Name);

        EventXCollection events = new EventXCollection();
        events.LoadAndCloseReader(DataService.GetReader(qc));

The above query works, but it's a freakishly ugly abomination in terms of aesthetics and I'd prefer not to leave a mess for whoever has to maintain this after me. I wrote this query after having tried and failed to get it working using a SqlQuery instead. The closest I came to getting it right before giving up on SqlQuery and writing the functional-but-ugly hack above was the following (incorrect) query:

        SqlQuery qry = new Select().From(EventX.Schema.TableName);
        qry.WhereExpression(EventX.Columns.StartTime).IsGreaterThan(startTime);
        qry.And(EventX.Columns.StartTime).IsLessThan(endTime);
        qry.OrExpression(EventX.Columns.EndTime).IsGreaterThan(startTime);
        qry.And(EventX.Columns.EndTime).IsLessThan(endTime);
        qry.AndExpression(EventX.Columns.EventID).IsNotEqualTo(eventId);
        qry.And(EventX.Columns.StatusID).In((int)EventStatus.Approved, (int)EventStatus.Completed);

I'm working on maintaining an application written by a developer who has since moved on and this is my first experience working with Subsonic. Note that if event A ends at 2:00 PM and event B starts at 2:00 PM, then events A and B do not constitute a scheduling conflict and the query should reflect that. How would you write this query using Subsonic?

Was it helpful?

Solution

If i'm correct, EventX.Schema.TableName and EventX.Columns.xyz are just constants, and actual variables are startTime, endTime and eventId right? If so, why not make a stored proc, or at least a parameterized query, so SQL can cache execution plans? IIRC Subsonic supports SPs, but the project site looks like it's about to fall apart.

Sql something like this, could be an SP, or a parameterized query:

 SELECT * FROM EventTable
 WHERE 
        ( 
            ( 
                 StartTime >= @startTime 
                  AND StartTime < @endTime 
            ) 
             OR 
            (
                 EndTime > @startTime 
                  AND EndTime <= @endTime 
            )
        )
        AND
        (
            EventID <> @eventId
             AND 
            StatusID IN ( 1,2 ) 
            --just made up these numbers, should be the ID of Approved and Completed ...
        )

And pass in @startTime, @endTime and @eventId as parameters?

Although I don't know the state of you project, but if I was in your place, probably I would try to leave what's working correctly as it is. And slowly try to replace Subsonic with something actively used in the world, or you are experienced in using it. Would be better both for you, and whoever will have to maintain it later.

Subsonic 3.0 was released about two ago, and feels pretty much dead now. Rob Connery created Massive since, and pretty much abandoned Subsonic if I'm correct...

Edit

Just to make it clear: I'm not saying Subsonic is bad, used and tweaked it's T4 templates a lot to suit my needs. About 2 years ago. But not a lot of people are familiar with it, so moved to other, more widely known data access techniques...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top