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