pike.git / lib / modules / Sql.pmod / tds.pike

version» Context lines:

pike.git/lib/modules/Sql.pmod/tds.pike:1:   /* -  * $Id: tds.pike,v 1.22 2007/01/01 04:51:39 nilsson Exp $ +  * $Id: tds.pike,v 1.23 2007/04/13 11:48:17 grubba Exp $    *    * A Pike implementation of the TDS protocol.    *    * Henrik Grubbström 2006-02-08.    */      #pike __REAL_VERSION__    - #define TDS_DEBUG - #define TDS_CONVERT_DEBUG + /* #define TDS_DEBUG */ + /* #define TDS_CONVERT_DEBUG */      #ifdef TDS_DEBUG   #define TDS_WERROR(X...) werror("TDS:" + X)   #else   #define TDS_WERROR(X...)   #endif   #ifdef TDS_CONVERT_DEBUG   #define TDS_CONV_WERROR(X...) werror("TDS: Convert: " + X)   #else   #define TDS_CONV_WERROR(X...)
pike.git/lib/modules/Sql.pmod/tds.pike:961: Inside #if 0
   TDS_WERROR("TDS_COLINFO_TOKEN\n");    inp->get_byte();    //process_colinfo(); // FIXME!    }    }   #endif /* 0 */    break;    case TDS_TABNAME_TOKEN:    inp->get_byte();    string raw = inp->get_raw(inp->get_smallint()); -  werror("Got TAB_NAME: %O\n" +  werror("Got TDS_TABNAME_TOKEN: %O\n"    "column_info: %O\n",    raw, column_info);    break;    case TDS_COLINFO_TOKEN:    inp->get_byte();    string colinfo = inp->get_raw(inp->get_smallint());    if (column_info) {    while (sizeof(colinfo) > 2) {    int colno = colinfo[0];    int flags = colinfo[2];
pike.git/lib/modules/Sql.pmod/tds.pike:991:    string raw = colinfo[1..len];    colinfo = colinfo[len+1..];    column_info[colno]->real_name = utf16_to_string(raw);    }    }    } else {    TDS_WERROR("TDS_COLINFO_TOKEN without active column info\n"    " raw: %O\n", colinfo);    }    break; +  case TDS_DONEINPROC_TOKEN: +  inp->get_byte(); +  int flags = inp->get_smallint(); +  int state = inp->get_smallint(); +  TDS_WERROR("TDS_DONEINPROC_TOKEN: flags: 0x%04x, state: 0x%04x\n", +  flags, state); +  if (flags & 0x10) { +  // Count field is valid. +  int rows_affected = inp->get_int(); +  TDS_WERROR(" rows_affected: %d\n", rows_affected); +  } else { +  inp->get_int(); // Invalid. +  } +  if (flags & 1) { +  // More results pending. +  continue; +  } +  return column_info; +  case TDS_DONEPROC_TOKEN: +  TDS_WERROR(" TDS_DONEINPROC_TOKEN pending\n"); +  return column_info;    case TDS_DONE_TOKEN:    TDS_WERROR(" TDS_DONE_TOKEN pending\n");    return column_info;    case TDS_ROW_TOKEN:    TDS_WERROR(" TDS_ROW_TOKEN pending\n");    return column_info;    case TDS_RETURNSTATUS_TOKEN:    inp->get_byte();    int ret_status = inp->get_int();    TDS_WERROR("Return status: %d\n", ret_status);
pike.git/lib/modules/Sql.pmod/tds.pike:1167:    sprintf("%d", array_sscanf(raw[1..],    "%-" + (sizeof(raw)-1) + "c")[0]);       // Move decimal point @[scale] positions.    int scale = info->column_scale;    if (sizeof(res) < scale) {    res = "0." + ("0" * (scale - sizeof(res))) + res;    } else if (sizeof(res) == scale) {    res = "0." + res;    } else if (scale) { -  res = res[..<scale] + "." + +  res = res[..sizeof(res)-2] + "." +    res[sizeof(res)-scale..];    }       // Fix the sign.    if (!res[0]) {    res = "-" + res;    }    TDS_CONV_WERROR("%O (scale: %d) ==> %O\n",    raw, scale, res);    return res;
pike.git/lib/modules/Sql.pmod/tds.pike:1280:    case TDS_ROWFMT2_TOKEN:    case TDS7_RESULT_TOKEN:    // Some other result starts here.    return 0;    case TDS_ROW_TOKEN:    inp->get_byte();    return process_row(inp, col_info);    break;    case TDS_DONE_TOKEN:    case TDS_DONEPROC_TOKEN: -  case TDS_DONEINPROC_TOKEN: +     if (!leave_end_token) inp->get_byte();    return 0;    default:    inp->get_byte();    process_default_tokens(inp, token_type);    break;    }    }    }   
pike.git/lib/modules/Sql.pmod/tds.pike:1348:    }    } while (token_type != TDS_DONE_TOKEN);    //if (!(spid = rows_affected)) set_spid();    if (!ok) tds_error("Login failed.\n");    TDS_WERROR("Login ok!\n");    }       InPacket submit_query(compile_query query, void|array(mixed) params)    {    Packet p = Packet(); -  if (!query->n_param || !params || !sizeof(params)) { +     string raw = query->splitted_query*"?"; -  p->put_raw(raw, sizeof(raw)); +  p->put_raw(query->query_string, sizeof(query->query_string));    return send_packet(p, 0x01, 1); -  } else { -  tds_error("parametrized queries not supported yet.\n"); +     } -  +  +  InPacket submit_execdirect(compile_query query, void|array(mixed) params) +  { +  Packet p = Packet(); +  p->put_smallint(-1); +  p->put_smallint(10); // TDS_SP_EXECUTESQL +  p->put_smallint(0); +  +  p->put_smallint(0); +  p->put_byte(SYBNTEXT); +  p->put_int(sizeof(query->query_string)); +  p->put_raw(COLLATION, sizeof(COLLATION)); +  p->put_int(sizeof(query->query_string)); +  p->put_raw(query->query_string, sizeof(query->query_string)); +  +  int i; +  string param_definitions = +  string_to_utf16(map(params, +  lambda(mixed val) { +  return sprintf("@P%d ntext", ++i); +  })*","); +  +  p->put_smallint(0); +  p->put_byte(SYBNTEXT); +  p->put_int(sizeof(param_definitions)); +  p->put_raw(COLLATION, sizeof(COLLATION)); +  p->put_int(sizeof(param_definitions)||-1); +  p->put_raw(param_definitions, sizeof(param_definitions)); +  +  foreach(params; int i; mixed param) { +  p->put_data_info(param, 0); +  p->put_data(param, /* current_row, */ i);    }    -  +  return send_packet(p, 0x03, 1); +  } +     void disconnect()    {    socket->close();    destruct();    }       static void create(string server, int port, string database,    string username, string auth)    {    string domain;
pike.git/lib/modules/Sql.pmod/tds.pike:1424: Inside #if (__REAL_MAJOR__ > 7) || ((__REAL_MAJOR__ == 7) && (__REAL_MINOR__ >= 6))
     #if (__REAL_MAJOR__ > 7) || ((__REAL_MAJOR__ == 7) && (__REAL_MINOR__ >= 6))   };   #endif /* Pike 7.6 or later */         //! A compiled query.   class compile_query   {    int n_param; +  string query_string;    array(string) splitted_query;       array(mixed) params;       void parse_prepared_query()    {    // FIXME:    }       static array(string) split_query_on_placeholders(string query)
pike.git/lib/modules/Sql.pmod/tds.pike:1498:       //! Compile a query.    //!    //! @seealso    //! @[big_query()]    static void create(string query)    {    TDS_WERROR("Compiling query: %O\n", query);    splitted_query = split_query_on_placeholders(query);    n_param = sizeof(splitted_query)-1; +  if (n_param) { +  // Insert placeholders. +  int i; +  query_string = map((splitted_query/1)*({0}), +  lambda(string segment) { +  if (segment) return segment; +  return string_to_utf16(sprintf("@P%d", ++i)); +  })*""; +  } else { +  query_string = splitted_query[0];    }    } -  + }      //! A query result set.   class big_query   {    static int row_no;    static int eot;       static Connection.InPacket result_packet;    static array(mapping(string:mixed)) column_info;   
pike.git/lib/modules/Sql.pmod/tds.pike:1595:    static void create(string|compile_query query)    {    if (stringp(query)) {    query = compile_query(query);    }       query->parse_prepared_query();    if (busy) {    tds_error("Connection not idle.\n");    } -  if (!query->params) { +  if (!query->n_param) {    result_packet = con->submit_query(query);    } else {    result_packet = con->submit_execdirect(query, query->params);    }    column_info = con->process_result_tokens(result_packet);    if (!column_info) destruct();    }   }      static compile_query compiled_insert_id =
pike.git/lib/modules/Sql.pmod/tds.pike:1674:    string|void user, string|void password)   {    if (con) {    Disconnect(1);    }    int port = DEF_PORT;    if (server) {    array(string) tmp = server/":";    if (sizeof(tmp) > 1) {    port = (int)tmp[-1]; -  server = tmp[..<1]*":"; +  server = tmp[..sizeof(tmp)-2]*":";    }    } else {    server = "127.0.0.1";    }    Connect(server, port, database || "default",    user || "", password || "");   }