Passed
Branch master (6b380a)
by Vincent
13:11
created

create(Record)   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
/*
2
 * This file is part of Araknemu.
3
 *
4
 * Araknemu is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Lesser General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * Araknemu is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with Araknemu.  If not, see <https://www.gnu.org/licenses/>.
16
 *
17
 * Copyright (c) 2017-2019 Vincent Quatrevieux
18
 */
19
20
package fr.quatrevieux.araknemu.data.living.repository.implementation.sql;
21
22
import fr.quatrevieux.araknemu.core.dbal.executor.QueryExecutor;
23
import fr.quatrevieux.araknemu.core.dbal.repository.EntityNotFoundException;
24
import fr.quatrevieux.araknemu.core.dbal.repository.Record;
25
import fr.quatrevieux.araknemu.core.dbal.repository.RepositoryException;
26
import fr.quatrevieux.araknemu.core.dbal.repository.RepositoryUtils;
27
import fr.quatrevieux.araknemu.data.living.entity.account.AccountBank;
28
import fr.quatrevieux.araknemu.data.living.repository.account.AccountBankRepository;
29
30
import java.sql.ResultSet;
31
import java.sql.SQLException;
32
33
/**
34
 * SQL implementation of {@link AccountBankRepository}
35
 */
36
final class SqlAccountBankRepository implements AccountBankRepository {
37
    private final QueryExecutor executor;
38
    private final RepositoryUtils<AccountBank> utils;
39
40 1
    public SqlAccountBankRepository(QueryExecutor executor) {
41 1
        this.executor = executor;
42 1
        this.utils = new RepositoryUtils<>(this.executor, new Loader());
43 1
    }
44
45
    @Override
46
    public void initialize() throws RepositoryException {
47
        try {
48 1
            executor.query(
49
                "CREATE TABLE BANK (" +
50
                    "ACCOUNT_ID INTEGER," +
51
                    "SERVER_ID INTEGER," +
52
                    "BANK_KAMAS BIGINT," +
53
                    "PRIMARY KEY (ACCOUNT_ID, SERVER_ID)" +
54
                ")"
55
            );
56
        } catch (SQLException e) {
57
            throw new RepositoryException(e);
58 1
        }
59 1
    }
60
61
    @Override
62
    public void destroy() throws RepositoryException {
63
        try {
64 1
            executor.query("DROP TABLE BANK");
65
        } catch (SQLException e) {
66
            throw new RepositoryException(e);
67 1
        }
68 1
    }
69
70
    @Override
71
    public AccountBank add(AccountBank entity) {
72 1
        utils.update(
73
            "REPLACE INTO BANK (`ACCOUNT_ID`, `SERVER_ID`, `BANK_KAMAS`) VALUES (?, ?, ?)",
74
            rs -> {
75 1
                rs.setInt(1, entity.accountId());
76 1
                rs.setInt(2, entity.serverId());
77 1
                rs.setLong(3, entity.kamas());
78 1
            }
79
        );
80
81 1
        return entity;
82
    }
83
84
    @Override
85
    public void delete(AccountBank entity) {
86 1
        utils.update("DELETE FROM BANK WHERE ACCOUNT_ID = ? AND SERVER_ID = ?", rs -> {
87 1
            rs.setInt(1, entity.accountId());
88 1
            rs.setInt(2, entity.serverId());
89 1
        });
90 1
    }
91
92
    @Override
93
    public AccountBank get(AccountBank entity) throws RepositoryException {
94
        try {
95 1
            return utils.findOne("SELECT * FROM BANK WHERE ACCOUNT_ID = ? AND SERVER_ID = ?", rs -> {
96 1
                rs.setInt(1, entity.accountId());
97 1
                rs.setInt(2, entity.serverId());
98 1
            });
99 1
        } catch (EntityNotFoundException e) {
100 1
            entity.setKamas(0); // Ensure that kamas are set to zero
101
102 1
            return entity;
103
        }
104
    }
105
106
    @Override
107
    public boolean has(AccountBank entity) {
108 1
        return utils.aggregate("SELECT COUNT(*) FROM BANK WHERE ACCOUNT_ID = ? AND SERVER_ID = ?", rs -> {
109 1
            rs.setInt(1, entity.accountId());
110 1
            rs.setInt(2, entity.serverId());
111 1
        }) > 0;
112
    }
113
114
    private static class Loader implements RepositoryUtils.Loader<AccountBank> {
115
        @Override
116
        public AccountBank create(Record record) throws SQLException {
117 1
            return new AccountBank(
118 1
                record.getInt("ACCOUNT_ID"),
119 1
                record.getInt("SERVER_ID"),
120 1
                record.getNonNegativeLong("BANK_KAMAS")
121
            );
122
        }
123
124
        @Override
125
        public AccountBank fillKeys(AccountBank entity, ResultSet keys) {
126
            throw new UnsupportedOperationException();
127
        }
128
    }
129
}
130