Esempio n. 1
0
        public int AddEntityJoinCustomKey(IAbstractDal pObj, string pEntityFrom, string pKey)
        {
            if (string.IsNullOrEmpty(pObj.Database))
            {
                pObj.Database = DatabaseDefault;
            }

            if (string.IsNullOrEmpty(pObj.Schema))
            {
                pObj.Schema = SchemaDefault;
            }

            if (pObj.Parent != null && pObj.Parent.EntityLeft)
            {
                return(AddEntityLeftJoinCustomKey(pObj, pEntityFrom, pKey));
            }
            else
            {
                pObj.EntityAlias = (++EntityCount).ToString();
                var lEntity = string.Concat("[", pObj.Database, "].[", pObj.Schema, "].[", pObj.Entity, "]", " as [", pObj.EntityAlias, "]");
                Entities.Append(string.Concat(" inner join ", lEntity, " on [", pEntityFrom, "].[", pKey, "] = [", pObj.EntityAlias, "].[", pObj.EntityKey, "] ")).AppendLine();

                if (pEntityFrom == "1" && Persistence)
                {
                    SaveAtributes.Add(pKey, string.Concat("@", pKey));
                }

                pObj.Query = this;

                return(EntityCount);
            }
        }
Esempio n. 2
0
        public int AddEntityLeftJoinCustomKey(IAbstractDal pObj, string pEntityFrom, string pKey, VO.CustomKeyHand pHand, bool pReadOnly)
        {
            string lLeftKey  = pObj.EntityKey;
            string lRightKey = pObj.EntityKey;

            switch (pHand)
            {
            case VO.CustomKeyHand.Left:
                lLeftKey = pKey;
                break;

            case VO.CustomKeyHand.Right:
                lRightKey = pKey;
                break;

            case VO.CustomKeyHand.Both:
                lLeftKey = lRightKey = pKey;
                break;
            }

            if (string.IsNullOrEmpty(pObj.Database))
            {
                pObj.Database = DatabaseDefault;
            }

            if (string.IsNullOrEmpty(pObj.Schema))
            {
                pObj.Schema = SchemaDefault;
            }

            pObj.EntityLeft  = true;
            pObj.EntityAlias = (++EntityCount).ToString();

            var lEntity = string.Concat("[", pObj.Database, "].[", pObj.Schema, "].[", pObj.Entity, "]", " as [", pObj.EntityAlias, "]");

            Entities.Append(string.Concat(" left join ", lEntity, " on [", pEntityFrom, "].[", lLeftKey, "] = [", pObj.EntityAlias, "].[", lRightKey, "] ")).AppendLine();

            if (pEntityFrom == "1" && (!pReadOnly && Persistence))
            {
                SaveAtributes.Add(pKey, string.Concat("@", pKey));
            }

            pObj.Query = this;

            return(EntityCount);
        }
Esempio n. 3
0
        public void BuildPersistence(IAbstractDal obj, bool isNew = true)
        {
            string lKey = string.Empty;

            if (string.IsNullOrEmpty(obj.Database))
            {
                obj.Database = DatabaseDefault;
            }

            if (string.IsNullOrEmpty(obj.Schema))
            {
                obj.Schema = SchemaDefault;
            }

            if (isNew)
            {
                lKey = string.Empty;
                lKey = SaveAtributes.Keys.DefaultIfEmpty(string.Empty).SingleOrDefault(x => x.EndsWith("_atualizacao_us_id"));

                if (!string.IsNullOrEmpty(lKey))
                {
                    SaveAtributes.Remove(lKey);
                }

                lKey = string.Empty;
                lKey = SaveAtributes.Keys.DefaultIfEmpty(string.Empty).SingleOrDefault(x => x.EndsWith("_cadastro_us_id"));

                if (string.IsNullOrEmpty(lKey))
                {
                    SaveAtributes.Add(string.Concat(obj.EntityKey.Remove(obj.EntityKey.LastIndexOf('_')), "_cadastro_us_id"),
                                      string.Concat("@", obj.EntityKey.Remove(obj.EntityKey.LastIndexOf('_')), "_cadastro_us_id"));
                }
            }
            else
            {
                lKey = string.Empty;
                lKey = SaveAtributes.Keys.DefaultIfEmpty(string.Empty).SingleOrDefault(x => x.EndsWith("_cadastro_us_id"));

                if (!string.IsNullOrEmpty(lKey))
                {
                    SaveAtributes.Remove(lKey);
                }

                lKey = string.Empty;
                lKey = SaveAtributes.Keys.DefaultIfEmpty(string.Empty).SingleOrDefault(x => x.EndsWith("_atualizacao_us_id"));

                if (string.IsNullOrEmpty(lKey))
                {
                    SaveAtributes.Add(string.Concat(obj.EntityKey.Remove(obj.EntityKey.LastIndexOf('_')), "_atualizacao_us_id"),
                                      string.Concat("@", obj.EntityKey.Remove(obj.EntityKey.LastIndexOf('_')), "_atualizacao_us_id"));
                }
            }

            if (!DisableDateFieldPersistence.Value)
            {
                if (isNew)
                {
                    lKey = string.Empty;
                    lKey = SaveAtributes.Keys.DefaultIfEmpty(string.Empty).SingleOrDefault(x => x.EndsWith("_cadastro"));

                    if (string.IsNullOrEmpty(lKey))
                    {
                        SaveAtributes.Add(string.Concat(obj.EntityKey.Remove(obj.EntityKey.LastIndexOf('_')), "_cadastro"), "getdate()");
                    }
                    else
                    {
                        SaveAtributes[lKey] = "getdate()";
                    }

                    lKey = string.Empty;
                    lKey = SaveAtributes.Keys.DefaultIfEmpty(string.Empty).SingleOrDefault(x => x.EndsWith("_atualizacao"));

                    if (string.IsNullOrEmpty(lKey))
                    {
                        SaveAtributes.Add(string.Concat(obj.EntityKey.Remove(obj.EntityKey.LastIndexOf('_')), "_atualizacao"), "getdate()");
                    }
                    else
                    {
                        SaveAtributes[lKey] = "getdate()";
                    }
                }
                else
                {
                    lKey = string.Empty;
                    lKey = SaveAtributes.Keys.DefaultIfEmpty(string.Empty).SingleOrDefault(x => x.EndsWith("_cadastro"));
                    if (!string.IsNullOrEmpty(lKey))
                    {
                        SaveAtributes.Remove(lKey);
                    }

                    lKey = string.Empty;
                    lKey = SaveAtributes.Keys.DefaultIfEmpty(string.Empty).SingleOrDefault(x => x.EndsWith("_atualizacao"));

                    if (string.IsNullOrEmpty(lKey))
                    {
                        SaveAtributes.Add(string.Concat(obj.EntityKey.Remove(obj.EntityKey.LastIndexOf('_')), "_atualizacao"), "getdate()");
                    }
                    else
                    {
                        SaveAtributes[lKey] = "getdate()";
                    }
                }
            }
            else
            {
                SaveAtributes.Keys.Where(k => k.EndsWith("_cadastro") || k.EndsWith("_atualizacao")).ToList().ForEach(attr => SaveAtributes.Remove(attr));
            }

            if (isNew)
            {
                var insert = "insert into [{0}].[{1}].[{2}] ({3}) values ({4});";

                if (obj.EntityKeyIdentity)
                {
                    SaveAtributes.Remove(obj.EntityKey);
                    insert = string.Concat(insert, " set @", obj.EntityKey, " = scope_identity();");
                }

                //Tratamento para os campos Chave, apenas considera chaves com a mesma nomenclatura do campo Id da tabela, para evitar conflitos entre chaves em ForeignKey
                lKey = obj.EntityKey.Replace("id", "key");
                if (SaveAtributes.Keys.Contains(lKey))
                {
                    insert = string.Concat("set @", lKey, " = convert(int,Rand() * power(10,8)); ", insert);
                }

                InsertQuery = string.Format(insert,
                                            obj.Database,
                                            obj.Schema,
                                            obj.Entity,
                                            string.Join(",", SaveAtributes.Keys),
                                            string.Join(",", SaveAtributes.Values));
            }
            else
            {
                //Remove o atributo ID da lista, pois na atualização este campo não deve sofrer alteração.
                SaveAtributes.Remove(obj.EntityKey);

                lKey = string.Empty;
                lKey = SaveAtributes.Keys.DefaultIfEmpty(string.Empty).SingleOrDefault(x => x.EndsWith("_ativo"));

                if (string.IsNullOrEmpty(lKey))
                {
                    SaveAtributes.Add(string.Format("{0}_ativo", obj.EntityKey.Remove(obj.EntityKey.LastIndexOf('_'))),
                                      string.Format("@{0}_ativo", obj.EntityKey.Remove(obj.EntityKey.LastIndexOf('_'))));
                }

                UpdateQuery = string.Format("update [{0}].[{1}].[{2}] set {3} where [{0}].[{1}].[{2}].[{4}] = @{4}",
                                            obj.Database,
                                            obj.Schema,
                                            obj.Entity,
                                            string.Join(",", SaveAtributes.Select(a => string.Format(" [{0}] = {1}", a.Key, a.Value))),
                                            obj.EntityKey);
            }
        }