summaryrefslogtreecommitdiffstats
path: root/das-java/branches/das-java-beta1/samples/customer/src/main/java/org/apache/tuscany/samples/das/databaseSetup/DatabaseSetup.java
blob: 4195dbcac77c37cb177f0ae0bd0ea5e145972f6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */
package org.apache.tuscany.samples.das.databaseSetup;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.util.StringTokenizer;

public class DatabaseSetup{
    protected Statement s;
    protected String platformName = "Not initialized";
    protected String driverName = "Not initialized";
    protected String databaseURL = "Not initialized";
    protected String databaseName = null; 
    protected String userName;
    protected String password;
    
    // Data Types
    public static final String stringType = "VARCHAR";
    public static final String integerType = "INT";
    public static final String timestampType = "TIMESTAMP";
    public static final String floatType = "FLOAT";
    public static final String decimalType = "DECIMAL";
    protected Connection connection;
    
    //database types
    public static final String DERBY = "derby";
    public static final String MYSQL = "mysql";
    public static final String DB2 = "db2";
    
    public DatabaseSetup(String databaseInfo){
    	StringTokenizer strTok = new StringTokenizer(databaseInfo, "-");
    	
    	if(strTok.hasMoreTokens()){
    		databaseName =  strTok.nextToken();
    	}
    	
    	if(strTok.hasMoreTokens()){
    		userName = strTok.nextToken();
    	}
    	
    	if(strTok.hasMoreTokens()){
    		password = strTok.nextToken();
    	}
    	
        initConnectionProtocol(databaseName);
        initConnection();
        try{
        	this.setUp();
        }catch(Exception e){
        	e.printStackTrace();
        }
    }

    protected void initConnectionProtocol( String databaseName){
    }

    private void initConnection() {

        try {
            Class.forName(driverName).newInstance();
            Properties props = new Properties();
            //System.out.println("platform name:"+this.platformName);
            
            if(this.platformName.equals(DERBY)){
                if (userName != null) {
                	//System.out.println("derby trying for "+userName+","+password+","+databaseURL);
                    connection = DriverManager.getConnection(databaseURL, userName, password);
                } else {
                  	//System.out.println("derby trying for "+databaseURL);                                  	
                    connection = DriverManager.getConnection(databaseURL);
                }            	
            }
            if(this.platformName.equals(DB2)){
            	if (userName != null && password != null) {
            		props.put("user", userName);
            		props.put("password", password);
            		
            		//System.out.println("db2 trying for "+databaseURL+"user,pwd"+userName+","+password);
            		connection = DriverManager.getConnection(databaseURL, props);            		
            	}else{
            		//System.out.println("db2 trying for "+databaseURL);
            		connection = DriverManager.getConnection(databaseURL);
            	}            	
            }
            if(this.platformName.equals(MYSQL)){
            	databaseURL=databaseURL+"?user="+userName+"&password="+password+"&createDatabaseIfNotExist="+"true";
              	//System.out.println("mysql trying for "+userName+","+password+","+databaseURL);                
            	connection = DriverManager.getConnection(databaseURL, props);
            }

            connection.setAutoCommit(true);
        } catch (SQLException e) {
        	e.printStackTrace();
            if (e.getNextException() != null) {
                e.getNextException().printStackTrace();
            } else {
                e.printStackTrace();
            }

            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
        	e.printStackTrace();
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
        	e.printStackTrace();
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
        	e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    
    public Connection getConnection(){
    	return this.connection;
    }
    
    protected void setUp() throws Exception {
        System.out.println("Setting up for " + platformName + " run!");
    }

    private void insertData(){
    }
    
    private void dropTables() {        
    }

    protected void dropTriggers() {
    }

    protected void createTriggers() {
    }

    protected void dropSequences() {
    }

    protected void createSequences() {
    }

    protected void dropProcedures() {
    }

    private void createTables() {
    }

    protected void createProcedures() {
    }

    //
    // This section povides methods that return strings for table creation.
    // Platform-specific sublcasses
    // can override these as necessary
    //

    protected String getCreateCustomer() {
    	return null;
    }
    // /////////////////

    protected String getForeignKeyConstraint(String pkTable, String pkColumn, String foreignKey) {
    	return null;
    }

    protected String getStringColumn(String name, int length) {
    	return null;
    }

    protected String getIntegerColumn(String name) {
    	return null;
    }

    protected String getGeneratedKeyClause() {
    	return null;
    }

    protected String getDecimalColumn(String name, int size1, int size2) {
    	return null;
    }

    protected String getFloatColumn(String name) {
    	return null;
    }

    protected String getTimestampColumn(String name) {
    	return null;
    }        
}