SqlBuilder.java 5.4 KB
Newer Older
K
KomachiSion 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

18
package info.avalon566.shardingscaling.core.execute.writer;
A
avalon566 已提交
19 20 21 22

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
K
KomachiSion 已提交
23

A
avalon566 已提交
24
import javax.sql.DataSource;
K
KomachiSion 已提交
25
import java.util.List;
A
avalon566 已提交
26 27
import java.util.concurrent.ExecutionException;

28 29
import info.avalon566.shardingscaling.core.execute.metadata.ColumnMetaData;
import info.avalon566.shardingscaling.core.execute.util.DbMetaDataUtil;
30

A
avalon566 已提交
31
/**
A
avalon566 已提交
32
 * Sql builder.
K
KomachiSion 已提交
33
 *
A
avalon566 已提交
34 35 36
 * @author avalon566
 */
public final class SqlBuilder {
K
KomachiSion 已提交
37
    
A
avalon566 已提交
38
    private static final String LEFT_ESCAPE_QUOTE = "`";
A
avalon566 已提交
39

A
avalon566 已提交
40
    private static final String RIGHT_ESCAPE_QUOTE = "`";
A
avalon566 已提交
41

A
avalon566 已提交
42
    private static final String INSERT_SQL_CACHE_KEY_PREFIX = "INSERT_";
A
avalon566 已提交
43

A
avalon566 已提交
44
    private static final String UPDATE_SQL_CACHE_KEY_PREFIX = "UPDATE_";
A
avalon566 已提交
45

A
avalon566 已提交
46 47 48
    private static final String DELETE_SQL_CACHE_KEY_PREFIX = "DELETE_";

    private final LoadingCache<String, String> sqlCache;
A
avalon566 已提交
49

A
avalon566 已提交
50 51
    private final DbMetaDataUtil dbMetaDataUtil;

A
avalon566 已提交
52 53
    public SqlBuilder(final DataSource dataSource) {
        this.dbMetaDataUtil = new DbMetaDataUtil(dataSource);
A
avalon566 已提交
54 55 56 57
        sqlCache = CacheBuilder.newBuilder()
                .maximumSize(64)
                .build(new CacheLoader<String, String>() {
                    @Override
A
avalon566 已提交
58
                    public String load(final String key) {
A
avalon566 已提交
59 60 61 62 63 64 65 66 67 68 69
                        if (key.startsWith(INSERT_SQL_CACHE_KEY_PREFIX)) {
                            return buildInsertSqlInternal(key.replaceFirst(INSERT_SQL_CACHE_KEY_PREFIX, ""));
                        } else if (key.startsWith(UPDATE_SQL_CACHE_KEY_PREFIX)) {
                            return buildUpdateSqlInternal(key.replaceFirst(UPDATE_SQL_CACHE_KEY_PREFIX, ""));
                        } else {
                            return buildDeleteSqlInternal(key.replaceFirst(DELETE_SQL_CACHE_KEY_PREFIX, ""));
                        }
                    }
                });
    }

A
avalon566 已提交
70 71
    /**
     * Build insert sql.
K
KomachiSion 已提交
72
     *
A
avalon566 已提交
73 74 75 76
     * @param tableName table name
     * @return sql
     */
    public String buildInsertSql(final String tableName) {
A
avalon566 已提交
77 78 79 80 81 82 83
        try {
            return sqlCache.get(INSERT_SQL_CACHE_KEY_PREFIX + tableName);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
    }

A
avalon566 已提交
84 85
    /**
     * Build update sql.
K
KomachiSion 已提交
86
     *
A
avalon566 已提交
87 88 89 90
     * @param tableName table name
     * @return sql
     */
    public String buildUpdateSql(final String tableName) {
A
avalon566 已提交
91 92 93 94 95 96 97
        try {
            return sqlCache.get(UPDATE_SQL_CACHE_KEY_PREFIX + tableName);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
    }

A
avalon566 已提交
98 99
    /**
     * Build delete sql.
K
KomachiSion 已提交
100
     *
A
avalon566 已提交
101 102 103 104
     * @param tableName table name
     * @return sql
     */
    public String buildDeleteSql(final String tableName) {
A
avalon566 已提交
105 106 107 108 109 110 111
        try {
            return sqlCache.get(DELETE_SQL_CACHE_KEY_PREFIX + tableName);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
    }

A
avalon566 已提交
112
    private String buildInsertSqlInternal(final String tableName) {
K
KomachiSion 已提交
113 114 115 116 117 118
        List<ColumnMetaData> metaData = dbMetaDataUtil.getColumnNames(tableName);
        StringBuilder columns = new StringBuilder();
        StringBuilder holder = new StringBuilder();
        for (ColumnMetaData each : metaData) {
            columns.append(String.format("%s%s%s,", LEFT_ESCAPE_QUOTE, each.getColumnName(), RIGHT_ESCAPE_QUOTE));
            holder.append("?,");
A
avalon566 已提交
119
        }
K
KomachiSion 已提交
120 121 122
        columns.setLength(columns.length() - 1);
        holder.setLength(holder.length() - 1);
        return String.format("INSERT INTO %s%s%s(%s) VALUES(%s)", LEFT_ESCAPE_QUOTE, tableName, RIGHT_ESCAPE_QUOTE, columns.toString(), holder.toString());
A
avalon566 已提交
123 124
    }

A
avalon566 已提交
125
    private String buildDeleteSqlInternal(final String tableName) {
K
KomachiSion 已提交
126 127 128 129
        List<String> primaryKeys = dbMetaDataUtil.getPrimaryKeys(tableName);
        StringBuilder where = new StringBuilder();
        for (String each : primaryKeys) {
            where.append(String.format("%s%s%s = ?,", LEFT_ESCAPE_QUOTE, each, RIGHT_ESCAPE_QUOTE));
A
avalon566 已提交
130
        }
K
KomachiSion 已提交
131 132
        where.setLength(where.length() - 1);
        return String.format("DELETE FROM %s%s%s WHERE %s", LEFT_ESCAPE_QUOTE, tableName, RIGHT_ESCAPE_QUOTE, where.toString());
A
avalon566 已提交
133 134
    }

A
avalon566 已提交
135
    private String buildUpdateSqlInternal(final String tableName) {
K
KomachiSion 已提交
136 137 138 139
        List<String> primaryKeys = dbMetaDataUtil.getPrimaryKeys(tableName);
        StringBuilder where = new StringBuilder();
        for (String each : primaryKeys) {
            where.append(String.format("%s%s%s = ?,", LEFT_ESCAPE_QUOTE, each, RIGHT_ESCAPE_QUOTE));
A
avalon566 已提交
140
        }
K
KomachiSion 已提交
141 142
        where.setLength(where.length() - 1);
        return String.format("UPDATE %s%s%s SET %%s WHERE %s", LEFT_ESCAPE_QUOTE, tableName, RIGHT_ESCAPE_QUOTE, where.toString());
A
avalon566 已提交
143 144
    }
}