001package gudusoft.gsqlparser.nodes.netezza;
002
003import java.util.regex.Matcher;
004import java.util.regex.Pattern;
005
006import gudusoft.gsqlparser.nodes.TParseTreeNode;
007
008public class TExternalTableOption extends TParseTreeNode {
009        
010        private boolean init = false;;
011
012        private String delimiter;
013        private String dateTimeDelim;
014        private String decimalDelim;
015        private String dateDelim;
016        private String escapeChar;
017        private String logDir;
018        private String meridianDelim;
019        private String nullValue;
020        private String recordDelim;
021        private String timeDelim;
022        private String compress;
023        private String includeHeader;
024        private String includeZeroSeconds;
025        private String requireQuotes;
026        private String timeRoundNanos;
027        private String timeExtraZeros;
028        private String truncString;
029        private String boolStyle;
030        private String dateStyle;
031        private String encoding;
032        private String format;
033        private String quotedValue;
034        private String remoteSource;
035        private String timeStyle;
036        private String dataObject;
037        private String recordLength;
038        private String socketBufSize;
039        private Boolean cRinString;
040        private Boolean ctrlChars;
041        private Boolean fillRecord;
042        private Boolean ignoreZero;
043        private Boolean lFinString;
044        private Integer maxErrors;
045        private Integer maxRows;
046        private Integer y2Base;
047        private Long skipRows;
048
049        public String getDelimiter() {
050                parseTableOptions();
051                return delimiter;
052        }
053
054        public String getDateTimeDelim() {
055                parseTableOptions();
056                return dateTimeDelim;
057        }
058
059        public String getDecimalDelim() {
060                parseTableOptions();
061                return decimalDelim;
062        }
063
064        public String getDateDelim() {
065                parseTableOptions();
066                return dateDelim;
067        }
068
069        public String getEscapeChar() {
070                parseTableOptions();
071                return escapeChar;
072        }
073
074        public String getLogDir() {
075                parseTableOptions();
076                return logDir;
077        }
078
079        public String getMeridianDelim() {
080                parseTableOptions();
081                return meridianDelim;
082        }
083
084        public String getNullValue() {
085                parseTableOptions();
086                return nullValue;
087        }
088
089        public String getRecordDelim() {
090                parseTableOptions();
091                return recordDelim;
092        }
093
094        public String getTimeDelim() {
095                parseTableOptions();
096                return timeDelim;
097        }
098
099        public String getCompress() {
100                parseTableOptions();
101                return compress;
102        }
103
104        public String getIncludeHeader() {
105                parseTableOptions();
106                return includeHeader;
107        }
108
109        public String getIncludeZeroSeconds() {
110                parseTableOptions();
111                return includeZeroSeconds;
112        }
113
114        public String getRequireQuotes() {
115                parseTableOptions();
116                return requireQuotes;
117        }
118
119        public String getTimeRoundNanos() {
120                parseTableOptions();
121                return timeRoundNanos;
122        }
123
124        public String getTimeExtraZeros() {
125                parseTableOptions();
126                return timeExtraZeros;
127        }
128
129        public String getTruncString() {
130                parseTableOptions();
131                return truncString;
132        }
133
134        public String getBoolStyle() {
135                parseTableOptions();
136                return boolStyle;
137        }
138
139        public String getDateStyle() {
140                parseTableOptions();
141                return dateStyle;
142        }
143
144        public String getEncoding() {
145                parseTableOptions();
146                return encoding;
147        }
148
149        public String getFormat() {
150                parseTableOptions();
151                return format;
152        }
153
154        public String getQuotedValue() {
155                parseTableOptions();
156                return quotedValue;
157        }
158
159        public String getRemoteSource() {
160                parseTableOptions();
161                return remoteSource;
162        }
163
164        public String getTimeStyle() {
165                parseTableOptions();
166                return timeStyle;
167        }
168
169        public String getDataObject() {
170                parseTableOptions();
171                return dataObject;
172        }
173
174        public String getRecordLength() {
175                parseTableOptions();
176                return recordLength;
177        }
178
179        public String getSocketBufSize() {
180                parseTableOptions();
181                return socketBufSize;
182        }
183
184        public Boolean getcRinString() {
185                parseTableOptions();
186                return cRinString;
187        }
188
189        public Boolean getCtrlChars() {
190                parseTableOptions();
191                return ctrlChars;
192        }
193
194        public Boolean getFillRecord() {
195                parseTableOptions();
196                return fillRecord;
197        }
198
199        public Boolean getIgnoreZero() {
200                parseTableOptions();
201                return ignoreZero;
202        }
203
204        public Boolean getlFinString() {
205                parseTableOptions();
206                return lFinString;
207        }
208
209        public Integer getMaxErrors() {
210                parseTableOptions();
211                return maxErrors;
212        }
213
214        public Integer getMaxRows() {
215                parseTableOptions();
216                return maxRows;
217        }
218
219        public Integer getY2Base() {
220                parseTableOptions();
221                return y2Base;
222        }
223
224        public Long getSkipRows() {
225                parseTableOptions();
226                return skipRows;
227        }
228
229        public TExternalTableOption(String optionText) {
230                this.optionText = optionText;
231        }
232
233        private String optionText;
234
235        public void setOptionText(String optionText) {
236                this.optionText = optionText;
237                init = false;
238        }
239
240        public String getOptionText() {
241                return optionText;
242        }
243
244        public void parseTableOptions() {
245                if (!init && optionText != null) {
246                        this.delimiter = getPatternString(optionText, "delimiter");
247                        this.dateTimeDelim = getPatternString(optionText, "dateTimeDelim");
248                        this.decimalDelim = getPatternString(optionText, "decimalDelim");
249                        this.dateDelim = getPatternString(optionText, "dateDelim");
250                        this.escapeChar = getPatternString(optionText, "escapeChar");
251                        this.logDir = getPatternString(optionText, "logDir");
252                        this.meridianDelim = getPatternString(optionText, "meridianDelim");
253                        this.nullValue = getPatternString(optionText, "nullValue");
254                        this.recordDelim = getPatternString(optionText, "recordDelim");
255                        this.timeDelim = getPatternString(optionText, "timeDelim");
256                        this.compress = getPatternString(optionText, "compress");
257                        this.includeHeader = getPatternString(optionText, "includeHeader");
258                        this.includeZeroSeconds = getPatternString(optionText, "includeZeroSeconds");
259                        this.requireQuotes = getPatternString(optionText, "requireQuotes");
260                        this.timeRoundNanos = getPatternString(optionText, "timeRoundNanos");
261                        this.timeExtraZeros = getPatternString(optionText, "timeExtraZeros");
262                        this.truncString = getPatternString(optionText, "truncString");
263                        this.dataObject = getPatternString(optionText, "dataObject");
264                        this.boolStyle = getPatternString(optionText, "boolStyle");
265                        this.boolStyle = getPatternString(optionText, "boolStyle");
266                        this.dateStyle = getPatternString(optionText, "dateStyle");
267                        this.encoding = getPatternString(optionText, "encoding");
268                        this.format = getPatternString(optionText, "format");
269                        this.quotedValue = getPatternString(optionText, "quotedValue");
270                        this.remoteSource = getPatternString(optionText, "remoteSource");
271                        this.timeStyle = getPatternString(optionText, "timeStyle");
272                        this.recordLength = getPatternString(optionText, "recordLength");
273                        this.socketBufSize = getPatternString(optionText, "socketBufSize");
274                        this.cRinString = getPatternBoolean(optionText, "cRinString");
275                        this.ctrlChars = getPatternBoolean(optionText, "ctrlChars");
276                        this.fillRecord = getPatternBoolean(optionText, "fillRecord");
277                        this.ignoreZero = getPatternBoolean(optionText, "ignoreZero");
278                        this.ignoreZero = getPatternBoolean(optionText, "ignoreZero");
279                        this.lFinString = getPatternBoolean(optionText, "lFinString");
280                        this.maxErrors = getPatternInteger(optionText, "maxErrors");
281                        this.maxRows = getPatternInteger(optionText, "maxRows");
282                        this.y2Base = getPatternInteger(optionText, "y2Base");
283                        this.skipRows = getPatternLong(optionText, "skipRows");
284                }
285                init = true;
286        }
287
288        private Long getPatternLong(String content, String field) {
289                String b = getPatternString(content, field);
290                if (null == b) {
291                        return null;
292                }
293                return Long.parseLong(b);
294        }
295
296        private Integer getPatternInteger(String content, String field) {
297                String b = getPatternString(content, field);
298                if (null == b) {
299                        return null;
300                }
301                return Integer.parseInt(b);
302        }
303
304        private Boolean getPatternBoolean(String content, String field) {
305                String b = getPatternString(content, field);
306                if (null == b) {
307                        return null;
308                }
309                if ("on".equalsIgnoreCase(b)) {
310                        return true;
311                } else if ("off".equalsIgnoreCase(b)) {
312                        return false;
313                }
314                return Boolean.parseBoolean(b);
315        }
316
317        private String getPatternString(String content, String field) {
318                String patternExp = "(?is)" + field + "\\s*('.+?')\\s*";
319                Pattern pattern = Pattern.compile(patternExp);
320                Matcher matcher = pattern.matcher(content);
321                if (matcher.find()) {
322                        String s = matcher.group(1).trim().replaceAll("'", "");
323                        return "".equals(s) ? "'" : s;
324                }
325                
326                patternExp = "(?is)" + field + "\\s*\\((.+?)\\)";
327                pattern = Pattern.compile(patternExp);
328                matcher = pattern.matcher(content);
329                if (matcher.find()) {
330                        String s = matcher.group(1).trim().replaceAll("'", "");
331                        return "".equals(s) ? "'" : s;
332                }
333                
334                patternExp = "(?is)" + field + "\\s*(.+)";
335                pattern = Pattern.compile(patternExp);
336                matcher = pattern.matcher(content);
337                if (matcher.find()) {
338                        String c = matcher.group(1).trim();
339                        return c.replaceAll("\r\n", ".").replaceAll("\n", ".").replaceAll("\r", ".").replaceAll(" ", ".")
340                                        .split("\\.")[0].replaceAll("\"", "");
341                }
342                return null;
343        }
344
345}