Esempio n. 1
0
        public static T Statement <T>(String statement, dynamic parameters = null, String connectionName = null)
        {
            using (var cmd = Command(connectionName))
            {
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = statement;

                if (parameters != null)
                {
                    Utility.AddParametersToCommand(parameters, cmd);
                }

                var type = typeof(T);

                using (var dr = new IndexedSqlDataReader(cmd.ExecuteReader(CommandBehavior.CloseConnection)))
                {
                    if (dr.Read())
                    {
                        if (type.IsValueType || type.Equals(typeof(String)) || type.Equals(typeof(DateTime)))
                        {
                            return(dr.Get <T>(0));
                        }

                        return(dr.ReadObject <T>(returnNullOnEmpty: true));
                    }
                    else
                    {
                        return(default(T));
                    }
                }
            }
        }
Esempio n. 2
0
        private IEnumerable <T> _fastPipeline()
        {
            var connectionName = Model.ConnectionName(_t);

            using (var cmd = Destrier.Execute.Command(connectionName))
            {
                cmd.CommandText = this.QueryBody;
                cmd.CommandType = System.Data.CommandType.Text;
                Destrier.Execute.Utility.AddParametersToCommand(_parameters, cmd, connectionName);
                using (var dr = new IndexedSqlDataReader(cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection), type: _t))
                {
                    while (dr.Read())
                    {
                        T newObject = (T)ReflectionHelper.GetNewObject(_t);
                        dr.DeepPopulate(newObject, _t);
                        yield return(newObject);
                    }
                }
            }
        }
Esempio n. 3
0
        private IEnumerable <T> _slowPipeline()
        {
            var list           = new List <T>();
            var connectionName = Model.ConnectionName(_t);

            using (var cmd = Destrier.Execute.Command(connectionName))
            {
                cmd.CommandText = this.QueryBody;
                cmd.CommandType = System.Data.CommandType.Text;
                Destrier.Execute.Utility.AddParametersToCommand(_parameters, cmd, connectionName);

                using (var dr = new IndexedSqlDataReader(cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection), type: _t))
                {
                    var objectLookups = new Dictionary <Type, Dictionary <Object, Object> >();
                    var parentDict    = new Dictionary <Object, Object>();
                    objectLookups.Add(_t, parentDict);

                    while (dr.Read())
                    {
                        var newObject = (T)ReflectionHelper.GetNewObject(_t);
                        dr.DeepPopulate(newObject, objectLookups: objectLookups, thisType: _t);
                        list.Add(newObject);
                    }

                    if (_builder.ChildCollections.Any())
                    {
                        foreach (var cm in _builder.ChildCollections.Where(cm => !cm.IsLazy))
                        {
                            var root   = cm.Root;
                            var parent = cm.Parent ?? cm.Root;
                            var referencedParentPrimaryKey = cm.ReferencedProperty;

                            dr.NextResult(cm.CollectionType);

                            if (!objectLookups.ContainsKey(cm.CollectionType))
                            {
                                objectLookups.Add(cm.CollectionType, new Dictionary <Object, Object>());
                            }

                            dr.ReadIntoParentCollection(cm.CollectionType, (reader, obj) =>
                            {
                                var pkValue         = referencedParentPrimaryKey.GetValue(obj);
                                var pkValueAsString = pkValue != null ? pkValue.ToString() : null;

                                var objPrimaryKeys = Model.ColumnsPrimaryKey(cm.CollectionType);

                                object objPrimaryKeyValue = Model.InstancePrimaryKeyValue(cm.CollectionType, obj);

                                object parentObj = null;
                                objectLookups[cm.DeclaringType].TryGetValue(pkValueAsString, out parentObj);

                                if (parentObj != null)
                                {
                                    var parentCollectionProperty = cm.Property;

                                    //new up the collection if it hasn't been delcared yet.
                                    if (parentCollectionProperty.GetValue(parentObj) == null)
                                    {
                                        parentCollectionProperty.SetValue(parentObj, ReflectionHelper.GetNewObject(cm.Type));
                                    }

                                    //set up lookup for the object
                                    Dictionary <Object, Object> objLookup = null;
                                    objectLookups.TryGetValue(cm.CollectionType, out objLookup);

                                    if (objLookup != null && objPrimaryKeyValue != null)
                                    {
                                        if (!objLookup.ContainsKey(objPrimaryKeyValue))
                                        {
                                            objLookup.Add(objPrimaryKeyValue, obj);
                                        }
                                        else
                                        {
                                            obj = objLookup[objPrimaryKeyValue];
                                        }
                                    }

                                    var collection = parentCollectionProperty.GetValue(parentObj);
                                    ((System.Collections.IList)collection).Add(obj);
                                }
                            }, populateFullResults: true, advanceToNextResultAfter: false, objectLookups: objectLookups);
                        }
                    }
                }
            }
            return(list);
        }