MultiColumnSort ClassCleanCode C# Libraries v1.2.03 API
Manipulates the DataTable of a DataGridView based on a query text to provide a multi-column sort.
Inheritance Hierarchy

OnlineSystem Object
  CleanCode.Data MultiColumnSort

Namespace: CleanCode.Data
Assembly: CleanCode (in CleanCode.dll) Version: 1.2.3.0 (1.2.03)
Syntax

public class MultiColumnSort
Remarks

This class parses a SQL query, identifies the fields desired for sorting (from the ORDER BY clause), maps any aliases (from the "xxx as yyy" parts of the SELECT clause), then generates a new ORDER BY clause to feed to the Sort property of a DataView where the elements refer to the DataTable columns rather than the database columns.

Note that any compound expressions (e.g. len(name)) must have an alias. Though, for example, SQL Server could process this query:

select name, len(name)
from accounts
order by len(name)
the MultiColumnSort can only use the actual column names in the result set. Since an unaliased column will receive a generic "Columnn" label, it could not easily be associated with the compound expression. The solution is to require an alias as in:
select name, len(name) as myLength
from accounts
order by myLength
This allows MultiColumnSort to convert the specified sort expression of len(name) to a realized sort expression of myLength. Note that most databases (SQL Server and Oracle, for example) allow you to use either the actual expression or the alias in the order by clause itself, so MultiColumnSort allows this as well. Thus this will also work:
select name, len(name) as myLength
from accounts
order by len(name)

There are some drivers, however (notably the Microsoft Jet driver used by Access as well as ODBC/CSV connections) that do not allow aliases in the order by clause. In those situations, you must define the alias but then you must use the original expression in the order by, as in the last example above.

Some valid SQL code will not quite work properly due to a limitation of MultiColumnSort. An expression such as this:

SELECT users.name
FROM users
ORDER BY users.name
is fine for a SQL interpreter but MultiColumnSort operates on column names or aliases to column names. Since the resulting column from the above example would be name MultiColumnSort cannot match the element users.name in the order by clause to it. The workaround to this is simply to alias any compound names such as users.name to a simple name, as in:
SELECT users.name as name
FROM users
ORDER BY users.name
Here is a sampling of expressions that all work just fine:

-- Unaliased simple name: 
SELECT name FROM NameTable ORDER BY name

-- Aliased simple name ordered by alias: 
SELECT name as myName FROM NameTable ORDER BY myName

-- Aliased simple name ordered by name: 
SELECT name as myName FROM NameTable ORDER BY name

-- Aliased compound name ordered by alias: 
SELECT table.name as myName FROM NameTable ORDER BY myName

-- Aliased compound name ordered by compound name: 
SELECT table.name as myName FROM NameTable ORDER BY table.name

-- Aliased expression ordered by alias: 
SELECT len(name) as myNameLength FROM NameTable ORDER BY myNameLength

-- Aliased expression ordered by expression: 
SELECT len(name) as myNameLength FROM NameTable ORDER BY len(name)

-- Delimited simple name ordered by non-delimited name: 
SELECT [name] FROM NameTable ORDER BY name

-- Delimited simple name ordered by delimited name: 
SELECT [name] FROM NameTable ORDER BY [name]

-- Aliased, delimited compound name ordered by delimited, compound name: 
SELECT [nt].[name] as myName FROM NameTable nt ORDER BY [nt].[name]

-- Aliased, delimited compound name ordered by alias: 
SELECT [nt].[name] as myName FROM NameTable nt ORDER BY myName

MultiColumnSort is flexible in field name delimiters: you may use square brackets as shown in the table as well as single quotes, double quotes, or backquotes. Since different databases have different requirements on these, MultiColumnSort allows the superset.

MultiColumnSort uses simple parsing techniques; it is not designed to handle overly complex queries. For example, the parser expects that the list of fields in the ORDER BY clause is comprised of everything from the ORDER BY literal until the end of the query text. If it is not, it will not return a valid result. You can provide a hint, though, for cases where it cannot properly identify the ORDER BY clause, such as when there is cleanup following, like this (note the 4-character rather than 2-character comment prefix):

SELECT field1, field2, field3
FROM #myTempTable
---- [ORDERING START] 
ORDER BY field1, field2, field3
---- [ORDERING END] 
DROP TABLE #myTempTable

Similarly, a complex query that may have multiple select statements could befuddle the simple parser, so use [SELECT START] and [SELECT END] brackets from the SELECT keyword up to and including the FROM keyword.

---- [SELECT START] 
SELECT field1, field2, field3
FROM #myTempTable
---- [SELECT END] 
---- [ORDERING START] 
ORDER BY field1, field2, field3
---- [ORDERING END] 
DROP TABLE #myTempTable

Since CleanCode 0.9.13.

See Also