Ticket #2170: mysql_as_string.patch
| File mysql_as_string.patch, 5.6 KB (added by NotFound, 22 months ago) |
|---|
-
examples/Mysql.winxed
diff --git a/examples/Mysql.winxed b/examples/Mysql.winxed index cc389a3..436ba83 100644
a b 43 43 44 44 function str_to_cstring(string s) 45 45 { 46 var to_cstring = dlfunc(null, 'Parrot_str_to_cstring', 'ppS'); 47 return to_cstring(getinterp(), s); 48 } 49 50 function str_free_cstring(s) 51 { 52 var free_cstring = dlfunc(null, 'Parrot_str_free_cstring', 'vp'); 53 free_cstring(s); 46 var cstring = new ['ByteBuffer']; 47 cstring =: s; 48 push(cstring, 0); 49 return cstring; 54 50 } 55 51 56 52 function get_parrot_func(string name, string sig) … … 72 68 return found; 73 69 } 74 70 75 function raw_encode(string s, string encoding)76 {77 int got_encoding_num;78 string got_encoding;79 ${ encoding got_encoding_num, s };80 ${ encodingname got_encoding, got_encoding_num };81 if (got_encoding != encoding) {82 switch {83 case got_encoding == 'ascii' && encoding == 'iso-8859-1':84 case got_encoding == 'ascii' && encoding == 'utf8':85 // Compatible encodings, do nothing86 break;87 default:88 // trans_encoding is not useful here, must encode89 // from the raw bytes.90 var b = new ['ByteBuffer'];91 b =: s;92 s = b.get_string(encoding);93 }94 }95 return s;96 }97 98 function get_encoding_func()99 {100 var found;101 102 found = get_parrot_func('Parrot_str_from_platform_cstring', 'Spp');103 if (found != null) {104 return function(p, string encoding[optional])105 {106 var interp = getinterp();107 return found(interp, p);108 };109 }110 111 found = get_parrot_func('Parrot_str_new', 'SppI');112 if (found != null) {113 return function(p, string encoding[optional])114 {115 var interp = getinterp();116 return found(interp, p, 0);117 };118 }119 return null;120 }121 122 function string_from_nci(p, string encoding[optional])123 {124 var interp = getinterp();125 126 var f = get_encoding_func();127 if (! f)128 throw "Cannot get string from NCI";129 130 string s;131 if (f != null)132 s = f(p, encoding);133 134 if (s != null && encoding != null)135 s = raw_encode(s, encoding);136 return s;137 }138 139 71 } // namespace __private; 140 72 141 73 using namespace __private; … … 143 75 class Row 144 76 { 145 77 var mrow; 78 var desc; 146 79 var nfields; 147 80 var encoding; 148 function Row(var myrow, int n, string encoding[optional])81 function Row(var myrow, var desc, int n, string encoding[optional]) 149 82 { 150 83 self.mrow = myrow; 84 self.desc = desc; 151 85 self.nfields = n; 152 86 if (encoding != null) 153 87 self.encoding = encoding; 154 88 } 155 89 function get(int i) 156 90 { 157 string s = self.mrow[i]; 158 if (s != null && self.encoding != null) 159 s = raw_encode(s, self.encoding); 160 return s; 91 string encoding; 92 if (self.encoding != null) 93 encoding = self.encoding; 94 var p = self.desc[self.mrow, 0, i]; 95 string result; 96 if (p != null) { 97 result = p.as_string(encoding); 98 } 99 else 100 result = ''; 101 return result; 161 102 } 162 103 } 163 104 … … 175 116 var f = dlfunc(getlib(), 'mysql_field_count', 'ip'); 176 117 int count = f(my.mysql); 177 118 self.nfields = count; 178 int desc [3 * count]; 179 // Store the fields description for use in fetchs. 180 for (int i = 0; i < count; ++i) { 181 desc[i * 3] = DATATYPE_CSTR; 182 desc[i * 3 + 1] = 1; 183 desc[i * 3 + 2] = 0; 184 } 119 var desc = new ['StructView'] ( [DATATYPE_STRUCT, 1, 120 DATATYPE_PTR, count 121 ] 122 ); 185 123 self.desc = desc; 186 124 } 187 125 function field_count() … … 200 138 if (frow == none) 201 139 return null; 202 140 203 frow =: self.desc;204 141 string encoding; 205 142 if (self.mysql.encoding != null) 206 143 encoding = self.mysql.encoding; 207 return new WinxedMysql.Row(frow, self. nfields, encoding);144 return new WinxedMysql.Row(frow, self.desc, self.nfields, encoding); 208 145 } 209 146 function close() 210 147 { … … 243 180 { 244 181 using WinxedMysql.getlib; 245 182 var f = dlfunc(getlib(), 'mysql_get_client_info', 'p'); 246 return string_from_nci(f(),'utf8');183 return f().as_string('utf8'); 247 184 } 248 185 function error() 249 186 { 250 187 using WinxedMysql.getlib; 251 188 var f = dlfunc(getlib(), 'mysql_error', 'pp'); 252 return string_from_nci(f(self.mysql));189 return f(self.mysql).as_string('utf8'); 253 190 } 254 191 function connect(string host, string user, string pass, string database, 255 192 string encoding) … … 262 199 var ppass = str_to_cstring(pass); 263 200 var pdatabase = str_to_cstring(database); 264 201 var p = f(self.mysql, phost, puser, ppass, pdatabase, 0, null, 0); 265 str_free_cstring(phost);266 str_free_cstring(puser);267 str_free_cstring(ppass);268 str_free_cstring(pdatabase);269 202 if (p == null) 270 203 throw Error(self.error(), ERROR, TYPE); 271 204 var none = new 'UnManagedStruct'; … … 287 220 var f = dlfunc(getlib(), 'mysql_query', 'ipp'); 288 221 var pstmt = str_to_cstring(stmt); 289 222 int q = f(self.mysql, pstmt); 290 str_free_cstring(pstmt);291 223 if (q != 0) 292 224 throw Error(self.error(), ERROR, TYPE); 293 225 }
