View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  
21  package org.waarp.openr66.dao.database;
22  
23  import org.waarp.common.database.exception.WaarpDatabaseSqlException;
24  import org.waarp.openr66.dao.MultipleMonitorDAO;
25  import org.waarp.openr66.pojo.MultipleMonitor;
26  
27  import java.sql.Connection;
28  import java.sql.ResultSet;
29  import java.sql.SQLException;
30  
31  /**
32   * Implementation of MultipleMonitorDAO for standard SQL databases
33   */
34  public class DBMultipleMonitorDAO extends StatementExecutor<MultipleMonitor>
35      implements MultipleMonitorDAO {
36  
37    protected static final String TABLE = "multiplemonitor";
38  
39    public static final String HOSTID_FIELD = "hostid";
40    public static final String COUNT_CONFIG_FIELD = "countconfig";
41    public static final String COUNT_HOST_FIELD = "counthost";
42    public static final String COUNT_RULE_FIELD = "countrule";
43  
44    protected static final String SQL_DELETE_ALL = "DELETE FROM " + TABLE;
45    protected static final String SQL_DELETE =
46        "DELETE FROM " + TABLE + WHERE + HOSTID_FIELD + PARAMETER;
47    protected static final String SQL_GET_ALL = "SELECT * FROM " + TABLE;
48    protected static final String SQL_COUNT_ALL = SQL_COUNT_ALL_PREFIX + TABLE;
49    protected static final String SQL_EXIST =
50        "SELECT 1 FROM " + TABLE + WHERE + HOSTID_FIELD + PARAMETER;
51    protected static final String SQL_SELECT =
52        "SELECT * FROM " + TABLE + WHERE + HOSTID_FIELD + PARAMETER;
53    protected static final String SQL_INSERT =
54        "INSERT INTO " + TABLE + " (" + HOSTID_FIELD + ", " + COUNT_CONFIG_FIELD +
55        ", " + COUNT_HOST_FIELD + ", " + COUNT_RULE_FIELD + ") VALUES (?,?,?,?)";
56  
57    protected static final String SQL_UPDATE =
58        "UPDATE " + TABLE + " SET " + HOSTID_FIELD + PARAMETER_COMMA +
59        COUNT_CONFIG_FIELD + PARAMETER_COMMA + COUNT_HOST_FIELD +
60        PARAMETER_COMMA + COUNT_RULE_FIELD + " = ? WHERE " + HOSTID_FIELD +
61        PARAMETER;
62  
63  
64    public DBMultipleMonitorDAO(final Connection con) {
65      super(con);
66    }
67  
68    @Override
69    protected final boolean isCachedEnable() {
70      return false;
71    }
72  
73    @Override
74    protected final String getId(final MultipleMonitor e1) {
75      return e1.getHostid();
76    }
77  
78    @Override
79    protected final String getTable() {
80      return TABLE;
81    }
82  
83    @Override
84    protected final String getSelectRequest() {
85      return SQL_SELECT;
86    }
87  
88    @Override
89    protected final String getGetAllRequest() {
90      return SQL_GET_ALL;
91    }
92  
93    @Override
94    protected final String getCountRequest() {
95      return SQL_COUNT_ALL;
96    }
97  
98    @Override
99    protected final String getExistRequest() {
100     return SQL_EXIST;
101   }
102 
103   @Override
104   protected final Object[] getInsertValues(
105       final MultipleMonitor multipleMonitor) {
106     return new Object[] {
107         multipleMonitor.getHostid(), multipleMonitor.getCountConfig(),
108         multipleMonitor.getCountHost(), multipleMonitor.getCountRule()
109     };
110   }
111 
112   @Override
113   protected final String getInsertRequest() {
114     return SQL_INSERT;
115   }
116 
117   @Override
118   protected final Object[] getUpdateValues(
119       final MultipleMonitor multipleMonitor) {
120     return new Object[] {
121         multipleMonitor.getHostid(), multipleMonitor.getCountConfig(),
122         multipleMonitor.getCountHost(), multipleMonitor.getCountRule(),
123         multipleMonitor.getHostid()
124     };
125   }
126 
127   @Override
128   protected final String getUpdateRequest() {
129     return SQL_UPDATE;
130   }
131 
132   @Override
133   protected final String getDeleteRequest() {
134     return SQL_DELETE;
135   }
136 
137   @Override
138   protected final String getDeleteAllRequest() {
139     return SQL_DELETE_ALL;
140   }
141 
142   @Override
143   public final MultipleMonitor getFromResultSet(final ResultSet set)
144       throws SQLException {
145     try {
146       return new MultipleMonitor(set.getString(HOSTID_FIELD),
147                                  set.getInt(COUNT_CONFIG_FIELD),
148                                  set.getInt(COUNT_HOST_FIELD),
149                                  set.getInt(COUNT_RULE_FIELD));
150     } catch (final WaarpDatabaseSqlException e) {
151       throw new SQLException(e);
152     }
153   }
154 
155   @Override
156   protected final boolean isDbTransfer() {
157     return false;
158   }
159 }