View Javadoc

1   /**
2    * Copyright 2011 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.common.impex.schema.impl.mysql;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.kuali.common.impex.ProducerUtils;
22  import org.kuali.common.impex.model.View;
23  import org.kuali.common.util.CollectionUtils;
24  
25  public class MySqlViewSqlProducer {
26  
27      protected static final String DROP_PREFIX = "DROP VIEW IF EXISTS ";
28  
29      protected static final String CREATE_PREFIX = "CREATE VIEW ";
30      protected static final String CREATE_AS_KEYWORD = " AS\n";
31  
32  
33      public List<String> getViewsSql(List<View> views) {
34          List<String> results = new ArrayList<String>();
35  
36          for (View view : CollectionUtils.toEmptyList(views)) {
37              results.addAll(generateStatementsForView(view));
38          }
39  
40          return results;
41      }
42  
43      protected List<String> generateStatementsForView(View view) {
44          List<String> results = new ArrayList<String>(2);
45  
46          StringBuilder sb = new StringBuilder();
47  
48          sb.append(DROP_PREFIX);
49          sb.append(view.getName());
50          sb.append(ProducerUtils.NEWLINE);
51  
52          results.add(sb.toString());
53          sb = new StringBuilder();
54  
55          sb.append(CREATE_PREFIX);
56          sb.append(view.getName());
57          sb.append(CREATE_AS_KEYWORD);
58          sb.append(view.getQueryString());
59          sb.append(ProducerUtils.NEWLINE);
60          results.add(sb.toString());
61  
62          return results;
63      }
64  }