Changeset 8745


Ignore:
Timestamp:
2004-12-07T11:43:39+13:00 (19 years ago)
Author:
kjdon
Message:

reorganised the GS3SQLConnection and connectionFactory. now have a base connection class, SQLCOnnection, which does general connection stuff, while GS3SQLConnection extends this, and does collection specific stuff. methods in the factory have been renamed - can now ask for a Connection, SQLCOnnection or GS3SQLConnection.

Location:
trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/CollectionManager.java

    r8742 r8745  
    175175    this.qualifiedCollectionName = site+"_"+collection;
    176176
    177     this.database = GS3SQLConnectionFactory.createConnection(this.qualifiedCollectionName);
     177    this.database = GS3SQLConnectionFactory.getGS3SQLConnection(this.qualifiedCollectionName);
    178178    /*    if (this.database != null) {
    179179      this.database.clearCollection(collection);
     
    182182    */
    183183    if (this.database == null) {
    184       this.database = GS3SQLConnectionFactory.createConnection("test");
     184      this.database = GS3SQLConnectionFactory.getGS3SQLConnection("test");
    185185      this.database.initCollection(this.qualifiedCollectionName);
    186186    }
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/GSDataDump.java

    r8742 r8745  
    1414    String database = args[0];
    1515
    16     GS3SQLConnection connection = GS3SQLConnectionFactory.createConnection(database);
     16    GS3SQLConnection connection = GS3SQLConnectionFactory.getGS3SQLConnection(database);
    1717    DocumentList docList = DocumentList.readSQLDocuments(connection);
    1818    docList.writeDocuments(new File(database));
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/database/GS3SQLConnection.java

    r8741 r8745  
    1010import java.sql.ResultSet;
    1111
    12 public class GS3SQLConnection
     12
     13public class GS3SQLConnection extends SQLConnection
    1314{
    14   private Connection connection;
    15   private Statement  statement;
    16 
    17   class GS3SQLCreateTable
    18   { String tableName;
    19     List   properties;
    20     List   primaryKey;
    21     List   indexed;
    22 
    23     public GS3SQLCreateTable(String tableName)
    24     { this.tableName = tableName;
    25       this.properties = new ArrayList();
    26       this.primaryKey = new ArrayList();
    27       this.indexed    = new ArrayList();
    28     }
    29 
    30     public void addAutoPrimaryKey(String fieldName)
    31     {
    32       GS3SQLField field = new GS3SQLField(fieldName, GS3SQLField.AUTOINTEGER_TYPE);
    33       this.properties.add(field);
    34       this.setPrimaryKey(fieldName);
    35     }
    36 
    37     public void addProperty(String fieldname)
    38     { GS3SQLField field = new GS3SQLField(fieldname);
    39       this.properties.add(field);
    40     }
    41    
    42     public void addProperty(String fieldname, String type)
    43     { GS3SQLField field = new GS3SQLField(fieldname, type);
    44       this.properties.add(field);
    45     }
    46 
    47     public void addProperty(String fieldname, int length)
    48     { GS3SQLField field = new GS3SQLField(fieldname, length);
    49       this.properties.add(field);
    50     }
    51 
    52     public void setPrimaryKey(String fieldname)
    53     { this.primaryKey.clear();
    54       this.primaryKey.add(fieldname);
    55     }
    56 
    57     public void extendPrimaryKey(String fieldname)
    58     { this.primaryKey.add(fieldname);
    59     }
    60    
    61     public void addIndex(String fieldname)
    62     { this.indexed.add(fieldname);
    63     }
    64 
    65     public String toString()
    66     {   
    67       StringBuffer reply = new StringBuffer();
    68 
    69       reply.append("CREATE TABLE ");
    70       reply.append(this.tableName);
    71       reply.append(" (");
    72       for (int f = 0; f < this.properties.size(); f ++)
    73       { GS3SQLField field = (GS3SQLField) this.properties.get(f);
    74     if (f != 0)
    75     { reply.append(",");
    76     }
    77     reply.append(field.toString());
    78       }
    79 
    80       for (int p = 0; p < this.primaryKey.size(); p ++)
    81       { if (p == 0)
    82     { reply.append(", PRIMARY KEY(");
    83     }
    84         else
    85     { reply.append(",");
    86     }
    87         reply.append(this.primaryKey.get(p).toString());
    88 
    89     if (p == this.primaryKey.size() - 1)
    90     { reply.append(")");
    91     }
    92       }
    93       // TODO: add primary key, index etc. notations
    94       reply.append(");");
    95      
    96       return reply.toString();
    97     }
    98   }
    99 
    100   public GS3SQLConnection(java.sql.Connection connection)
    101   { this.connection = connection;
    102   }
    103 
    104   public boolean execute(String sql)
    105   {
    106     try {
    107       this.statement = this.connection.createStatement();
    108       this.statement.execute(sql);
    109     }
    110     catch (SQLException ex) {
    111     System.out.println(ex);
    112       return false;
    113     }
    114     return true;
    115   }
    116  
    117   public Statement createStatement()
    118   { try {
    119       return this.connection.createStatement();
    120     }
    121     catch (SQLException ex)
    122     {
    123       return null;
    124     }
    125   }
    126 
    127   public Statement getStatement()
    128   { return this.statement;
    129   }
    130 
    131   public ResultSet getResultSet()
    132   { try {
    133       return this.statement.getResultSet();
    134     }
    135     catch (SQLException ex)
    136     {
    137       return null;
    138     }
    139   }
     15
     16    public GS3SQLConnection(java.sql.Connection connection)
     17    {
     18    super(connection);
     19    }
     20   
    14021
    14122  public void clearCollection(String collectionName)
    14223  {
    143     // strip the old database
    144     String killCommand = "DROP DATABASE " + collectionName;
    145 
    146     try {
    147       if (/*makeClean*/true) {
    148     this.statement = this.connection.createStatement();
    149     this.statement.execute(killCommand);
    150     System.out.println("attempting: " + killCommand);
    151       }
    152     }
    153     catch (SQLException sqlEx) {
    154       System.out.println(sqlEx);
    155     }
     24      dropDatabase(collectionName);
    15625  }
    157 
     26   
    15827
    15928  /**
     
    17746    // if there is an error, assume that the database doesn't exist...
    17847    // and reconnect to a "test" database for now...
    179     this.connection = GS3SQLConnectionFactory.reconnect("test");
     48    this.connection = GS3SQLConnectionFactory.getConnection("test");
    18049    */
    18150
    182     //   create database
    183     String command = "CREATE DATABASE " + collectionName;
    184 
     51      // create the new db
     52      if (!createDatabase(collectionName)) {
     53      return false;
     54      }
     55      // now connect to it
     56      if (connectToDatabase(collectionName)) {
     57      return false;
     58      }
     59   
     60     
    18561    try {
    186       this.statement = this.connection.createStatement();
    187       this.statement.execute(command);
    188 
    189       // reconnect with the new database
    190       this.connection = GS3SQLConnectionFactory.reconnect(collectionName);
    191 
    19262      statement = this.connection.createStatement();
    19363
     
    365235  }
    366236
    367   public void deleteCollection(String collection)
    368   {
    369     try {
    370       statement = this.connection.createStatement();
    371       statement.execute("DROP DATABASE "+collection+";");
    372     }
    373     catch (SQLException ex)
    374     {
    375     }
    376   }
    377237}
    378238
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/gs3build/database/GS3SQLConnectionFactory.java

    r8741 r8745  
    55import java.sql.SQLException;
    66
     7
    78public class GS3SQLConnectionFactory
    89{
    9     public static Connection reconnect(String database)
     10    public static Connection getConnection(String database)
    1011    {
    1112    try {
     
    2526    }
    2627   
    27     public static GS3SQLConnection /*GS3SQLConnection*/ createConnection(String database)
    28     {   
    29     try
    30         {   Class.forName("com.mysql.jdbc.Driver").newInstance();
    31          
    32             Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/"+database+"?user=gsdl3admin"/*&password=greenstone"*/);
    33         return new GS3SQLConnection(connection);
    34         }
    35     catch (SQLException sqlEx)
    36         {   System.out.println(sqlEx.toString());
    37         }
    38     catch (Exception ex)
    39         {   System.out.println(ex.toString());
    40         return null;
    41         }
    42     return null;
     28    public static SQLConnection getSQLConnection(String database)
     29    {
     30    Connection c = getConnection(database);
     31    if (c== null) {
     32        return null;
     33    }
     34    return new SQLConnection(c);
    4335    }
    4436
    45     public static GS3SQLConnection createConnection()
    46     { return createConnection("test");
     37   
     38    public static GS3SQLConnection getGS3SQLConnection(String database)
     39    {
     40    Connection c = getConnection(database);
     41    if (c== null) {
     42        return null;
     43    }
     44    return new GS3SQLConnection(c);
     45    }
     46
     47    /* returns a Connection to the server, */
     48    public static Connection getConnection()
     49    {
     50    return getConnection("test");
     51    }
     52
     53    /* returns a Connection to the server, */
     54    public static SQLConnection getSQLConnection()
     55    {
     56    return getSQLConnection("test");
     57    }
     58
     59    public static GS3SQLConnection getGS3SQLConnection()
     60    {
     61    return getGS3SQLConnection("test");
    4762    }
    4863
    4964    public static void main(String args[])
    50     { GS3SQLConnection connection = createConnection();
    51     connection.initCollection("maya");
     65    {
     66    GS3SQLConnection connection = getGS3SQLConnection();
     67    connection.initCollection("maya");
    5268    }
    5369}
Note: See TracChangeset for help on using the changeset viewer.