use of com.sforce.soap.partner.DescribeSObjectResult in project teiid by teiid.
the class SalesForceMetadataProcessor method getColumnsAndRelationships.
private void getColumnsAndRelationships(List<String> names) throws TranslatorException {
try {
DescribeSObjectResult[] objectMetadatas = connection.getObjectMetaData(names.toArray(new String[names.size()]));
for (DescribeSObjectResult objectMetadata : objectMetadatas) {
getRelationships(objectMetadata);
Table table = this.tableMap.get(objectMetadata.getName());
boolean hasUpdateableColumn = addColumns(objectMetadata, table);
// Some SF objects return true for isUpdateable() but have no updateable columns.
if (objectMetadata.isDeletable() || (hasUpdateableColumn && (objectMetadata.isUpdateable() || objectMetadata.isCreateable()))) {
table.setSupportsUpdate(true);
}
}
} catch (ResourceException e) {
throw new TranslatorException(e);
}
names.clear();
}
use of com.sforce.soap.partner.DescribeSObjectResult in project components by Talend.
the class SalesforceSourceOrSink method guessSchema.
@Override
public Schema guessSchema(String soqlQuery) throws IOException {
SoqlQuery query = SoqlQuery.getInstance();
query.init(soqlQuery);
SchemaBuilder.FieldAssembler fieldAssembler = SchemaBuilder.record("GuessedSchema").fields();
DescribeSObjectResult describeSObjectResult = null;
try {
describeSObjectResult = connect(null).connection.describeSObject(query.getDrivingEntityName());
} catch (ConnectionException e) {
throw new RuntimeException(e.getMessage());
}
Schema entitySchema = SalesforceAvroRegistry.get().inferSchema(describeSObjectResult);
for (FieldDescription fieldDescription : query.getFieldDescriptions()) {
Schema.Field schemaField = entitySchema.getField(fieldDescription.getSimpleName());
SchemaBuilder.FieldBuilder builder = fieldAssembler.name(fieldDescription.getFullName());
Schema fieldType = null;
if (schemaField != null) {
Map<String, Object> props = schemaField.getObjectProps();
for (Map.Entry<String, Object> entry : props.entrySet()) {
builder.prop(entry.getKey(), String.valueOf(entry.getValue()));
}
fieldType = schemaField.schema();
} else {
fieldType = DEFAULT_GUESS_SCHEMA_TYPE;
}
builder.type(fieldType).noDefault();
}
return (Schema) fieldAssembler.endRecord();
}
use of com.sforce.soap.partner.DescribeSObjectResult in project components by Talend.
the class SalesforceSourceOrSink method getSchema.
protected Schema getSchema(PartnerConnection connection, String module) throws IOException {
try {
DescribeSObjectResult[] describeSObjectResults = new DescribeSObjectResult[0];
describeSObjectResults = connection.describeSObjects(new String[] { module });
return SalesforceAvroRegistry.get().inferSchema(describeSObjectResults[0]);
} catch (ConnectionException e) {
throw new IOException(e);
}
}
use of com.sforce.soap.partner.DescribeSObjectResult in project components by Talend.
the class SalesforceAvroRegistryTest method testInferSchemaDescribeSObjectResult.
/**
* Tests that the {@link SalesforceAvroRegistry} has added support to get a {@link Schema} that describes
* {@link DescribeSObjectResult} objects.
*/
@Test
public void testInferSchemaDescribeSObjectResult() {
Schema s;
// Setup.
{
Field booleanField = new Field();
booleanField.setName("valid");
booleanField.setType(FieldType._boolean);
Field defaultField = new Field();
defaultField.setName("def");
defaultField.setType(FieldType._boolean);
defaultField.setDefaultValueFormula(Boolean.TRUE.toString());
Field dateField = new Field();
dateField.setName("date");
dateField.setType(FieldType.date);
Field stringWithLengthField = new Field();
stringWithLengthField.setName("string_with_length");
stringWithLengthField.setType(FieldType.string);
stringWithLengthField.setLength(20);
Field numberWithScaleAndPrecisionField = new Field();
numberWithScaleAndPrecisionField.setName("number_with_scale_and_precision");
numberWithScaleAndPrecisionField.setType(FieldType._double);
numberWithScaleAndPrecisionField.setPrecision(10);
numberWithScaleAndPrecisionField.setScale(2);
Field doubleWithNullable = new Field();
doubleWithNullable.setName("double_with_nullable");
doubleWithNullable.setType(FieldType._double);
doubleWithNullable.setPrecision(18);
doubleWithNullable.setScale(15);
doubleWithNullable.setNillable(true);
DescribeSObjectResult dsor = new DescribeSObjectResult();
dsor.setName("MySObjectRecord");
dsor.setFields(new Field[] { booleanField, defaultField, dateField, stringWithLengthField, numberWithScaleAndPrecisionField, doubleWithNullable });
s = sRegistry.inferSchema(dsor);
}
assertThat(s.getType(), is(Schema.Type.RECORD));
assertThat(s.getName(), is("MySObjectRecord"));
assertThat(s.getFields(), hasSize(6));
assertThat(s.getObjectProps().keySet(), empty());
// Check out the field.
Schema.Field f = s.getFields().get(0);
assertThat(f.name(), is("valid"));
assertThat(f.schema().getType(), is(Schema.Type.BOOLEAN));
assertThat(f.schema().getObjectProps().keySet(), empty());
f = s.getField("def");
assertThat(f.name(), is("def"));
assertThat(f.schema().getType(), is(Schema.Type.BOOLEAN));
assertThat(f.getObjectProps().keySet(), containsInAnyOrder(TALEND_COLUMN_DEFAULT));
assertThat(f.getProp(TALEND_COLUMN_DEFAULT), is(Boolean.TRUE.toString()));
f = s.getField("date");
assertThat(f.name(), is("date"));
assertTrue(AvroUtils.isSameType(f.schema(), AvroUtils._date()));
assertThat(f.getObjectProps().keySet(), containsInAnyOrder(SchemaConstants.TALEND_COLUMN_PATTERN));
assertThat(f.getProp(SchemaConstants.TALEND_COLUMN_PATTERN), is("yyyy-MM-dd"));
f = s.getField("string_with_length");
assertThat(f.name(), is("string_with_length"));
assertTrue(AvroUtils.isSameType(f.schema(), AvroUtils._string()));
assertThat(f.getObjectProps().keySet(), containsInAnyOrder(SchemaConstants.TALEND_COLUMN_DB_LENGTH));
assertThat(f.getProp(SchemaConstants.TALEND_COLUMN_DB_LENGTH), is("20"));
f = s.getField("number_with_scale_and_precision");
assertThat(f.name(), is("number_with_scale_and_precision"));
assertTrue(AvroUtils.isSameType(f.schema(), AvroUtils._double()));
assertThat(f.getObjectProps().keySet(), containsInAnyOrder(SchemaConstants.TALEND_COLUMN_DB_LENGTH, SchemaConstants.TALEND_COLUMN_PRECISION));
assertThat(f.getProp(SchemaConstants.TALEND_COLUMN_DB_LENGTH), is("10"));
assertThat(f.getProp(SchemaConstants.TALEND_COLUMN_PRECISION), is("2"));
f = s.getField("double_with_nullable");
assertThat(f.name(), is("double_with_nullable"));
assertThat(f.schema().getType(), is(Schema.Type.UNION));
assertThat(f.schema().getTypes(), containsInAnyOrder(AvroUtils._double(), Schema.create(Schema.Type.NULL)));
assertThat(f.getObjectProps().keySet(), containsInAnyOrder(SchemaConstants.TALEND_COLUMN_DB_LENGTH, SchemaConstants.TALEND_COLUMN_PRECISION));
assertThat(f.getProp(SchemaConstants.TALEND_COLUMN_DB_LENGTH), is("18"));
assertThat(f.getProp(SchemaConstants.TALEND_COLUMN_PRECISION), is("15"));
}
use of com.sforce.soap.partner.DescribeSObjectResult in project components by Talend.
the class SalesforceAvroRegistryTest method testInferSchemaWithReferenceField.
@Test
public void testInferSchemaWithReferenceField() {
Field referenceField = new Field();
referenceField.setName("reference");
referenceField.setType(FieldType.string);
referenceField.setReferenceTo(new String[] { "SomeRecord" });
referenceField.setRelationshipName("relationship");
DescribeSObjectResult dsor = new DescribeSObjectResult();
dsor.setName("MySObjectRecord");
dsor.setFields(new Field[] { referenceField });
Schema schema = sRegistry.inferSchema(dsor);
Schema.Field field = schema.getField("reference");
assertThat(field.schema().getType(), is(Schema.Type.STRING));
assertThat(field.getProp(SalesforceSchemaConstants.REF_MODULE_NAME), is("SomeRecord"));
assertThat(field.getProp(SalesforceSchemaConstants.REF_FIELD_NAME), is("relationship"));
}
Aggregations