001/*
002 * Copyright 2015-2022 Transmogrify LLC, 2022-2026 Revetware LLC.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.pyranid;
018
019import org.jspecify.annotations.NonNull;
020import org.jspecify.annotations.Nullable;
021
022import javax.annotation.concurrent.ThreadSafe;
023import java.util.ArrayList;
024import java.util.Collections;
025import java.util.List;
026import java.util.Objects;
027
028import static java.lang.String.format;
029import static java.util.Objects.requireNonNull;
030
031/**
032 * Represents a SQL statement and an identifier for it.
033 *
034 * @author <a href="https://www.revetkn.com">Mark Allen</a>
035 * @since 2.0.0
036 */
037@ThreadSafe
038public final class Statement {
039        @NonNull
040        private final Object id;
041        @NonNull
042        private final String sql;
043        @Nullable
044        private final List<@Nullable String> parameterNames;
045
046        private Statement(@NonNull Object id,
047                                                                                @NonNull String sql,
048                                                                                @Nullable List<@Nullable String> parameterNames) {
049                requireNonNull(id);
050                requireNonNull(sql);
051
052                this.id = id;
053                this.sql = sql;
054                this.parameterNames = parameterNames == null ? null
055                                : Collections.unmodifiableList(new ArrayList<>(parameterNames));
056        }
057
058        /**
059         * Factory method for providing {@link Statement} instances.
060         *
061         * @param id  the statment's identifier
062         * @param sql the SQL being identified
063         * @return a statement instance
064         */
065        @NonNull
066        public static Statement of(@NonNull Object id,
067                                                                                                                 @NonNull String sql) {
068                requireNonNull(id);
069                requireNonNull(sql);
070
071                return new Statement(id, sql, null);
072        }
073
074        /** Named-slot metadata belongs to an execution, never to the parsed-SQL cache or public value identity. */
075        @NonNull
076        static Statement named(@NonNull Object id, @NonNull String sql,
077                        @NonNull List<@Nullable String> parameterNames) {
078                return new Statement(id, sql, requireNonNull(parameterNames));
079        }
080
081        @Nullable
082        List<@Nullable String> parameterNames() {
083                return this.parameterNames;
084        }
085
086        @Override
087        public int hashCode() {
088                return Objects.hash(getId(), getSql());
089        }
090
091        @Override
092        public boolean equals(Object object) {
093                if (this == object)
094                        return true;
095
096                if (!(object instanceof Statement))
097                        return false;
098
099                Statement statement = (Statement) object;
100
101                return Objects.equals(statement.getId(), getId())
102                                && Objects.equals(statement.getSql(), getSql());
103        }
104
105        @Override
106        @NonNull
107        public String toString() {
108                // Strip out newlines for more compact SQL representation
109                return format("%s{id=%s, sql=%s}", getClass().getSimpleName(),
110                                getId(), getSql().replaceAll("\n+", " ").trim());
111        }
112
113        @NonNull
114        public Object getId() {
115                return this.id;
116        }
117
118        @NonNull
119        public String getSql() {
120                return this.sql;
121        }
122}