|
|
|
|
|
|
|
#pike __REAL_VERSION__ |
|
|
|
|
|
|
|
string quote(string s) |
{ |
return replace(s, "\'", "\'\'"); |
} |
|
|
void fallback() |
{ |
error( "Function not supported in this database." ); |
} |
|
|
|
|
|
protected class ZeroWrapper |
{ |
|
|
|
|
|
|
|
|
|
protected string _sprintf(int fmt, mapping(string:mixed) params) |
{ |
if (fmt == 's') return "NULL"; |
if (fmt == 'O') return "ZeroWrapper()"; |
return sprintf(sprintf("%%*%c", fmt), params, 0); |
} |
} |
|
|
protected ZeroWrapper zero = ZeroWrapper(); |
|
protected class NullArg |
{ |
protected string _sprintf (int fmt) |
{return fmt == 'O' ? "NullArg()" : "NULL";} |
} |
protected NullArg null_arg = NullArg(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
array(string|mapping(string|int:mixed)) |
handle_extraargs(string query, array(mixed) extraargs, |
void|mapping(string|int:mixed) bindings) { |
|
array(mixed) args=allocate(sizeof(extraargs)); |
if (!bindings) bindings = ([]); |
|
int a, new_bindings; |
foreach(extraargs; int j; mixed s) { |
if (stringp(s) || multisetp(s)) { |
string bind_name; |
do { |
bind_name = ":arg"+(a++); |
} while (has_index (bindings, bind_name)); |
args[j]=bind_name; |
bindings[bind_name] = s; |
new_bindings = 1; |
continue; |
} |
if (intp(s) || floatp(s)) { |
args[j] = s || zero; |
continue; |
} |
if (objectp (s) && s->is_val_null) { |
args[j] = null_arg; |
continue; |
} |
error("Wrong type to query argument %d: %O\n", j + 1, s); |
} |
|
return ({sprintf(query,@args), new_bindings && bindings}); |
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string emulate_bindings(string query, mapping(string|int:mixed)|void bindings, |
void|object driver) |
{ |
array(string)k, v; |
if (!bindings) |
return query; |
function my_quote=(driver&&driver->quote?driver->quote:quote); |
v=map(values(bindings), |
lambda(mixed m) { |
if(undefinedp(m)) |
return "NULL"; |
if (objectp (m) && m->is_val_null) |
|
|
|
|
return "NULL"; |
if(multisetp(m)) |
return sizeof(m) ? indices(m)[0] : ""; |
return "'"+(intp(m)?(string)m:my_quote((string)m))+"'"; |
}); |
|
k=map(indices(bindings),lambda(string s){ |
return ( (stringp(s)&&s[0]==':') ? |
s : ":"+s); |
}); |
return replace(query,k,v); |
} |
|
|
class UnicodeWrapper ( |
|
protected object master_result |
) |
{ |
inherit Sql.Result; |
|
int num_rows() |
{ |
return master_result->num_rows(); |
} |
|
|
int num_fields() |
{ |
return master_result->num_fields(); |
} |
|
|
int(0..1) eof() |
{ |
return master_result->eof(); |
} |
|
|
protected array(int|mapping(string:mixed)) field_info; |
|
|
|
|
|
|
|
|
|
|
|
|
array(int|mapping(string:mixed)) fetch_fields() |
{ |
if (!field_info) { |
field_info = master_result->fetch_fields(); |
foreach(field_info, int|mapping(string:mixed) field) { |
if (mappingp(field)) { |
field->name = utf8_to_string(field->name); |
if (field->table) { |
field->table = utf8_to_string(field->table); |
} |
if (field->default) { |
field->default = utf8_to_string(field->default); |
} |
} |
} |
} |
return field_info; |
} |
|
|
void seek(int rows) |
{ |
master_result->seek(rows); |
} |
|
|
|
|
int|array(string) fetch_row() |
{ |
int|array(string) row = master_result->fetch_row(); |
if (!arrayp(row)) return row; |
array(int|mapping(string:mixed)) field_info = fetch_fields(); |
foreach(row; int i; string|int val) { |
if (stringp(val)) { |
row[i] = utf8_to_string(val); |
} |
} |
return row; |
} |
|
|
int|string fetch_json_result() |
{ |
return master_result->fetch_json_result(); |
} |
} |
|
class MySQLUnicodeWrapper |
|
|
|
|
|
|
|
{ |
inherit UnicodeWrapper; |
|
int|array(string) fetch_row() |
{ |
int|array(string) row = master_result->fetch_row(); |
if (!arrayp(row)) return row; |
array(int|mapping(string:mixed)) field_info = fetch_fields(); |
foreach(row; int i; string|int val) { |
if (stringp(val) && field_info[i]->charsetnr != 63) { |
row[i] = utf8_to_string(val); |
} |
} |
return row; |
} |
} |
|
class MySQLBrokenUnicodeWrapper |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{ |
inherit UnicodeWrapper; |
|
int|array(string) fetch_row() |
{ |
int|array(string) row = master_result->fetch_row(); |
if (!arrayp(row)) return row; |
array(int|mapping(string:mixed)) field_info = fetch_fields(); |
foreach(row; int i; string|int val) { |
if (stringp(val) && field_info[i]->flags && |
!field_info[i]->flags->binary) { |
row[i] = utf8_to_string(val); |
} |
} |
return row; |
} |
} |
|
|