private FieldDescriptorProto createFieldDescriptorProto(String name, int index, Type type, String typeName, Label label, String defaultValue, String extendee, UnknownFieldSet unknownFields, FieldOptions options) { FieldDescriptorProto.Builder fieldBuilder = FieldDescriptorProto.newBuilder(); return fieldBuilder .setName(name) .setNumber(index) .setType(type) .setTypeName(typeName) .setLabel(label) .setDefaultValue(defaultValue) .setExtendee(extendee) .setUnknownFields(unknownFields) .setOptions(options) .build(); }
/** * 插入对象到给定的表中。注意,这里并不自动生成ID。 * * @param message * @param tableName * @return */ public long insert(M message, String tableName) { StringBuilder insertSql = new StringBuilder("insert into "); insertSql.append(tableName).append("("); StringBuilder fields = new StringBuilder(""); StringBuilder values = new StringBuilder(""); List<Object> args = new ArrayList<Object>(); Map<FieldDescriptor, Object> fieldMap = message.getAllFields(); for (Entry<FieldDescriptor, Object> entry : fieldMap.entrySet()) { FieldDescriptor fieldDescriptor = entry.getKey(); FieldOptions fieldOptions = fieldDescriptor.getOptions(); ColumnFieldOption columnFieldOption = fieldOptions .getExtension(Taglib.columnOption); String fieldName = fieldDescriptor.getName(); Object value = entry.getValue(); if (columnFieldOption.getColumnType() == ColumnType.DATETIME || columnFieldOption.getColumnType() == ColumnType.TIMESTAMP) {// datetime类型 if (value != null && (long) value > 0) { fields.append('`').append(fieldName).append("`,"); values.append("?, "); args.add(new Timestamp((long) value)); } } else { fields.append('`').append(fieldName).append("`,"); values.append("?, "); args.add(value); } } int tmpIndex = fields.lastIndexOf(","); insertSql.append(fields.substring(0, tmpIndex)).append(") values("); tmpIndex = values.lastIndexOf(","); insertSql.append(values.substring(0, tmpIndex)).append(")"); String sql = insertSql.toString(); return update(sql, args); }
public CausalOrderProtobufSerializer(@NotNull final Serializer<T> objectSerializer) { this.objectSerializer = objectSerializer; try { final FileDescriptorProto timestampedMessageFile = FileDescriptorProto.newBuilder() .addMessageType( DescriptorProto.newBuilder() .setName(MESSAGE_NAME) .addField( FieldDescriptorProto.newBuilder() .setLabel(FieldDescriptorProto.Label.LABEL_REPEATED) .setName(IDS_FIELD_NAME) .setNumber(IDS_FIELD_NUMBER) .setType(FieldDescriptorProto.Type.TYPE_BYTES)) .addField( FieldDescriptorProto.newBuilder() .setLabel(FieldDescriptorProto.Label.LABEL_REPEATED) .setName(TIMESTAMPS_FIELD_NAME) .setNumber(TIMESTAMPS_FIELD_NUMBER) .setOptions(FieldOptions.newBuilder() .setPacked(true)) .setType(FieldDescriptorProto.Type.TYPE_UINT64)) .addField( FieldDescriptorProto.newBuilder() .setName(VALUE_FIELD_NAME) .setNumber(VALUE_FIELD_NUMBER) .setType(FieldDescriptorProto.Type.TYPE_BYTES))) .build(); this.messageDescriptor = FileDescriptor .buildFrom(timestampedMessageFile, new FileDescriptor[0]) .findMessageTypeByName(MESSAGE_NAME); this.ids = messageDescriptor.findFieldByName(IDS_FIELD_NAME); this.timestamps = messageDescriptor.findFieldByName(TIMESTAMPS_FIELD_NAME); this.value = messageDescriptor.findFieldByName(VALUE_FIELD_NAME); this.messageParser = DynamicMessage.newBuilder(messageDescriptor) .buildPartial() .getParserForType(); } catch (final DescriptorValidationException e) { throw new RuntimeException(e); } }
private FieldDescriptorProto generateField(Field field) { FieldDescriptorProto.Builder builder = FieldDescriptorProto.newBuilder(); builder.setName(getFieldName(field)); builder.setNumber(field.getNumber()); builder.setLabel(toLabel(field.getCardinality())); builder.setType(toType(field.getKind())); if (field.getKind() == Kind.TYPE_ENUM || field.getKind() == Kind.TYPE_MESSAGE || field.getKind() == Kind.TYPE_GROUP) { builder.setTypeName(getTypeName(field.getTypeUrl())); } // NOTE: extendee not supported // NOTE: default_value not supported if (field.getOneofIndex() != 0) { // Index in the containing type's oneof_decl is zero-based. // Index in google.protobuf.type.Field.oneof_index is one-based. builder.setOneofIndex(field.getOneofIndex() - 1); } if (!Strings.isNullOrEmpty(field.getDefaultValue())) { builder.setDefaultValue(field.getDefaultValue()); } FieldOptions options = getFieldOptions(field); if (!options.equals(FieldOptions.getDefaultInstance())) { builder.setOptions(options); } return builder.build(); }
private FieldOptions getFieldOptions(Field field) { FieldOptions.Builder builder = FieldOptions.newBuilder(); if (field.getPacked()) { builder.setPacked(true); } setOptions(builder, field.getOptionsList(), FIELD_OPTION_NAME_PREFIX); return builder.build(); }
protected void addColumn(Column column) { String fieldName = uniqueIdent(ident(column.getName(), false), fieldNames); fieldBuilder = messageBuilder.addFieldBuilder(); fieldBuilder.setName(fieldName); fieldBuilder.setLabel(Label.LABEL_OPTIONAL); FieldOptions.Builder fieldBuilderOptions = FieldOptions.newBuilder(); ColumnOptions.Builder columnOptions = ColumnOptions.newBuilder(); if (!fieldName.equals(column.getName())) { columnOptions.setName(column.getName()); } columnOptions.setSqlType(column.getTypeDescription().toUpperCase()); columnOptions.setUuid(column.getUuid().toString()); priorField = null; if (priorMessage != null) { for (FieldDescriptorProto field : priorMessage.getFieldList()) { FieldOptions options = field.getOptions(); if ((options != null) && (options.hasExtension(ColumnOptions.fdbsql))) { ColumnOptions coptions = options.getExtension(ColumnOptions.fdbsql); if (coptions.getUuid().equals(columnOptions.getUuid())) { priorField = field; break; } } } } setColumnType(column, columnOptions); setFieldNumber(); fieldBuilderOptions.setExtension(ColumnOptions.fdbsql, columnOptions.build()); fieldBuilder.setOptions(fieldBuilderOptions); if (column.getNullable() && ((column.getDefaultValue() != null) || (column.getDefaultFunction() != null))) { addNullForField(column.getName(), fieldBuilder.getNumber()); } }
protected void addNullForField(String columnName, int forField) { String fieldName = uniqueIdent("_" + ident(columnName, false) + "_is_null", fieldNames); fieldBuilder = messageBuilder.addFieldBuilder(); fieldBuilder.setName(fieldName); fieldBuilder.setType(Type.TYPE_BOOL); fieldBuilder.setLabel(Label.LABEL_OPTIONAL); FieldOptions.Builder fieldBuilderOptions = FieldOptions.newBuilder(); ColumnOptions.Builder columnOptions = ColumnOptions.newBuilder(); columnOptions.setNullForField(forField); priorField = null; if (priorMessage != null) { for (FieldDescriptorProto field : priorMessage.getFieldList()) { FieldOptions options = field.getOptions(); if ((options != null) && (options.hasExtension(ColumnOptions.fdbsql))) { ColumnOptions coptions = options.getExtension(ColumnOptions.fdbsql); if (coptions.hasNullForField() && (coptions.getNullForField() == forField)) { priorField = field; break; } } } } setFieldNumber(); fieldBuilderOptions.setExtension(ColumnOptions.fdbsql, columnOptions.build()); fieldBuilder.setOptions(fieldBuilderOptions); }
protected void addChildTable(Table table) { String fieldName = uniqueIdent(ident(table.getName().getTableName(), false), fieldNames); fieldBuilder = messageBuilder.addFieldBuilder(); fieldBuilder.setName(fieldName); fieldBuilder.setLabel(Label.LABEL_REPEATED); fieldBuilder.setType(Type.TYPE_MESSAGE); fieldBuilder.setTypeName(tableMessageNames.get(table)); FieldOptions.Builder fieldBuilderOptions = FieldOptions.newBuilder(); ColumnOptions.Builder columnOptions = ColumnOptions.newBuilder(); columnOptions.setUuid(table.getUuid().toString()); priorField = null; if (priorMessage != null) { for (FieldDescriptorProto field : priorMessage.getFieldList()) { FieldOptions options = field.getOptions(); if ((options != null) && (options.hasExtension(ColumnOptions.fdbsql))) { ColumnOptions coptions = options.getExtension(ColumnOptions.fdbsql); if (coptions.getUuid().equals(columnOptions.getUuid())) { priorField = field; break; } } } } setFieldNumber(); fieldBuilderOptions.setExtension(ColumnOptions.fdbsql, columnOptions.build()); fieldBuilder.setOptions(fieldBuilderOptions); }
/** * * @param message * @param conditionFields * @param conditionParams * @param tableName * @return */ protected int updateMessageByCondition(M message, String[] conditionFields, Object[] conditionParams, String tableName) { StringBuilder updateSql = new StringBuilder("update "); updateSql.append(tableName).append(" set "); StringBuilder options = new StringBuilder(""); List<Object> args = new ArrayList<Object>(); Map<FieldDescriptor, Object> fieldMap = message.getAllFields(); for (Entry<FieldDescriptor, Object> entry : fieldMap.entrySet()) { FieldDescriptor fieldDescriptor = entry.getKey(); if (!Arrays.asList(conditionFields).contains( fieldDescriptor.getName())) { FieldOptions fieldOptions = fieldDescriptor.getOptions(); ColumnFieldOption columnFieldOption = fieldOptions .getExtension(Taglib.columnOption); String fieldName = fieldDescriptor.getName(); Object value = entry.getValue(); if (columnFieldOption.getColumnType() == ColumnType.DATETIME || columnFieldOption.getColumnType() == ColumnType.TIMESTAMP) {// datetime类型 if (value != null && (long) value > 0) { options.append(fieldName).append("=?, "); args.add(new Timestamp((long) value)); } } else { options.append(fieldName).append("=?, "); args.add(value); } } } int tmpIndex = options.lastIndexOf(","); updateSql.append(options.substring(0, tmpIndex)).append(" where 1=1 "); StringBuilder condition = new StringBuilder(); if (conditionFields.length != conditionParams.length) { throw new IllegalArgumentException("condition error"); } else { for (int i = 0; i < conditionFields.length; i++) { condition.append("AND ").append(conditionFields[i]) .append("=? "); args.add(conditionParams[i]); } updateSql.append(condition); String sql = updateSql.toString(); logger.debug(sql); return update(sql, args); } }
/** Get the {@code FieldOptions}, defined in {@code descriptor.proto}. */ public FieldOptions getOptions() { return proto.getOptions(); }
/** * Get the {@code FieldOptions}, defined in {@code descriptor.proto}. */ public FieldOptions getOptions () { return proto.getOptions (); }
@Override public void exitStandardFieldOptionCTypeOption(final StandardFieldOptionCTypeOptionContext ctx) { verifyOptionNameUnique("ctype", ctx.getStart()); scopes.getFieldOptions().setCtype(FieldOptions.CType.valueOf(ctx.cType().getText())); }
public FieldOptions.Builder getFieldOptions() { return currentScope.getFieldOptions(); }
protected FieldOptions.Builder getFieldOptions() { throw new RuntimeException(NOT_APPLICABLE_IN_CURRENT_SCOPE); }
@Override protected FieldOptions.Builder getFieldOptions() { return protoBuilder.getOptionsBuilder(); }