Ejemplo n.º 1
0
        private void save(IObjectProxy proxy, System.Data.SqlClient.SqlTransaction trans = null, string tagetCol = null, object targetId = null)
        {
            var items = GetColsAndValues(proxy, trans, OperationType.Update);

            System.Data.SqlClient.SqlConnection con = null;
            if (trans != null)
            {
                con = trans.Connection;
            }
            else
            {
                con = new System.Data.SqlClient.SqlConnection(this.GetSqlCon(proxy.Model).ToString());
                con.Open();
            }

            var commnad = con.CreateCommand();

            if (trans != null)
            {
                commnad.Transaction = trans;
            }

            commnad.CommandText = string.Format("UPDATE {0} SET ", proxy.Model.DataTableName);
            var str = SqlHelper.GetKeyCol(proxy.Model);

            foreach (var key in items.Keys.Where(p => p != str))
            {
                commnad.CommandText += String.Format("\r\n {0}=@{0},", key);
                commnad.Parameters.AddWithValue(key, items[key]);
            }
            if (string.IsNullOrEmpty(tagetCol) == false)
            {
                if (commnad.Parameters.Contains(tagetCol) == false)
                {
                    commnad.Parameters.Add(new System.Data.SqlClient.SqlParameter(tagetCol, targetId));
                    commnad.CommandText += String.Format("\r\n {0}=@{0},", tagetCol);
                }
            }

            commnad.CommandText  = commnad.CommandText.Substring(0, commnad.CommandText.Length - 1);
            commnad.CommandText += string.Format(" WHERE {0}=@{0}", SqlHelper.GetKeyCol(proxy.Model));
            commnad.Parameters.Add(new System.Data.SqlClient.SqlParameter(SqlHelper.GetKeyCol(proxy.Model), proxy.OldId ??
                                                                          proxy.GetOld(proxy.Model.IdProperty)));
            commnad.ExecuteNonQuery();
            foreach (var property in proxy.Model.Properties.Where(
                         p => p.IsArray))
            {
                if (proxy.GetLoadType(property) == LoadType.Complete)
                {
                    dynamic proxyItems = proxy[property];
                    var     relation   = proxy.Model.Relations.First(p => p.Property == property);

                    if (relation.RelationType == RelationType.One2Many)
                    {
                        foreach (IObjectProxy itemProxy in proxyItems)
                        {
                            if (IsExits(itemProxy, trans, relation.TargetColumn, proxy.ID) == false)
                            {
                                Create(itemProxy, trans, relation.TargetColumn, proxy.ID);
                            }
                            else
                            {
                                save(itemProxy, trans, relation.TargetColumn, proxy.ID);
                            }
                        }
                        foreach (IObjectProxy deleteProxy in proxyItems.ToRemove)
                        {
                            delete(deleteProxy, trans);
                        }
                    }
                    else if (relation.RelationType == RelationType.Many2Many ||
                             relation.RelationType == RelationType.Recurve)
                    {
                        foreach (IObjectProxy item in proxyItems)
                        {
                            //if (IsExits(item, trans) == false)
                            //    Create(item, trans);
                            CreateComplexRelationBuild(proxy, trans, relation, item);
                        }

                        foreach (IObjectProxy item in proxyItems.ToRemove)
                        {
                            //if (IsExits(item, trans) == false)
                            //    Create(item, trans);
                            DeleteComplexRelationBuild(proxy, trans, relation, item);
                        }
                    }
                }
            }
        }