映射可变长度数据到固定结构的最佳方法是什么?

huangapple go评论64阅读模式
英文:

Best approach to map data of variable length to a fixed structure?

问题

给定一个固定的目标数据表,如:

 1      2         3       4      5
 西班牙,葡萄牙,法国,意大利,希腊

输入包括数据列以及一个可配置的启用列列表,例如 [1, 4],即输入可能只包含 西班牙意大利

目的是构建一个 SQL 语句(虽然本质上不是 SQL 问题),该语句仅会将在输入中可用的列设置到它们正确的位置。

可能的输入:

启用的列:2,4,5
数据:
    p1    i1    g1
    p2    i2    g2
    .............

试图以编程方式实现这一点(使用 Java),而不是使用 SQL,但不太确定如何避免疯狂地分支使用 ifelse

这个问题不仅限于特定情况,也不仅限于 SQL 或其他编程语言,而更关注于如何根据某种形式的定义(例如这种情况下的索引)将可变输入映射到固定输出。

英文:

Given a fixed target data table such as, e.g.

 1      2         3       4      5
 Spain, Portugal, France, Italy, Greece

the input consists of data columns plus a configurable list of enabled columns such as [1, 4] i.e. the input might only contain Spain and Italy.

The purpose is to build an SQL statement (although it's not an SQL question per se) that will only set the columns that are available in the input to their correct positions.

Possible input:

enabled columns: 2,4,5
data:
    p1    i1    g1
    p2    i2    g2
    .............

Attempting to do this programmatically (in java), not in SQL, and not quite sure how to avoid crazy branching of if's and else's.

The question is not just about this specific case, and is not really sql or another programming language, but rather about how to map variable inputs to a fixed output given a definition of some form, e.g. indices in this case.

答案1

得分: 2

如果我没有误解您的问题,您想在SQL中生成一系列的INSERT INTO命令,以向表中添加新的元组。这些元组包含在您程序输入中未传递的字段,使用一些默认值(例如NULL)。因此,在您的示例中,您想要生成以下SQL语句:

INSERT INTO YOUR_TABLE_NAME(NULL,p1,NULL,i1,g1);
INSERT INTO YOUR_TABLE_NAME(NULL,p2,NULL,i2,g2);

然后,此函数可以按照以下方式实现:

    public static String insertOptional(int[] inputColumns, String[][] data) {
        // 简单算法的开始
        final String tableName = "YOUR_TABLE_NAME";
        final int columnsOfTable = 5; // 就像您的示例中一样

        StringBuilder sqlStatement = new StringBuilder();

        for (String[] tuple : data) {
            sqlStatement.append("INSERT INTO ")
                    .append(tableName)
                    .append("(");
            int inputColumnIndex = 0;
            for (int columnIndex = 1; columnIndex <= columnsOfTable; columnIndex++) {
                if (columnIndex == inputColumns[inputColumnIndex]) {
                    sqlStatement.append(tuple[inputColumnIndex]);
                    inputColumnIndex++;
                } else {
                    // 用未给定输入的字段的默认值替换"NULL"
                    sqlStatement.append("NULL");
                }
                if (columnIndex < columnsOfTable)
                    sqlStatement.append(",");
            }
            sqlStatement.append(");\n");
        }
        return sqlStatement.toString();
    }

而且,您的示例可以按照以下方式执行:

    public static void main(String[] args) {
        int[] inputColumns = { 2, 4, 5 };
        String[][] data = { { "p1", "i1", "g1" }, { "p2", "i2", "g2" } };
        String sqlStatement = insertOptional(inputColumns, data);
        System.out.println(sqlStatement);
    }

此主方法会打印出您期望的内容:

INSERT INTO YOUR_TABLE_NAME(NULL,p1,NULL,i1,g1);
INSERT INTO YOUR_TABLE_NAME(NULL,p2,NULL,i2,g2);
英文:

If I have not misunderstood your question, you want generate a sequence of INSERT INTO commands in SQL, to add new tuples to a table, holding the fileds that are not passed in the input of your program with some default value (e.g. NULL). So, in your example, you want generate the following SQL statement:

INSERT INTO YOUR_TABLE_NAME(NULL,p1,NULL,i1,g1);
INSERT INTO YOUR_TABLE_NAME(NULL,p2,NULL,i2,g2);

Then, this function can be implemented as follows:

    public static String insertOptional(int[] inputColumns, String[][] data) {
		// start of the simple algorithm
		final String tableName = &quot;YOUR_TABLE_NAME&quot;;
		final int columnsOfTable = 5; // as in your example

		StringBuilder sqlStatement = new StringBuilder();

		for (String[] tuple : data) {
			sqlStatement.append(&quot;INSERT INTO &quot;)
					.append(tableName)
					.append(&quot;(&quot;);
			int inputColumnIndex = 0;
			for (int columnIndex = 1; columnIndex &lt;= columnsOfTable; columnIndex++) {
				if (columnIndex == inputColumns[inputColumnIndex]) {
					sqlStatement.append(tuple[inputColumnIndex]);
					inputColumnIndex++;
				} else {
					// replace &quot;NULL&quot; with your default value for
					// fields that are not given as input
					sqlStatement.append(&quot;NULL&quot;);
				}
				if (columnIndex &lt; columnsOfTable)
					sqlStatement.append(&quot;,&quot;);
			}
			sqlStatement.append(&quot;);\n&quot;);
		}
		return sqlStatement.toString();
	}

and, your example can be executed as follows:

	public static void main(String[] args) {
		int[] inputColumns = { 2, 4, 5 };
		String[][] data = { { &quot;p1&quot;, &quot;i1&quot;, &quot;g1&quot; }, { &quot;p2&quot;, &quot;i2&quot;, &quot;g2&quot; } };
		String sqlStatement = insertOptional(inputColumns, data);
		System.out.println(sqlStatement);
	}

This main method prints exactly what I thought you expect:

INSERT INTO YOUR_TABLE_NAME(NULL,p1,NULL,i1,g1);
INSERT INTO YOUR_TABLE_NAME(NULL,p2,NULL,i2,g2);

huangapple
  • 本文由 发表于 2020年3月16日 04:21:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/60697274.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定