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
|
|
|
import org.apache.commons.lang3.StringUtils; |
20
|
|
|
|
21
|
|
|
import lombok.extern.log4j.Log4j2; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* MS SQL Server implementation of the ISqlBuilder |
25
|
|
|
* |
26
|
|
|
* @author Armenak Grigoryan |
27
|
|
|
* @author Luis Marques |
28
|
|
|
*/ |
29
|
|
|
@Log4j2 |
30
|
|
|
public class MsSqlBuilder extends SqlBuilder { |
31
|
|
|
|
32
|
|
|
public MsSqlBuilder(DbConfig config) { |
33
|
|
|
super(config); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Uses TOP to limit the passed query. |
38
|
|
|
* |
39
|
|
|
* @param sqlString |
40
|
|
|
* @param limit |
41
|
|
|
* @return |
42
|
|
|
*/ |
43
|
|
|
@Override |
44
|
|
|
public String buildSelectWithLimit(final String sqlString, final int limit) { |
45
|
|
|
String query = sqlString.replaceFirst("(?i)(SELECT)(\\s+)", "$1 TOP " + limit + " $2"); |
46
|
|
|
log.debug("Query after adding limit: [{}]", query); |
47
|
|
|
return query; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Prefixes the schema name, followed by a "." character if set. |
52
|
|
|
* Additionally surrounds schema name (if set) and tableName with square |
53
|
|
|
* brackets, so the end result is either "[tableName]" or |
54
|
|
|
* "[schemaName].[tableName]". |
55
|
|
|
* |
56
|
|
|
* @param tableName |
57
|
|
|
* @return |
58
|
|
|
*/ |
59
|
|
|
@Override |
60
|
|
|
public String prefixSchema(final String tableName) { |
61
|
|
|
final String schema = config.getSchema(); |
62
|
|
|
if (StringUtils.isNotBlank(schema)) { |
63
|
|
|
return "[" + schema + "].[" + tableName + "]"; |
64
|
|
|
} |
65
|
|
|
return "[" + tableName + "]"; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|