Radarr/Marr.Data/QGen/SqlitePagingQueryDecorator.cs

162 lines
5.0 KiB
C#
Raw Normal View History

2013-05-07 02:32:43 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Marr.Data.Mapping;
using Marr.Data.QGen.Dialects;
namespace Marr.Data.QGen
{
/// <summary>
/// Decorates the SelectQuery by wrapping it in a paging query.
/// </summary>
public class SqlitePagingQueryDecorator : IQuery
{
private SelectQuery _innerQuery;
private int _skip;
private int _take;
public SqlitePagingQueryDecorator(SelectQuery innerQuery, int skip, int take)
{
if (string.IsNullOrEmpty(innerQuery.OrderBy.ToString()))
{
throw new DataMappingException("A paged query must specify an order by clause.");
}
_innerQuery = innerQuery;
_skip = skip;
_take = take;
}
public string Generate()
{
if (_innerQuery.IsView || _innerQuery.IsJoin)
{
return ComplexPaging();
}
else
{
return SimplePaging();
}
}
private string SimplePaging()
{
// Create paged query
2013-05-07 02:32:43 +00:00
StringBuilder sql = new StringBuilder();
_innerQuery.BuildSelectClause(sql);
_innerQuery.BuildFromClause(sql);
_innerQuery.BuildJoinClauses(sql);
_innerQuery.BuildWhereClause(sql);
_innerQuery.BuildOrderClause(sql);
sql.AppendLine(String.Format(" LIMIT {0},{1}", _skip, _take));
return sql.ToString();
}
private string ComplexPaging()
{
var baseTable = _innerQuery.Tables.First();
StringBuilder sql = new StringBuilder();
_innerQuery.BuildSelectClause(sql);
sql.Append(" FROM (");
BuildSimpleInnerSelect(sql);
_innerQuery.BuildFromClause(sql);
2013-05-31 00:30:32 +00:00
_innerQuery.BuildJoinClauses(sql);
_innerQuery.BuildWhereClause(sql);
2013-05-31 00:30:32 +00:00
BuildGroupBy(sql);
BuildOrderBy(sql);
sql.AppendFormat(" LIMIT {0},{1}", _skip, _take);
sql.AppendFormat(") AS {0} ", _innerQuery.Dialect.CreateToken(baseTable.Alias));
_innerQuery.BuildJoinClauses(sql);
return sql.ToString();
}
2013-05-07 02:32:43 +00:00
public void BuildSelectClause(StringBuilder sql)
{
List<string> appended = new List<string>();
sql.Append("SELECT ");
int startIndex = sql.Length;
// COLUMNS
foreach (Table join in _innerQuery.Tables)
{
for (int i = 0; i < join.Columns.Count; i++)
{
var c = join.Columns[i];
if (sql.Length > startIndex && sql[sql.Length - 1] != ',')
sql.Append(",");
if (join is View)
{
string token = _innerQuery.Dialect.CreateToken(string.Concat(join.Alias, ".", _innerQuery.NameOrAltName(c.ColumnInfo)));
if (appended.Contains(token))
continue;
sql.Append(token);
appended.Add(token);
}
else
{
string token = string.Concat(join.Alias, ".", c.ColumnInfo.Name);
if (appended.Contains(token))
continue;
sql.Append(_innerQuery.Dialect.CreateToken(token));
if (_innerQuery.UseAltName && c.ColumnInfo.AltName != null && c.ColumnInfo.AltName != c.ColumnInfo.Name)
{
string altName = c.ColumnInfo.AltName;
sql.AppendFormat(" AS {0}", altName);
}
}
}
}
}
private void BuildSimpleInnerSelect(StringBuilder sql)
{
sql.Append("SELECT ");
int startIndex = sql.Length;
// COLUMNS
var join = _innerQuery.Tables.First();
for (int i = 0; i < join.Columns.Count; i++)
{
var c = join.Columns[i];
if (sql.Length > startIndex)
sql.Append(",");
2013-05-31 00:30:32 +00:00
string token = string.Concat(join.Alias, ".", c.ColumnInfo.Name);
sql.Append(_innerQuery.Dialect.CreateToken(token));
}
}
2013-05-31 00:30:32 +00:00
private void BuildOrderBy(StringBuilder sql)
{
sql.Append(_innerQuery.OrderBy.BuildQuery(false));
}
private void BuildGroupBy(StringBuilder sql)
{
var baseTable = _innerQuery.Tables.First();
var primaryKeyColumn = baseTable.Columns.Single(c => c.ColumnInfo.IsPrimaryKey);
2013-05-31 05:15:21 +00:00
string token = _innerQuery.Dialect.CreateToken(string.Concat(baseTable.Alias, ".", primaryKeyColumn.ColumnInfo.Name));
2013-05-31 00:30:32 +00:00
sql.AppendFormat(" GROUP BY {0}", token);
}
2013-05-07 02:32:43 +00:00
}
}