Java Code Examples for com.google.protobuf.DescriptorProtos.FieldOptions#getExtension()

The following examples show how to use com.google.protobuf.DescriptorProtos.FieldOptions#getExtension() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JdbcProtobufTemplate.java    From jigsaw-payment with Apache License 2.0 5 votes vote down vote up
/**
 * 插入对象到给定的表中。注意,这里并不自动生成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);
}
 
Example 2
Source File: AISToProtobuf.java    From sql-layer with GNU Affero General Public License v3.0 5 votes vote down vote up
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());
    }
}
 
Example 3
Source File: AISToProtobuf.java    From sql-layer with GNU Affero General Public License v3.0 5 votes vote down vote up
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);
}
 
Example 4
Source File: AISToProtobuf.java    From sql-layer with GNU Affero General Public License v3.0 5 votes vote down vote up
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);
}
 
Example 5
Source File: ProtoTypeAdapter.java    From gson with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieves the custom field name from the given options, and if not found, returns the specified
 * default name.
 */
private String getCustSerializedName(FieldOptions options, String defaultName) {
  for (Extension<FieldOptions, String> extension : serializedNameExtensions) {
    if (options.hasExtension(extension)) {
      return options.getExtension(extension);
    }
  }
  return protoFormat.to(jsonFormat, defaultName);
}
 
Example 6
Source File: JdbcProtobufTemplate.java    From jigsaw-payment with Apache License 2.0 4 votes vote down vote up
/**
 * 
 * @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);
	}
}