001/*
002 * ============================================================================
003 * Copyright © 2002-2022 by Thomas Thrien.
004 * All Rights Reserved.
005 * ============================================================================
006 * Licensed to the public under the agreements of the GNU Lesser General Public
007 * License, version 3.0 (the "License"). You may obtain a copy of the License at
008 *
009 *      http://www.gnu.org/licenses/lgpl.html
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, WITHOUT
013 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014 * License for the specific language governing permissions and limitations
015 * under the License.
016 */
017
018package org.tquadrat.foundation.sql;
019
020import static org.apiguardian.api.API.Status.STABLE;
021import static org.tquadrat.foundation.lang.Objects.nonNull;
022import static org.tquadrat.foundation.lang.Objects.requireNotEmptyArgument;
023
024import java.sql.Connection;
025import java.sql.DriverManager;
026import java.sql.SQLException;
027import java.util.Properties;
028
029import org.apiguardian.api.API;
030import org.tquadrat.foundation.annotation.ClassVersion;
031import org.tquadrat.foundation.lang.Status;
032
033/**
034 *  An implementation of
035 *  {@link ConnectionProvider}
036 *  that will just call
037 *  {@link DriverManager#getConnection(String,Properties)}
038 *  to obtain a
039 *  {@link Connection}.
040 *
041 *  @version $Id: SimpleConnectionProvider.java 1030 2022-04-06 13:42:02Z tquadrat $
042 *  @extauthor Thomas Thrien - thomas.thrien@tquadrat.org
043 *  @UMLGraph.link
044 *  @since 0.1.0
045 */
046@ClassVersion( sourceVersion = "$Id: SimpleConnectionProvider.java 1030 2022-04-06 13:42:02Z tquadrat $" )
047@API( status = STABLE, since = "0.1.0" )
048public final class SimpleConnectionProvider implements ConnectionProvider
049{
050        /*------------*\
051    ====** Attributes **=======================================================
052        \*------------*/
053    /**
054     *  The JDBC URL.
055     */
056    private final String m_URL;
057
058    /**
059     *  The properties.
060     */
061    private final Properties m_Properties = new Properties();
062
063        /*--------------*\
064    ====** Constructors **=====================================================
065        \*--------------*/
066    /**
067     *  Creates a new instance of {@code SimpleConnectionProvider}.
068     *
069     *  @param  url The JDBC URL for the database with the calendar.
070     *  @param  properties  The properties.
071     */
072    @SuppressWarnings( "CollectionDeclaredAsConcreteClass" )
073    public SimpleConnectionProvider( final String url, final Properties properties )
074    {
075        m_URL = requireNotEmptyArgument( url, "url" );
076        if( nonNull( properties ) && !properties.isEmpty() ) m_Properties.putAll( properties );
077    }   //  SimpleConnectionProvider()
078
079        /*---------*\
080    ====** Methods **==========================================================
081        \*---------*/
082
083    /**
084     *  {@inheritDoc}
085     */
086    @Override
087    public Status<Connection,Throwable> getConnection()
088    {
089        Status<Connection,Throwable> retValue;
090        try
091        {
092            @SuppressWarnings( "CallToDriverManagerGetConnection" )
093            final var connection = DriverManager.getConnection( m_URL, m_Properties );
094            retValue = new Status<>( connection, null );
095        }
096        catch( final SQLException e )
097        {
098            retValue = new Status<>( null, e );
099        }
100
101        //---* Done *----------------------------------------------------------
102        return retValue;
103    }   //  getConnection()
104}
105//  class SimpleConnectionProvider
106
107/*
108 *  End of File
109 */