1 19 package org.lucane.server.store.sql; 20 21 import java.util.*; 22 import java.sql.*; 23 24 import org.lucane.common.concepts.PluginConcept; 25 import org.lucane.server.store.PluginStore; 26 import org.lucane.server.database.*; 27 import org.lucane.server.*; 28 29 public class SqlPluginStore extends PluginStore 30 { 31 private static final String TABLENAME = "plugins"; 32 private DatabaseAbstractionLayer layer; 33 34 public SqlPluginStore() 35 throws Exception 36 { 37 this.layer = Server.getInstance().getDBLayer(); 38 } 39 40 public void storePlugin(PluginConcept plugin) 42 throws SQLException 43 { 44 Connection c = layer.getConnection(); 46 PreparedStatement insert = c.prepareStatement("INSERT INTO " + TABLENAME 47 + " VALUES(?, ?, ?)"); 48 insert.setString(1, plugin.getName()); 49 insert.setString(2, plugin.getVersion()); 50 insert.setString(3, plugin.getDescription()); 51 insert.execute(); 52 insert.close(); 53 c.close(); 54 } 55 56 public void updatePlugin(PluginConcept plugin) 57 throws SQLException 58 { 59 Connection c = layer.getConnection(); 60 61 try { 63 PreparedStatement delete = c.prepareStatement("DELETE FROM " + TABLENAME 64 + " WHERE name=?"); 65 delete.setString(1, plugin.getName()); 66 delete.execute(); 67 delete.close(); 68 } catch(SQLException e) { 69 } 71 72 PreparedStatement insert = c.prepareStatement("INSERT INTO " + TABLENAME 74 + " VALUES(?, ?, ?)"); 75 insert.setString(1, plugin.getName()); 76 insert.setString(2, plugin.getVersion()); 77 insert.setString(3, plugin.getDescription()); 78 insert.execute(); 79 insert.close(); 80 81 c.close(); 82 } 83 84 public void removePlugin(PluginConcept plugin) 85 throws SQLException 86 { 87 Connection c = layer.getConnection(); 88 Statement s = c.createStatement(); 89 90 PreparedStatement delete = c.prepareStatement("DELETE FROM " + TABLENAME 91 + " WHERE name=?"); 92 delete.setString(1, plugin.getName()); 93 delete.execute(); 94 delete.close(); 95 96 delete = c.prepareStatement("DELETE FROM " + SqlGroupStore.PLUGINLINKS 97 + " WHERE plugin=?"); 98 delete.setString(1, plugin.getName()); 99 delete.execute(); 100 delete.close(); 101 102 s.close(); 103 c.close(); 104 } 105 106 public PluginConcept getPlugin(String name) 107 throws SQLException 108 { 109 PluginConcept plugin = null; 110 111 Connection c = layer.getConnection(); 112 PreparedStatement select = c.prepareStatement("SELECT * FROM " + TABLENAME 113 + " WHERE name=?"); 114 select.setString(1, name); 115 ResultSet rs = select.executeQuery(); 116 117 if(rs.next()) 118 { 119 name = rs.getString(1); 120 String version = rs.getString(2); 121 String description = rs.getString(3); 122 123 plugin = new PluginConcept(name, version); 124 plugin.setDescription(description); 125 } 126 127 rs.close(); 128 select.close(); 129 c.close(); 130 131 return plugin; 132 } 133 134 public Iterator getAllPlugins() 135 throws SQLException 136 { 137 ArrayList all = new ArrayList(); 138 139 Connection c = layer.getConnection(); 140 PreparedStatement select = c.prepareStatement("SELECT * FROM " + TABLENAME); 141 ResultSet rs = select.executeQuery(); 142 143 while(rs.next()) 144 { 145 String name = rs.getString(1); 146 String version = rs.getString(2); 147 String description = rs.getString(3); 148 149 PluginConcept plugin = new PluginConcept(name, version); 150 plugin.setDescription(description); 151 all.add(plugin); 152 } 153 154 rs.close(); 155 select.close(); 156 c.close(); 157 158 return all.iterator(); 159 } 160 } | Popular Tags |