@Test public void testScroll() throws Exception { POResultSet<MTable> rs = new Query(getCtx(), "AD_Table", "TableName IN (?,?)", getTrxName()) .setParameters(new Object[] {"C_Invoice", "M_InOut"}) .setOrderBy("TableName") .scroll(); try { int i = 0; while (rs.hasNext()) { MTable t = rs.next(); if (i == 0) { assertEquals("Invalid object " + i, "C_Invoice", t.getTableName()); } else if (i == 1) { assertEquals("Invalid object " + i, "M_InOut", t.getTableName()); } else { assertFalse("More objects retrived than expected", true); } i++; } } finally { DB.close(rs); rs = null; } }
private void createSequence(MTable table, String trxName) { if (!table.isView()) { if (!MSequence.createTableSequence(getCtx(), table.getTableName(), trxName)) { throw new AdempiereException( "Can not create Native Sequence for table " + table.getTableName()); } else { this.addLog("Create Native Sequence for : " + table.getTableName()); } } }
@Test public void testFirst() throws Exception { MTable t = new Query(getCtx(), "AD_Table", "TableName IN (?,?)", getTrxName()) .setParameters(new Object[] {"C_Invoice", "M_InOut"}) .setOrderBy("TableName") .first(); assertEquals("Invalid object", "C_Invoice", t.getTableName()); }
@Test public void testIterate() throws Exception { final Iterator<MTable> it = new Query(getCtx(), "AD_Table", "TableName IN (?,?)", getTrxName()) .setParameters(new Object[] {"C_Invoice", "M_InOut"}) .setOrderBy("TableName") .iterate(null, false); // guaranteed=false int i = 0; while (it.hasNext()) { MTable t = it.next(); if (i == 0) { assertEquals("Invalid object " + i, "C_Invoice", t.getTableName()); } else if (i == 1) { assertEquals("Invalid object " + i, "M_InOut", t.getTableName()); } else { assertFalse("More objects retrived than expected", true); } i++; } }
@Override public String retrieveTableName(final int adTableId) { final Properties ctx = Env.getCtx(); // guard against 0 AD_Table_ID if (adTableId <= 0) { return null; } @SuppressWarnings("deprecation") final String tableName = MTable.getTableName(ctx, adTableId); return tableName; }
/** * Process * * @return info * @throws Exception */ protected String doIt() throws Exception { if (p_target_AD_Table_ID == 0) throw new AdempiereSystemError("@NotFound@ @AD_Table_ID@ " + p_target_AD_Table_ID); if (p_source_AD_Table_ID == 0) throw new AdempiereSystemError("@NotFound@ @AD_Table_ID@ " + p_source_AD_Table_ID); if (log.isLoggable(Level.INFO)) log.info( "Source AD_Table_ID=" + p_source_AD_Table_ID + ", Target AD_Table_ID=" + p_target_AD_Table_ID); MTable targetTable = new MTable(getCtx(), p_target_AD_Table_ID, get_TrxName()); MColumn[] targetColumns = targetTable.getColumns(true); if (targetColumns.length > 0) // TODO: dictionary message throw new AdempiereSystemError("Target table must not have columns"); MTable sourceTable = new MTable(getCtx(), p_source_AD_Table_ID, get_TrxName()); MColumn[] sourceColumns = sourceTable.getColumns(true); for (int i = 0; i < sourceColumns.length; i++) { MColumn colTarget = new MColumn(targetTable); PO.copyValues(sourceColumns[i], colTarget); colTarget.setAD_Table_ID(targetTable.getAD_Table_ID()); colTarget.setEntityType(targetTable.getEntityType()); // special case the key -> sourceTable_ID if (sourceColumns[i].getColumnName().equals(sourceTable.getTableName() + "_ID")) { String targetColumnName = new String(targetTable.getTableName() + "_ID"); colTarget.setColumnName(targetColumnName); // if the element doesn't exist, create it M_Element element = M_Element.get(getCtx(), targetColumnName); if (element == null) { element = new M_Element(getCtx(), targetColumnName, targetTable.getEntityType(), get_TrxName()); if (targetColumnName.equalsIgnoreCase(targetTable.getTableName() + "_ID")) { element.setColumnName(targetTable.getTableName() + "_ID"); element.setName(targetTable.getName()); element.setPrintName(targetTable.getName()); } element.saveEx(get_TrxName()); } colTarget.setAD_Element_ID(element.getAD_Element_ID()); colTarget.setName(targetTable.getName()); colTarget.setDescription(targetTable.getDescription()); colTarget.setHelp(targetTable.getHelp()); } // special case the UUID column -> sourceTable_UU if (sourceColumns[i].getColumnName().equals(sourceTable.getTableName() + "_UU")) { String targetColumnName = new String(targetTable.getTableName() + "_UU"); colTarget.setColumnName(targetColumnName); // if the element doesn't exist, create it M_Element element = M_Element.get(getCtx(), targetColumnName); if (element == null) { element = new M_Element(getCtx(), targetColumnName, targetTable.getEntityType(), get_TrxName()); if (targetColumnName.equalsIgnoreCase(targetTable.getTableName() + "_UU")) { element.setColumnName(targetTable.getTableName() + "_UU"); element.setName(targetTable.getTableName() + "_UU"); element.setPrintName(targetTable.getTableName() + "_UU"); } element.saveEx(get_TrxName()); } colTarget.setAD_Element_ID(element.getAD_Element_ID()); colTarget.setName(targetTable.getName()); colTarget.setDescription(targetTable.getDescription()); colTarget.setHelp(targetTable.getHelp()); } colTarget.setIsActive(sourceColumns[i].isActive()); colTarget.saveEx(get_TrxName()); // TODO: Copy translations m_count++; } // return "#" + m_count; } // doIt
/** * Check if column exists in database and modify. If not create column. * * @param tablename * @param columnname * @param v_AD_Reference_ID * @param v_FieldLength * @param v_DefaultValue * @param v_IsMandatory */ private int createColumn(Properties ctx, MTable table, MColumn column, boolean doAlter) { int no = 0; String sql = null; ResultSet rst = null; ResultSet rsc = null; Connection conn = null; Trx trx = Trx.get(getTrxName(ctx), true); if (!trx.commit()) return 0; try { // Find Column in Database conn = trx.getConnection(); DatabaseMetaData md = conn.getMetaData(); String catalog = DB.getDatabase().getCatalog(); String schema = DB.getDatabase().getSchema(); String tableName = table.getTableName(); String columnName = column.getColumnName(); if (DB.isOracle()) { tableName = tableName.toUpperCase(); columnName = columnName.toUpperCase(); } else if (DB.isPostgreSQL()) { tableName = tableName.toLowerCase(); columnName = columnName.toLowerCase(); } rst = md.getTables(catalog, schema, tableName, new String[] {"TABLE"}); if (!rst.next()) { // table doesn't exist sql = table.getSQLCreate(); } else { // rsc = md.getColumns(catalog, schema, tableName, columnName); if (rsc.next()) { if (doAlter) { // update existing column boolean notNull = DatabaseMetaData.columnNoNulls == rsc.getInt("NULLABLE"); sql = column.getSQLModify(table, column.isMandatory() != notNull); } } else { // No existing column sql = column.getSQLAdd(table); } rsc.close(); rsc = null; } rst.close(); rst = null; // execute modify or add if needed if (sql != null && sql.trim().length() > 0) { log.info(sql); if (sql.indexOf(DB.SQLSTATEMENT_SEPARATOR) == -1) { no = DB.executeUpdate(sql, false, trx.getTrxName()); if (no == -1) return 0; } else { String statements[] = sql.split(DB.SQLSTATEMENT_SEPARATOR); for (int i = 0; i < statements.length; i++) { int count = DB.executeUpdate(statements[i], false, trx.getTrxName()); if (count == -1) { return 0; } no += count; } } } trx.commit(true); } catch (SQLException e) { log.log(Level.SEVERE, e.getLocalizedMessage(), e); if (rsc != null) { try { rsc.close(); } catch (SQLException e1) { } rsc = null; } if (rst != null) { try { rst.close(); } catch (SQLException e1) { } rst = null; } trx.rollback(); return 0; } return 1; }