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