001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.beanquery.sql;
018
019import java.util.*;
020
021/**
022 * A fully-rendered SQL statement: the text (with {@code ?} placeholders) plus its ordered bind values, and the
023 * selected column names in {@code SELECT} order.
024 *
025 * <p>
026 * {@link SqlBeanQueryContext} renders these purely (no {@code DataSource} needed), so callers &mdash; and tests
027 * &mdash; can inspect the generated SQL and binds before any execution.
028 *
029 * @since 10.0.0
030 */
031public final class SqlStatement {
032
033   private final String sql;
034   private final List<Object> binds;
035   private final List<String> columns;
036   private final boolean hasRegex;
037
038   SqlStatement(String sql, List<Object> binds, List<String> columns, boolean hasRegex) {
039      this.sql = sql;
040      this.binds = List.copyOf(binds);
041      this.columns = List.copyOf(columns);
042      this.hasRegex = hasRegex;
043   }
044
045   // Package-private: lets SqlBeanQuerySession.prepare() apply a bounded default statement timeout when this
046   // statement's search includes $regex and no explicit queryTimeout was configured.
047   boolean hasRegex() {
048      return hasRegex;
049   }
050
051   /**
052    * The SQL text, with a {@code ?} placeholder for each bind value.
053    *
054    * @return The SQL text.
055    */
056   public String sql() {
057      return sql;
058   }
059
060   /**
061    * The bind values in placeholder order.
062    *
063    * @return An unmodifiable list of bind values.
064    */
065   public List<Object> binds() {
066      return binds;
067   }
068
069   /**
070    * The selected column names in {@code SELECT} order (empty for a {@code COUNT} statement).
071    *
072    * @return An unmodifiable list of column names.
073    */
074   public List<String> columns() {
075      return columns;
076   }
077
078   @Override /* Object */
079   public String toString() {
080      return sql + " " + binds;
081   }
082}