001/*
002 * The MIT License
003 *
004 * Copyright (c) 2012, Ninja Squad
005 *
006 * Permission is hereby granted, free of charge, to any person obtaining a copy
007 * of this software and associated documentation files (the "Software"), to deal
008 * in the Software without restriction, including without limitation the rights
009 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
010 * copies of the Software, and to permit persons to whom the Software is
011 * furnished to do so, subject to the following conditions:
012 *
013 * The above copyright notice and this permission notice shall be included in
014 * all copies or substantial portions of the Software.
015 *
016 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
017 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
018 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
019 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
020 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
021 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
022 * THE SOFTWARE.
023 */
024
025package com.ninja_squad.dbsetup.destination;
026
027import com.ninja_squad.dbsetup.util.Preconditions;
028
029import javax.annotation.Nonnull;
030import javax.annotation.concurrent.Immutable;
031import java.sql.Connection;
032import java.sql.DriverManager;
033import java.sql.SQLException;
034
035/**
036 * A destination which uses the {@link DriverManager} to get a connection
037 * @author JB Nizet
038 */
039@Immutable
040public final class DriverManagerDestination implements Destination {
041
042    private final String url;
043    private final String user;
044    private final String password;
045
046    /**
047     * Constructor
048     * @param url the URL of the database
049     * @param user the user used to get a connection
050     * @param password the password used to get a connection
051     */
052    public DriverManagerDestination(@Nonnull String url, String user, String password) {
053        Preconditions.checkNotNull(url, "url may not be null");
054        this.url = url;
055        this.user = user;
056        this.password = password;
057    }
058
059    /**
060     * Factory method creating a new DriverManagerDestination. This allows a more readable style than using the
061     * constructor:
062     *
063     * <pre>
064     *    DbSetup dbSetup = new DbSetup(DriverManagerDestination.with(url, user, password), operation);
065     * </pre>
066     *
067     * or, if this method is statically imported:
068     *
069     * <pre>
070     *    DbSetup dbSetup = new DbSetup(with(url, user, password), operation);
071     * </pre>
072     *
073     * instead of
074     *
075     * <pre>
076     *    DbSetup dbSetup = new DbSetup(new DriverManagerDestination(url, user, password), operation);
077     * </pre>
078     *
079     * @param url the URL of the database
080     * @param user the user used to get a connection
081     * @param password the password used to get a connection
082     */
083    public static DriverManagerDestination with(@Nonnull String url, String user, String password) {
084        return new DriverManagerDestination(url, user, password);
085    }
086
087    @Override
088    public Connection getConnection() throws SQLException {
089        return DriverManager.getConnection(url, user, password);
090    }
091
092    @Override
093    public String toString() {
094        return "DriverManagerDestination [url="
095               + url
096               + ", user="
097               + user
098               + ", password="
099               + password
100               + "]";
101    }
102
103    @Override
104    public int hashCode() {
105        final int prime = 31;
106        int result = 1;
107        result = prime * result + url.hashCode();
108        result = prime * result + ((password == null) ? 0 : password.hashCode());
109        result = prime * result + ((user == null) ? 0 : user.hashCode());
110        return result;
111    }
112
113    @Override
114    public boolean equals(Object obj) {
115        if (this == obj) {
116            return true;
117        }
118        if (obj == null) {
119            return false;
120        }
121        if (getClass() != obj.getClass()) {
122            return false;
123        }
124        DriverManagerDestination other = (DriverManagerDestination) obj;
125        if (password == null) {
126            if (other.password != null) {
127                return false;
128            }
129        }
130        else if (!password.equals(other.password)) {
131            return false;
132        }
133        if (!url.equals(other.url)) {
134            return false;
135        }
136        if (user == null) {
137            if (other.user != null) {
138                return false;
139            }
140        }
141        else if (!user.equals(other.user)) {
142            return false;
143        }
144        return true;
145    }
146}