use of java.sql.ResultSetMetaData in project gerrit by GerritCodeReview.
the class Schema_115 method getColumns.
private Set<String> getColumns(ResultSet rs) throws SQLException {
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
Set<String> columns = new HashSet<>(columnCount);
for (int i = 1; i <= columnCount; i++) {
columns.add(metaData.getColumnLabel(i).toLowerCase());
}
return columns;
}
use of java.sql.ResultSetMetaData in project gerrit by GerritCodeReview.
the class QueryShell method showResultSetJson.
/**
* Outputs a result set to stdout in Json format.
*
* @param rs ResultSet to show.
* @param alreadyOnRow true if rs is already on the first row. false otherwise.
* @param start Timestamp in milliseconds when executing the statement started. This timestamp is
* used to compute statistics about the statement. If no statistics should be shown, set it to
* 0.
* @param show Functions to map columns
* @throws SQLException
*/
private void showResultSetJson(final ResultSet rs, boolean alreadyOnRow, long start, Function... show) throws SQLException {
JsonArray collector = new JsonArray();
final ResultSetMetaData meta = rs.getMetaData();
final Function[] columnMap;
if (show != null && 0 < show.length) {
columnMap = show;
} else {
final int colCnt = meta.getColumnCount();
columnMap = new Function[colCnt];
for (int colId = 0; colId < colCnt; colId++) {
final int p = colId + 1;
final String name = meta.getColumnLabel(p);
columnMap[colId] = new Identity(p, name);
}
}
int rowCnt = 0;
while (alreadyOnRow || rs.next()) {
final JsonObject row = new JsonObject();
final JsonObject cols = new JsonObject();
for (Function function : columnMap) {
String v = function.apply(rs);
if (v == null) {
continue;
}
cols.addProperty(function.name.toLowerCase(), v);
}
row.addProperty("type", "row");
row.add("columns", cols);
switch(outputFormat) {
case JSON:
println(row.toString());
break;
case JSON_SINGLE:
collector.add(row);
break;
case PRETTY:
default:
final JsonObject obj = new JsonObject();
obj.addProperty("type", "error");
obj.addProperty("message", "Unsupported Json variant");
println(obj.toString());
return;
}
alreadyOnRow = false;
rowCnt++;
}
JsonObject tail = null;
if (start != 0) {
tail = new JsonObject();
tail.addProperty("type", "query-stats");
tail.addProperty("rowCount", rowCnt);
final long ms = TimeUtil.nowMs() - start;
tail.addProperty("runTimeMilliseconds", ms);
}
switch(outputFormat) {
case JSON:
if (tail != null) {
println(tail.toString());
}
break;
case JSON_SINGLE:
if (tail != null) {
collector.add(tail);
}
println(collector.toString());
break;
case PRETTY:
default:
final JsonObject obj = new JsonObject();
obj.addProperty("type", "error");
obj.addProperty("message", "Unsupported Json variant");
println(obj.toString());
}
}
use of java.sql.ResultSetMetaData in project gerrit by GerritCodeReview.
the class QueryShell method showResultSetPretty.
/**
* Outputs a result set to stdout in plain text format.
*
* @param rs ResultSet to show.
* @param alreadyOnRow true if rs is already on the first row. false otherwise.
* @param start Timestamp in milliseconds when executing the statement started. This timestamp is
* used to compute statistics about the statement. If no statistics should be shown, set it to
* 0.
* @param show Functions to map columns
* @throws SQLException
*/
private void showResultSetPretty(final ResultSet rs, boolean alreadyOnRow, long start, Function... show) throws SQLException {
final ResultSetMetaData meta = rs.getMetaData();
final Function[] columnMap;
if (show != null && 0 < show.length) {
columnMap = show;
} else {
final int colCnt = meta.getColumnCount();
columnMap = new Function[colCnt];
for (int colId = 0; colId < colCnt; colId++) {
final int p = colId + 1;
final String name = meta.getColumnLabel(p);
columnMap[colId] = new Identity(p, name);
}
}
final int colCnt = columnMap.length;
final int[] widths = new int[colCnt];
for (int c = 0; c < colCnt; c++) {
widths[c] = columnMap[c].name.length();
}
final List<String[]> rows = new ArrayList<>();
while (alreadyOnRow || rs.next()) {
final String[] row = new String[columnMap.length];
for (int c = 0; c < colCnt; c++) {
row[c] = columnMap[c].apply(rs);
if (row[c] == null) {
row[c] = "NULL";
}
widths[c] = Math.max(widths[c], row[c].length());
}
rows.add(row);
alreadyOnRow = false;
}
final StringBuilder b = new StringBuilder();
for (int c = 0; c < colCnt; c++) {
if (0 < c) {
b.append(" | ");
}
String n = columnMap[c].name;
if (widths[c] < n.length()) {
n = n.substring(0, widths[c]);
}
b.append(n);
if (c < colCnt - 1) {
for (int pad = n.length(); pad < widths[c]; pad++) {
b.append(' ');
}
}
}
println(" " + b.toString());
b.setLength(0);
for (int c = 0; c < colCnt; c++) {
if (0 < c) {
b.append("-+-");
}
for (int pad = 0; pad < widths[c]; pad++) {
b.append('-');
}
}
println(" " + b.toString());
boolean dataTruncated = false;
for (String[] row : rows) {
b.setLength(0);
b.append(' ');
for (int c = 0; c < colCnt; c++) {
final int max = widths[c];
if (0 < c) {
b.append(" | ");
}
String s = row[c];
if (1 < colCnt && max < s.length()) {
s = s.substring(0, max);
dataTruncated = true;
}
b.append(s);
if (c < colCnt - 1) {
for (int pad = s.length(); pad < max; pad++) {
b.append(' ');
}
}
}
println(b.toString());
}
if (dataTruncated) {
warning("some column data was truncated");
}
if (start != 0) {
final int rowCount = rows.size();
final long ms = TimeUtil.nowMs() - start;
println("(" + rowCount + (rowCount == 1 ? " row" : " rows") + "; " + ms + " ms)");
}
}
use of java.sql.ResultSetMetaData in project lucene-solr by apache.
the class TestJdbcDataSource method testEmptyResultSet.
@Test
public void testEmptyResultSet() throws Exception {
MockInitialContextFactory.bind("java:comp/env/jdbc/JndiDB", dataSource);
props.put(JdbcDataSource.JNDI_NAME, "java:comp/env/jdbc/JndiDB");
when(dataSource.getConnection()).thenReturn(connection);
jdbcDataSource.init(context, props);
Statement statement = mock(Statement.class);
when(connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)).thenReturn(statement);
when(statement.execute("query")).thenReturn(true);
ResultSet resultSet = mock(ResultSet.class);
when(statement.getResultSet()).thenReturn(resultSet);
ResultSetMetaData metaData = mock(ResultSetMetaData.class);
when(resultSet.getMetaData()).thenReturn(metaData);
when(metaData.getColumnCount()).thenReturn(0);
when(resultSet.next()).thenReturn(false);
when(statement.getMoreResults()).thenReturn(false);
when(statement.getUpdateCount()).thenReturn(-1);
Iterator<Map<String, Object>> resultSetIterator = jdbcDataSource.getData("query");
resultSetIterator.hasNext();
resultSetIterator.hasNext();
verify(connection).setAutoCommit(false);
verify(connection).createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
verify(statement).setFetchSize(500);
verify(statement).setMaxRows(0);
verify(statement).execute("query");
verify(statement).getResultSet();
verify(resultSet).getMetaData();
verify(metaData).getColumnCount();
verify(resultSet).next();
verify(resultSet).close();
verify(statement).getMoreResults();
verify(statement).getUpdateCount();
verify(statement).close();
}
use of java.sql.ResultSetMetaData in project adempiere by adempiere.
the class MenuElementHandler method startElement.
public void startElement(Properties ctx, Element element) throws SAXException {
String elementValue = element.getElementValue();
int AD_Backup_ID = -1;
String Object_Status = null;
Attributes atts = element.attributes;
log.info(elementValue + " " + atts.getValue("ADMenuNameID"));
// String entitytype = atts.getValue("EntityType");
// if (entitytype.compareTo("U") == 0 || entitytype.compareTo("D") == 0
// && m_UpdateMode == true ) {
String name = null;
int idDetail = 0;
StringBuffer sqlB = null;
name = atts.getValue("ADMenuNameID");
int menuid = get_IDWithColumn(ctx, "AD_Menu", "Name", name);
X_AD_Menu m_Menu = new X_AD_Menu(ctx, menuid, getTrxName(ctx));
if (menuid <= 0 && atts.getValue("AD_Menu_ID") != null && Integer.parseInt(atts.getValue("AD_Menu_ID")) <= PackOut.MAX_OFFICIAL_ID)
m_Menu.setAD_Menu_ID(Integer.parseInt(atts.getValue("AD_Menu_ID")));
if (menuid > 0) {
AD_Backup_ID = copyRecord(ctx, "AD_Menu", m_Menu);
Object_Status = "Update";
} else {
Object_Status = "New";
AD_Backup_ID = 0;
}
m_Menu.setName(name);
name = atts.getValue("ADWindowNameID");
if (name != null && name.trim().length() > 0) {
int id = get_IDWithColumn(ctx, "AD_Window", "Name", name);
if (id <= 0) {
element.defer = true;
return;
}
m_Menu.setAD_Window_ID(id);
}
name = atts.getValue("ADProcessNameID");
if (name != null && name.trim().length() > 0) {
int id = get_IDWithColumn(ctx, "AD_Process", "Name", name);
if (id <= 0) {
element.defer = true;
return;
}
m_Menu.setAD_Process_ID(id);
}
name = atts.getValue("ADFormNameID");
if (name != null && name.trim().length() > 0) {
int id = get_IDWithColumn(ctx, "AD_Form", "Name", name);
if (id <= 0) {
element.defer = true;
return;
}
m_Menu.setAD_Form_ID(id);
}
name = atts.getValue("ADBrowseNameID");
if (name != null && name.trim().length() > 0) {
int id = get_IDWithColumn(ctx, "AD_Browse", "Name", name);
if (id <= 0) {
element.defer = true;
return;
}
m_Menu.setAD_Browse_ID(id);
}
name = atts.getValue("ADTaskNameID");
if (name != null && name.trim().length() > 0) {
int id = get_IDWithColumn(ctx, "AD_Task", "Name", name);
if (id <= 0) {
element.defer = true;
return;
}
m_Menu.setAD_Task_ID(id);
}
name = atts.getValue("ADWorkbenchNameID");
if (name != null && name.trim().length() > 0) {
int id = get_IDWithColumn(ctx, "AD_Workbench", "Name", name);
if (id <= 0) {
element.defer = true;
return;
}
m_Menu.setAD_Workbench_ID(id);
}
name = atts.getValue("ADWorkflowNameID");
if (name != null && name.trim().length() > 0) {
int id = get_IDWithColumn(ctx, "AD_Workflow", "Name", name);
if (id <= 0) {
element.defer = true;
return;
}
m_Menu.setAD_Workflow_ID(id);
}
String action = (atts.getValue("Action") != null ? atts.getValue("Action") : " ");
if (action.compareTo(" ") > -1)
m_Menu.setAction(action);
m_Menu.setDescription(getStringValue(atts, "Description"));
m_Menu.setEntityType(atts.getValue("EntityType"));
m_Menu.setIsReadOnly(Boolean.valueOf(atts.getValue("isReadOnly")).booleanValue());
m_Menu.setIsSOTrx(Boolean.valueOf(atts.getValue("isSOTrx")).booleanValue());
m_Menu.setIsSummary(Boolean.valueOf(atts.getValue("isSummary")).booleanValue());
m_Menu.setIsActive(Boolean.valueOf(atts.getValue("isActive")).booleanValue());
if (m_Menu.save(getTrxName(ctx)) == true) {
try {
idDetail = record_log(ctx, 1, m_Menu.getName(), "Menu", m_Menu.get_ID(), AD_Backup_ID, Object_Status, "AD_Menu", get_IDWithColumn(ctx, "AD_Table", "TableName", "AD_Menu"));
} catch (SAXException e) {
log.info("setmenu:" + e);
}
} else {
try {
idDetail = record_log(ctx, 0, m_Menu.getName(), "Menu", m_Menu.get_ID(), AD_Backup_ID, Object_Status, "AD_Menu", get_IDWithColumn(ctx, "AD_Table", "TableName", "AD_Menu"));
} catch (SAXException e) {
log.info("setmenu:" + e);
}
}
name = atts.getValue("ADParentMenuNameID");
int id = get_ID(ctx, "AD_Menu", name);
String sql2 = "SELECT count(Parent_ID) FROM AD_TREENODEMM WHERE AD_Tree_ID = 10" + " AND Node_ID = " + menuid;
int countRecords = DB.getSQLValue(getTrxName(ctx), sql2);
if (countRecords > 0) {
StringBuffer sqlC = new StringBuffer("select * from AD_TREENODEMM where AD_Tree_ID = 10 and " + " Node_ID =?");
try {
PreparedStatement pstmt1 = DB.prepareStatement(sqlC.toString(), getTrxName(ctx));
pstmt1.setInt(1, menuid);
ResultSet rs1 = pstmt1.executeQuery();
if (rs1.next()) {
String colValue = null;
ResultSetMetaData meta = rs1.getMetaData();
int columns = meta.getColumnCount();
int tableID = get_IDWithColumn(ctx, "AD_Table", "TableName", "AD_TreeNodeMM");
for (int q = 1; q <= columns; q++) {
String col_Name = meta.getColumnName(q);
StringBuffer sql = new StringBuffer("SELECT AD_Column_ID FROM AD_column WHERE Upper(ColumnName) = '" + col_Name + "' AND AD_Table_ID = ?");
int columnID = DB.getSQLValue(getTrxName(ctx), sql.toString(), tableID);
sql = new StringBuffer("SELECT AD_Reference_ID FROM AD_COLUMN WHERE AD_Column_ID = " + (columnID == -1 ? "null" : columnID));
int referenceID = DB.getSQLValue(getTrxName(ctx), sql.toString());
int idBackup = DB.getNextID(Env.getAD_Client_ID(ctx), "AD_Package_Imp_Backup", getTrxName(ctx));
if (referenceID == 20 || referenceID == 28)
if (rs1.getObject(q).equals("Y"))
colValue = "true";
else
colValue = "false";
else
colValue = rs1.getObject(q).toString();
StringBuffer sqlD = new StringBuffer("INSERT INTO AD_Package_Imp_Backup" + "(AD_Client_ID, AD_Org_ID, CreatedBy, UpdatedBy, " + "AD_PACKAGE_IMP_BACKUP_ID, AD_PACKAGE_IMP_DETAIL_ID, AD_PACKAGE_IMP_ID," + " AD_TABLE_ID, AD_COLUMN_ID, AD_REFERENCE_ID, COLVALUE)" + "VALUES(" + " " + Env.getAD_Client_ID(ctx) + ", " + Env.getAD_Org_ID(ctx) + ", " + Env.getAD_User_ID(ctx) + ", " + Env.getAD_User_ID(ctx) + ", " + idBackup + ", " + idDetail + ", " + getPackageImpId(ctx) + ", " + tableID + ", " + (columnID == -1 ? "null" : columnID) + ", " + (referenceID == -1 ? "null" : referenceID) + ", '" + colValue + "')");
int no = DB.executeUpdate(sqlD.toString(), getTrxName(ctx));
if (no == -1)
log.info("Insert to import backup failed");
}
}
rs1.close();
pstmt1.close();
pstmt1 = null;
} catch (Exception e) {
log.info("get_IDWithMasterID:" + e);
}
MTree tree = new MTree(ctx, 10, getTrxName(ctx));
MTree_NodeMM treeNode = MTree_NodeMM.get(tree, m_Menu.getAD_Menu_ID());
treeNode.setSeqNo(Integer.valueOf(atts.getValue("ADParentSeqno")));
treeNode.set_CustomColumn("Parent_ID", id);
treeNode.saveEx();
} else {
MTree tree = new MTree(ctx, 10, getTrxName(ctx));
MTree_NodeMM treeNode = new MTree_NodeMM(tree, m_Menu.getAD_Menu_ID());
treeNode.setSeqNo(Integer.valueOf(atts.getValue("ADParentSeqno")));
treeNode.set_CustomColumn("Parent_ID", id);
treeNode.setNode_ID(m_Menu.getAD_Menu_ID());
treeNode.saveEx();
}
}
Aggregations