5 // Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne
\r
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
\r
8 // of this software and associated documentation files (the "Software"), to deal
\r
9 // in the Software without restriction, including without limitation the rights
\r
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
11 // copies of the Software, and to permit persons to whom the Software is
\r
12 // furnished to do so, subject to the following conditions:
\r
14 // The above copyright notice and this permission notice shall be included in
\r
15 // all copies or substantial portions of the Software.
\r
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
\r
29 using System.Data.Linq;
\r
30 using System.Reflection;
\r
31 using System.Diagnostics;
\r
32 using System.Collections;
\r
33 using System.Collections.Generic;
\r
35 using System.Linq.Expressions;
\r
36 using System.ComponentModel;
\r
39 using System.Data.Linq.Implementation;
\r
40 using System.Data.Linq.Sugar;
\r
41 using ITable = System.Data.Linq.ITable;
\r
43 using DbLinq.Data.Linq.Implementation;
\r
44 using DbLinq.Data.Linq.Sugar;
\r
45 using ITable = DbLinq.Data.Linq.ITable;
\r
52 namespace System.Data.Linq
\r
54 namespace DbLinq.Data.Linq
\r
58 /// T may be eg. class Employee or string - the output
\r
60 /// <typeparam name="TEntity">The type of the entity.</typeparam>
\r
61 public sealed partial class Table<TEntity> :
\r
65 IEnumerable<TEntity>,
\r
67 IQueryable<TEntity>,
\r
69 where TEntity : class
\r
72 /// the parent DataContext holds our connection etc
\r
74 public DataContext Context { get; private set; }
\r
76 // QueryProvider is the running entity, running through nested Expressions
\r
77 private readonly QueryProvider<TEntity> _queryProvider;
\r
80 /// Initializes a new instance of the <see cref="Table<TEntity>"/> class.
\r
82 /// <param name="parentContext">The parent context.</param>
\r
83 internal Table(DataContext parentContext)
\r
85 Context = parentContext;
\r
86 _queryProvider = new QueryProvider<TEntity>(parentContext);
\r
90 /// 'S' is the projected type. If you say 'from e in Employees select e.ID', then type S will be int.
\r
91 /// If you say 'select new {e.ID}', then type S will be something like Projection.f__1
\r
93 IQueryable<S> IQueryProvider.CreateQuery<S>(Expression expr)
\r
95 return _queryProvider.CreateQuery<S>(expr);
\r
99 /// this is only called during Dynamic Linq
\r
101 IQueryable IQueryProvider.CreateQuery(Expression expression)
\r
103 return _queryProvider.CreateQuery(expression);
\r
107 /// the query '(from o in Orders select o).First()' enters here
\r
109 S IQueryProvider.Execute<S>(Expression expression)
\r
111 return _queryProvider.Execute<S>(expression);
\r
115 /// Executes the current expression
\r
117 /// <param name="expression"></param>
\r
118 /// <returns></returns>
\r
119 object IQueryProvider.Execute(Expression expression)
\r
121 return _queryProvider.Execute(expression);
\r
125 /// entry point for 'foreach' statement.
\r
127 public IEnumerator<TEntity> GetEnumerator()
\r
129 var queryable = this as IQueryable<TEntity>;
\r
130 var query = queryable.Select(t => t);
\r
131 return query.GetEnumerator();
\r
134 IEnumerator<TEntity> IEnumerable<TEntity>.GetEnumerator()
\r
136 return GetEnumerator();
\r
140 /// Enumerates all table items
\r
142 /// <returns></returns>
\r
143 IEnumerator IEnumerable.GetEnumerator()
\r
145 return GetEnumerator();
\r
148 Type IQueryable.ElementType
\r
150 get { return _queryProvider.ElementType; }
\r
154 /// Returns this table as an Expression
\r
156 Expression IQueryable.Expression
\r
158 get { return Expression.Constant(this); } // do not change this to _queryProvider.Expression, Sugar doesn't fully handle QueryProviders by now
\r
162 /// IQueryable.Provider: represents the Table as a IQueryable provider (hence the name)
\r
164 IQueryProvider IQueryable.Provider
\r
166 get { return _queryProvider.Provider; }
\r
169 #region Insert functions
\r
171 void ITable.InsertOnSubmit(object entity)
\r
173 Context.RegisterInsert(entity);
\r
176 public void InsertOnSubmit(TEntity entity)
\r
178 Context.RegisterInsert(entity);
\r
181 void ITable.InsertAllOnSubmit(IEnumerable entities)
\r
183 foreach (var entity in entities)
\r
184 Context.RegisterInsert(entity);
\r
187 public void InsertAllOnSubmit<TSubEntity>(IEnumerable<TSubEntity> entities) where TSubEntity : TEntity
\r
189 if (entities == null)
\r
190 throw new ArgumentNullException("entities");
\r
192 foreach (var entity in entities)
\r
193 Context.RegisterInsert(entity);
\r
198 #region Delete functions
\r
200 void ITable.DeleteAllOnSubmit(IEnumerable entities)
\r
202 foreach (var entity in entities)
\r
203 Context.RegisterDelete(entity);
\r
207 /// required by ITable interface
\r
209 /// <param name="entity"></param>
\r
210 void ITable.DeleteOnSubmit(object entity)
\r
212 Context.RegisterDelete(entity);
\r
215 public void DeleteOnSubmit(TEntity entity)
\r
217 Context.RegisterDelete(entity);
\r
220 public void DeleteAllOnSubmit<TSubEntity>(IEnumerable<TSubEntity> entities) where TSubEntity : TEntity
\r
222 if (entities == null)
\r
223 throw new ArgumentNullException("entities");
\r
225 foreach (var row in entities)
\r
226 Context.RegisterDelete(row);
\r
231 #region Attach functions
\r
234 /// required for ITable
\r
236 /// <param name="entity"></param>
\r
237 void ITable.Attach(object entity)
\r
239 Context.RegisterUpdate(entity);
\r
242 void ITable.Attach(object entity, object original)
\r
244 Context.RegisterUpdate(entity, original);
\r
247 void ITable.Attach(object entity, bool asModified)
\r
249 Context.RegisterUpdate(entity, asModified ? null : entity);
\r
252 void ITable.AttachAll(IEnumerable entities)
\r
254 foreach (var entity in entities)
\r
255 Context.RegisterUpdate(entity);
\r
257 void ITable.AttachAll(IEnumerable entities, bool asModified)
\r
259 foreach (var entity in entities)
\r
260 Context.RegisterUpdate(entity);
\r
264 /// Attaches an entity from another Context to a table,
\r
265 /// with the intention to perform an update or delete operation
\r
267 /// <param name="entity">table row object to attach</param>
\r
268 public void Attach(TEntity entity)
\r
270 Context.RegisterUpdate(entity);
\r
274 public void Attach(TEntity entity, bool asModified)
\r
276 throw new NotImplementedException();
\r
279 public void AttachAll<TSubEntity>(IEnumerable<TSubEntity> entities) where TSubEntity : TEntity
\r
281 if (entities == null)
\r
282 throw new ArgumentNullException("entities");
\r
284 foreach (var entity in entities)
\r
285 Context.RegisterUpdate(entity);
\r
289 public void AttachAll<TSubEntity>(IEnumerable<TSubEntity> entities, bool asModified) where TSubEntity : TEntity
\r
291 throw new NotImplementedException();
\r
295 /// Attaches existing entity with original state
\r
297 /// <param name="entity">live entity added to change tracking</param>
\r
298 /// <param name="original">original unchanged property values</param>
\r
299 public void Attach(TEntity entity, TEntity original)
\r
301 Context.RegisterUpdate(entity, original);
\r
307 /// Gets a value indicating whether this instance is read only.
\r
310 /// <c>true</c> if this instance is read only; otherwise, <c>false</c>.
\r
312 public bool IsReadOnly { get { return false; } }
\r
314 // PC: this will probably required to recreate a new object instance with all original values
\r
315 // (that we currently do not always store, so we may need to make a differential copy
\r
316 [Obsolete("NOT IMPLEMENTED YET")]
\r
318 ModifiedMemberInfo[] ITable.GetModifiedMembers(object entity)
\r
320 throw new ApplicationException("L579 Not implemented");
\r
323 // PC: complementary to GetModifiedMembers(), we probably need a few changes to the IMemberModificationHandler,
\r
324 // to recall original values
\r
325 [Obsolete("NOT IMPLEMENTED YET")]
\r
327 object ITable.GetOriginalEntityState(object entity)
\r
329 throw new ApplicationException("L585 Not implemented");
\r
332 bool IListSource.ContainsListCollection
\r
334 get { return true; }
\r
337 IList IListSource.GetList()
\r
339 return this.ToList();
\r
343 public TEntity GetOriginalEntityState(TEntity entity)
\r
345 if (entity == null)
\r
346 throw new ArgumentNullException("entity");
\r
348 throw new NotImplementedException();
\r
352 public IBindingList GetNewBindingList()
\r
354 throw new NotImplementedException();
\r
358 public ModifiedMemberInfo[] GetModifiedMembers(TEntity entity)
\r
360 if (entity == null)
\r
361 throw new ArgumentNullException("entity");
\r
363 throw new NotImplementedException();
\r
366 public override string ToString()
\r
368 return string.Format("Table({0})", typeof(TEntity).Name);
\r