HomeMogDBMogDB StackUqbar
v5.0

Documentation:v5.0

Supported Versions:

Other Versions:

Error When Writing Illegal Characters

Availability

This feature is available since MogDB 5.0.2.

Introduction

Supports error reporting for input data not conforming to encoding rules when the server and client character set encodings are the same.

Benefits

Output illegal character set error message to facilitate users to repair the fault in time to ensure the normal operation of the system.

Description

Currently, when the server-side and client-side encoding is the same, data written through JDBC and other means will be modified to '?' by default if illegal characters appear. and there is no prompt message, resulting in mismatch between the actual data written and the expected data.

MogDB 5.0.2 version added session level USERSET type parameter emit_illegal_bind_chars, which is used to control whether to report error for illegal characters. The default value of this parameter is off, which is compatible with the behavior of the old version (no error reporting). Change the parameter value to on to enable error reporting.

Constraints

This feature is only related to the server-side and client-side character set is consistent, through the JDBC and other client-side PBE mode execution and the number of parameters is greater than 0, illegal characters in the parameters to report an error, the following cases are not within the scope of this feature:

  1. the character set is the same, but the number of parameters is 0, for the illegal characters in the statement, the current behavior is already an error.
  2. statements executed via gsql, etc., where the behavior is already an error.
  3. the behavior of the copy statement is controlled by COMPATIBLE_ILLEGAL_CHARS, which is out of the scope of this feature.
  4. When the server-side and client-side character sets do not match, whether or not to report an error is determined by the conversion function, and is not within the scope of this feature.

Examples

Create test database and table for use in subsequent DML statements.

CREATE DATABASE db_gbk TEMPLATE template0 encoding 'GBK' lc_ctype 'zh_CN.GBK' lc_collate 'zh_CN.GBK';

CREATE TABLE test (a integer,b character varying);

Using Go as an example, write code that connects to a database and executes a DML statement.

package main

import (
    "database/sql"
    "encoding/hex"
    _ "github.com/lib/pq"
)

func main() {
    db, err: = sql.Open("postgres", "port=5434 user=test password=Qwer1234 dbname=db_gbk sslmode=disable")
    checkErr(err)
    defer db.Close()
    _, err = db.Exec("set client_encoding = 'GBK'");
    checkErr(err)
    _, err = db.Exec("set emit_illegal_bind_chars = on")
    checkErr(err)
    s: = "8139EF31"
    b, _: = hex.DecodeString(s)
    _, err = db.Exec("insert into test values ($1, $2)", 1, string(b))
    checkErr(err)
}

func checkErr(err error) {
    if err != nil {
        panic(err)
    }
}

An error was reported:

panic: pq: invalid byte sequence for encoding "GBK": 0x81 0x39

goroutine 1 [running]:
main.checkErr(...)
        /home/test/Documents/test_go/main.go:25
main.main()
        /home/test/Documents/test_go/main.go:20 +0x225

emit_illegal_bind_chars

Copyright © 2011-2024 www.enmotech.com All rights reserved.