]> git.mxchange.org Git - jcore.git/blob - src/org/mxchange/jcore/database/result/DatabaseResult.java
e541bfa7f7c90ef16f4140d76c4b3f94caf4492c
[jcore.git] / src / org / mxchange / jcore / database / result / DatabaseResult.java
1 /*
2  * Copyright (C) 2015 Roland Haeder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU 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  * This program 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 General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jcore.database.result;
18
19 import java.sql.PreparedStatement;
20 import java.sql.SQLException;
21 import java.sql.SQLWarning;
22 import java.text.MessageFormat;
23 import java.util.Iterator;
24 import java.util.SortedSet;
25 import java.util.TreeSet;
26 import org.mxchange.jcore.BaseFrameworkSystem;
27 import org.mxchange.jcore.database.storage.Storeable;
28
29 /**
30  * A database result
31  * @author Roland Haeder
32  */
33 public class DatabaseResult extends BaseFrameworkSystem implements Result<Storeable> {
34         /**
35          * Set of all found Storeable instances
36          */
37         private final SortedSet<Storeable> result;
38
39         /**
40          * Status value from previous executeUpdate() call
41          */
42         private int status;
43
44         /**
45          * SQLWarning instance
46          */
47         private SQLWarning warnings;
48
49         /**
50          * Default constructor
51          */
52         public DatabaseResult () {
53                 // Trace message
54                 this.getLogger().trace("CALLED!"); //NOI18N
55
56                 // Init set here
57                 this.result = new TreeSet<>();
58         }
59
60         /**
61          * A constructor that accepts a status integer from previous executeUpdate()
62          * call and a the prepared statement instance.
63          *
64          * @param status Status code
65          * @param statement A PreparedStatement instance
66          * @throws java.sql.SQLException If any SQL error occurs
67          */
68         public DatabaseResult (final int status, final PreparedStatement statement) throws SQLException {
69                 // Call parent constructor
70                 this();
71
72                 // Trace message
73                 this.getLogger().trace(MessageFormat.format("status={0},statement={1} - CALLED!", status, statement)); //NOI18N
74
75                 // Set warnings
76                 this.setWarnings(statement.getWarnings());
77
78                 // Set status
79                 this.status = status;
80         }
81
82         /**
83          * Given Storeable instance as a query result.
84          *
85          * @param storeable An instance of a Storeable class
86          */
87         @Override
88         public final void add (final Storeable storeable) {
89                 // Add to result
90                 this.result.add(storeable);
91         }
92
93         /**
94          * Getter for warnings
95          *
96          * @return SQLQarning from ResultSet instance
97          */
98         @Override
99         public final SQLWarning getWarnings () {
100                 return this.warnings;
101         }
102
103         /**
104          * Setter for warnings
105          *
106          * @param warnings SQLQarning from ResultSet instance
107          */
108         @Override
109         public final void setWarnings (final SQLWarning warnings) {
110                 this.warnings = warnings;
111         }
112
113         @Override
114         public final boolean hasNext () {
115                 // Call iterator's method
116                 return this.iterator().hasNext();
117         }
118
119         @Override
120         public final Iterator<Storeable> iterator () {
121                 // Return iterator from result set
122                 return this.result.iterator();
123         }
124
125         @Override
126         public final Storeable next () {
127                 // Call iterator's method
128                 return this.iterator().next();
129         }
130
131         @Override
132         public final void remove () {
133                 // Call iterator's method
134                 this.iterator().remove();
135         }
136
137         @Override
138         public final int size () {
139                 return this.result.size();
140         }
141 }