001/**
002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com).
003 * <p>
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 * <p>
008 * http://www.apache.org/licenses/LICENSE-2.0
009 * <p>
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 */
016package io.jboot.db.dbpro;
017
018import com.jfinal.kit.StrKit;
019import com.jfinal.plugin.activerecord.*;
020import com.jfinal.plugin.activerecord.dialect.Dialect;
021import io.jboot.db.SqlDebugger;
022import io.jboot.db.dialect.JbootDialect;
023import io.jboot.db.model.Columns;
024
025import java.sql.*;
026import java.util.ArrayList;
027import java.util.List;
028import java.util.function.Function;
029
030/**
031 * @author Michael Yang 杨福海 (fuhai999@gmail.com)
032 * @version V1.0
033 */
034public class JbootDbPro extends DbPro {
035    private static final String[] NO_PRIMARY_KEYS = new String[0];
036
037    public JbootDbPro() {
038    }
039
040
041    public JbootDbPro(String configName) {
042        super(configName);
043    }
044
045
046    @Override
047    public List<Record> find(Config config, Connection conn, String sql, Object... paras) throws SQLException {
048        return SqlDebugger.run(() -> super.find(config, conn, sql, paras), config, sql, paras);
049    }
050
051
052    @Override
053    public <T> List<T> query(Config config, Connection conn, String sql, Object... paras) throws SQLException {
054        return SqlDebugger.run(() -> super.query(config, conn, sql, paras), config, sql, paras);
055    }
056
057
058    @Override
059    public int update(Config config, Connection conn, String sql, Object... paras) throws SQLException {
060        return SqlDebugger.run(() -> super.update(config, conn, sql, paras), config, sql, paras);
061    }
062
063
064    @Override
065    protected boolean save(Config config, Connection conn, String tableName, String primaryKey, Record record) throws SQLException {
066        String[] pKeys = StrKit.notBlank(primaryKey) ? primaryKey.split(",") : NO_PRIMARY_KEYS;
067        List<Object> paras = new ArrayList<Object>();
068        StringBuilder sql = new StringBuilder();
069
070        Dialect dialect = config.getDialect();
071
072        dialect.forDbSave(tableName, pKeys, record, sql, paras);
073
074        //add sql debug support
075        return SqlDebugger.run(() -> {
076            try (PreparedStatement pst =
077                         dialect.isOracle() ?
078                                 conn.prepareStatement(sql.toString(), pKeys) :
079                                 conn.prepareStatement(sql.toString(), Statement.RETURN_GENERATED_KEYS)) {
080                dialect.fillStatement(pst, paras);
081                int result = pst.executeUpdate();
082                dialect.getRecordGeneratedKey(pst, record, pKeys);
083                return result >= 1;
084            }
085        }, config, sql.toString(), paras.toArray());
086    }
087
088
089    public List<Record> find(String tableName, Columns columns) {
090        return find(tableName, columns, null, null);
091    }
092
093
094    public List<Record> find(String tableName, Columns columns, String orderBy) {
095        return find(tableName, columns, orderBy, null);
096    }
097
098
099    public List<Record> find(String tableName, Columns columns, Object limit) {
100        return find(tableName, columns, null, limit);
101    }
102
103
104    public List<Record> find(String tableName, Columns columns, String orderBy, Object limit) {
105        JbootDialect dialect = (JbootDialect) getConfig().getDialect();
106        String sql = dialect.forFindByColumns(null, null, tableName, "*", columns.getList(), orderBy, limit);
107        return columns.isEmpty() ? find(sql) : find(sql, columns.getValueArray());
108    }
109
110
111    public int delete(String tableName, Columns columns) {
112        JbootDialect dialect = (JbootDialect) getConfig().getDialect();
113        String sql = dialect.forDeleteByColumns(null, null, tableName, columns.getList());
114        return columns.isEmpty() ? delete(sql) : delete(sql, columns.getValueArray());
115    }
116
117    @Override
118    public void each(Function<Record, Boolean> func, String sql, Object... paras) {
119        //Connection conn = null;
120        //              try {
121        //                      conn = config.getConnection();
122        //
123        //                      try (PreparedStatement pst = conn.prepareStatement(sql)) {
124        //                              config.dialect.fillStatement(pst, paras);
125        //                              ResultSet rs = pst.executeQuery();
126        //                              config.dialect.eachRecord(config, rs, func);
127        //                              DbKit.close(rs);
128        //                      }
129        //
130        //              } catch (Exception e) {
131        //                      throw new ActiveRecordException(e);
132        //              } finally {
133        //                      config.close(conn);
134        //              }
135
136        Dialect dialect = config.getDialect();
137        try {
138            SqlDebugger.run(() -> {
139                try (Connection conn = config.getConnection();
140                     PreparedStatement pst = conn.prepareStatement(sql)) {
141
142                    dialect.fillStatement(pst, paras);
143
144                    try (ResultSet rs = pst.executeQuery();) {
145                        dialect.eachRecord(config, rs, func);
146                    }
147                }
148                return true;
149            }, config, sql, paras);
150        } catch (Exception e) {
151            throw new ActiveRecordException(e);
152        }
153    }
154}