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;
020
021import javax.annotation.concurrent.ThreadSafe;
022import java.util.Objects;
023
024import static java.lang.String.format;
025import static java.util.Objects.requireNonNull;
026
027/**
028 * Represents a SQL statement and an identifier for it.
029 *
030 * @author <a href="https://www.revetkn.com">Mark Allen</a>
031 * @since 2.0.0
032 */
033@ThreadSafe
034public final class Statement {
035        @NonNull
036        private final Object id;
037        @NonNull
038        private final String sql;
039
040        private Statement(@NonNull Object id,
041                                                                                @NonNull String sql) {
042                requireNonNull(id);
043                requireNonNull(sql);
044
045                this.id = id;
046                this.sql = sql;
047        }
048
049        /**
050         * Factory method for providing {@link Statement} instances.
051         *
052         * @param id  the statment's identifier
053         * @param sql the SQL being identified
054         * @return a statement instance
055         */
056        @NonNull
057        public static Statement of(@NonNull Object id,
058                                                                                                                 @NonNull String sql) {
059                requireNonNull(id);
060                requireNonNull(sql);
061
062                return new Statement(id, sql);
063        }
064
065        @Override
066        public int hashCode() {
067                return Objects.hash(getId(), getSql());
068        }
069
070        @Override
071        public boolean equals(Object object) {
072                if (this == object)
073                        return true;
074
075                if (!(object instanceof Statement))
076                        return false;
077
078                Statement statement = (Statement) object;
079
080                return Objects.equals(statement.getId(), getId())
081                                && Objects.equals(statement.getSql(), getSql());
082        }
083
084        @Override
085        @NonNull
086        public String toString() {
087                // Strip out newlines for more compact SQL representation
088                return format("%s{id=%s, sql=%s}", getClass().getSimpleName(),
089                                getId(), getSql().replaceAll("\n+", " ").trim());
090        }
091
092        @NonNull
093        public Object getId() {
094                return this.id;
095        }
096
097        @NonNull
098        public String getSql() {
099                return this.sql;
100        }
101}