001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.dao.jdbc.spring;
016    
017    import com.liferay.portal.kernel.dao.jdbc.MappingSqlQuery;
018    import com.liferay.portal.kernel.dao.jdbc.ParamSetter;
019    import com.liferay.portal.kernel.dao.jdbc.RowMapper;
020    
021    import java.sql.Connection;
022    import java.sql.PreparedStatement;
023    import java.sql.ResultSet;
024    import java.sql.SQLException;
025    
026    import java.util.ArrayList;
027    import java.util.Collections;
028    import java.util.List;
029    
030    import javax.sql.DataSource;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     * @author Shuyang Zhou
035     */
036    public class MappingSqlQueryImpl<T> implements MappingSqlQuery<T> {
037    
038            public MappingSqlQueryImpl(
039                    DataSource dataSource, String sql, RowMapper<T> rowMapper,
040                    ParamSetter... paramSetters) {
041    
042                    _dataSource = dataSource;
043                    _sql = sql;
044                    _rowMapper = rowMapper;
045                    _paramSetters = paramSetters;
046            }
047    
048            @Override
049            public List<T> execute(Object... params) throws SQLException {
050                    if (_paramSetters.length != params.length) {
051                            throw new IllegalArgumentException(
052                                    "Expected " + _paramSetters.length + " parameters instead of " +
053                                            params.length + " parameters");
054                    }
055    
056                    try (Connection connection = ConnectionUtil.getConnection(_dataSource);
057                            PreparedStatement preparedStatement = connection.prepareStatement(
058                                    _sql)) {
059    
060                            for (int i = 0; i < _paramSetters.length; i++) {
061                                    ParamSetter paramSetter = _paramSetters[i];
062    
063                                    paramSetter.set(preparedStatement, i + 1, params[i]);
064                            }
065    
066                            List<T> results = null;
067    
068                            try (ResultSet rs = preparedStatement.executeQuery()) {
069                                    while (rs.next()) {
070                                            if (results == null) {
071                                                    results = new ArrayList<>();
072                                            }
073    
074                                            results.add(_rowMapper.mapRow(rs));
075                                    }
076                            }
077    
078                            if (results == null) {
079                                    results = Collections.emptyList();
080                            }
081    
082                            return results;
083                    }
084            }
085    
086            private final DataSource _dataSource;
087            private final ParamSetter[] _paramSetters;
088            private final RowMapper<T> _rowMapper;
089            private final String _sql;
090    
091    }