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