Dastin_DV
Dastin_DV

Reputation: 11

How to convert unicode to ebcdic CCSIDs-1025 and write to a db2

My problem is that the input is an XML file with UTF-8 encoding.
The database is encoded with CCSIDs-1025 (DB2).
The application itself is windows-1251 encoded.

After parsing the XML, I save the data to a regular char array. Of course, in my system there is no mapping for Unicode conversion and the array stores the correct byte representation, but something like this: "RњRRќRR •R RЎRR'Rћ PYPyPPŽPP R R•RРRЈR'R›RRR R R•R›RR RRR€ RR™R†R"

It's not a problem. I can convert it to windows-1251 and write it correctly to DB2 with ebcdic or work in my code.
But!

Is it possible to directly convert unicode to EBCDIC before writing to a table?

I Found one useful instrument ICU4C and tried to convert "Moscow" (Москва) to a EBCDIC CCSIDs-1025.

        ```lang-cpp
        // CONVERTION USING ICU

        UChar source[] = { 0x041C, 0x043E, 0x0441, 0x043A, 0x0432,
                     0x0430, 0x0021, 0x0000 };
        char target[100];
        UErrorCode status = U_ZERO_ERROR;
        UConverter *conv;
        int32_t     len;

        // set up the converter
        //! [ucnv_open]

        conv = ucnv_openCCSID(1154, UCNV_IBM, &status);
        //! [ucnv_open]
        assert(U_SUCCESS(status));

        // convert to EBCDIC-1154.
        len = ucnv_fromUChars(conv, target, 100, source, -1, &status);
        assert(U_SUCCESS(status));
        ```

And again it converted correctly. And codes of symbols corresponds to a target CCSID.
\xCF \x9E \xAB \x9A \xAF \x77 \x4F - this is the result. But how i can store result and insert it to a table with correct mapping? It still takes symbols from windows-1251 (my application encoding) and "Москва" mapped as "ПћљЇwO". How can i said to DB2 to use characters from CCSIDs-1025.

Also i use embedded SQL and host variables in c++ application. May be i need to set encoding of host variables during bind?

Thank you!

Upvotes: 1

Views: 1136

Answers (1)

kendrick
kendrick

Reputation: 26

@Dastin_DV regarding your questions about CCSID for embedded SQL and host variables and the data encoding scheme your application handles, I'd like to explain it further here.

In Db2 for z/OS applications, one CCSID is associated with the source code and one or more CCSIDs can be associated with the data that your application manipulates. The CCSID that Db2 associates with the data is called the application encoding scheme.

There are a few general rules:

  • for application source code which includes SQL statements and literal strings in the SQL statements, if you are using the Db2 precompiler, use the CCSID SQL processing option when you precompile the application, specify the same CCSID when you compile the application; if you are using the Db2 coprocessor, use the language compiler to set the CCSID.

  • for application data, like values that are passed through host variables and parameter markers, within SQL statements, use one or more of the following Db2 mechanisms to set the CCSID value of the application data, which is called the application encoding scheme

    • use the ENCODING bind option.2.c This option typically yields the best performance.
    • override the CCSID for a particular host variable by using the DECLARE VARIABLE statement with the CCSID option.
    • override the CCSID for parameter markers in dynamic SQL by specifying the CURRENT APPLICATION ENCODING SCHEME special register. Db2 uses the value of this special register at the time that the statement is executed
  • for application data that is referenced outside of SQL statements, use the rules of the programming language, in some cases, the CCSID of this data is the same as the CCSID of the source code.

Upvotes: 0

Related Questions