1
|
|
|
/* |
2
|
|
|
* Copyright 2015, Armenak Grigoryan, and individual contributors as indicated |
3
|
|
|
* by the @authors tag. See the copyright.txt in the distribution for a |
4
|
|
|
* full listing of individual contributors. |
5
|
|
|
* |
6
|
|
|
* This is free software; you can redistribute it and/or modify it |
7
|
|
|
* under the terms of the GNU Lesser General Public License as |
8
|
|
|
* published by the Free Software Foundation; either version 2.1 of |
9
|
|
|
* the License, or (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* This software is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14
|
|
|
* Lesser General Public License for more details. |
15
|
|
|
*/ |
16
|
|
|
package com.strider.datadefender.database.sqlbuilder; |
17
|
|
|
|
18
|
|
|
import com.strider.datadefender.DbConfig; |
19
|
|
|
|
20
|
|
|
import org.apache.commons.lang3.StringUtils; |
21
|
|
|
|
22
|
|
|
import lombok.extern.log4j.Log4j2; |
23
|
|
|
import lombok.RequiredArgsConstructor; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Provides 'default' implementation which can be overridden. |
27
|
|
|
* @author Akira Matsuo |
28
|
|
|
*/ |
29
|
|
|
@Log4j2 |
30
|
|
|
@RequiredArgsConstructor |
31
|
|
|
public class SqlBuilder implements ISqlBuilder { |
32
|
|
|
|
33
|
|
|
protected final DbConfig config; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Appends "LIMIT {n}" to the end of the query. |
37
|
|
|
* |
38
|
|
|
* @param sqlString |
39
|
|
|
* @param limit |
40
|
|
|
* @return |
41
|
|
|
*/ |
42
|
2 |
|
@Override |
43
|
|
|
public String buildSelectWithLimit(final String sqlString, final int limit) { |
44
|
2 |
|
final StringBuilder sql = new StringBuilder(sqlString); |
45
|
4 |
|
if (limit != 0) { |
46
|
1 |
|
sql.append(" LIMIT ").append(limit); |
47
|
|
|
} |
48
|
2 |
|
log.debug("Query after adding limit: [{}]", sql); |
49
|
2 |
|
return sql.toString(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Prepends the schema name followed by a single "." to the passed tableName |
54
|
|
|
* if a schema name is configured. |
55
|
|
|
* |
56
|
|
|
* @param tableName |
57
|
|
|
* @return |
58
|
|
|
*/ |
59
|
4 |
|
@Override |
60
|
|
|
public String prefixSchema(final String tableName) { |
61
|
4 |
|
final String schema = config.getSchema(); |
62
|
8 |
|
if (StringUtils.isNotBlank(schema)) { |
63
|
1 |
|
return schema + "." + tableName; |
64
|
|
|
} |
65
|
3 |
|
return tableName; |
66
|
|
|
} |
67
|
|
|
} |