001// Copyright (c) 2001 Hursh Jain (http://www.mollypages.org) 
002// The Molly framework is freely distributable under the terms of an
003// MIT-style license. For details, see the molly pages web site at:
004// http://www.mollypages.org/. Use, modify, have fun !
005
006package fc.jdbc;
007
008import fc.util.*;
009
010/**
011Enumerates database names. Useful for methods that must be customized for
012each type of database.
013
014@author hursh jain
015*/
016public final class DBName
017{
018/* IMPORTANT:
019When more names are added, the fromDriver() method MUST ALSO BE UPDATED */
020/* The singleton DBName class corresponding to mysql. */
021public static final DBName mysql  = new DBName("mysql");
022
023/* The singleton DBName class corresponding to postgres. */
024public static final DBName postgres = new DBName("postgresql");
025
026/* The singleton DBName class corresponding to oracel. */
027public static final DBName oracle   = new DBName("oracle");
028
029private String name;
030private DBName(String name) { this.name = name;}
031
032/**
033Returns the DBName corresponding to the specified jdbc driver or
034<tt>null</tt> if the jdbc driver could not be parsed.
035
036@param  driver_name   a jdbc driver name such as
037            <tt>com.mysql.jdbc.Driver</tt> or
038            <tt>org.postgresql.Driver</tt>
039*/
040public static DBName fromDriver(String driver_name) 
041  {
042  driver_name = driver_name.toLowerCase();
043  Argcheck.notnull(driver_name, "the specified driver_name was null");
044
045  if (driver_name.indexOf(mysql.name.toLowerCase()) >= 0)
046    return mysql;
047  else if (driver_name.indexOf(postgres.name.toLowerCase()) >= 0)
048    return postgres;
049  else if (driver_name.indexOf(oracle.name.toLowerCase()) >=0 )
050    return oracle;
051  else 
052    return null;
053  }
054
055
056public String toString() { return name; }
057
058public static void main(String args[]) 
059  {
060  Args myargs = new Args(args);
061  myargs.setUsage("java fc.jdbc.DBName -fromDriver driver-name ");
062
063  if (! myargs.flagExists("fromDriver"))
064    {
065    myargs.showError(); 
066    }
067  
068  String driver = myargs.get("fromDriver");
069  if (driver != null) {
070    System.out.println("dbname=" + DBName.fromDriver(driver));
071    }
072  }
073}