1
19
20 package com.liferay.portal.tools.sql;
21
22 import com.liferay.portal.kernel.util.FileUtil;
23 import com.liferay.portal.kernel.util.StringUtil;
24
25 import java.io.BufferedReader;
26 import java.io.File;
27 import java.io.IOException;
28 import java.io.StringReader;
29
30
39 public class SybaseUtil extends DBUtil {
40
41 public static DBUtil getInstance() {
42 return _instance;
43 }
44
45 public String buildSQL(String template) throws IOException {
46 template = convertTimestamp(template);
47 template = replaceTemplate(template, getTemplate());
48
49 template = reword(template);
50 template = StringUtil.replace(template, ");\n", ")\ngo\n");
51 template = StringUtil.replace(template, "\ngo;\n", "\ngo\n");
52 template = StringUtil.replace(
53 template,
54 new String[] {"\\\\", "\\'", "\\\"", "\\n", "\\r"},
55 new String[] {"\\", "''", "\"", "\n", "\r"});
56
57 return template;
58 }
59
60 protected SybaseUtil() {
61 super(TYPE_SYBASE);
62 }
63
64 protected void buildCreateFile(String databaseName, boolean minimal)
65 throws IOException {
66
67 String minimalSuffix = getMinimalSuffix(minimal);
68
69 File file = new File(
70 "../sql/create" + minimalSuffix + "/create" + minimalSuffix +
71 "-sybase.sql");
72
73 StringBuilder sb = new StringBuilder();
74
75 sb = new StringBuilder();
76
77 sb.append("use master\n");
78 sb.append(
79 "exec sp_dboption '" + databaseName + "', " +
80 "'allow nulls by default' , true\n");
81 sb.append("go\n\n");
82 sb.append(
83 "exec sp_dboption '" + databaseName + "', " +
84 "'select into/bulkcopy/pllsort' , true\n");
85 sb.append("go\n\n");
86
87 sb.append("use " + databaseName + "\n\n");
88 sb.append(
89 FileUtil.read(
90 "../sql/portal" + minimalSuffix + "/portal" + minimalSuffix +
91 "-sybase.sql"));
92 sb.append("\n\n");
93 sb.append(FileUtil.read("../sql/indexes/indexes-sybase.sql"));
94 sb.append("\n\n");
95 sb.append(FileUtil.read("../sql/sequences/sequences-sybase.sql"));
96
97 FileUtil.write(file, sb.toString());
98 }
99
100 protected String getServerName() {
101 return "sybase";
102 }
103
104 protected String[] getTemplate() {
105 return _SYBASE;
106 }
107
108 protected String reword(String data) throws IOException {
109 BufferedReader br = new BufferedReader(new StringReader(data));
110
111 StringBuilder sb = new StringBuilder();
112
113 String line = null;
114
115 while ((line = br.readLine()) != null) {
116
117 if (line.indexOf(DROP_COLUMN) != -1) {
118 line = StringUtil.replace(line, " drop column ", " drop ");
119 }
120
121 if (line.startsWith(ALTER_COLUMN_NAME)) {
122 String[] template = buildColumnNameTokens(line);
123
124 line = StringUtil.replace(
125 "exec sp_rename '@table@.@old-column@', '@new-column@', " +
126 "'column';",
127 REWORD_TEMPLATE, template);
128 }
129 else if (line.startsWith(ALTER_COLUMN_TYPE)) {
130 String[] template = buildColumnTypeTokens(line);
131
132 line = StringUtil.replace(
133 "alter table @table@ alter column @old-column@ @type@;",
134 REWORD_TEMPLATE, template);
135 }
136
137 sb.append(line);
138 sb.append("\n");
139 }
140
141 br.close();
142
143 return sb.toString();
144 }
145
146 protected static String DROP_COLUMN = "drop column";
147
148 private static String[] _SYBASE = {
149 "--", "1", "0",
150 "'19700101'", "getdate()",
151 " image", " int", " datetime",
152 " float", " int", " decimal(20,0)",
153 " varchar(1000)", " text", " varchar",
154 " identity(1,1)", "go"
155 };
156
157 private static SybaseUtil _instance = new SybaseUtil();
158
159 }