1 /*
2 * ObjectLab, http://www.objectlab.co.uk/open is supporting FlatPack.
3 *
4 * Based in London, we are world leaders in the design and development
5 * of bespoke applications for the securities financing markets.
6 *
7 * <a href="http://www.objectlab.co.uk/open">Click here to learn more</a>
8 * ___ _ _ _ _ _
9 * / _ \| |__ (_) ___ ___| |_| | __ _| |__
10 * | | | | '_ \| |/ _ \/ __| __| | / _` | '_ \
11 * | |_| | |_) | | __/ (__| |_| |__| (_| | |_) |
12 * \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
13 * |__/
14 *
15 * www.ObjectLab.co.uk
16 *
17 * $Id: ColorProvider.java 74 2006-10-24 22:19:05Z benoitx $
18 *
19 * Copyright 2006 the original author or authors.
20 *
21 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
22 * use this file except in compliance with the License. You may obtain a copy of
23 * the License at
24 *
25 * http://www.apache.org/licenses/LICENSE-2.0
26 *
27 * Unless required by applicable law or agreed to in writing, software
28 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
29 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
30 * License for the specific language governing permissions and limitations under
31 * the License.
32 */
33 package net.sf.flatpack;
34
35 import java.io.FileNotFoundException;
36 import java.io.InputStream;
37 import java.io.InputStreamReader;
38 import java.io.Reader;
39 import java.sql.Connection;
40 import java.sql.SQLException;
41 import java.util.List;
42
43 import net.sf.flatpack.structure.ColumnMetaData;
44 import net.sf.flatpack.util.ParserUtils;
45
46 /**
47 * @author xhensevb
48 * @author zepernick
49 *
50 */
51 public class DBDelimiterParser extends AbstractDelimiterParser {
52 private final Connection con;
53
54 private InputStream dataSourceStream;
55
56 public DBDelimiterParser(final Connection con, final InputStream dataSourceStream, final String dataDefinition, final char delimiter,
57 final char qualifier, final boolean ignoreFirstRecord) {
58 super(null, dataDefinition, delimiter, qualifier, ignoreFirstRecord);
59 this.con = con;
60 this.dataSourceStream = dataSourceStream;
61 }
62
63 public DBDelimiterParser(final Connection con, final Reader dataSourceReader, final String dataDefinition, final char delimiter,
64 final char qualifier, final boolean ignoreFirstRecord) {
65 super(dataSourceReader, dataDefinition, delimiter, qualifier, ignoreFirstRecord);
66 this.con = con;
67 }
68
69 @Override
70 protected void init() {
71 try {
72 // check to see if the user is using a InputStream. This is
73 // here for backwards compatibility
74 if (dataSourceStream != null) {
75 final Reader r = new InputStreamReader(dataSourceStream);
76 setDataSourceReader(r);
77 addToCloseReaderList(r);
78 }
79
80 final List<ColumnMetaData> cmds = ParserUtils.buildMDFromSQLTable(con, getDataDefinition(), this);
81 addToMetaData(cmds);
82
83 if (cmds.isEmpty()) {
84 throw new FileNotFoundException("DATA DEFINITION CAN NOT BE FOUND IN THE DATABASE " + getDataDefinition());
85 }
86 setInitialised(true);
87 } catch (final SQLException | FileNotFoundException e) {
88 throw new InitialisationException(e);
89 }
90 }
91
92 @Override
93 protected boolean shouldCreateMDFromFile() {
94 // The MetaData should always be pulled from the DB for this implementation
95 return false;
96 }
97 }