ApplicationH2DAO.java 2.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright 2017, OpenSkywalking Organization All rights reserved.
 *
 * Licensed 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.
 *
 * Project repository: https://github.com/OpenSkywalking/skywalking
 */

19 20 21
package org.skywalking.apm.collector.agentregister.worker.application.dao;

import org.skywalking.apm.collector.client.h2.H2Client;
clevertension's avatar
clevertension 已提交
22
import org.skywalking.apm.collector.client.h2.H2ClientException;
23
import org.skywalking.apm.collector.storage.define.register.ApplicationDataDefine;
clevertension's avatar
clevertension 已提交
24
import org.skywalking.apm.collector.storage.define.register.ApplicationTable;
clevertension's avatar
clevertension 已提交
25
import org.skywalking.apm.collector.storage.h2.SqlBuilder;
26
import org.skywalking.apm.collector.storage.h2.dao.H2DAO;
clevertension's avatar
clevertension 已提交
27 28
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
29 30

/**
clevertension's avatar
clevertension 已提交
31
 * @author pengys5, clevertension
32 33
 */
public class ApplicationH2DAO extends H2DAO implements IApplicationDAO {
clevertension's avatar
clevertension 已提交
34
    private final Logger logger = LoggerFactory.getLogger(ApplicationH2DAO.class);
clevertension's avatar
clevertension 已提交
35
    private static final String INSERT_APPLICATION_SQL = "insert into {0}({1}, {2}) values(?, ?)";
36

clevertension's avatar
clevertension 已提交
37 38 39
    @Override
    public int getMaxApplicationId() {
        return getMaxId(ApplicationTable.TABLE, ApplicationTable.COLUMN_APPLICATION_ID);
40 41
    }

clevertension's avatar
clevertension 已提交
42 43 44
    @Override
    public int getMinApplicationId() {
        return getMinId(ApplicationTable.TABLE, ApplicationTable.COLUMN_APPLICATION_ID);
45 46
    }

clevertension's avatar
clevertension 已提交
47 48 49
    @Override
    public void save(ApplicationDataDefine.Application application) {
        H2Client client = getClient();
clevertension's avatar
clevertension 已提交
50
        String sql = SqlBuilder.buildSql(INSERT_APPLICATION_SQL, ApplicationTable.TABLE, ApplicationTable.COLUMN_APPLICATION_ID,
51
            ApplicationTable.COLUMN_APPLICATION_CODE);
clevertension's avatar
clevertension 已提交
52 53
        Object[] params = new Object[] {application.getApplicationId(), application.getApplicationCode()};
        try {
clevertension's avatar
clevertension 已提交
54
            client.execute(sql, params);
clevertension's avatar
clevertension 已提交
55 56 57
        } catch (H2ClientException e) {
            logger.error(e.getMessage(), e);
        }
58 59
    }
}