com.strider.datadefender.database.MsSqlDbConnection   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 30
ccs 0
cts 11
cp 0
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A connect() 0 3 1
A MsSqlDbConnection(DbConfig) 0 2 1
A getUrl() 0 9 3
1
/*
2
 * Copyright 2014, 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;
17
18
import java.sql.Connection;
19
import static java.sql.DriverManager.getConnection;
20
21
import com.strider.datadefender.DataDefenderException;
22
import com.strider.datadefender.DbConfig;
23
import org.apache.commons.lang3.StringUtils;
24
25
/**
26
 *
27
 * @author sdi
28
 */
29
public class MsSqlDbConnection extends DbConnection {
30
31
    public MsSqlDbConnection(DbConfig config) throws DataDefenderException {
32
        super(config);
33
    }
34
    
35
    /**
36
     * Establishes database connection
37
     *
38
     * @return Connection
39
     * @throws DatabaseException
40
     */
41
    @Override
42
    public Connection connect() throws DatabaseException {
43
        return doConnect(() -> getConnection(this.getUrl()));
44
    }
45
46
    /**
47
     * Get connection URL.
48
     * @return String
49
     */
50
    protected String getUrl() {
51
        StringBuilder sqlServerUrl = new StringBuilder(config.getUrl());
52
        if (!StringUtils.isBlank(config.getUsername())) {
53
            sqlServerUrl.append(";user=").append(config.getUsername());
54
        }
55
        if (!StringUtils.isBlank(config.getPassword())) {
56
            sqlServerUrl.append(";password=").append(config.getPassword());
57
        }
58
        return sqlServerUrl.toString();
59
    }
60
}
61