1 33 34 35 package com.internetcds.jdbc.tds; 36 37 38 import java.sql.*; 39 import java.util.*; 40 import com.internetcds.jdbc.tds.TdsException; 41 42 43 44 70 public class Driver implements java.sql.Driver 71 { 72 public static final String cvsVersion = "$Id: Driver.java,v 1.1 2006/06/23 10:39:04 sinisa Exp $"; 73 74 75 static 79 { 80 try { 81 java.sql.DriverManager.registerDriver(new Driver()); 82 } 83 catch (SQLException E) { 84 E.printStackTrace(); 85 } 86 } 87 88 89 static final boolean debug = false; 90 static final String oldSQLServerUrlPrefix = "jdbc:freetds://"; 91 static final String newSQLServerUrlPrefix = "jdbc:freetds:sqlserver://"; 92 static final String sybaseUrlPrefix = "jdbc:freetds:sybase://"; 93 static final String defaultSQLServerPort = "1433"; 94 static final String defaultSybasePort = "7100"; 95 96 97 98 private boolean isValidHostname(String host) 99 { 100 return true; } 102 103 109 protected boolean parseUrl(String url, Properties result) 110 { 111 String tmpUrl = url; 112 int serverType = -1; 113 114 if (tmpUrl.startsWith(oldSQLServerUrlPrefix) || 115 tmpUrl.startsWith(newSQLServerUrlPrefix) || 116 tmpUrl.startsWith(sybaseUrlPrefix)) 117 { 118 if (tmpUrl.startsWith(oldSQLServerUrlPrefix)) 119 { 120 serverType = Tds.SQLSERVER; 121 tmpUrl = tmpUrl.substring(oldSQLServerUrlPrefix.length()); 122 } 123 else if (tmpUrl.startsWith(newSQLServerUrlPrefix)) 124 { 125 serverType = Tds.SQLSERVER; 126 tmpUrl = tmpUrl.substring(newSQLServerUrlPrefix.length()); 127 } 128 else if (tmpUrl.startsWith(sybaseUrlPrefix)) 129 { 130 serverType = Tds.SYBASE; 131 tmpUrl = url.substring(sybaseUrlPrefix.length()); 132 } 133 134 135 try 136 { 137 StringTokenizer tokenizer = new StringTokenizer(tmpUrl, ":/;", 138 true); 139 String tmp; 140 String host = null; 141 String port = (serverType==Tds.SYBASE 142 ? defaultSybasePort 143 : defaultSQLServerPort); 144 String database = null; 145 String tdsVer = "7.0"; 148 149 host = tokenizer.nextToken(); 151 152 153 tmp = tokenizer.nextToken(); 155 if (tmp.equals(":")) 156 { 157 port = tokenizer.nextToken(); 158 tmp = tokenizer.nextToken(); 160 } 161 162 if (tmp.equals("/")) 163 { 164 database = tokenizer.nextToken(); 166 if (tokenizer.hasMoreTokens()) 167 tmp = tokenizer.nextToken(); 168 } 169 170 171 while (tmp.equals(";")) 173 { 174 String extra = tokenizer.nextToken(); 176 StringTokenizer tok2 = new StringTokenizer(extra, "=", false); 177 String key = tok2.nextToken().toUpperCase(); 178 if (tok2.hasMoreTokens()) 179 { 180 result.put(key, tok2.nextToken()); 181 } 182 183 if (tokenizer.hasMoreTokens()) 184 { 185 tmp = tokenizer.nextToken(); 186 } 187 else 188 { 189 break; 190 } 191 } 192 193 if ((! tokenizer.hasMoreTokens()) 195 && isValidHostname(host) 196 && database!=null) 197 { 198 result.put("HOST", host); 199 result.put("SERVERTYPE", "" + serverType); 200 result.put("PORT", port); 201 result.put("DBNAME", database); 202 } 203 else 204 { 205 return false; 206 } 207 } 208 catch (NoSuchElementException e) 209 { 210 return false; 211 } 212 } 213 else 214 { 215 return false; 216 } 217 218 return true; 219 } 220 221 222 227 public Driver() throws SQLException 228 { 229 } 230 231 232 271 public java.sql.Connection connect(String Url, Properties info) 272 throws SQLException 273 { 274 java.sql.Connection result = null; 275 276 if (!parseUrl(Url, info)) 277 { 278 return null; 279 } 280 else 281 { 282 try 283 { 284 result = Constructors.newConnection(info); 285 } 286 catch(NumberFormatException e) 287 { 288 throw new SQLException("NumberFormatException converting port number"); 289 } 290 catch(com.internetcds.jdbc.tds.TdsException e) 291 { 292 throw new SQLException(e.getMessage()); 293 } 294 } 295 return result; 296 } 297 298 332 public boolean acceptsURL(String url) throws SQLException 333 { 334 boolean result = parseUrl(url, new Properties()); 335 return result; 336 } 337 338 354 public DriverPropertyInfo[] getPropertyInfo(String Url, Properties Info) 355 throws SQLException 356 { 357 DriverPropertyInfo result[] = new DriverPropertyInfo[0]; 358 359 return result; 360 } 361 362 367 public int getMajorVersion() 368 { 369 return DriverVersion.getDriverMajorVersion(); 370 } 371 372 373 376 public int getMinorVersion() 377 { 378 return DriverVersion.getDriverMinorVersion(); 379 } 380 381 382 398 public boolean jdbcCompliant() 399 { 400 return false; 404 } 405 406 } 407 | Popular Tags |