예제 #1
0
        /// <summary>
        /// Executes a db command and fills in a list of objects with the result data
        /// </summary>
        /// <typeparam name="ReturnType">The type of object to return</typeparam>
        /// <param name="objectType">The type of object to return.</param>
        /// <param name="command">The command to execute</param>
        /// <returns></returns>
        protected virtual IEnumerable <ReturnType> ExecuteCommandLoadList <ReturnType>(Type objectType, IDbCommand command)
        {
            DatabaseTypeInfo ti = TypeInformationParser.GetTypeInfo(objectType);

            using (IQueryData dt = ExecuteCommands.ExecuteCommandQuery(command, Connection))
            {
                if (dt.QuerySuccessful)
                {
                    foreach (IQueryRow row in dt)
                    {
                        ReturnType toAdd;

                        if (objectType.IsSystemType())
                        {
                            toAdd = (ReturnType)Connection.CLRConverter.ConvertToType(row.GetDataForRowField(0), typeof(ReturnType));
                        }
                        else
                        {
                            toAdd = (ReturnType)BuildObject(row, ti);
                        }

                        yield return(toAdd);
                    }
                }
            }
        }
예제 #2
0
        /// <summary>
        /// This function will return an IQueryable appropriate for using with LINQ
        /// </summary>
        /// <typeparam name="T">The type to query</typeparam>
        /// <returns></returns>
        public virtual IQueryable <T> Query <T>()
        {
            IQueryable <T>   toReturn = new Query <T>(Connection.GetQueryProvider(this));
            DatabaseTypeInfo ti       = TypeInformationParser.GetTypeInfo(typeof(T));

            if (ti.QueryPredicate != null)
            {
                toReturn = ti.QueryPredicate.Invoke(toReturn);
            }

            return(toReturn);
        }
예제 #3
0
        /// <summary>
        /// Returns the key value for an object
        /// </summary>
        /// <param name="type">The type to check</param>
        /// <param name="item">The object to extract from</param>
        /// <returns></returns>
        public virtual object GetKeyForItemType(Type type, object item)
        {
            IEnumerable <DataFieldInfo> fields = TypeInformationParser.GetPrimaryKeys(type);

            if (fields.Count() > 1)
            {
                throw new DataStoreException("This Type contains more than one key");
            }
            if (fields.Count() < 1)
            {
                throw new DataStoreException("This type does not contain a key");
            }
            return(fields.ElementAt(0).Getter(item));
        }
예제 #4
0
        public async Task <IQueryable <T> > Query <T>()
        {
            var tTask = Task.Run(() => TypeInformationParser.GetTypeInfo(typeof(T)));
            var rTask = Task.Run(() => new Query <T>(Connection.GetQueryProvider(_dstore)));

            DatabaseTypeInfo ti       = await tTask;
            IQueryable <T>   toReturn = await rTask;

            if (ti.QueryPredicate != null)
            {
                toReturn = ti.QueryPredicate.Invoke(toReturn);
            }

            return(toReturn);
        }
예제 #5
0
 protected async Task <bool> ProcessCommandAsync(object item, IDbCommand command, bool FailIfNoRecords = false)
 {
     return(await ProcessCommandAsync(r =>
     {
         var en = r.GetQueryEnumerator();
         if (en.MoveNext())
         {
             var task = SetFieldDataAsync(TypeInformationParser.GetTypeInfo(item.GetType()), en.Current, item);
             return true;
         }
         else
         {
             return false;
         }
     }, command, FailIfNoRecords));
 }
예제 #6
0
 /// <summary>
 /// Executes a db command and fills in the object, if needed i.e. primary keys on insert etc
 /// </summary>
 /// <param name="item">The item being queried with</param>
 /// <param name="command">The db command</param>
 /// <param name="FailIfNoRecords">if set to <c>true</c> and there is no records in the result set, no further processing will be done</param>
 /// <returns></returns>
 protected virtual bool ProcessCommand(object item, IDbCommand command, bool FailIfNoRecords)
 {
     return(ProcessCommand(r =>
     {
         var en = r.GetQueryEnumerator();
         if (en.MoveNext())
         {
             SetFieldData(TypeInformationParser.GetTypeInfo(item.GetType()), en.Current, item);
             return true;
         }
         else
         {
             return false;
         }
     }, command, FailIfNoRecords));
 }
예제 #7
0
        /// <summary>
        /// Loads an object.
        /// </summary>
        /// <param name="item">The type to load.</param>
        /// <param name="key">The primary field</param>
        /// <returns></returns>
        public virtual object LoadObject(Type item, object key)
        {
            DatabaseTypeInfo ti = TypeInformationParser.GetTypeInfo(item);

            if (ti.PrimaryKeys.Count == 1)
            {
                object toReturn = CreateObjectSetKey(item, key, ti);
                if (!LoadObject(toReturn))
                {
                    toReturn = null;
                }
                return(toReturn);
            }
            else
            {
                throw new DataStoreException("This method is only valid when the item contains (1) key");
            }
        }
예제 #8
0
        public async Task <object> LoadObject(Type item, object key, bool LoadAllFields = false)
        {
            DatabaseTypeInfo ti = TypeInformationParser.GetTypeInfo(item);

            if (ti.PrimaryKeys.Count == 1)
            {
                object toReturn = CreateObjectSetKey(item, key, ti);
                if (!await LoadObject(toReturn, LoadAllFields))
                {
                    toReturn = null;
                }

                return(toReturn);
            }
            else
            {
                throw new DataStoreException("This method is only valid when the item contains (1) key");
            }
        }
예제 #9
0
        /// <summary>
        /// Executes a db command and fills in a list of objects when done, if needed i.e. primary keys on insert etc
        /// </summary>
        /// <param name="items">The items.</param>
        /// <param name="command">The db command</param>
        /// <param name="FailIfNoRecords">if set to <c>true</c> and there is no records in the result set, no further processing will be done</param>
        /// <returns></returns>
        protected virtual bool ProcessCommand(IList items, IDbCommand command, bool FailIfNoRecords)
        {
            return(ProcessCommand(dt =>
            {
                if (items.Count > 0)
                {
                    Type t = items[0].GetType();
                    IEnumerator <IQueryRow> rows = dt.GetQueryEnumerator();

                    for (int i = 0; i < items.Count; i++)
                    {
                        rows.MoveNext();
                        SetFieldData(TypeInformationParser.GetTypeInfo(t), rows.Current, items[i]);
                    }
                    return true;
                }
                return false;
            }, command, FailIfNoRecords));
        }
예제 #10
0
        public override bool InsertObject(object item)
        {
            DatabaseTypeInfo ti = TypeInformationParser.GetTypeInfo(item.GetType());

            EventHandler <CommandExecutingEventArgs> inserting = (sender, e) =>
            {
                if (ti.PrimaryKeys.Count == 1)
                {
                    SqlCeCommand cmd = new SqlCeCommand("SELECT @@Identity;");
                    cmd.Connection = (SqlCeConnection)e.RawConnection;
                    object data = cmd.ExecuteScalar();

                    ti.PrimaryKeys[0].Setter(item, Connection.CLRConverter.ConvertToType(data, ti.PrimaryKeys[0].PropertyType));
                }
            };

            this.ExecuteCommands.CommandExecuted += inserting;
            bool result = base.InsertObject(item);

            this.ExecuteCommands.CommandExecuted -= inserting;
            return(result);
        }
예제 #11
0
        public async Task <IEnumerable <ReturnType> > ExecuteCommandLoadList <ReturnType>(Type objectType, IDbCommand command)
        {
            List <ReturnType> toReturn = new List <ReturnType>();

            using (IQueryData dt = await Task.Run(() => ExecuteCommands.ExecuteCommandQuery(command, Connection)))
            {
                if (dt.QuerySuccessful)
                {
                    DatabaseTypeInfo ti = TypeInformationParser.GetTypeInfo(objectType);
                    foreach (IQueryRow row in dt)
                    {
                        if (objectType.IsSystemType())
                        {
                            toReturn.Add((ReturnType)Connection.CLRConverter.ConvertToType(row.GetDataForRowField(0), typeof(ReturnType)));
                        }
                        else
                        {
                            toReturn.Add((ReturnType)await BuildObjectAsync(row, ti));
                        }
                    }
                }
                return(toReturn);
            }
        }
예제 #12
0
 /// <summary>
 /// Creates an object and inits the primary key field
 /// </summary>
 /// <param name="item">The item.</param>
 /// <param name="key">The key.</param>
 /// <returns></returns>
 protected virtual object CreateObjectSetKey(Type item, object key)
 {
     return(CreateObjectSetKey(item, key, TypeInformationParser.GetTypeInfo(item)));
 }