1 package salto.tool.sql;
2
3 import java.io.Serializable;
4 import java.sql.Connection;
5
6 import salto.tool.sql.data.CatalogInfo;
7 import salto.tool.sql.data.ShemaInfo;
8
9
10
11 public class ConnectionInfo implements Serializable
12 {
13 private String driverJarFile;
14 private String url;
15 private String user;
16 private String pwd;
17 private String driver;
18 private String name;
19 private transient Connection conn;
20
21 private transient CatalogInfo[] catalogInfos;
22 private transient ShemaInfo[] shemaInfos;
23
24 /***
25 * @return Returns the catalogInfos.
26 */
27 public CatalogInfo[] getCatalogInfos() {
28 if (! isConnected()) return new CatalogInfo[0];
29 if (catalogInfos == null) {
30 catalogInfos = DatabaseInfo.getCatalogs(this);
31 if (catalogInfos.length == 0) {
32 catalogInfos = new CatalogInfo[] {new CatalogInfo(this,"default")};
33 }
34 }
35 return catalogInfos;
36 }
37
38 /***
39 * @param catalogInfos The catalogInfos to set.
40 */
41 public void setCatalogInfos(CatalogInfo[] catalogInfos) {
42 if (catalogInfos == null) catalogInfos = DatabaseInfo.getCatalogs(this);
43 this.catalogInfos = catalogInfos;
44 }
45
46 public ConnectionInfo(String driver,String url,String user,String pwd,String name) {
47 this.driver = driver;
48 this.url = url;
49 this.user = user;
50 this.pwd = pwd;
51 this.name = name;
52 }
53
54 public ConnectionInfo(String driver,String url,String user,String pwd,String name,String driverJarFile) {
55 this.driver = driver;
56 this.url = url;
57 this.user = user;
58 this.pwd = pwd;
59 this.name = name;
60 this.driverJarFile = driverJarFile;
61 }
62
63 /***
64 * Gets the driver.
65 * @return Returns a String
66 */
67 public String getDriver() {
68 return driver;
69 }
70
71 /***
72 * Sets the driver.
73 * @param driver The driver to set
74 */
75 public void setDriver(String driver) {
76 this.driver = driver;
77 }
78
79 /***
80 * Gets the pwd.
81 * @return Returns a String
82 */
83 public String getPwd() {
84 return pwd;
85 }
86
87 /***
88 * Sets the pwd.
89 * @param pwd The pwd to set
90 */
91 public void setPwd(String pwd) {
92 this.pwd = pwd;
93 }
94
95 /***
96 * Gets the url.
97 * @return Returns a String
98 */
99 public String getUrl() {
100 return url;
101 }
102
103 /***
104 * Sets the url.
105 * @param url The url to set
106 */
107 public void setUrl(String url) {
108 this.url = url;
109 }
110
111 /***
112 * Gets the user.
113 * @return Returns a String
114 */
115 public String getUser() {
116 return user;
117 }
118
119 /***
120 * Sets the user.
121 * @param user The user to set
122 */
123 public void setUser(String user) {
124 this.user = user;
125 }
126
127 /***
128 * Gets the conn.
129 * @return Returns a Connection
130 */
131 public Connection getConn() {
132 return conn;
133 }
134
135 /***
136 * Sets the conn.
137 * @param conn The conn to set
138 */
139 public void setConn(Connection conn) {
140 this.conn = conn;
141 if (conn == null) shemaInfos = null;
142 }
143
144 /***
145 * Gets the name.
146 * @return Returns a String
147 */
148 public String getName() {
149 return name;
150 }
151
152 /***
153 * Sets the name.
154 * @param name The name to set
155 */
156 public void setName(String name) {
157 this.name = name;
158 }
159
160 /***
161 * Gets the driverJarFile.
162 * @return Returns a String
163 */
164 public String getDriverJarFile() {
165 return driverJarFile;
166 }
167
168 /***
169 * Sets the driverJarFile.
170 * @param driverJarFile The driverJarFile to set
171 */
172 public void setDriverJarFile(String driverJarFile) {
173 this.driverJarFile = driverJarFile;
174 }
175
176 /***
177 * @see Object#finalize()
178 */
179 protected void finalize() throws Throwable {
180 try {
181 conn.close();
182 } catch (Exception e) {
183 }
184 }
185
186
187 public boolean isConnected() {
188 return (conn != null);
189 }
190
191 public ShemaInfo[] getShemaInfos() {
192 return getShemaInfos(true);
193 }
194 /***
195 * @return
196 */
197 public ShemaInfo[] getShemaInfos(boolean find) {
198 if (!isConnected()) return new ShemaInfo[0];
199 if (find && shemaInfos == null) {
200 shemaInfos = DatabaseInfo.getShemas(this);
201 if (shemaInfos.length == 0) {
202 ShemaInfo info = new ShemaInfo(this);
203 info.setCatalog("default");
204 info.setName("default");
205 shemaInfos = new ShemaInfo[] {info};
206 }
207 }
208 return shemaInfos;
209 }
210 }