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 super(DB_TYPE_DERBY);
61 }
62
63 protected void buildCreateFile(String databaseName, boolean minimal)
64 throws IOException {
65
66 String minimalSuffix = getMinimalSuffix(minimal);
67
68 File file = new File(
69 "../sql/create" + minimalSuffix + "/create" + minimalSuffix +
70 "-derby.sql");
71
72 StringBuilder sb = new StringBuilder();
73
74 sb.append("drop database " + databaseName + ";\n");
75 sb.append("create database " + databaseName + ";\n");
76 sb.append("connect to " + databaseName + ";\n");
77 sb.append(
78 FileUtil.read(
79 "../sql/portal" + minimalSuffix + "/portal" + minimalSuffix +
80 "-derby.sql"));
81 sb.append("\n\n");
82 sb.append(FileUtil.read("../sql/indexes/indexes-derby.sql"));
83 sb.append("\n\n");
84 sb.append(FileUtil.read("../sql/sequences/sequences-derby.sql"));
85
86 FileUtil.write(file, sb.toString());
87 }
88
89 protected String getServerName() {
90 return "derby";
91 }
92
93 protected String[] getTemplate() {
94 return _DERBY;
95 }
96
97 protected String reword(String data) throws IOException {
98 BufferedReader br = new BufferedReader(new StringReader(data));
99
100 StringBuilder sb = new StringBuilder();
101
102 String line = null;
103
104 while ((line = br.readLine()) != null) {
105 if (line.startsWith(ALTER_COLUMN_TYPE) ||
106 line.startsWith(ALTER_COLUMN_NAME)) {
107
108 line = "-- " + line;
109 }
110
111 sb.append(line);
112 sb.append("\n");
113 }
114
115 br.close();
116
117 return sb.toString();
118 }
119
120 private static String[] _DERBY = {
121 "--", "1", "0",
122 "'1970-01-01-00.00.00.000000'", "current timestamp",
123 " blob", " smallint", " timestamp",
124 " double", " integer", " bigint",
125 " long varchar", " clob", " varchar",
126 " generated always as identity", "commit"
127 };
128
129 private static DerbyUtil _instance = new DerbyUtil();
130
131 }