1
22
23 package com.liferay.portal.tools.sql;
24
25 import com.liferay.portal.kernel.util.FileUtil;
26 import com.liferay.portal.kernel.util.StringUtil;
27
28 import java.io.BufferedReader;
29 import java.io.File;
30 import java.io.IOException;
31 import java.io.StringReader;
32
33
41 public class DerbyUtil extends DBUtil {
42
43 public static DBUtil getInstance() {
44 return _instance;
45 }
46
47 public String buildSQL(String template) throws IOException {
48 template = convertTimestamp(template);
49 template = replaceTemplate(template, getTemplate());
50
51 template = reword(template );
52 template = removeNull(template);
54 template = StringUtil.replace(template , "\\'", "''");
55
56 return template;
57 }
58
59 protected DerbyUtil() {
60 }
61
62 protected void buildCreateFile(String databaseName, boolean minimal)
63 throws IOException {
64
65 String minimalSuffix = getMinimalSuffix(minimal);
66
67 File file = new File(
68 "../sql/create" + minimalSuffix + "/create" + minimalSuffix +
69 "-derby.sql");
70
71 StringBuilder sb = new StringBuilder();
72
73 sb.append("drop database " + databaseName + ";\n");
74 sb.append("create database " + databaseName + ";\n");
75 sb.append("connect to " + databaseName + ";\n");
76 sb.append(
77 FileUtil.read(
78 "../sql/portal" + minimalSuffix + "/portal" + minimalSuffix +
79 "-derby.sql"));
80 sb.append("\n\n");
81 sb.append(FileUtil.read("../sql/indexes/indexes-derby.sql"));
82 sb.append("\n\n");
83 sb.append(FileUtil.read("../sql/sequences/sequences-derby.sql"));
84
85 FileUtil.write(file, sb.toString());
86 }
87
88 protected String getServerName() {
89 return "derby";
90 }
91
92 protected String[] getTemplate() {
93 return _DERBY;
94 }
95
96 protected String reword(String data) throws IOException {
97 BufferedReader br = new BufferedReader(new StringReader(data));
98
99 StringBuilder sb = new StringBuilder();
100
101 String line = null;
102
103 while ((line = br.readLine()) != null) {
104 if (line.startsWith(ALTER_COLUMN_TYPE) ||
105 line.startsWith(ALTER_COLUMN_NAME)) {
106
107 line = "-- " + line;
108 }
109
110 sb.append(line);
111 sb.append("\n");
112 }
113
114 br.close();
115
116 return sb.toString();
117 }
118
119 private static String[] _DERBY = {
120 "--", "1", "0",
121 "'1970-01-01-00.00.00.000000'", "current timestamp",
122 " blob", " smallint", " timestamp",
123 " double", " integer", " bigint",
124 " long varchar", " clob", " varchar",
125 " generated always as identity", "commit"
126 };
127
128 private static DerbyUtil _instance = new DerbyUtil();
129
130 }