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;
017
018import com.jfinal.plugin.activerecord.Db;
019import com.jfinal.plugin.activerecord.Record;
020import io.jboot.db.dbpro.JbootDbPro;
021import io.jboot.db.model.Columns;
022import io.jboot.utils.StrUtil;
023
024import java.util.List;
025
026
027public class JbootDb extends Db {
028
029    private static ThreadLocal<String> CONFIG_NAME_TL = new ThreadLocal<>();
030
031    public static String getCurrentConfigName() {
032        return CONFIG_NAME_TL.get();
033    }
034
035    public static void setCurrentConfigName(String configName) {
036        CONFIG_NAME_TL.set(configName);
037    }
038
039    public static void clearCurrentConfigName() {
040        CONFIG_NAME_TL.remove();
041    }
042
043    public static JbootDbPro use(String configName) {
044        return (JbootDbPro) Db.use(configName);
045    }
046
047    public static JbootDbPro use() {
048        String currentConfigName = getCurrentConfigName();
049        return StrUtil.isBlank(currentConfigName) ? (JbootDbPro) Db.use() : use(currentConfigName);
050    }
051
052
053    public static List<Record> find(String tableName, Columns columns) {
054        return find(tableName, columns, null, null);
055    }
056
057
058    public static List<Record> find(String tableName, Columns columns, String orderBy) {
059        return find(tableName, columns, orderBy, null);
060    }
061
062
063    public static List<Record> find(String tableName, Columns columns, Object limit) {
064        return find(tableName, columns, null, limit);
065    }
066
067
068    public static List<Record> find(String tableName, Columns columns, String orderBy, Object limit) {
069        return use().find(tableName, columns, orderBy, limit);
070    }
071
072
073    public static Record findFirst(String tableName, Columns columns) {
074        return findFirst(tableName, columns, null);
075    }
076
077
078    public static Record findFirst(String tableName, Columns columns, String orderBy) {
079        final List<Record> records = use().find(tableName, columns, orderBy, 1);
080        return records != null && !records.isEmpty() ? records.get(0) : null;
081    }
082
083
084    public static int delete(String tableName, Columns columns) {
085        return use().delete(tableName, columns);
086    }
087
088
089}