private InsertSql buildInsertSql()

in holo-client/src/main/java/com/alibaba/hologres/client/impl/UnnestUpsertStatementBuilder.java [121:226]


	private InsertSql buildInsertSql(Tuple4<TableSchema, TableName, WriteMode, CheckAndPutCondition> tuple, Tuple<BitSet, BitSet> input) {
		TableSchema schema = tuple.f0;
		TableName tableName = tuple.f1;
		WriteMode mode = tuple.f2;
		CheckAndPutCondition checkAndPutCondition = tuple.f3;
		BitSet set = input.l;
		BitSet onlyInsertSet = input.r;
		boolean isUnnest = false;
		StringBuilder sb = new StringBuilder();
		sb.append("insert into ").append(tableName.getFullName());

		// 无论是与表中已有数据还是常量比较,都需要as重命名为old
		if (checkAndPutCondition != null) {
			sb.append(" as old ");
		}

		sb.append("(");
		first = true;
		set.stream().forEach((index) -> {
			if (!first) {
				sb.append(",");
			}
			first = false;
			sb.append(IdentifierUtil.quoteIdentifier(schema.getColumn(index).getName(), true));
		});
		sb.append(")");

		if (isSupportUnnest(schema, input)) {
			isUnnest = true;
			sb.append(" select ");
			first = true;
			set.stream().forEach((index) -> {
				if (!first) {
					sb.append(",");
				}
				first = false;
				Column column = schema.getColumn(index);
				sb.append("unnest(?::").append(getRealTypeName(column.getType(), column.getTypeName())).append("[])");
				/*Column column = schema.getColumn(index);
				if (Types.BIT == column.getType() && "bit".equals(column.getTypeName())) {
					sb.append("::bit(").append(column.getPrecision()).append(")");
				} else if (Types.OTHER == column.getType() && "varbit".equals(column.getTypeName())) {
					sb.append("::bit varying(").append(column.getPrecision()).append(")");
				}*/
			});

		} else {

			sb.append(" values (");
			first = true;
			set.stream().forEach((index) -> {
				if (!first) {
					sb.append(",");
				}
				first = false;
				sb.append("?");
				Column column = schema.getColumn(index);
				if (Types.BIT == column.getType() && "bit".equals(column.getTypeName())) {
					sb.append("::bit(").append(column.getPrecision()).append(")");
				} else if (Types.OTHER == column.getType() && "varbit".equals(column.getTypeName())) {
					sb.append("::bit varying(").append(column.getPrecision()).append(")");
				}
			});
			sb.append(")");
		}
		if (schema.getKeyIndex().length > 0) {
			sb.append(" on conflict (");
			first = true;
			for (int index : schema.getKeyIndex()) {
				if (!first) {
					sb.append(",");
				}
				first = false;
				sb.append(IdentifierUtil.quoteIdentifier(schema.getColumnSchema()[index].getName(), true));
			}
			sb.append(") do ");
			if (WriteMode.INSERT_OR_IGNORE == mode) {
				sb.append("nothing");
			} else {
				sb.append("update set ");
				first = true;
				set.stream().forEach((index) -> {
					if (!onlyInsertSet.get(index)) {
						if (!first) {
							sb.append(",");
						}
						first = false;
						String columnName = IdentifierUtil.quoteIdentifier(schema.getColumnSchema()[index].getName(), true);
						sb.append(columnName).append("=excluded.").append(columnName);
					}
				});
			}
			if (checkAndPutCondition != null) {
				sb.append(" where");
				sb.append(buildCheckAndPutPattern(checkAndPutCondition));
			}
		}

		String sql = sb.toString();

		LOGGER.debug("new sql:{}", sql);
		InsertSql insertSql = new InsertSql();
		insertSql.isUnnest = isUnnest;
		insertSql.sql = sql;
		return insertSql;
	}