@Override protected void loadPrimaryKeys(DefaultRelations relations) throws SQLException { for (Record record : create() .select( concat(SYSTABLE.TABLE_NAME, val("__"), SYSIDX.INDEX_NAME).as("indexName"), SYSTABLE.TABLE_NAME, SYSTABCOL.COLUMN_NAME) .from(SYSIDX) .join(SYSIDXCOL) .on(SYSIDX.TABLE_ID.equal(SYSIDXCOL.TABLE_ID)) .and(SYSIDX.INDEX_ID.equal(SYSIDXCOL.INDEX_ID)) .join(SYSTABLE) .on(SYSIDXCOL.TABLE_ID.equal(SYSTABLE.TABLE_ID)) .join(SYSTABCOL) .on(SYSIDXCOL.TABLE_ID.equal(SYSTABCOL.TABLE_ID)) .and(SYSIDXCOL.COLUMN_ID.equal(SYSTABCOL.COLUMN_ID)) .where(SYSIDX.INDEX_CATEGORY.equal((byte) 1)) .orderBy(SYSIDXCOL.SEQUENCE) .fetch()) { String key = record.getValue("indexName", String.class); String tableName = record.getValue(SYSTABLE.TABLE_NAME); String columnName = record.getValue(SYSTABCOL.COLUMN_NAME); TableDefinition table = getTable(getSchema(), tableName); if (table != null) { relations.addPrimaryKey(key, table.getColumn(columnName)); } } }
@Override protected List<RoutineDefinition> getRoutines0() { List<RoutineDefinition> result = new ArrayList<RoutineDefinition>(); for (Record record : create() .selectDistinct( ALL_ARGUMENTS.OBJECT_NAME, ALL_ARGUMENTS.OBJECT_ID, ALL_ARGUMENTS.OVERLOAD) .from(ALL_ARGUMENTS) .where(ALL_ARGUMENTS.OWNER.equal(getSchema().getName())) .and(ALL_ARGUMENTS.PACKAGE_NAME.equal(getName())) .orderBy(ALL_ARGUMENTS.OBJECT_NAME, ALL_ARGUMENTS.OVERLOAD) .fetch()) { result.add( new OracleRoutineDefinition( getSchema(), this, record.getValue(ALL_ARGUMENTS.OBJECT_NAME), "", record.getValue(ALL_ARGUMENTS.OBJECT_ID), record.getValue(ALL_ARGUMENTS.OVERLOAD))); } return result; }
@Test public void testConditionsAsFields() throws Exception { Record record = create().select(field(one().eq(zero())), field(one().eq(1))).fetchOne(); assertEquals(false, record.getValue(0)); assertEquals(true, record.getValue(1)); }
public static Event fromRecord(Record record) { return new Event( record.getValue(EVENT.ID), record.getValue(EVENT.PLAYER_ID), record.getValue(EVENT.PLANET_ID), EventType.fromId(record.getValue(EVENT.TYPE)), record.getValue(EVENT.ROUND), Optional.ofNullable(record.getValue(EVENT.FIGHT_ID))); }
@Override protected List<EnumDefinition> getEnums0() throws SQLException { List<EnumDefinition> result = new ArrayList<EnumDefinition>(); Result<Record5<String, String, String, String, String>> records = create() .select( Columns.TABLE_SCHEMA, Columns.COLUMN_COMMENT, Columns.TABLE_NAME, Columns.COLUMN_NAME, Columns.COLUMN_TYPE) .from(COLUMNS) .where( Columns.COLUMN_TYPE .like("enum(%)") .and(Columns.TABLE_SCHEMA.in(getInputSchemata()))) .orderBy( Columns.TABLE_SCHEMA.asc(), Columns.TABLE_NAME.asc(), Columns.COLUMN_NAME.asc()) .fetch(); for (Record record : records) { SchemaDefinition schema = getSchema(record.getValue(Columns.TABLE_SCHEMA)); String comment = record.getValue(Columns.COLUMN_COMMENT); String table = record.getValue(Columns.TABLE_NAME); String column = record.getValue(Columns.COLUMN_NAME); String name = table + "_" + column; String columnType = record.getValue(Columns.COLUMN_TYPE); // [#1237] Don't generate enum classes for columns in MySQL tables // that are excluded from code generation TableDefinition tableDefinition = getTable(schema, table); if (tableDefinition != null) { ColumnDefinition columnDefinition = tableDefinition.getColumn(column); if (columnDefinition != null) { // [#1137] Avoid generating enum classes for enum types that // are explicitly forced to another type if (getConfiguredForcedType(columnDefinition) == null) { DefaultEnumDefinition definition = new DefaultEnumDefinition(schema, name, comment); for (String string : columnType.replaceAll("enum\\(|\\)", "").split(",")) { definition.addLiteral(string.trim().replaceAll("'", "")); } result.add(definition); } } } } return result; }
@Override public void fromRecord(Record r, Logo l) { // TODO remove it, get id from db Integer id = r.getValue(OFC_LOGO.POS); LogoPosition p = LogoDao.getPositionFromId(id); l.setId(id); l.setPosition(p); l.setImage(r.getValue(OFC_LOGO.IMAGE)); // TODO set contentType // l.setContentType(contentType); }
@Override protected void loadForeignKeys(DefaultRelations relations) throws SQLException { Sysidx fkIndex = SYSIDX.as("fkIndex"); Sysidx ukIndex = SYSIDX.as("ukIndex"); Systable fkTable = SYSTABLE.as("fkTable"); Systable ukTable = SYSTABLE.as("ukTable"); for (Record record : create() .select( concat(fkTable.TABLE_NAME, val("__"), fkIndex.INDEX_NAME).as("fkIndexName"), fkTable.TABLE_NAME, SYSTABCOL.COLUMN_NAME, concat(ukTable.TABLE_NAME, val("__"), ukIndex.INDEX_NAME).as("ukIndexName")) .from(SYSFKEY) .join(fkIndex) .on(SYSFKEY.FOREIGN_INDEX_ID.equal(fkIndex.INDEX_ID)) .and(SYSFKEY.FOREIGN_TABLE_ID.equal(fkIndex.TABLE_ID)) .join(SYSIDXCOL) .on(fkIndex.INDEX_ID.equal(SYSIDXCOL.INDEX_ID)) .and(fkIndex.TABLE_ID.equal(SYSIDXCOL.TABLE_ID)) .join(fkTable) .on(SYSFKEY.FOREIGN_TABLE_ID.equal(fkTable.TABLE_ID)) .join(SYSTABCOL) .on(SYSIDXCOL.TABLE_ID.equal(SYSTABCOL.TABLE_ID)) .and(SYSIDXCOL.COLUMN_ID.equal(SYSTABCOL.COLUMN_ID)) .join(ukIndex) .on(SYSFKEY.PRIMARY_INDEX_ID.equal(ukIndex.INDEX_ID)) .and(SYSFKEY.PRIMARY_TABLE_ID.equal(ukIndex.TABLE_ID)) .join(ukTable) .on(SYSFKEY.PRIMARY_TABLE_ID.equal(ukTable.TABLE_ID)) .orderBy(fkTable.TABLE_NAME.asc(), fkIndex.INDEX_NAME.asc(), SYSIDXCOL.SEQUENCE.asc()) .fetch()) { String foreignKey = record.getValue("fkIndexName", String.class); String foreignKeyTableName = record.getValue(SYSTABLE.TABLE_NAME); String foreignKeyColumn = record.getValue(SYSTABCOL.COLUMN_NAME); String referencedKey = record.getValue("ukIndexName", String.class); TableDefinition foreignKeyTable = getTable(getSchema(), foreignKeyTableName); if (foreignKeyTable != null) { ColumnDefinition referencingColumn = foreignKeyTable.getColumn(foreignKeyColumn); relations.addForeignKey(foreignKey, referencedKey, referencingColumn, getSchema()); } } }
/** * 班级数据 * * @param gradeVo * @return */ @RequestMapping("/maintainer/grade/gradeData") @ResponseBody public AjaxData<GradeVo> gradeData(GradeVo gradeVo) { AjaxData<GradeVo> ajaxData = new AjaxData<>(); // 通过用户类型获取系表ID Record record = usersService.findAll(usersService.getUserName()); int tieId = 0; if (!ObjectUtils.isEmpty(record)) { tieId = record.getValue(Tables.TIE.ID); } List<GradeVo> gradeVos = new ArrayList<>(); if (tieId > 0) { Result<Record7<Integer, String, Integer, String, String, String, String>> record7s = gradeService.findAllByPage(gradeVo, tieId); if (record7s.isNotEmpty()) { gradeVos = record7s.into(GradeVo.class); PaginationData paginationData = new PaginationData(); paginationData.setPageNum(gradeVo.getPageNum()); paginationData.setPageSize(gradeVo.getPageSize()); paginationData.setTotalDatas(gradeService.findAllByPageCount(gradeVo, tieId)); ajaxData.success().listData(gradeVos).paginationData(paginationData); } else { ajaxData.fail().listData(gradeVos); } } else { ajaxData.fail().listData(gradeVos); } return ajaxData; }
/** * 班级主任自动完成数据 * * @param search * @return */ @RequestMapping("/maintainer/grade/gradeHead") @ResponseBody public List<AutoCompleteData> gradeHead(String search) { List<AutoCompleteData> autoCompleteDatas = new ArrayList<>(); // 通过用户类型获取系表ID Record record = usersService.findAll(usersService.getUserName()); int tieId = 0; if (!ObjectUtils.isEmpty(record)) { tieId = record.getValue(Tables.TIE.ID); } Result<Record4<Integer, Integer, String, String>> teachers = teacherService.findByTieIdAndTeacherName(search, tieId); if (teachers.isNotEmpty()) { teachers.forEach( teacherRecord -> { AutoCompleteData data = new AutoCompleteData(); data.setValue(teacherRecord.getValue(Tables.TEACHER.TEACHER_JOB_NUMBER)); data.setTitle(teacherRecord.getValue(Tables.USERS.REAL_NAME)); data.setUrl("#"); data.setText("账号:" + teacherRecord.getValue(Tables.TEACHER.TEACHER_JOB_NUMBER)); autoCompleteDatas.add(data); }); } return autoCompleteDatas; }
@Override protected List<TableDefinition> getTables0() throws SQLException { List<TableDefinition> result = new ArrayList<TableDefinition>(); for (Record record : create().select(SYSTABLE.TABLE_NAME, SYSTABLE.REMARKS).from(SYSTABLE).fetch()) { String name = record.getValue(SYSTABLE.TABLE_NAME); String comment = record.getValue(SYSTABLE.REMARKS); SybaseTableDefinition table = new SybaseTableDefinition(getSchema(), name, comment); result.add(table); } return result; }
@Override public final String formatHTML() { StringBuilder sb = new StringBuilder(); sb.append("<table>"); sb.append("<thead>"); sb.append("<tr>"); for (Field<?> field : fields) { sb.append("<th>"); sb.append(field.getName()); sb.append("</th>"); } sb.append("</tr>"); sb.append("</thead>"); sb.append("<tbody>"); for (Record record : this) { sb.append("<tr>"); for (int index = 0; index < fields.fields.length; index++) { sb.append("<td>"); sb.append(format0(record.getValue(index), false)); sb.append("</td>"); } sb.append("</tr>"); } sb.append("</tbody>"); sb.append("</table>"); return sb.toString(); }
@Test public void testT65822() { Connection conn = null; String userName = "******"; String password = "******"; String url = "jdbc:derby://localhost:1527/test;create=true"; try { Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance(); conn = DriverManager.getConnection(url, userName, password); TestFactory create = new TestFactory(conn); // MP-MANAGED-UPDATABLE-BEGINNING-DISABLE @jooq-unittest-testT65822-test@ // write your own tests, just set DISABLE to ENABLE in the comment above // future generation will not erase your code ;) Result<Record> result = create.select().from(__T_658_22).limit(1).fetch(); for (Record r : result) { java.lang.Integer id = r.getValue(__T_658_22.ID); System.out.println("id : " + id + ""); // MP-MANAGED-UPDATABLE-ENDING } } catch (Exception e) { e.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
@Override public final String formatJSON() { List<Map<String, String>> f = new ArrayList<Map<String, String>>(); List<List<Object>> r = new ArrayList<List<Object>>(); Map<String, String> fieldMap; for (Field<?> field : fields) { fieldMap = new LinkedHashMap<String, String>(); fieldMap.put("name", field.getName()); fieldMap.put("type", field.getDataType().getTypeName().toUpperCase()); f.add(fieldMap); } for (Record record : this) { List<Object> list = new ArrayList<Object>(); for (int index = 0; index < fields.fields.length; index++) { list.add(record.getValue(index)); } r.add(list); } Map<String, List<?>> map = new LinkedHashMap<String, List<?>>(); map.put("fields", f); map.put("records", r); return JSONObject.toJSONString(map); }
@Override public final String formatCSV(char delimiter, String nullString) { StringBuilder sb = new StringBuilder(); String sep1 = ""; for (Field<?> field : fields) { sb.append(sep1); sb.append(formatCSV0(field.getName(), "")); sep1 = Character.toString(delimiter); } sb.append("\n"); for (Record record : this) { String sep2 = ""; for (int index = 0; index < fields.fields.length; index++) { sb.append(sep2); sb.append(formatCSV0(record.getValue(index), nullString)); sep2 = Character.toString(delimiter); } sb.append("\n"); } return sb.toString(); }
@Override protected void loadUniqueKeys(DefaultRelations relations) throws SQLException { for (Record record : fetchKeys("UNIQUE")) { SchemaDefinition schema = getSchema(record.getValue(KeyColumnUsage.TABLE_SCHEMA)); String constraintName = record.getValue(KeyColumnUsage.CONSTRAINT_NAME); String tableName = record.getValue(KeyColumnUsage.TABLE_NAME); String columnName = record.getValue(KeyColumnUsage.COLUMN_NAME); String key = getKeyName(tableName, constraintName); TableDefinition table = getTable(schema, tableName); if (table != null) { relations.addUniqueKey(key, table.getColumn(columnName)); } } }
@Override public List<ColumnDefinition> getElements0() throws SQLException { List<ColumnDefinition> result = new ArrayList<ColumnDefinition>(); for (Record record : create() .select( COLUMNS.COLUMN_NAME, COLUMNS.ORDINAL_POSITION, COLUMNS.DATA_TYPE, COLUMNS.CHARACTER_MAXIMUM_LENGTH, COLUMNS.NUMERIC_PRECISION, COLUMNS.NUMERIC_SCALE, COLUMNS.IS_NULLABLE, COLUMNS.IDENTITY_GENERATION, COLUMNS.COLUMN_DEFAULT, COLUMNS.UDT_NAME) .from(COLUMNS) .where(COLUMNS.TABLE_SCHEMA.equal(getSchema().getName())) .and(COLUMNS.TABLE_NAME.equal(getName())) .orderBy(COLUMNS.ORDINAL_POSITION) .fetch()) { DataTypeDefinition type = new DefaultDataTypeDefinition( getDatabase(), getSchema(), record.getValue(COLUMNS.DATA_TYPE), record.getValue(COLUMNS.CHARACTER_MAXIMUM_LENGTH), record.getValue(COLUMNS.NUMERIC_PRECISION), record.getValue(COLUMNS.NUMERIC_SCALE), record.getValue(COLUMNS.UDT_NAME)); ColumnDefinition column = new DefaultColumnDefinition( getDatabase().getTable(getSchema(), getName()), record.getValue(COLUMNS.COLUMN_NAME), record.getValue(COLUMNS.ORDINAL_POSITION, int.class), type, record.getValue(COLUMNS.IS_NULLABLE, boolean.class), record.getValue(COLUMNS.COLUMN_DEFAULT, "").startsWith("nextval"), null); result.add(column); } return result; }
@Test public void testPlainSQLAndComments() throws Exception { // Skip comments test for most dialects, as the behaviour w.r.t. comments // may differ if (dialect() != H2) { log.info("SKIPPING", "Skip comments tests"); return; } // [#1797] Plain SQL should be allowed to contain comments. Special care // must be taken when comments contain ' or ? characters // Single-line comments // -------------------- // Render bind values Record record1 = create() .fetchOne("select 1 x -- what's this ?'? \n" + ", '-- no comment' y from t_book \n" + " -- what's this ?'?\r" + "where id = ?", 1); assertEquals(1, record1.getValue(0)); assertEquals("-- no comment", record1.getValue(1)); // Inline bind values Record record2 = create(new Settings().withStatementType(STATIC_STATEMENT)) .fetchOne("select 1 x -- what's this ?'? \n" + ", '-- no comment' y from t_book \n" + " -- what's this ?'?\r" + "where id = ?", 1); assertEquals(1, record2.getValue(0)); assertEquals("-- no comment", record2.getValue(1)); // Multi-line comments // ------------------- // Render bind values Record record3 = create() .fetchOne("select /* what's this ?'?\n\r?'? */ 1 x, '/* no comment */' y from t_book where id = ?", 1); assertEquals(1, record3.getValue(0)); assertEquals("/* no comment */", record3.getValue(1)); // Inline bind values Record record4 = create(new Settings().withStatementType(STATIC_STATEMENT)) .fetchOne("select /* what's this ?'?\n\r?'? */ 1 x, '/* no comment */' y from t_book where id = ?", 1); assertEquals(1, record4.getValue(0)); assertEquals("/* no comment */", record4.getValue(1)); }
@Override protected List<TableDefinition> getTables0() throws SQLException { List<TableDefinition> result = new ArrayList<TableDefinition>(); for (Record record : create() .select(Tables.TABLE_SCHEMA, Tables.TABLE_NAME, Tables.TABLE_COMMENT) .from(TABLES) .where(Tables.TABLE_SCHEMA.in(getInputSchemata())) .orderBy(Tables.TABLE_SCHEMA, Tables.TABLE_NAME) .fetch()) { SchemaDefinition schema = getSchema(record.getValue(Tables.TABLE_SCHEMA)); String name = record.getValue(Tables.TABLE_NAME); String comment = record.getValue(Tables.TABLE_COMMENT); MySQLTableDefinition table = new MySQLTableDefinition(schema, name, comment); result.add(table); } return result; }
/** * 班级管理界面 * * @param modelMap * @return */ @RequestMapping("/maintainer/grade/gradeManager") public String gradeManager(ModelMap modelMap, GradeVo gradeVo) { // 通过用户类型获取系表ID Record record = usersService.findAll(usersService.getUserName()); int tieId = 0; if (!ObjectUtils.isEmpty(record)) { tieId = record.getValue(Tables.TIE.ID); } List<Major> majors = majorService.findByTieId(tieId); modelMap.addAttribute("majorNames", majors); modelMap.addAttribute("gradeVo", gradeVo); return "/maintainer/grade/gradelist"; }
@Override protected List<AttributeDefinition> getElements0() throws SQLException { List<AttributeDefinition> result = new ArrayList<AttributeDefinition>(); for (Record record : create() .select( ALL_TYPE_ATTRS.ATTR_NAME, ALL_TYPE_ATTRS.ATTR_NO, ALL_TYPE_ATTRS.ATTR_TYPE_NAME, ALL_TYPE_ATTRS.PRECISION, ALL_TYPE_ATTRS.SCALE) .from(ALL_TYPE_ATTRS) .where(ALL_TYPE_ATTRS.OWNER.equal(getSchema().getName())) .and(ALL_TYPE_ATTRS.TYPE_NAME.equal(getName())) .orderBy(ALL_TYPE_ATTRS.ATTR_NO) .fetch()) { DataTypeDefinition type = new DefaultDataTypeDefinition( getDatabase(), getSchema(), record.getValue(ALL_TYPE_ATTRS.ATTR_TYPE_NAME), record.getValueAsInteger(ALL_TYPE_ATTRS.PRECISION, 0), record.getValueAsInteger(ALL_TYPE_ATTRS.SCALE, 0)); AttributeDefinition attribute = new DefaultAttributeDefinition( this, record.getValue(ALL_TYPE_ATTRS.ATTR_NAME), record.getValueAsInteger(ALL_TYPE_ATTRS.ATTR_NO), type); result.add(attribute); } return result; }
@Override protected List<RoutineDefinition> getRoutines0() throws SQLException { List<RoutineDefinition> result = new ArrayList<RoutineDefinition>(); for (Record record : create() .select(Proc.DB, Proc.NAME, Proc.COMMENT, Proc.PARAM_LIST, Proc.RETURNS) .from(PROC) .where(DB.in(getInputSchemata())) .orderBy(DB, Proc.NAME) .fetch()) { SchemaDefinition schema = getSchema(record.getValue(DB)); String name = record.getValue(Proc.NAME); String comment = record.getValue(Proc.COMMENT); String params = new String(record.getValue(Proc.PARAM_LIST)); String returns = new String(record.getValue(Proc.RETURNS)); result.add(new MySQLRoutineDefinition(schema, name, comment, params, returns)); } return result; }
@Override protected List<AttributeDefinition> getElements0() throws SQLException { List<AttributeDefinition> result = new ArrayList<AttributeDefinition>(); for (Record record : create() .select( Attributes.ATTR_NAME, Attributes.ORDINAL, Attributes.ATTR_TYPENAME, Attributes.LENGTH, Attributes.SCALE) .from(ATTRIBUTES) .where(Attributes.TYPESCHEMA.equal(getSchemaName())) .and(Attributes.TYPENAME.equal(getName())) .orderBy(Attributes.ORDINAL) .fetch()) { DataTypeDefinition type = new DefaultDataTypeDefinition( getDatabase(), record.getValue(Attributes.ATTR_TYPENAME), record.getValue(Attributes.LENGTH), record.getValue(Attributes.SCALE)); AttributeDefinition column = new DefaultAttributeDefinition( this, record.getValue(Attributes.ATTR_NAME), record.getValue(Attributes.ORDINAL), type); result.add(column); } return result; }
@Test public void testXUnused() { Connection conn = null; String userName = "******"; String password = ""; String url = "jdbc:mysql://localhost/test"; try { Class.forName("com.mysql.jdbc.Driver").newInstance(); conn = DriverManager.getConnection(url, userName, password); TestFactory create = new TestFactory(conn); // MP-MANAGED-UPDATABLE-BEGINNING-DISABLE @jooq-unittest-testXUnused-test@ // write your own tests, just set DISABLE to ENABLE in the comment above // future generation will not erase your code ;) Result<Record> result = create.select().from(__X_UNUSED).limit(1).fetch(); for (Record r : result) { java.lang.Integer id = r.getValue(__X_UNUSED.ID); java.lang.String name = r.getValue(__X_UNUSED.NAME); java.lang.Integer idRef = r.getValue(__X_UNUSED.ID_REF); System.out.println("id : " + id + "name : " + name + "idRef : " + idRef + ""); // MP-MANAGED-UPDATABLE-ENDING } } catch (Exception e) { e.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
@Override public final String formatXML() { final int size = getFields().size(); StringBuilder sb = new StringBuilder(); sb.append("<result xmlns=\"http://www.jooq.org/xsd/jooq-export-2.6.0.xsd\">"); sb.append("<fields>"); for (Field<?> field : getFields()) { sb.append("<field name=\""); sb.append(escapeXML(field.getName())); sb.append("\" "); sb.append("type=\""); sb.append(field.getDataType().getTypeName().toUpperCase()); sb.append("\"/>"); } sb.append("</fields>"); sb.append("<records>"); for (Record record : this) { sb.append("<record>"); for (int index = 0; index < size; index++) { Field<?> field = getField(index); Object value = record.getValue(index); sb.append("<value field=\""); sb.append(escapeXML(field.getName())); sb.append("\""); if (value == null) { sb.append("/>"); } else { sb.append(">"); sb.append(escapeXML(format0(value))); sb.append("</value>"); } } sb.append("</record>"); } sb.append("</records>"); sb.append("</result>"); return sb.toString(); }
@Override public final Document intoXML() { final int size = getFields().size(); try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.newDocument(); Element eResult = document.createElement("result"); eResult.setAttribute("xmlns", "http://www.jooq.org/xsd/jooq-export-2.6.0.xsd"); document.appendChild(eResult); Element eFields = document.createElement("fields"); eResult.appendChild(eFields); for (Field<?> field : getFields()) { Element eField = document.createElement("field"); eField.setAttribute("name", field.getName()); eField.setAttribute("type", field.getDataType().getTypeName().toUpperCase()); eFields.appendChild(eField); } Element eRecords = document.createElement("records"); eResult.appendChild(eRecords); for (Record record : this) { Element eRecord = document.createElement("record"); eRecords.appendChild(eRecord); for (int index = 0; index < size; index++) { Field<?> field = getField(index); Object value = record.getValue(index); Element eValue = document.createElement("value"); eValue.setAttribute("field", field.getName()); eRecord.appendChild(eValue); if (value != null) { eValue.setTextContent(format0(value)); } } } return document; } catch (ParserConfigurationException ignore) { throw new RuntimeException(ignore); } }
@SuppressWarnings("rawtypes") private final void map(Record record, Object result, java.lang.reflect.Field member, int index) throws IllegalAccessException { Class<?> mType = member.getType(); if (mType.isPrimitive()) { if (mType == byte.class) { map(record.getValue(index, byte.class), result, member); } else if (mType == short.class) { map(record.getValue(index, short.class), result, member); } else if (mType == int.class) { map(record.getValue(index, int.class), result, member); } else if (mType == long.class) { map(record.getValue(index, long.class), result, member); } else if (mType == float.class) { map(record.getValue(index, float.class), result, member); } else if (mType == double.class) { map(record.getValue(index, double.class), result, member); } else if (mType == boolean.class) { map(record.getValue(index, boolean.class), result, member); } else if (mType == char.class) { map(record.getValue(index, char.class), result, member); } } else { Object value = record.getValue(index, mType); // [#3082] Map nested collection types if (value instanceof Collection && List.class.isAssignableFrom(mType)) { Class componentType = (Class) ((ParameterizedType) member.getGenericType()).getActualTypeArguments()[0]; member.set(result, Convert.convert((Collection) value, componentType)); } // Default reference types (including arrays) else { map(value, result, member); } } }
@Override protected List<RoutineDefinition> getRoutines0() throws SQLException { List<RoutineDefinition> result = new ArrayList<RoutineDefinition>(); for (Record record : create() .select(SYSPROCEDURE.PROC_NAME) .from(SYSPROCEDURE) .orderBy(SYSPROCEDURE.PROC_NAME) .fetch()) { String name = record.getValue(SYSPROCEDURE.PROC_NAME); result.add(new SybaseRoutineDefinition(getSchema(), null, name)); } return result; }
@Override protected void loadForeignKeys(DefaultRelations relations) throws SQLException { for (Record record : create() .select( ReferentialConstraints.CONSTRAINT_SCHEMA, ReferentialConstraints.CONSTRAINT_NAME, ReferentialConstraints.TABLE_NAME, ReferentialConstraints.REFERENCED_TABLE_NAME, ReferentialConstraints.UNIQUE_CONSTRAINT_NAME, ReferentialConstraints.UNIQUE_CONSTRAINT_SCHEMA, KeyColumnUsage.COLUMN_NAME) .from(REFERENTIAL_CONSTRAINTS) .join(KEY_COLUMN_USAGE) .on(ReferentialConstraints.CONSTRAINT_SCHEMA.equal(KeyColumnUsage.CONSTRAINT_SCHEMA)) .and(ReferentialConstraints.CONSTRAINT_NAME.equal(KeyColumnUsage.CONSTRAINT_NAME)) .where(ReferentialConstraints.CONSTRAINT_SCHEMA.in(getInputSchemata())) .orderBy( KeyColumnUsage.CONSTRAINT_SCHEMA.asc(), KeyColumnUsage.CONSTRAINT_NAME.asc(), KeyColumnUsage.ORDINAL_POSITION.asc()) .fetch()) { SchemaDefinition foreignKeySchema = getSchema(record.getValue(ReferentialConstraints.CONSTRAINT_SCHEMA)); SchemaDefinition uniqueKeySchema = getSchema(record.getValue(ReferentialConstraints.UNIQUE_CONSTRAINT_SCHEMA)); String foreignKey = record.getValue(ReferentialConstraints.CONSTRAINT_NAME); String foreignKeyColumn = record.getValue(KeyColumnUsage.COLUMN_NAME); String foreignKeyTableName = record.getValue(ReferentialConstraints.TABLE_NAME); String referencedKey = record.getValue(ReferentialConstraints.UNIQUE_CONSTRAINT_NAME); String referencedTableName = record.getValue(ReferentialConstraints.REFERENCED_TABLE_NAME); TableDefinition foreignKeyTable = getTable(foreignKeySchema, foreignKeyTableName); if (foreignKeyTable != null) { ColumnDefinition column = foreignKeyTable.getColumn(foreignKeyColumn); String key = getKeyName(referencedTableName, referencedKey); relations.addForeignKey(foreignKey, key, column, uniqueKeySchema); } } }
@Override public final String formatHTML() { final int size = getFields().size(); StringBuilder sb = new StringBuilder(); sb.append("<table>"); sb.append("<thead>"); sb.append("<tr>"); for (Field<?> field : getFields()) { sb.append("<th>"); sb.append(field.getName()); sb.append("</th>"); } sb.append("</tr>"); sb.append("</thead>"); sb.append("<tbody>"); for (Record record : this) { sb.append("<tr>"); for (int index = 0; index < size; index++) { sb.append("<td>"); sb.append(format0(record.getValue(index))); sb.append("</td>"); } sb.append("</tr>"); } sb.append("</tbody>"); sb.append("</table>"); return sb.toString(); }
@Test public void testInsertAndReturn() { // Add another author, with a generated ID Record record = create .insertInto(AUTHOR, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME) .values("Charlotte", "Roche") .returning(AUTHOR.ID) .fetchOne(); System.out.println(record.getValue(AUTHOR.ID)); // For some RDBMS, this also works when inserting several values // The following should return a 2x2 table Result<?> result = create .insertInto(AUTHOR, AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME) .values("Johann Wolfgang", "von Goethe") .values("Friedrich", "Schiller") // You can request any field. Also trigger-generated values .returning(AUTHOR.ID, AUTHOR.LAST_NAME) .fetch(); System.out.println(result.getValue(0, AUTHOR.ID)); System.out.println(result.getValue(1, AUTHOR.ID)); }