KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mapper > storage > PersistentRepositoryInfo


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
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 GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.mapper.storage;
24
25 import java.sql.ResultSet JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.sql.Statement JavaDoc;
28
29 import org.xquark.mapper.dbms.AbstractConnection;
30 import org.xquark.mapper.dbms.TableInfo;
31 import org.xquark.mapper.metadata.RepositoryInfo;
32
33 /** Data structure that corresponds to the storage table for repository
34  * configuration and is used when creating a repository.
35  * <p>This object contains all its JDBC calls and owns a connection
36  * in order to perform database access on his own</p>
37  */

38
39 public class PersistentRepositoryInfo
40 {
41     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
42     private static final String JavaDoc RCSName = "$Name: $";
43     
44     /////////////////////////////////////
45
// Storage JDBC variables
46
/////////////////////////////////////
47

48     private RepositoryInfo config = null;
49     
50     private TableInfo configTable = null;
51     
52     /** Connection object used by this data object. */
53     private AbstractConnection connection;
54
55     public PersistentRepositoryInfo(TableInfo configTable, AbstractConnection connection)
56     {
57         this.config = new RepositoryInfo();
58         this.connection = connection;
59         this.configTable = configTable;
60     }
61
62     public PersistentRepositoryInfo(RepositoryInfo config, TableInfo configTable, AbstractConnection connection)
63     {
64         this.config = config;
65         this.connection = connection;
66         this.configTable = configTable;
67     }
68
69     public RepositoryInfo getRepositoryInfo()
70     {
71         return config;
72     }
73
74     /**
75      * Save this object data into a config table row with batch style.
76      * Automatic flush is proceeded when max size is reached
77      * @throws SQLException if a database access error occurs.
78      */

79     public void save()
80     throws SQLException JavaDoc
81     {
82         connection.executeUpdate("DELETE FROM "+ configTable.getName());
83         StringBuffer JavaDoc stmt = new StringBuffer JavaDoc();
84         stmt.append("INSERT INTO ");
85         stmt.append(configTable.getName());
86         stmt.append(" VALUES('");
87         stmt.append(config.getName());
88         stmt.append("',");
89         stmt.append(config.getColIDSize());
90         stmt.append(",'");
91         stmt.append(config.getVersion());
92         stmt.append("')");
93         connection.executeUpdate(stmt.toString());
94     }
95
96     public boolean fetchRow()
97     throws SQLException JavaDoc
98     {
99         Statement JavaDoc stmt = connection.getConnection().createStatement();
100         ResultSet JavaDoc rs = null;
101         try {
102             rs = stmt.executeQuery("SELECT NAME,CID_SIZE,VERSION FROM " + configTable.getName());
103
104             if (rs.next())
105             {
106                 config.setName(rs.getString(1));
107                 config.setColIDSize(rs.getByte(2));
108                 config.setVersion(rs.getString(3));
109                 return true; // Take first row encountered
110
}
111             else
112                 return false; // If no row found
113
}
114         finally {
115             if (rs != null)
116                 rs.close();
117             stmt.close();
118         }
119     }
120
121 }
122
Popular Tags