@@ -302,10 +302,13 @@ _mysql_ResultObject_Initialize(
302
302
long flags = fields [i ].flags ;
303
303
PyObject * fun2 = NULL ;
304
304
int j , n2 = PySequence_Size (fun );
305
- if (fields [i ].charsetnr != 63 ) { /* maaagic */
306
- flags &= ~BINARY_FLAG ;
307
- } else {
305
+ // BINARY_FLAG means ***_bin collation is used.
306
+ // To distinguish text and binary, we shoud use charsetnr==63 (binary).
307
+ // But we abuse BINARY_FLAG for historical reason.
308
+ if (fields [i ].charsetnr == 63 ) {
308
309
flags |= BINARY_FLAG ;
310
+ } else {
311
+ flags &= ~BINARY_FLAG ;
309
312
}
310
313
for (j = 0 ; j < n2 ; j ++ ) {
311
314
PyObject * t = PySequence_GetItem (fun , j );
@@ -1098,58 +1101,61 @@ _mysql_field_to_python(
1098
1101
MYSQL_FIELD * field ,
1099
1102
const char * encoding )
1100
1103
{
1101
- PyObject * v ;
1102
- #ifdef IS_PY3K
1103
- int field_type = field -> type ;
1104
- // Return bytes for binary and string types.
1105
- int binary = 0 ;
1106
- if (field_type == FIELD_TYPE_TINY_BLOB ||
1107
- field_type == FIELD_TYPE_MEDIUM_BLOB ||
1108
- field_type == FIELD_TYPE_LONG_BLOB ||
1109
- field_type == FIELD_TYPE_BLOB ||
1110
- field_type == FIELD_TYPE_VAR_STRING ||
1111
- field_type == FIELD_TYPE_STRING ||
1112
- field_type == FIELD_TYPE_GEOMETRY ||
1113
- field_type == FIELD_TYPE_BIT ) {
1114
- binary = 1 ;
1104
+ if (rowitem == NULL ) {
1105
+ Py_RETURN_NONE ;
1115
1106
}
1116
- #endif
1117
- if (rowitem ) {
1118
- if (converter == (PyObject * )& PyUnicode_Type ) {
1119
- if (encoding == utf8 ) {
1120
- //fprintf(stderr, "decoding with utf8!\n");
1121
- v = PyUnicode_DecodeUTF8 (rowitem , length , NULL );
1122
- } else {
1123
- //fprintf(stderr, "decoding with %s\n", encoding);
1124
- v = PyUnicode_Decode (rowitem , length , encoding , NULL );
1125
- }
1126
- }
1127
- else if (converter == (PyObject * )& PyBytes_Type || converter == Py_None ) {
1128
- //fprintf(stderr, "decoding with bytes\n", encoding);
1129
- v = PyBytes_FromStringAndSize (rowitem , length );
1130
- }
1131
- else if (converter == (PyObject * )& PyInt_Type ) {
1132
- //fprintf(stderr, "decoding with int\n", encoding);
1133
- v = PyInt_FromString (rowitem , NULL , 10 );
1107
+
1108
+ // Fast paths for int, string and binary.
1109
+ if (converter == (PyObject * )& PyUnicode_Type ) {
1110
+ if (encoding == utf8 ) {
1111
+ //fprintf(stderr, "decoding with utf8!\n");
1112
+ return PyUnicode_DecodeUTF8 (rowitem , length , NULL );
1113
+ } else {
1114
+ //fprintf(stderr, "decoding with %s\n", encoding);
1115
+ return PyUnicode_Decode (rowitem , length , encoding , NULL );
1134
1116
}
1135
- else {
1136
- //fprintf(stderr, "decoding with callback\n");
1137
- //PyObject_Print(converter, stderr, 0);
1138
- //fprintf(stderr, "\n");
1139
- v = PyObject_CallFunction (converter ,
1117
+ }
1118
+ if (converter == (PyObject * )& PyBytes_Type || converter == Py_None ) {
1119
+ //fprintf(stderr, "decoding with bytes\n", encoding);
1120
+ return PyBytes_FromStringAndSize (rowitem , length );
1121
+ }
1122
+ if (converter == (PyObject * )& PyInt_Type ) {
1123
+ //fprintf(stderr, "decoding with int\n", encoding);
1124
+ return PyInt_FromString (rowitem , NULL , 10 );
1125
+ }
1126
+
1127
+ //fprintf(stderr, "decoding with callback\n");
1128
+ //PyObject_Print(converter, stderr, 0);
1129
+ //fprintf(stderr, "\n");
1140
1130
#ifdef IS_PY3K
1141
- binary ? "y#" : "s#" ,
1131
+ int binary ;
1132
+ switch (field -> type ) {
1133
+ case FIELD_TYPE_TINY_BLOB :
1134
+ case FIELD_TYPE_MEDIUM_BLOB :
1135
+ case FIELD_TYPE_LONG_BLOB :
1136
+ case FIELD_TYPE_BLOB :
1137
+ case FIELD_TYPE_VAR_STRING :
1138
+ case FIELD_TYPE_STRING :
1139
+ case FIELD_TYPE_GEOMETRY :
1140
+ case FIELD_TYPE_BIT :
1141
+ #ifdef FIELD_TYPE_JSON
1142
+ case FIELD_TYPE_JSON :
1142
1143
#else
1143
- "s#" ,
1144
+ case 245 : // JSON
1144
1145
#endif
1145
- rowitem ,
1146
- (int )length );
1147
- }
1148
- } else {
1149
- Py_INCREF (Py_None );
1150
- v = Py_None ;
1146
+ // Call converter with bytes
1147
+ binary = 1 ;
1148
+ default : // e.g. FIELD_TYPE_DATETIME, etc.
1149
+ // Call converter with unicode string
1150
+ binary = 0 ;
1151
1151
}
1152
- return v ;
1152
+ return PyObject_CallFunction (converter ,
1153
+ binary ? "y#" : "s#" ,
1154
+ rowitem , (int )length );
1155
+ #else
1156
+ return PyObject_CallFunction (converter ,
1157
+ "s#" , rowitem , (int )length );
1158
+ #endif
1153
1159
}
1154
1160
1155
1161
static PyObject *
@@ -1226,7 +1232,7 @@ _mysql_row_to_dict_old(
1226
1232
unsigned int n , i ;
1227
1233
unsigned long * length ;
1228
1234
PyObject * r , * c ;
1229
- MYSQL_FIELD * fields ;
1235
+ MYSQL_FIELD * fields ;
1230
1236
1231
1237
n = mysql_num_fields (self -> result );
1232
1238
if (!(r = PyDict_New ())) return NULL ;
0 commit comments