Пример #1
0
        public List <dynamic> GetBySqlCommand(Model model, System.Data.SqlClient.SqlCommand command)
        {
            using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(SqlHelper.GetSqlCon(this.sqlCon, model).ToString()))
            {
                con.Open();
                command.Connection = con;
                var table = SqlDataLoader.GetSqlData(command);

                var keyCol = SqlHelper.GetKeyCol(model);

                List <dynamic> items = new List <dynamic>();
                con.Close();
                if (table.Columns.Contains(keyCol) == false)
                {
                    throw new Exception("Can't Gerneration Query Because The Id Column Isn't Included!");
                }
                if (table.Rows.Count > 0)
                {
                    foreach (DataRow row in table.Rows)
                    {
                        IObjectProxy proxy = SqlDataLoader.getProxy(model, row[keyCol], this.sqlCon, this.ConFac);
                        SqlDataLoader.LoadSqlData(proxy, row, this.sqlCon, this.ConFac);
                        proxy.IsSave = SaveType.Exists;
                        items.Add(proxy);
                    }
                }
                return(items);
            }
        }
Пример #2
0
        public IObjectProxy GetDetail(Model model, object id, bool LoadDetail = true)
        {
            if (String.IsNullOrEmpty((id ?? "").ToString()))
            {
                return(null);
            }
            IObjectProxy proxy = SqlDataLoader.getProxy(model, id, this.sqlCon, this.ConFac);

            LoadDataDetail(model, id, LoadDetail, proxy);
            return(proxy);
        }
Пример #3
0
        internal List <IObjectProxy> LoadArrayProperty(
            IObjectProxy ParentProxy,
            Property property)
        {
            var sql = SqlHelper.GetSqlCon(property, this.sqlCon, (property.Model == null?ParentProxy.Model:property.Model));

            if (sql != null)
            {
                using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(
                           sql.ToString()
                           ))
                {
                    con.Open();
                    var    sqlCommand = SqlHelper.generatGetItemsCommand(property, ParentProxy.Model, ParentProxy.ID);
                    string itemKeyCol = SqlHelper.GetKeyCol(property.Model);
                    sqlCommand.Connection = con;

                    System.Diagnostics.Trace.WriteLine(sqlCommand.CommandText);
                    var table2 = SqlDataLoader.GetSqlData(sqlCommand);

                    List <IObjectProxy> result = new List <IObjectProxy>();
                    if (table2.Rows.Count > 0)
                    {
                        for (int i = 0; i < table2.Rows.Count; i++)
                        {
                            var itemproxy = SqlDataLoader.getProxy(property.Model, table2.Rows[i][itemKeyCol], this.sqlCon, this.ConFac);

                            if (itemproxy != null)
                            {
                                itemproxy.Owner = ParentProxy;
                                result.Add(itemproxy);
                                SqlDataLoader.LoadSqlData(itemproxy, table2.Rows[i], this.sqlCon, this.ConFac);

                                itemproxy.IsLoad = LoadType.Complete;
                            }
                        }
                    }
                    return(result);
                }
            }
            else
            {
                return(new List <IObjectProxy>());
            }
        }
Пример #4
0
        public void LoadDataDetail(Model model, object id, bool LoadDetail, IObjectProxy proxy)
        {
            if (proxy.IsLoad == LoadType.Null || proxy.IsLoad == LoadType.NoObj || proxy.IsLoad == LoadType.Partial)
            {
                proxy.IsLoad = LoadType.Complete;
                if (model.ModelType == ModelType.Enum)
                {
                    proxy.ID = id;
                }
                else
                {
                    using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(SqlHelper.GetSqlCon(this.sqlCon, proxy.Model).ToString()))
                    {
                        con.Open();

                        var command = SqlHelper.GenerateGetItemsCommand(model);
                        command.Connection = con;
                        var  keycols = SqlHelper.GetKeyCols(model);
                        int  j       = 0;
                        bool isAdd   = false;
                        for (int i = 0; i < keycols.Length; i++)// keycol in keycols)
                        {
                            var keycol = keycols[i];



                            var property = model.Properties.FirstOrDefault(p => p.DBName == keycol);
                            if (property != null)
                            {
                                Guid     guid;
                                DateTime date;
                                if (property.PropertyType == PropertyType.DateTime && DateTime.TryParse((id ?? "").ToString(), out date))
                                {
                                    isAdd = true;
                                }
                                else if (property.PropertyType == PropertyType.Time && DateTime.TryParseExact((id ?? "").ToString(), property.Format, null, System.Globalization.DateTimeStyles.NoCurrentDateDefault, out date))
                                {
                                    isAdd = true;
                                }
                                else if (property.PropertyType == PropertyType.Time && DateTime.TryParseExact((id ?? "").ToString(), property.Format, null, System.Globalization.DateTimeStyles.AllowInnerWhite, out date))
                                {
                                    isAdd = true;
                                }
                                else
                                if (PropertyTypeAdaper.GetPropertyType(id.GetType()) == property.PropertyType ||
                                    (property.PropertyType == PropertyType.IdentifyId && CheckDecimal(id)) || property.PropertyType == PropertyType.SerialNo)
                                {
                                    isAdd = true;
                                }
                                else if (
                                    property.PropertyType == PropertyType.Guid &&
                                    Guid.TryParse((id ?? "").ToString(), out guid) == true)
                                {
                                    isAdd = true;
                                }
                            }
                            else
                            if (keycols.Length == 1)
                            {
                                isAdd = true;
                            }
                            else
                            {
                                isAdd = CheckDecimal(id);
                            }

                            if (isAdd)
                            {
                                string pre = j == 0 ? " WHERE " : " OR ";
                                command.CommandText += pre + "  [" + keycol + "]=@" + keycol;

                                command.Parameters.Add(new System.Data.SqlClient.SqlParameter("@" + keycol, id));
                                j++;
                                isAdd = false;
                            }
                        }


                        var table = SqlDataLoader.GetSqlData(command);

                        if (table.Rows.Count > 0)
                        {
                            SqlDataLoader.LoadSqlData(proxy, table.Rows[0], this.sqlCon, this.ConFac);


                            if (LoadDetail)
                            {
                                proxy.IsLoad = LoadType.Complete;
                                foreach (var property in
                                         model.Properties.Where(p => p.IsArray == false &&
                                                                p.PropertyType == PropertyType.BusinessObject))
                                {
                                    ObjectProxyClass propertyProxy = proxy[property] as ObjectProxyClass;
                                    if ((propertyProxy != null &&
                                         propertyProxy.IsLoad != LoadType.Complete) ||
                                        (property.PropertySqlCon != null && property.PropertySqlCon.ToString() != propertyProxy.Con.ToString()))
                                    {
                                        proxy[property] = new dbContext(GetPropertyCon(property), this.ConFac).GetDetail(propertyProxy.Model, propertyProxy.ID, false);
                                    }
                                }
                            }
                        }
                        else
                        {
                            proxy.IsLoad = LoadType.NoObj;
                        }
                    }
                }
            }
        }