use of com.teradata.jaqy.connection.JaqyResultSetMetaData in project jaqy by Teradata.
the class TablePrinter method print.
public long print(JaqyResultSet rs, PrintWriter pw, long limit, JaqyInterpreter interpreter) throws Exception {
JaqyHelper helper = rs.getHelper();
// rewind operation.
if (m_autoShrink && rs.getType() == ResultSet.TYPE_FORWARD_ONLY) {
JaqyResultSet newRS = ResultSetUtils.copyResultSet(rs, limit, interpreter);
rs.close();
rs = newRS;
}
JaqyResultSetMetaData metaData = rs.getMetaData();
int columns = metaData.getColumnCount();
boolean[] leftAligns = new boolean[columns];
int[] widths = new int[columns];
TypeHandler[] handlers = new TypeHandler[columns];
for (int i = 0; i < columns; ++i) {
int columnIndex = i + 1;
int type = metaData.getColumnType(columnIndex);
leftAligns[i] = isLeftAlign(type);
handlers[i] = helper.getTypeHandler(rs, i + 1);
}
if (m_autoShrink) {
//
for (int i = 0; i < columns; ++i) {
widths[i] = 0;
int dispSize = ResultSetUtils.getDisplayWidth(rs, i + 1);
if (dispSize <= m_columnThreshold)
widths[i] = dispSize;
}
shrink(columns, widths, rs, handlers, limit, interpreter);
} else {
//
for (int i = 0; i < columns; ++i) {
widths[i] = ResultSetUtils.getDisplayWidth(rs, i + 1);
if (widths[i] > m_maxColumnSize)
widths[i] = m_maxColumnSize;
}
}
// Make sure the title length is considered as well.
for (int i = 0; i < columns; ++i) {
String title = metaData.getColumnLabel(i + 1);
if (widths[i] < title.length())
widths[i] = title.length();
}
if (m_border)
printBorder(pw, widths);
// Print the title
if (m_border)
pw.print("| ");
for (int i = 0; i < columns; ++i) {
String title = metaData.getColumnLabel(i + 1);
if (i > 0) {
if (m_border)
pw.print(" | ");
else
pw.print(' ');
}
StringUtils.print(pw, title, widths[i], leftAligns[i], (i < (columns - 1)) || m_border);
}
if (m_border)
pw.print(" |");
pw.println();
// print the dashes
if (m_border)
printBorder(pw, widths);
else
printDash(pw, widths);
long count = 0;
if (limit == 0)
limit = Long.MAX_VALUE;
while (rs.next() && count < limit) {
++count;
if (m_border)
pw.print("| ");
for (int i = 0; i < columns; ++i) {
if (i > 0) {
if (m_border)
pw.print(" | ");
else
pw.print(' ');
}
StringUtils.print(pw, handlers[i].getString(rs, i + 1, interpreter), widths[i], leftAligns[i], (i < (columns - 1)) || m_border);
}
if (m_border)
pw.print(" |");
pw.println();
}
if (m_border)
printBorder(pw, widths);
return count;
}
use of com.teradata.jaqy.connection.JaqyResultSetMetaData 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.connection.JaqyResultSetMetaData in project jaqy by Teradata.
the class CSVPrinter method print.
@Override
public long print(JaqyResultSet rs, PrintWriter pw, long limit, JaqyInterpreter interpreter) throws Exception {
JaqyHelper helper = rs.getHelper();
JaqyResultSetMetaData metaData = rs.getMetaData();
int columns = metaData.getColumnCount();
TypeHandler[] handlers = new TypeHandler[columns];
@SuppressWarnings("resource") org.apache.commons.csv.CSVPrinter printer = new org.apache.commons.csv.CSVPrinter(pw, m_format);
for (int i = 0; i < columns; ++i) {
// print the header row
printer.print(metaData.getColumnLabel(i + 1));
handlers[i] = helper.getTypeHandler(rs, i + 1);
}
printer.println();
long count = 0;
if (limit == 0)
limit = Long.MAX_VALUE;
while (rs.next() && count < limit) {
++count;
for (int i = 0; i < columns; ++i) {
printer.print(handlers[i].getString(rs, i + 1, interpreter));
}
printer.println();
}
printer.flush();
return count;
}
use of com.teradata.jaqy.connection.JaqyResultSetMetaData in project jaqy by Teradata.
the class JsonUtils method print.
public static long print(Globals globals, CookJsonGenerator g, JaqyResultSet rs, long limit) throws SQLException {
JaqyResultSetMetaData metaData = rs.getMetaData();
int columns = metaData.getColumnCount();
String[] headers = new String[columns];
boolean[] jsonCheck = new boolean[columns];
for (int i = 0; i < columns; ++i) {
headers[i] = metaData.getColumnLabel(i + 1);
jsonCheck[i] = metaData.isJson(i + 1);
}
g.writeStartArray();
long count = 0;
if (limit == 0)
limit = Long.MAX_VALUE;
while (rs.next() && count < limit) {
++count;
g.writeStartObject();
for (int i = 0; i < columns; ++i) {
Object obj = rs.getObject(i + 1);
if (obj != null && jsonCheck[i]) {
globals.log(Level.INFO, "Column " + (i + 1) + " is a JSON column: " + obj.getClass());
String str = null;
// We only deal with textual form of JSON here.
if (obj instanceof Clob) {
Clob clob = (Clob) obj;
str = clob.getSubString(1, (int) clob.length());
clob.free();
} else {
str = obj.toString();
}
if (str != null) {
// Now convert str to JsonValue
try {
CookJsonParser p = new TextJsonParser(new StringReader(str));
p.next();
obj = p.getValue();
p.close();
} catch (Exception ex) {
globals.log(Level.INFO, ex);
}
}
}
print(g, headers[i], obj);
}
g.writeEnd();
}
g.writeEnd();
g.flush();
return count;
}
use of com.teradata.jaqy.connection.JaqyResultSetMetaData in project jaqy by Teradata.
the class ProjectColumnList method bind.
public void bind(JaqyResultSet rs, JaqyInterpreter interpreter) throws SQLException {
int numCols = m_columnList.size();
ExpNode[] exps = new ExpNode[numCols];
FullColumnInfo[] columnInfos = new FullColumnInfo[numCols];
ResultSetMetaData rsmd = rs.getMetaData().getMetaData();
JaqyHelper helper = rs.getHelper();
for (int i = 0; i < numCols; ++i) {
ProjectColumn column = m_columnList.get(i);
int index = rs.findColumn(column.name);
columnInfos[i] = ResultSetMetaDataUtils.getColumnInfo(rsmd, index, helper);
columnInfos[i].name = column.asName;
columnInfos[i].label = column.asName;
ColumnNode exp = new ColumnNode(column.name);
exp.bind(rs, null, interpreter);
exps[i] = exp;
}
m_project = new ExpNodeProject(exps);
m_meta = new JaqyResultSetMetaData(new InMemoryResultSetMetaData(columnInfos), DummyHelper.getInstance());
}
Aggregations