Ejemplo n.º 1
0
        public PgSqlCommand GetSqlCommand(TableDefinition tableDefinition)
        {
            var sqlCommand = new PgSqlCommand();

            sqlCommand.AppendCommand("SELECT ");

            if (_exists != null)
            {
                var existsCommand = _exists.GetSqlCommand(tableDefinition);
                sqlCommand.AppendCommand(existsCommand.Command, existsCommand.Parameters);
                return(sqlCommand);
            }

            if (_distinct != null)
            {
                if (_distinct is bool)
                {
                    sqlCommand.AppendCommand($"DISTINCT ");
                }
                else if (_distinct is string)
                {
                    sqlCommand.AppendCommand($"DISTINCT ON ({_distinct}) ");
                }
            }

            var selectComm = _selectList.GetSqlCommand(tableDefinition);

            sqlCommand.AppendCommandLine($"{selectComm.Command}", selectComm.Parameters);


            if (_from != null)
            {
                var fromComm = _from.GetSqlCommand(tableDefinition);
                sqlCommand.AppendCommandLine($"FROM {fromComm.Command}", fromComm.Parameters);
            }

            if (_where != null)
            {
                var whereComm = _where.GetSqlCommand(tableDefinition);
                if (!String.IsNullOrWhiteSpace(whereComm.Command))
                {
                    sqlCommand.AppendCommandLine($"WHERE {whereComm.Command}", whereComm.Parameters);
                }
            }

            if (_groupBy != null)
            {
                var groupByComm = _groupBy.GetSqlCommand(tableDefinition);
                sqlCommand.AppendCommandLine($"GROUP BY {groupByComm.Command}");
            }

            if (_orderBy != null)
            {
                var orderByComm = _orderBy.GetSqlCommand(tableDefinition);
                sqlCommand.AppendCommandLine($"ORDER BY {orderByComm.Command}");
            }

            if (_offset != null)
            {
                var offsetComm = _offset.GetSqlCommand(tableDefinition);
                sqlCommand.AppendCommandLine($"OFFSET {offsetComm.Command}");
            }


            if (_limit != null)
            {
                var limitComm = _limit.GetSqlCommand(tableDefinition);
                sqlCommand.AppendCommandLine($"LIMIT {limitComm.Command}");
            }

            if (_for != null)
            {
                var forComm = _for.GetSqlCommand(tableDefinition);
                sqlCommand.AppendCommandLine($"FOR {forComm.Command}");
            }

            return(sqlCommand);
        }