Initial import.
2 * StarOffice News Server
3 * see AUTHORS for the list of contributors
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package com.so.news.storage;
21 import java.sql.Connection;
22 import java.sql.DriverManager;
23 import java.sql.ResultSet;
24 import java.sql.SQLException;
25 import java.sql.Statement;
27 import java.util.zip.CRC32;
28 import com.so.news.Config;
29 import com.so.news.util.StringTemplate;
32 * Database abstraction class.
33 * @author Christian Lins (christian.lins@web.de)
37 private static Database instance = null;
40 * Initializes the Database subsystem, e.g. loading a JDBC driver and
41 * connection to the Database Managment System.
42 * This method is called when the daemon starts up or at the first
43 * call to Database.getInstance().
44 * @throws java.lang.Exception
46 public static void arise()
49 // Tries to load the Database driver and establish a connection.
51 instance = new Database();
55 * @return Instance of the current Database backend. Returns null if an error
58 public static Database getInstance()
72 private Connection conn = null;
75 * Private constructor.
76 * @throws java.lang.Exception
82 Config.getInstance().get("n3tpd.storage.dbmsdriver", ""));
83 this.conn = DriverManager.getConnection(
84 Config.getInstance().get("n3tpd.storage.database", ""),
85 Config.getInstance().get("n3tpd.storage.user", "n3tpd_user"),
86 Config.getInstance().get("n3tpd.storage.password", ""));
87 this.conn.setAutoCommit(false);
91 * Adds an article to the database.
94 * @throws java.sql.SQLException
96 public boolean addArticle(Article article)
99 Statement stmt = this.conn.createStatement();
101 String sql0 = "START TRANSACTION";
102 String sql1 = "INSERT INTO articles (message_id,header,body)" +
103 "VALUES('%mid', '%header', '%body')";
104 StringTemplate tmpl = new StringTemplate(sql1);
105 tmpl.set("body", article.getBody());
106 tmpl.set("mid", article.getMessageID());
107 tmpl.set("header", article.getHeaderSource());
108 sql1 = tmpl.toString();
110 String sql2 = "COMMIT";
112 // Add statements as batch
116 // TODO: For each newsgroup add a reference
117 String sql = "INSERT INTO postings (group_id, article_id, article_index)" +
118 "VALUES (%gid, (SELECT article_id FROM articles WHERE message_id = '%mid')," +
121 tmpl = new StringTemplate(sql);
122 tmpl.set("gid", article.getGroupID());
123 tmpl.set("mid", article.getMessageID());
124 tmpl.set("idx", getMaxArticleIndex() + 1);
125 stmt.addBatch(tmpl.toString());
130 // And execute the batch
137 * Adds a group to the Database.
139 * @throws java.sql.SQLException
141 public boolean addGroup(String name)
144 CRC32 crc = new CRC32();
145 crc.update(name.getBytes());
147 long id = crc.getValue();
149 Statement stmt = conn.createStatement();
150 return 1 == stmt.executeUpdate("INSERT INTO Groups (ID, Name) VALUES (" + id + ", '" + name + "')");
153 public void delete(Article article)
158 public void delete(Group group)
163 public Article getArticle(String messageID)
166 Statement stmt = this.conn.createStatement();
168 stmt.executeQuery("SELECT * FROM articles WHERE message_id = '" + messageID + "'");
170 return new Article(rs);
173 public Article getArticle(long gid, long article_id)
176 Statement stmt = this.conn.createStatement();
177 String sql = "SELECT * FROM articles WHERE article_id = " +
178 "(SELECT article_id FROM postings WHERE " +
179 "group_id = " + gid + " AND article_id = " + article_id +")";
181 stmt.executeQuery(sql);
184 return new Article(rs);
189 public ResultSet getArticles()
192 Statement stmt = conn.createStatement();
193 return stmt.executeQuery("SELECT * FROM articles");
197 * Reads all Groups from the Database.
199 * @throws java.sql.SQLException
201 public ResultSet getGroups()
204 Statement stmt = conn.createStatement();
205 ResultSet rs = stmt.executeQuery("SELECT * FROM groups");
211 * Returns the Group that is identified by the name.
214 * @throws java.sql.SQLException
216 public Group getGroup(String name)
219 Statement stmt = this.conn.createStatement();
220 String sql = "SELECT group_id FROM groups WHERE Name = '%name'";
221 StringTemplate tmpl = new StringTemplate(sql);
222 tmpl.set("name", name);
224 ResultSet rs = stmt.executeQuery(tmpl.toString());
230 long id = rs.getLong("group_id");
231 return new Group(name, id);
235 public int getMaxArticleIndex()
238 Statement stmt = conn.createStatement();
239 ResultSet rs = stmt.executeQuery(
240 "SELECT Max(article_index) FROM postings");
248 public int getLastArticleNumber(Group group)
251 Statement stmt = conn.createStatement();
252 ResultSet rs = stmt.executeQuery(
253 "SELECT Max(article_index) FROM postings WHERE group_id = " + group.getID());
261 public int getFirstArticleNumber(Group group)
264 Statement stmt = conn.createStatement();
265 ResultSet rs = stmt.executeQuery(
266 "SELECT Min(article_index) FROM postings WHERE group_id = " + group.getID());
275 * Returns a group name identified by the given id.
278 * @throws java.sql.SQLException
280 public String getGroup(int id)
283 Statement stmt = conn.createStatement();
284 ResultSet rs = stmt.executeQuery(
285 "SELECT name FROM groups WHERE group_id = '" + id + "'");
289 return rs.getString(1);
295 public Article getOldestArticle()
298 Statement stmt = conn.createStatement();
300 stmt.executeQuery("SELECT * FROM Articles WHERE Date = (SELECT Min(Date) FROM Articles)");
303 return new Article(rs);
309 * Checks if there is a group with the given name in the Database.
312 * @throws java.sql.SQLException
314 public boolean isGroupExisting(String name)
317 Statement stmt = this.conn.createStatement();
318 ResultSet rs = stmt.executeQuery("SELECT * FROM Groups WHERE Name = '" + name + "'");
323 public void updateArticle(Article article)