use of org.jooq.TableField in project jOOQ by jOOQ.
the class InformationSchemaMetaImpl method init.
private final void init(InformationSchema meta) {
List<String> errors = new ArrayList<String>();
// -------------------------------------------------------------------------------------------------------------
for (org.jooq.util.xml.jaxb.Schema xs : meta.getSchemata()) {
InformationSchemaCatalog catalog = new InformationSchemaCatalog(xs.getCatalogName());
if (!catalogs.contains(catalog))
catalogs.add(catalog);
InformationSchemaSchema is = new InformationSchemaSchema(xs.getSchemaName(), catalog);
schemas.add(is);
schemasByName.put(name(xs.getCatalogName(), xs.getSchemaName()), is);
}
// -------------------------------------------------------------------------------------------------------------
tableLoop: for (org.jooq.util.xml.jaxb.Table xt : meta.getTables()) {
Name schemaName = name(xt.getTableCatalog(), xt.getTableSchema());
Schema schema = schemasByName.get(schemaName);
if (schema == null) {
errors.add(String.format("Schema " + schemaName + " not defined for table " + xt.getTableName()));
continue tableLoop;
}
InformationSchemaTable it = new InformationSchemaTable(xt.getTableName(), schema);
tables.add(it);
tablesByName.put(name(xt.getTableCatalog(), xt.getTableSchema(), xt.getTableName()), it);
}
// Columns
// -------------------------------------------------------------------------------------------------------------
List<Column> columns = new ArrayList<Column>(meta.getColumns());
Collections.sort(columns, new Comparator<Column>() {
@Override
public int compare(Column o1, Column o2) {
Integer p1 = o1.getOrdinalPosition();
Integer p2 = o2.getOrdinalPosition();
if (p1 == p2)
return 0;
if (p1 == null)
return -1;
if (p2 == null)
return 1;
return p1.compareTo(p2);
}
});
columnLoop: for (Column xc : columns) {
String typeName = xc.getDataType();
int length = xc.getCharacterMaximumLength() == null ? 0 : xc.getCharacterMaximumLength();
int precision = xc.getNumericPrecision() == null ? 0 : xc.getNumericPrecision();
int scale = xc.getNumericScale() == null ? 0 : xc.getNumericScale();
boolean nullable = xc.isIsNullable() == null ? true : xc.isIsNullable();
// TODO: Exception handling should be moved inside SQLDataType
Name tableName = name(xc.getTableCatalog(), xc.getTableSchema(), xc.getTableName());
InformationSchemaTable table = tablesByName.get(tableName);
if (table == null) {
errors.add(String.format("Table " + tableName + " not defined for column " + xc.getColumnName()));
continue columnLoop;
}
AbstractTable.createField(xc.getColumnName(), type(typeName, length, precision, scale, nullable), table);
}
// Constraints
// -------------------------------------------------------------------------------------------------------------
Map<Name, List<TableField<Record, ?>>> columnsByConstraint = new HashMap<Name, List<TableField<Record, ?>>>();
List<KeyColumnUsage> keyColumnUsages = new ArrayList<KeyColumnUsage>(meta.getKeyColumnUsages());
Collections.sort(keyColumnUsages, new Comparator<KeyColumnUsage>() {
@Override
public int compare(KeyColumnUsage o1, KeyColumnUsage o2) {
int p1 = o1.getOrdinalPosition();
int p2 = o2.getOrdinalPosition();
return (p1 < p2) ? -1 : ((p1 == p2) ? 0 : 1);
}
});
keyColumnLoop: for (KeyColumnUsage xc : keyColumnUsages) {
Name constraintName = name(xc.getConstraintCatalog(), xc.getConstraintSchema(), xc.getConstraintName());
List<TableField<Record, ?>> fields = columnsByConstraint.get(constraintName);
if (fields == null) {
fields = new ArrayList<TableField<Record, ?>>();
columnsByConstraint.put(constraintName, fields);
}
Name tableName = name(xc.getTableCatalog(), xc.getTableSchema(), xc.getTableName());
InformationSchemaTable table = tablesByName.get(tableName);
if (table == null) {
errors.add(String.format("Table " + tableName + " not defined for constraint " + constraintName));
continue keyColumnLoop;
}
TableField<Record, ?> field = (TableField<Record, ?>) table.field(xc.getColumnName());
if (field == null) {
errors.add(String.format("Column " + xc.getColumnName() + " not defined for table " + tableName));
continue keyColumnLoop;
}
fields.add(field);
}
tableConstraintLoop: for (TableConstraint xc : meta.getTableConstraints()) {
switch(xc.getConstraintType()) {
case PRIMARY_KEY:
case UNIQUE:
{
Name tableName = name(xc.getTableCatalog(), xc.getTableSchema(), xc.getTableName());
Name constraintName = name(xc.getConstraintCatalog(), xc.getConstraintSchema(), xc.getConstraintName());
InformationSchemaTable table = tablesByName.get(tableName);
if (table == null) {
errors.add(String.format("Table " + tableName + " not defined for constraint " + constraintName));
continue tableConstraintLoop;
}
List<TableField<Record, ?>> c = columnsByConstraint.get(constraintName);
if (c == null || c.isEmpty()) {
errors.add(String.format("No columns defined for constraint " + constraintName));
continue tableConstraintLoop;
}
UniqueKeyImpl<Record> key = (UniqueKeyImpl<Record>) AbstractKeys.createUniqueKey(table, xc.getConstraintName(), c.toArray(new TableField[0]));
if (xc.getConstraintType() == PRIMARY_KEY) {
table.primaryKey = key;
primaryKeys.add(key);
}
table.uniqueKeys.add(key);
uniqueKeysByName.put(constraintName, key);
break;
}
}
}
for (ReferentialConstraint xr : meta.getReferentialConstraints()) {
referentialKeys.put(name(xr.getConstraintCatalog(), xr.getConstraintSchema(), xr.getConstraintName()), name(xr.getUniqueConstraintCatalog(), xr.getUniqueConstraintSchema(), xr.getUniqueConstraintName()));
}
tableConstraintLoop: for (TableConstraint xc : meta.getTableConstraints()) {
switch(xc.getConstraintType()) {
case FOREIGN_KEY:
{
Name tableName = name(xc.getTableCatalog(), xc.getTableSchema(), xc.getTableName());
Name constraintName = name(xc.getConstraintCatalog(), xc.getConstraintSchema(), xc.getConstraintName());
InformationSchemaTable table = tablesByName.get(tableName);
if (table == null) {
errors.add(String.format("Table " + tableName + " not defined for constraint " + constraintName));
continue tableConstraintLoop;
}
List<TableField<Record, ?>> c = columnsByConstraint.get(constraintName);
if (c == null || c.isEmpty()) {
errors.add(String.format("No columns defined for constraint " + constraintName));
continue tableConstraintLoop;
}
UniqueKeyImpl<Record> uniqueKey = uniqueKeysByName.get(referentialKeys.get(constraintName));
if (uniqueKey == null) {
errors.add(String.format("No unique key defined for foreign key " + constraintName));
continue tableConstraintLoop;
}
ForeignKey<Record, Record> key = AbstractKeys.createForeignKey(uniqueKey, table, xc.getConstraintName(), c.toArray(new TableField[0]));
table.foreignKeys.add(key);
break;
}
}
}
// -------------------------------------------------------------------------------------------------------------
sequenceLoop: for (org.jooq.util.xml.jaxb.Sequence xs : meta.getSequences()) {
Name schemaName = name(xs.getSequenceCatalog(), xs.getSequenceSchema());
Schema schema = schemasByName.get(schemaName);
if (schema == null) {
errors.add(String.format("Schema " + schemaName + " not defined for sequence " + xs.getSequenceName()));
continue sequenceLoop;
}
String typeName = xs.getDataType();
int length = xs.getCharacterMaximumLength() == null ? 0 : xs.getCharacterMaximumLength();
int precision = xs.getNumericPrecision() == null ? 0 : xs.getNumericPrecision();
int scale = xs.getNumericScale() == null ? 0 : xs.getNumericScale();
boolean nullable = true;
@SuppressWarnings({ "rawtypes", "unchecked" }) InformationSchemaSequence is = new InformationSchemaSequence(xs.getSequenceName(), schema, type(typeName, length, precision, scale, nullable));
sequences.add(is);
}
// -------------------------------------------------------------------------------------------------------------
for (Schema s : schemas) {
Catalog c = s.getCatalog();
List<Schema> list = schemasPerCatalog.get(c);
if (list == null) {
list = new ArrayList<Schema>();
schemasPerCatalog.put(c, list);
}
list.add(s);
}
for (InformationSchemaTable t : tables) {
Schema s = t.getSchema();
List<InformationSchemaTable> list = tablesPerSchema.get(s);
if (list == null) {
list = new ArrayList<InformationSchemaTable>();
tablesPerSchema.put(s, list);
}
list.add(t);
}
for (Sequence<?> q : sequences) {
Schema s = q.getSchema();
List<Sequence<?>> list = sequencesPerSchema.get(s);
if (list == null) {
list = new ArrayList<Sequence<?>>();
sequencesPerSchema.put(s, list);
}
list.add(q);
}
if (!errors.isEmpty())
throw new IllegalArgumentException(errors.toString());
}
use of org.jooq.TableField in project jOOQ by jOOQ.
the class ResultImpl method intoXML.
@Override
public final <H extends ContentHandler> H intoXML(H handler, XMLFormat format) throws SAXException {
Attributes empty = new AttributesImpl();
handler.startDocument();
if (format.xmlns())
handler.startPrefixMapping("", Constants.NS_EXPORT);
handler.startElement("", "", "result", empty);
if (format.header()) {
handler.startElement("", "", "fields", empty);
for (Field<?> field : fields.fields) {
AttributesImpl attrs = new AttributesImpl();
if (field instanceof TableField<?, ?>) {
Table<?> table = ((TableField) field).getTable();
if (table != null) {
Schema schema = table.getSchema();
if (schema != null) {
attrs.addAttribute("", "", "schema", "CDATA", schema.getName());
}
attrs.addAttribute("", "", "table", "CDATA", table.getName());
}
}
attrs.addAttribute("", "", "name", "CDATA", field.getName());
attrs.addAttribute("", "", "type", "CDATA", field.getDataType().getTypeName().toUpperCase());
handler.startElement("", "", "field", attrs);
handler.endElement("", "", "field");
}
handler.endElement("", "", "fields");
handler.startElement("", "", "records", empty);
}
for (Record record : this) {
handler.startElement("", "", "record", empty);
for (int index = 0; index < fields.fields.length; index++) {
Field<?> field = fields.fields[index];
Object value = record.get(index);
String tag = format.recordFormat() == COLUMN_NAME_ELEMENTS ? escapeXML(fields.fields[index].getName()) : "value";
AttributesImpl attrs = new AttributesImpl();
if (format.recordFormat() == VALUE_ELEMENTS_WITH_FIELD_ATTRIBUTE)
attrs.addAttribute("", "", "field", "CDATA", field.getName());
handler.startElement("", "", tag, attrs);
if (value != null) {
char[] chars = format0(value, false, false).toCharArray();
handler.characters(chars, 0, chars.length);
}
handler.endElement("", "", tag);
}
handler.endElement("", "", "record");
}
if (format.header())
handler.endElement("", "", "records");
if (format.xmlns())
handler.endPrefixMapping("");
handler.endDocument();
return handler;
}
use of org.jooq.TableField in project jOOQ by jOOQ.
the class ResultImpl method intoXML.
@Override
public final Document intoXML(XMLFormat format) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
Element eResult = document.createElement("result");
if (format.xmlns())
eResult.setAttribute("xmlns", Constants.NS_EXPORT);
document.appendChild(eResult);
Element eRecordParent = eResult;
if (format.header()) {
Element eFields = document.createElement("fields");
eResult.appendChild(eFields);
for (Field<?> field : fields.fields) {
Element eField = document.createElement("field");
if (field instanceof TableField<?, ?>) {
Table<?> table = ((TableField) field).getTable();
if (table != null) {
Schema schema = table.getSchema();
if (schema != null) {
eField.setAttribute("schema", schema.getName());
}
eField.setAttribute("table", table.getName());
}
}
eField.setAttribute("name", field.getName());
eField.setAttribute("type", field.getDataType().getTypeName().toUpperCase());
eFields.appendChild(eField);
}
Element eRecords = document.createElement("records");
eResult.appendChild(eRecords);
eRecordParent = eRecords;
}
for (Record record : this) {
Element eRecord = document.createElement("record");
eRecordParent.appendChild(eRecord);
for (int index = 0; index < fields.fields.length; index++) {
Field<?> field = fields.fields[index];
Object value = record.get(index);
String tag = format.recordFormat() == COLUMN_NAME_ELEMENTS ? escapeXML(fields.fields[index].getName()) : "value";
Element eValue = document.createElement(tag);
if (format.recordFormat() == VALUE_ELEMENTS_WITH_FIELD_ATTRIBUTE)
eValue.setAttribute("field", field.getName());
eRecord.appendChild(eValue);
if (value != null) {
eValue.setTextContent(format0(value, false, false));
}
}
}
return document;
} catch (ParserConfigurationException ignore) {
throw new RuntimeException(ignore);
}
}
use of org.jooq.TableField in project jOOQ by jOOQ.
the class ResultImpl method formatXML.
@Override
public final void formatXML(Writer writer, XMLFormat format) {
String newline = format.newline();
int recordLevel = format.header() ? 2 : 1;
try {
writer.append("<result");
if (format.xmlns())
writer.append(" xmlns=\"" + Constants.NS_EXPORT + "\"");
writer.append(">");
if (format.header()) {
writer.append(newline).append(format.indentString(1)).append("<fields>");
for (Field<?> field : fields.fields) {
writer.append(newline).append(format.indentString(2)).append("<field");
if (field instanceof TableField) {
Table<?> table = ((TableField<?, ?>) field).getTable();
if (table != null) {
Schema schema = table.getSchema();
if (schema != null) {
writer.append(" schema=\"");
writer.append(escapeXML(schema.getName()));
writer.append("\"");
}
writer.append(" table=\"");
writer.append(escapeXML(table.getName()));
writer.append("\"");
}
}
writer.append(" name=\"");
writer.append(escapeXML(field.getName()));
writer.append("\"");
writer.append(" type=\"");
writer.append(field.getDataType().getTypeName().toUpperCase());
writer.append("\"/>");
}
writer.append(newline).append(format.indentString(1)).append("</fields>");
writer.append(newline).append(format.indentString(1)).append("<records>");
}
for (Record record : this) {
writer.append(newline).append(format.indentString(recordLevel));
formatXMLRecord(writer, format, recordLevel, record, fields);
}
if (format.header())
writer.append(newline).append(format.indentString(1)).append("</records>");
writer.append(newline).append("</result>");
writer.flush();
} catch (java.io.IOException e) {
throw new IOException("Exception while writing XML", e);
}
}
use of org.jooq.TableField in project zipkin by openzipkin.
the class MySQLSpanConsumer method accept.
/** Blocking version of {@link AsyncSpanConsumer#accept} */
@Override
public void accept(List<Span> spans) {
if (spans.isEmpty())
return;
try (Connection conn = datasource.getConnection()) {
DSLContext create = context.get(conn);
List<Query> inserts = new ArrayList<>();
for (Span span : spans) {
Long overridingTimestamp = authoritativeTimestamp(span);
Long timestamp = overridingTimestamp != null ? overridingTimestamp : guessTimestamp(span);
Map<TableField<Record, ?>, Object> updateFields = new LinkedHashMap<>();
if (!span.name.equals("") && !span.name.equals("unknown")) {
updateFields.put(ZIPKIN_SPANS.NAME, span.name);
}
// replace any tentative timestamp with the authoritative one.
if (overridingTimestamp != null) {
updateFields.put(ZIPKIN_SPANS.START_TS, overridingTimestamp);
}
if (span.duration != null) {
updateFields.put(ZIPKIN_SPANS.DURATION, span.duration);
}
InsertSetMoreStep<Record> insertSpan = create.insertInto(ZIPKIN_SPANS).set(ZIPKIN_SPANS.TRACE_ID, span.traceId).set(ZIPKIN_SPANS.ID, span.id).set(ZIPKIN_SPANS.PARENT_ID, span.parentId).set(ZIPKIN_SPANS.NAME, span.name).set(ZIPKIN_SPANS.DEBUG, span.debug).set(ZIPKIN_SPANS.START_TS, timestamp).set(ZIPKIN_SPANS.DURATION, span.duration);
if (span.traceIdHigh != 0 && schema.hasTraceIdHigh) {
insertSpan.set(ZIPKIN_SPANS.TRACE_ID_HIGH, span.traceIdHigh);
}
inserts.add(updateFields.isEmpty() ? insertSpan.onDuplicateKeyIgnore() : insertSpan.onDuplicateKeyUpdate().set(updateFields));
for (Annotation annotation : span.annotations) {
InsertSetMoreStep<Record> insert = create.insertInto(ZIPKIN_ANNOTATIONS).set(ZIPKIN_ANNOTATIONS.TRACE_ID, span.traceId).set(ZIPKIN_ANNOTATIONS.SPAN_ID, span.id).set(ZIPKIN_ANNOTATIONS.A_KEY, annotation.value).set(ZIPKIN_ANNOTATIONS.A_TYPE, -1).set(ZIPKIN_ANNOTATIONS.A_TIMESTAMP, annotation.timestamp);
if (span.traceIdHigh != 0 && schema.hasTraceIdHigh) {
insert.set(ZIPKIN_ANNOTATIONS.TRACE_ID_HIGH, span.traceIdHigh);
}
if (annotation.endpoint != null) {
insert.set(ZIPKIN_ANNOTATIONS.ENDPOINT_SERVICE_NAME, annotation.endpoint.serviceName);
insert.set(ZIPKIN_ANNOTATIONS.ENDPOINT_IPV4, annotation.endpoint.ipv4);
if (annotation.endpoint.ipv6 != null && schema.hasIpv6) {
insert.set(ZIPKIN_ANNOTATIONS.ENDPOINT_IPV6, annotation.endpoint.ipv6);
}
insert.set(ZIPKIN_ANNOTATIONS.ENDPOINT_PORT, annotation.endpoint.port);
}
inserts.add(insert.onDuplicateKeyIgnore());
}
for (BinaryAnnotation annotation : span.binaryAnnotations) {
InsertSetMoreStep<Record> insert = create.insertInto(ZIPKIN_ANNOTATIONS).set(ZIPKIN_ANNOTATIONS.TRACE_ID, span.traceId).set(ZIPKIN_ANNOTATIONS.SPAN_ID, span.id).set(ZIPKIN_ANNOTATIONS.A_KEY, annotation.key).set(ZIPKIN_ANNOTATIONS.A_VALUE, annotation.value).set(ZIPKIN_ANNOTATIONS.A_TYPE, annotation.type.value).set(ZIPKIN_ANNOTATIONS.A_TIMESTAMP, timestamp);
if (span.traceIdHigh != 0 && schema.hasTraceIdHigh) {
insert.set(ZIPKIN_ANNOTATIONS.TRACE_ID_HIGH, span.traceIdHigh);
}
if (annotation.endpoint != null) {
insert.set(ZIPKIN_ANNOTATIONS.ENDPOINT_SERVICE_NAME, annotation.endpoint.serviceName);
insert.set(ZIPKIN_ANNOTATIONS.ENDPOINT_IPV4, annotation.endpoint.ipv4);
if (annotation.endpoint.ipv6 != null && schema.hasIpv6) {
insert.set(ZIPKIN_ANNOTATIONS.ENDPOINT_IPV6, annotation.endpoint.ipv6);
}
insert.set(ZIPKIN_ANNOTATIONS.ENDPOINT_PORT, annotation.endpoint.port);
}
inserts.add(insert.onDuplicateKeyIgnore());
}
}
create.batch(inserts).execute();
} catch (SQLException e) {
// TODO
throw new RuntimeException(e);
}
}
Aggregations