Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Updates for take and skip |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | sourceforge |
Files: | files | file ages | folders |
SHA1: |
fc483e780aa9432a88ded0da3c7ea01a |
User & Date: | rmsimpson 2010-08-11 16:17:20.000 |
Context
2010-08-15
| ||
22:10 | Fix guid type Closed-Leaf check-in: 555d0d530d user: rmsimpson tags: sourceforge | |
2010-08-11
| ||
16:17 | Updates for take and skip check-in: fc483e780a user: rmsimpson tags: sourceforge | |
16:15 | Updates to the test app check-in: 7bfffba74e user: rmsimpson tags: sourceforge | |
Changes
Changes to System.Data.SQLite.Linq/SQL.
|
| | | | < < < > > | | | | | < | > | | | < > | < | > > | < < > > > > > | > > > | < > > | > > > | < < > > | > > > | < < < < < < < < < < < | < < | < > | | < < < | > > > | | | | | > > | < < | | < < < < < | | < > | > > > < > > | < | < > | > > | | | < | < < > > | | | | > > | > | < < < | > | > > > > | | < | < < < < > | < < > > | | < | | | > > | < > | > > > > | | | < < | | | | | < | < > | < < > > | | | | > > | < | | | | | > | < < < > | < < < > > | | < > | < | < < | | < | < < | | > | < | < | < > > | > | > < < | < < | < | | < | < < > > > | < | > > > > > > > > | < < < < | < | > | < > > | < > | < | | < | < > | > | | | | | | > | | < < < | > > > | > | < < > | | | | < < | < | < | > > > > | > < < | > > > | < | | < | > | < < | | < > | < > > | | < > | < > | | | | > | | < | | < < | > > | | | | > > < | | < | > | > | < > | < > > > | < > | < > > | < < < < > < < | > > | < | > | > | < | | | | | < | < > < > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 | //--------------------------------------------------------------------- // <copyright file="SqlSelectStatement.cs" company="Microsoft"> // Copyright (c) Microsoft Corporation. All rights reserved. // </copyright> //--------------------------------------------------------------------- namespace System.Data.SQLite { using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Text; using System.Data.Metadata.Edm; using System.Data.Common.CommandTrees; /// <summary> /// A SqlSelectStatement represents a canonical SQL SELECT statement. /// It has fields for the 6 main clauses /// <list type="number"> /// <item>SELECT</item> /// <item>FROM</item> /// <item>WHERE</item> /// <item>GROUP BY</item> /// <item>ORDER BY</item> /// <item>LIMIT</item> /// </list> /// We do not have HAVING, since it does not correspond to anything in the DbCommandTree. /// Each of the fields is a SqlBuilder, so we can keep appending SQL strings /// or other fragments to build up the clause. /// /// We have a IsDistinct property to indicate that we want distict columns. /// This is given out of band, since the input expression to the select clause /// may already have some columns projected out, and we use append-only SqlBuilders. /// The DISTINCT is inserted when we finally write the object into a string. /// /// Also, we have a Top property, which is non-null if the number of results should /// be limited to certain number. It is given out of band for the same reasons as DISTINCT. /// /// The FromExtents contains the list of inputs in use for the select statement. /// There is usually just one element in this - Select statements for joins may /// temporarily have more than one. /// /// If the select statement is created by a Join node, we maintain a list of /// all the extents that have been flattened in the join in AllJoinExtents /// <example> /// in J(j1= J(a,b), c) /// FromExtents has 2 nodes JoinSymbol(name=j1, ...) and Symbol(name=c) /// AllJoinExtents has 3 nodes Symbol(name=a), Symbol(name=b), Symbol(name=c) /// </example> /// /// If any expression in the non-FROM clause refers to an extent in a higher scope, /// we add that extent to the OuterExtents list. This list denotes the list /// of extent aliases that may collide with the aliases used in this select statement. /// It is set by <see cref="SqlGenerator.Visit(DbVariableReferenceExpression)"/>. /// An extent is an outer extent if it is not one of the FromExtents. /// /// /// </summary> internal sealed class SqlSelectStatement : ISqlFragment { private bool isDistinct; /// <summary> /// Do we need to add a DISTINCT at the beginning of the SELECT /// </summary> internal bool IsDistinct { get { return isDistinct; } set { isDistinct = value; } } private List<Symbol> allJoinExtents; internal List<Symbol> AllJoinExtents { get { return allJoinExtents; } // We have a setter as well, even though this is a list, // since we use this field only in special cases. set { allJoinExtents = value; } } private List<Symbol> fromExtents; internal List<Symbol> FromExtents { get { if (null == fromExtents) { fromExtents = new List<Symbol>(); } return fromExtents; } } private Dictionary<Symbol, bool> outerExtents; internal Dictionary<Symbol, bool> OuterExtents { get { if (null == outerExtents) { outerExtents = new Dictionary<Symbol, bool>(); } return outerExtents; } } private TopClause top; internal TopClause Top { get { return top; } set { Debug.Assert(top == null, "SqlSelectStatement.Top has already been set"); top = value; } } private SqlBuilder select = new SqlBuilder(); internal SqlBuilder Select { get { return select; } } private SqlBuilder from = new SqlBuilder(); internal SqlBuilder From { get { return from; } } private SqlBuilder where; internal SqlBuilder Where { get { if (null == where) { where = new SqlBuilder(); } return where; } } private SqlBuilder groupBy; internal SqlBuilder GroupBy { get { if (null == groupBy) { groupBy = new SqlBuilder(); } return groupBy; } } private SqlBuilder orderBy; public SqlBuilder OrderBy { get { if (null == orderBy) { orderBy = new SqlBuilder(); } return orderBy; } } private string limit; public string Limit { get { return limit; } set { limit = value; } } private string offset; public string OffSet { get { return offset; } set { offset = value; } } //indicates whether it is the top most select statement, // if not Order By should be omitted unless there is a corresponding TOP private bool isTopMost; internal bool IsTopMost { get { return this.isTopMost; } set { this.isTopMost = value; } } #region ISqlFragment Members /// <summary> /// Write out a SQL select statement as a string. /// We have to /// <list type="number"> /// <item>Check whether the aliases extents we use in this statement have /// to be renamed. /// We first create a list of all the aliases used by the outer extents. /// For each of the FromExtents( or AllJoinExtents if it is non-null), /// rename it if it collides with the previous list. /// </item> /// <item>Write each of the clauses (if it exists) as a string</item> /// </list> /// </summary> /// <param name="writer"></param> /// <param name="sqlGenerator"></param> public void WriteSql(SqlWriter writer, SqlGenerator sqlGenerator) { #region Check if FROM aliases need to be renamed // Create a list of the aliases used by the outer extents // JoinSymbols have to be treated specially. List<string> outerExtentAliases = null; if ((null != outerExtents) && (0 < outerExtents.Count)) { foreach (Symbol outerExtent in outerExtents.Keys) { JoinSymbol joinSymbol = outerExtent as JoinSymbol; if (joinSymbol != null) { foreach (Symbol symbol in joinSymbol.FlattenedExtentList) { if (null == outerExtentAliases) { outerExtentAliases = new List<string>(); } outerExtentAliases.Add(symbol.NewName); } } else { if (null == outerExtentAliases) { outerExtentAliases = new List<string>(); } outerExtentAliases.Add(outerExtent.NewName); } } } // An then rename each of the FromExtents we have // If AllJoinExtents is non-null - it has precedence. // The new name is derived from the old name - we append an increasing int. List<Symbol> extentList = this.AllJoinExtents ?? this.fromExtents; if (null != extentList) { foreach (Symbol fromAlias in extentList) { if ((null != outerExtentAliases) && outerExtentAliases.Contains(fromAlias.Name)) { int i = sqlGenerator.AllExtentNames[fromAlias.Name]; string newName; do { ++i; newName = fromAlias.Name + i.ToString(System.Globalization.CultureInfo.InvariantCulture); } while (sqlGenerator.AllExtentNames.ContainsKey(newName)); sqlGenerator.AllExtentNames[fromAlias.Name] = i; fromAlias.NewName = newName; // Add extent to list of known names (although i is always incrementing, "prefix11" can // eventually collide with "prefix1" when it is extended) sqlGenerator.AllExtentNames[newName] = 0; } // Add the current alias to the list, so that the extents // that follow do not collide with me. if (null == outerExtentAliases) { outerExtentAliases = new List<string>(); } outerExtentAliases.Add(fromAlias.NewName); } } #endregion // Increase the indent, so that the Sql statement is nested by one tab. writer.Indent += 1; // ++ can be confusing in this context writer.Write("SELECT "); if (IsDistinct) { writer.Write("DISTINCT "); } if ((null == this.select) || this.Select.IsEmpty) { Debug.Assert(false); // we have removed all possibilities of SELECT *. writer.Write("*"); } else { this.Select.WriteSql(writer, sqlGenerator); } writer.WriteLine(); writer.Write("FROM "); this.From.WriteSql(writer, sqlGenerator); if ((null != this.where) && !this.Where.IsEmpty) { writer.WriteLine(); writer.Write("WHERE "); this.Where.WriteSql(writer, sqlGenerator); } if ((null != this.groupBy) && !this.GroupBy.IsEmpty) { writer.WriteLine(); writer.Write("GROUP BY "); this.GroupBy.WriteSql(writer, sqlGenerator); } if ((null != this.orderBy) && !this.OrderBy.IsEmpty && (this.IsTopMost || this.Top != null)) { writer.WriteLine(); writer.Write("ORDER BY "); this.OrderBy.WriteSql(writer, sqlGenerator); } if ((null != this.limit) && this.limit.Length != 0) //&& (this.IsTopMost || this.Top != null)) { writer.WriteLine(); writer.Write(string.Format("LIMIT {0} ", limit)); } if ((null != this.offset) && this.offset.Length != 0)// && (this.IsTopMost || this.Top != null)) { if (this.limit != null && this.limit.Length != 0) writer.Write(string.Format("OFFSET {0} ", offset)); else { writer.WriteLine(); writer.Write(string.Format("LIMIT 8999999999999999999 OFFSET {0} ", offset)); } } if (this.Top != null) { this.Top.WriteSql(writer, sqlGenerator); } --writer.Indent; } #endregion } } |