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.LimitDAO;
25  import org.waarp.openr66.pojo.Limit;
26  import org.waarp.openr66.pojo.UpdatedInfo;
27  
28  import java.sql.Connection;
29  import java.sql.ResultSet;
30  import java.sql.SQLException;
31  
32  /**
33   * Implementation of LimitDAO for standard SQL databases
34   */
35  public class DBLimitDAO extends StatementExecutor<Limit> implements LimitDAO {
36  
37    protected static final String TABLE = "configuration";
38  
39    public static final String HOSTID_FIELD = "hostid";
40    public static final String READ_GLOBAL_LIMIT_FIELD = "readgloballimit";
41    public static final String WRITE_GLOBAL_LIMIT_FIELD = "writegloballimit";
42    public static final String READ_SESSION_LIMIT_FIELD = "readsessionlimit";
43    public static final String WRITE_SESSION_LIMIT_FIELD = "writesessionlimit";
44    public static final String DELAY_LIMIT_FIELD = "delaylimit";
45    public static final String UPDATED_INFO_FIELD = "updatedinfo";
46  
47    protected static final String SQL_DELETE_ALL = "DELETE FROM " + TABLE;
48    protected static final String SQL_DELETE =
49        "DELETE FROM " + TABLE + WHERE + HOSTID_FIELD + PARAMETER;
50    protected static final String SQL_GET_ALL = "SELECT * FROM " + TABLE;
51    protected static final String SQL_COUNT_ALL = SQL_COUNT_ALL_PREFIX + TABLE;
52    protected static final String SQL_EXIST =
53        "SELECT 1 FROM " + TABLE + WHERE + HOSTID_FIELD + PARAMETER;
54    protected static final String SQL_SELECT =
55        "SELECT * FROM " + TABLE + WHERE + HOSTID_FIELD + PARAMETER;
56    protected static final String SQL_INSERT =
57        "INSERT INTO " + TABLE + " (" + HOSTID_FIELD + ", " +
58        READ_GLOBAL_LIMIT_FIELD + ", " + WRITE_GLOBAL_LIMIT_FIELD + ", " +
59        READ_SESSION_LIMIT_FIELD + ", " + WRITE_SESSION_LIMIT_FIELD + ", " +
60        DELAY_LIMIT_FIELD + ", " + UPDATED_INFO_FIELD +
61        ") VALUES (?,?,?,?,?,?,?)";
62  
63    protected static final String SQL_UPDATE =
64        "UPDATE " + TABLE + " SET " + HOSTID_FIELD + PARAMETER_COMMA +
65        READ_GLOBAL_LIMIT_FIELD + PARAMETER_COMMA + WRITE_GLOBAL_LIMIT_FIELD +
66        PARAMETER_COMMA + READ_SESSION_LIMIT_FIELD + PARAMETER_COMMA +
67        WRITE_SESSION_LIMIT_FIELD + PARAMETER_COMMA + DELAY_LIMIT_FIELD +
68        PARAMETER_COMMA + UPDATED_INFO_FIELD + " = ? WHERE " + HOSTID_FIELD +
69        PARAMETER;
70  
71    public DBLimitDAO(final Connection con) {
72      super(con);
73    }
74  
75    @Override
76    protected final boolean isCachedEnable() {
77      return false;
78    }
79  
80    @Override
81    protected final String getId(final Limit e1) {
82      return e1.getHostid();
83    }
84  
85    @Override
86    protected final String getTable() {
87      return TABLE;
88    }
89  
90    @Override
91    protected final String getSelectRequest() {
92      return SQL_SELECT;
93    }
94  
95    @Override
96    protected final String getGetAllRequest() {
97      return SQL_GET_ALL;
98    }
99  
100   @Override
101   protected final String getCountRequest() {
102     return SQL_COUNT_ALL;
103   }
104 
105   @Override
106   protected final String getExistRequest() {
107     return SQL_EXIST;
108   }
109 
110   @Override
111   protected final Object[] getInsertValues(final Limit limit) {
112     return new Object[] {
113         limit.getHostid(), limit.getReadGlobalLimit(),
114         limit.getWriteGlobalLimit(), limit.getReadSessionLimit(),
115         limit.getWriteSessionLimit(), limit.getDelayLimit(),
116         limit.getUpdatedInfo().ordinal()
117     };
118   }
119 
120   @Override
121   protected final String getInsertRequest() {
122     return SQL_INSERT;
123   }
124 
125   @Override
126   protected final Object[] getUpdateValues(final Limit limit) {
127     return new Object[] {
128         limit.getHostid(), limit.getReadGlobalLimit(),
129         limit.getWriteGlobalLimit(), limit.getReadSessionLimit(),
130         limit.getWriteSessionLimit(), limit.getDelayLimit(),
131         limit.getUpdatedInfo().ordinal(), limit.getHostid()
132     };
133   }
134 
135   @Override
136   protected final String getUpdateRequest() {
137     return SQL_UPDATE;
138   }
139 
140   @Override
141   protected final String getDeleteRequest() {
142     return SQL_DELETE;
143   }
144 
145   @Override
146   protected final String getDeleteAllRequest() {
147     return SQL_DELETE_ALL;
148   }
149 
150   @Override
151   public final Limit getFromResultSet(final ResultSet set) throws SQLException {
152     try {
153       return new Limit(set.getString(HOSTID_FIELD),
154                        set.getLong(DELAY_LIMIT_FIELD),
155                        set.getLong(READ_GLOBAL_LIMIT_FIELD),
156                        set.getLong(WRITE_GLOBAL_LIMIT_FIELD),
157                        set.getLong(READ_SESSION_LIMIT_FIELD),
158                        set.getLong(WRITE_SESSION_LIMIT_FIELD),
159                        UpdatedInfo.valueOf(set.getInt(UPDATED_INFO_FIELD)));
160     } catch (final WaarpDatabaseSqlException e) {
161       throw new SQLException(e);
162     }
163   }
164 
165   @Override
166   protected final boolean isDbTransfer() {
167     return false;
168   }
169 }