public SqlQueryBuilder(DataEntity operationInput, EntityProperties filterColumns, Globals.QueryType queryType)
 {
     Parse(operationInput, filterColumns, queryType);
 }
 public SqlQueryBuilder(DataEntity operationInput, Globals.QueryType queryType)
 {
     Parse(operationInput, new EntityProperties(), queryType);
 }
 /// <summary>
 /// Constructor for building a select statement for using in CRUD operations
 /// with a command builder.
 /// </summary>
 /// <param name="operationInput"></param>
 /// <param name="lookupCondition"></param>
 /// <param name="queryType"></param>
 public SqlQueryBuilder(DataEntity operationInput, Expression lookupCondition, Globals.QueryType queryType)
 {
     Parse(operationInput, lookupCondition, queryType);
 }
        private string ParseQueryType(DataEntity input, Globals.QueryType queryType)
        {
            StringBuilder query;

            switch (queryType)
            {
                case Globals.QueryType.Insert:
                    query = new StringBuilder(CreateKeyword);
                    // add into clause
                    query.Append(ParseInsertQuery(input));
                    break;
                case Globals.QueryType.Delete:
                    query = new StringBuilder(DeleteKeyword);
                    // add from clause
                    query.Append(ParseFromClause(input.ObjectDefinitionFullName));
                    break;
                case Globals.QueryType.Update:
                    query = new StringBuilder(UpdateKeyword);
                    //add the set values
                    query.Append(ParseUpdateQuery(input));
                    break;
                case Globals.QueryType.Count:
                    query = new StringBuilder(SelectKeyword);
                    query.Append(" COUNT(*) ");
                    query.Append(ParseFromClause(input.ObjectDefinitionFullName));
                    break;
                case Globals.QueryType.Select:
                default:
                    query = new StringBuilder(SelectKeyword);
                    //retrieve the list of column names
                    List<string> columNames = input.Properties.Select(entityProperty => entityProperty.Key).ToList();
                    // add columns
                    query.Append(ParseColumns(columNames, input.ObjectDefinitionFullName));
                    // add from clause
                    query.Append(ParseFromClause(input.ObjectDefinitionFullName));
                    break;
            }

            return query.ToString();
        }
        /// <summary>
        /// Create a select string from the given table and columns.
        /// </summary>
        /// <param name="input"></param>
        /// <param name="lookupCondition"></param>
        /// <param name="queryType"></param>
        private void Parse(DataEntity input, Expression lookupCondition, Globals.QueryType queryType)
        {
            var query = new StringBuilder(ParseQueryType(input, queryType));

            // add where clause (optional))
            if (lookupCondition != null && queryType != Globals.QueryType.Insert)
            {
                var whereClause = new StringBuilder(" " + WhereKeyword + " ");
                ParseWhereClause(whereClause, lookupCondition);
                query.Append(whereClause);
            }

            _query = query.ToString();
        }
        private void Parse(DataEntity input, EntityProperties filterColumns, Globals.QueryType queryType)
        {
            var query = new StringBuilder();

            if (queryType == Globals.QueryType.Upsert)
            {
                var whereClause = ParseWhereClause(filterColumns);
                query.AppendFormat("{0} {3} ({1}{2})", IfKeyword, ParseQueryType(input, Globals.QueryType.Select), whereClause, ExistsKeyword);
                query.AppendFormat("{0}{1}{0}{5}{6}{0}{3}{0}{4}{0}{1}{0}{2}{0}{3}", Environment.NewLine, BeginKeyword,
                    ParseQueryType(input, Globals.QueryType.Insert),
                    EndKeyword, ElseKeyword, ParseQueryType(input, Globals.QueryType.Update), whereClause);
            }
            else
            {
                query.Append(ParseQueryType(input, queryType));
                query.Append(ParseWhereClause(filterColumns));
            }

            _query = query.ToString();
        }