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