• Compartir
  • ?
  • Perfiles ▼
  • Comunidades ▼
  • Aplicaciones ▼

Blogs

  • Mis blogs
  • Blogs públicos
  • Mis actualizaciones

Esta comunidad puede tener miembros ajenos a la organización. Yonghang's DB2 Note

  • Iniciar sesión para participar

▼ Etiquetas

▼ Archivador

  • abril de 2015
  • julio de 2013
  • marzo de 2012
  • febrero de 2012
  • enero de 2012

▼ Autores de blog

Yonghang's DB2 Note

Ver todas las entradas
Si pulsa el botón se actualizará la página completa. El usuario podría ir a la región "Lista de entradas" para ver el nuevo contenido.) Lista de entradas

string_reverse with UTF-8 (2)

Yonghang 0600007VU3 | ‎ | 4.293 vistas

last time we found the physical layout of varchar column is not so "utf-8".   DB2 chooses to store the data that way out of some undocumented reason.  Until it's released, we should not trust what we guess even it's proven correct from limited test.

But the principal we should trust in, whatever db2 saves the data, it should deliver standard byte stream to its client.  Serge has the implementation with c.  this time I'll use Java.

 

Register a Java UDF stringscan(varchar(4000)) with below implementation,
 
 
public class StringScan{
 
        public static String scan(String instr) throws SQLException, Exception{
                String srtn="";
                for(int i=0; i<instr.length(); i++)
                {
                        char c=instr.charAt(i);
                        int it=c;
                        srtn += it;
                        srtn += " ";
 
                }
                return srtn;
        };
};
 

Take "天朝上国" as example,

in db2, the byte list is,

string                                 byte#    byte_value

------------------------------------------------------------

天朝上国                                       1         195
天朝上国                                       2         140
天朝上国                                       3         195
天朝上国                                       4         172
天朝上国                                       5         194
天朝上国                                       6         179
天朝上国                                       7         194
天朝上国                                       8         175
天朝上国                                       9         195
天朝上国                                      10         137
天朝上国                                      11         195
天朝上国                                      12         143
天朝上国                                      13         194
天朝上国                                      14         185
天朝上国                                      15         195
天朝上国                                      16         186

 

When it is passed to Java,

-- select vchar, varchar(stringscan(vchar),100) byte_list  from utf8test where vchar = '天朝上国'

天朝上国                     229 164 169 230 156 157 228 184 138 229 155 189

This time it looks all right.

To make it easier for more processing in pure SQL, we need several element function,

1.  character_width_utf8(varchar()),   how many byte for the first utf-8 character. return -1 when it's not first byte of a utf-8 character.

 

        public static int (String instr) throws SQLException, Exception{
                char c=instr.charAt(0);
                unsigned int it=c;
 
                int rtn=-1;
 
                if(it<128) {rtn=1}
                else if(it<224 && it>=192) {rtn=2}
                else if(it<240 && it>=224) {rtn=3}
                else if(it>=240) {rtn=4}
 
                return rtn;
 
        };
 

Then we can easily have more,

2. length_utf8

3. substr_utf8

4. replace_utf8

5. overlay_utf8

etc.

I'm planning a project as db2 external library. will treat utf-8 string functions as first part to implement.

Modificado el por Yonghang 0600007VU3
  • Añadir un comentario Añadir un comentario
  • Editar
  • Más acciones v
  • Poner esta entrada en cuarentena
Notificar a otras personas
notification

Enviar notificación por correo electrónico

+

Poner esta entrada en cuarentena

deleteEntry
duplicateEntry

Marcar como duplicado

  • Entrada anterior
  • Principal
  • Entrada siguiente
Canal de información para entradas de blogs | Canal de información para comentarios de blogs | Canal de información para comentarios en esta entrada