use of com.teradata.jaqy.utils.CSVExportInfo in project jaqy by Teradata.
the class CSVExporter method export.
@Override
public long export(JaqyResultSet rs, JaqyInterpreter interpreter) throws Exception {
PrintWriter pw = new PrintWriter(new BufferedWriter(m_out));
JaqyResultSetMetaData metaData = rs.getMetaData();
int columns = metaData.getColumnCount();
TypeHandler[] handlers = new TypeHandler[columns];
JaqyHelper helper = rs.getHelper();
CSVPrinter printer = new CSVPrinter(pw, m_format);
for (int i = 0; i < columns; ++i) {
// print the header row
printer.print(metaData.getColumnLabel(i + 1));
CSVExportInfo fileInfo = m_fileInfoMap.get(i + 1);
if (fileInfo == null)
handlers[i] = helper.getTypeHandler(rs, i + 1);
else
handlers[i] = new FileHandler(m_file, fileInfo);
}
printer.println();
long count = 0;
while (rs.next()) {
++count;
for (int i = 0; i < columns; ++i) {
printer.print(handlers[i].getString(rs, i + 1, interpreter));
}
printer.println();
}
printer.close();
return count;
}
use of com.teradata.jaqy.utils.CSVExportInfo in project jaqy by Teradata.
the class CSVExporterFactory method getHandler.
@Override
public JaqyExporter getHandler(CommandLine cmdLine, JaqyInterpreter interpreter) throws Exception {
Charset charset = DEFAULT_CHARSET;
CSVFormat format = CSVFormat.DEFAULT;
HashMap<Integer, CSVExportInfo> exportInfoMap = new HashMap<Integer, CSVExportInfo>();
CSVNameGen nameGen = new CSVNameGen(DEFAULT_NAME_PATTERN);
Charset encoding = DEFAULT_CHARSET;
for (Option option : cmdLine.getOptions()) {
switch(option.getOpt().charAt(0)) {
case 'c':
{
charset = Charset.forName(option.getValue());
break;
}
case 'd':
{
char delimiter = CSVUtils.getChar(option.getValue());
if (delimiter == 0)
throw new IllegalArgumentException("invalid delimiter: " + option.getValue());
format = format.withDelimiter(delimiter);
break;
}
case 't':
{
format = CSVUtils.getFormat(option.getValue());
break;
}
case 'n':
{
String fmt = option.getValue();
nameGen = new CSVNameGen(fmt);
// now check if the name is a valid format.
if (fmt.equals(nameGen.getName(1)))
interpreter.error("Invalid name pattern: " + fmt);
break;
}
case 'e':
{
encoding = Charset.forName(option.getValue());
break;
}
case 'f':
{
int column = Integer.parseInt(option.getValue());
if (column < 1) {
interpreter.error("Column index cannot be smaller than 1.");
}
CSVExportInfo info = new CSVExportInfo(nameGen, encoding);
exportInfoMap.put(column, info);
break;
}
}
}
String[] args = cmdLine.getArgs();
if (args.length == 0)
throw new IllegalArgumentException("missing file name.");
Path file = interpreter.getPath(args[0]);
return new CSVExporter(file, charset, format, exportInfoMap);
}
Aggregations