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

ccpizza :

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.

Salvatore Giampà :

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 = "YOUR_TABLE_NAME";
        final int columnsOfTable = 5; // as in your example

        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 {
                    // replace "NULL" with your default value for
                    // fields that are not given as input
                    sqlStatement.append("NULL");
                }
                if (columnIndex < columnsOfTable)
                    sqlStatement.append(",");
            }
            sqlStatement.append(");\n");
        }
        return sqlStatement.toString();
    }

and, your example can be executed as follows:

    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);
    }

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);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=291310&siteId=1