Пример #1
0
        internal void PrepareCommandSelect(TableDefinitionAttribute tableDefinition,
                                           Where where, OrderBy order, IEnumerable <DbParameter> whereParameters, ref Command command)
        {
            #region Variáveis
            string sqlWhere = "";
            var    props    = model.GetType().GetProperties();

            //-------------------------------------------------------------------------
            // Salva os campos que já foram adicionados para não adicionar novamente
            //-------------------------------------------------------------------------
            Dictionary <string, SelectField> fields = new Dictionary <string, SelectField>();

            string commandText  = "";
            string selectFields = "";

            commandText = @"SELECT {selectFields} {from} {where} {limit}";

            if (where == null)
            {
                where = new Where();
            }
            #endregion

            #region Select

            foreach (MyHierarchicalType hType in Utilities.DbUtils.GetModels(model).OrderByDescending(o => o.Order))
            {
                //recuperar todos os selectedsFields
                IList <SelectField> selectList = GetSelectAux(hType.Type);

                if (selectList != null)
                {
                    foreach (SelectField item in selectList)
                    {
                        if (!fields.ContainsKey(item.FieldAlias))
                        {
                            selectFields += item.ToString() + ",";
                            fields.Add(item.FieldAlias, item);
                        }
                    }
                }
            }

            if (BeforePrepareSelectAction != null)
            {
                BeforePrepareSelectAction(command, selectFields);
            }

            selectFields = selectFields.Trim();

            if (!string.IsNullOrEmpty(selectFields))
            {
                selectFields = selectFields.Substring(0, selectFields.Length - 1);
            }

            #region where
            if (whereParameters != null && whereParameters.Count() > 0)
            {
                foreach (Parameter item in whereParameters)
                {
                    command.Parameters.Add(item);
                }
            }

            model.PrepareReader(command, where);

            if (where.Count > 0)
            {
                sqlWhere = " WHERE " + where.ToString();

                foreach (Parameter p in where.Parameters)
                {
                    command.Parameters.Add(p);
                }
            }
            #endregion

            #endregion

            #region Return
            commandText = commandText.Replace("{from}", GetFrom())
                          .Replace("{selectFields}", selectFields)
                          .Replace("{where}", sqlWhere)
                          .Replace("{limit}", where.Limit.ToString());

            command.CommandText = commandText;
            #endregion
        }