FreeTDS API: Query

- Main Page
- Modules
- Classes
- Files
- Related Pages

# Query
[LibTDS API]

Function to handle query. More...

## Defines
#define | START_QUERY do { ; } while(0)
#define | tds_convert_string_free(original, converted) do { if (original != converted) free((char*) converted); } while(0)
#define | TDS_PUT_N_AS_UCS2(tds, s)
## Enumerations
enum | { MUL_STARTED = 1 }
## Functions
static char * | tds5_fix_dot_query (const char *query, int *query_len, TDSPARAMINFO *params)
static char * | tds7_build_param_def_from_params (TDSSOCKET *tds, const char *query, size_t query_len, TDSPARAMINFO *params, size_t *out_len)
| Return string with parameters definition, useful for TDS7+.
static char * | tds7_build_param_def_from_query (TDSSOCKET *tds, const char *converted_query, int converted_query_len, TDSPARAMINFO *params, size_t *out_len)
| Return string with parameters definition, useful for TDS7+.
static void | tds7_put_cursor_fetch (TDSSOCKET *tds, TDS_INT cursor_id, TDS_TINYINT fetch_type, TDS_INT i_row, TDS_INT num_rows)
static void | tds7_put_params_definition (TDSSOCKET *tds, const char *param_definition, size_t param_length)
static void | tds7_put_query_params (TDSSOCKET *tds, const char *query, int query_len)
| Output params types and query (required by sp_prepare/sp_executesql/sp_prepexec).
static void | tds7_send_execute (TDSSOCKET *tds, TDSDYNAMIC *dyn)
static int | tds_ascii_to_ucs2 (char *buffer, const char *buf)
| Accept an ASCII string, convert it to UCS2-LE The input is null-terminated, but the output excludes the null.
static const char * | tds_convert_string (TDSSOCKET *tds, const TDSICONV *char_conv, const char *s, int len, int *out_len)
| Convert a string in an allocated buffer.
int | tds_count_placeholders (const char *query)
| Count the number of placeholders in query.
static int | tds_count_placeholders_ucs2le (const char *query, const char *query_end)
int | tds_cursor_close (TDSSOCKET *tds, TDSCURSOR *cursor)
int | tds_cursor_dealloc (TDSSOCKET *tds, TDSCURSOR *cursor)
| Send a deallocation request to server libTDS care for all deallocation stuff (memory and server cursor) Caller should not use cursor pointer anymore.
int | tds_cursor_declare (TDSSOCKET *tds, TDSCURSOR *cursor, TDSPARAMINFO *params, int *something_to_send)
int | tds_cursor_fetch (TDSSOCKET *tds, TDSCURSOR *cursor, TDS_CURSOR_FETCH fetch_type, TDS_INT i_row)
int | tds_cursor_open (TDSSOCKET *tds, TDSCURSOR *cursor, TDSPARAMINFO *params, int *something_to_send)
int | tds_cursor_setname (TDSSOCKET *tds, TDSCURSOR *cursor)
int | tds_cursor_setrows (TDSSOCKET *tds, TDSCURSOR *cursor, int *something_to_send)
int | tds_cursor_update (TDSSOCKET *tds, TDSCURSOR *cursor, TDS_CURSOR_OPERATION op, TDS_INT i_row, TDSPARAMINFO *params)
static int | tds_get_column_declaration (TDSSOCKET *tds, TDSCOLUMN *curcol, char *out)
| Return declaration for column (like "varchar(20)").
int | tds_multiple_done (TDSSOCKET *tds, TDSMULTIPLE *multiple)
int | tds_multiple_execute (TDSSOCKET *tds, TDSMULTIPLE *multiple, TDSDYNAMIC *dyn)
int | tds_multiple_init (TDSSOCKET *tds, TDSMULTIPLE *multiple, TDS_MULTIPLE_TYPE type)
int | tds_multiple_query (TDSSOCKET *tds, TDSMULTIPLE *multiple, const char *query, TDSPARAMINFO *params)
const char * | tds_next_placeholder (const char *start)
| Get position of next placeholder.
static const char * | tds_next_placeholder_ucs2le (const char *start, const char *end, int named)
static int | tds_put_data (TDSSOCKET *tds, TDSCOLUMN *curcol)
| Write data to wire.
static int | tds_put_data_info (TDSSOCKET *tds, TDSCOLUMN *curcol, int flags)
| Put data information to wire.
static int | tds_put_data_info_length (TDSSOCKET *tds, TDSCOLUMN *curcol, int flags)
| Calc information length in bytes (useful for calculating full packet length).
static int | tds_put_param_as_string (TDSSOCKET *tds, TDSPARAMINFO *params, int n)
static void | tds_put_params (TDSSOCKET *tds, TDSPARAMINFO *info, int flags)
static int | tds_query_flush_packet (TDSSOCKET *tds)
static int | tds_quote (TDSSOCKET *tds, char *buffer, char quoting, const char *id, int len)
static void | tds_quote_and_put (TDSSOCKET *tds, const char *s, const char *end)
int | tds_quote_id (TDSSOCKET *tds, char *buffer, const char *id, int idlen)
| Quote an id.
int | tds_quote_string (TDSSOCKET *tds, char *buffer, const char *str, int len)
| Quote a string.
int | tds_send_cancel (TDSSOCKET *tds)
| tds_send_cancel() sends an empty packet (8 byte header only) tds_process_cancel should be called directly after this.
static int | tds_send_emulated_execute (TDSSOCKET *tds, const char *query, TDSPARAMINFO *params)
| Emulate prepared execute traslating to a normal language.
static void | tds_set_cur_cursor (TDSSOCKET *tds, TDSCURSOR *cursor)
static const char * | tds_skip_comment (const char *s)
static const char * | tds_skip_comment_ucs2le (const char *s, const char *end)
const char * | tds_skip_quoted (const char *s)
| Skip quoting string (like 'sfsf', "dflkdj" or [dfkjd]).
static const char * | tds_skip_quoted_ucs2le (const char *s, const char *end)
int | tds_submit_execdirect (TDSSOCKET *tds, const char *query, TDSPARAMINFO *params)
| Submit a prepared query with parameters.
int | tds_submit_execute (TDSSOCKET *tds, TDSDYNAMIC *dyn)
| tds_submit_execute() sends a previously prepared dynamic statement to the server.
int | tds_submit_optioncmd (TDSSOCKET *tds, TDS_OPTION_CMD command, TDS_OPTION option, TDS_OPTION_ARG *param, TDS_INT param_size)
int | tds_submit_prepare (TDSSOCKET *tds, const char *query, const char *id, TDSDYNAMIC **dyn_out, TDSPARAMINFO *params)
| tds_submit_prepare() creates a temporary stored procedure in the server.
int | tds_submit_query (TDSSOCKET *tds, const char *query)
| tds_submit_query() sends a language string to the database server for processing.
int | tds_submit_query_params (TDSSOCKET *tds, const char *query, TDSPARAMINFO *params)
| tds_submit_query_params() sends a language string to the database server for processing.
int | tds_submit_queryf (TDSSOCKET *tds, const char *queryf,...)
int | tds_submit_rpc (TDSSOCKET *tds, const char *rpc_name, TDSPARAMINFO *params)
| tds_submit_rpc() call a RPC from server.
int | tds_submit_unprepare (TDSSOCKET *tds, TDSDYNAMIC *dyn)
| Send a unprepare request for a prepared query.

## Detailed Description

Function to handle query.

## Define Documentation

#define TDS_PUT_N_AS_UCS2 | ( | tds,
|  | s |  | ) |

Value:

    do { \
            char buffer[sizeof(s)*2-2]; \
            tds_put_n(tds, buffer, tds_ascii_to_ucs2(buffer, s)); \
    } while(0)

## Function Documentation

static char * tds7_build_param_def_from_params | ( | TDSSOCKET * | tds,
|  | const char * | query,
|  | size_t | query_len,
|  | TDSPARAMINFO * | params,
|  | size_t * | out_len |
| ) |  |  | `[static]`

Return string with parameters definition, useful for TDS7+.

Parameters:

  | tds | state information for the socket and the TDS protocol
  | params | parameters to build declaration
  | out_len | length output buffer in bytes

Returns:
  allocated and filled string or NULL on failure (coded in ucs2le charset )

static char * tds7_build_param_def_from_query | ( | TDSSOCKET * | tds,
|  | const char * | converted_query,
|  | int | converted_query_len,
|  | TDSPARAMINFO * | params,
|  | size_t * | out_len |
| ) |  |  | `[static]`

Return string with parameters definition, useful for TDS7+.

Parameters:

  | tds | state information for the socket and the TDS protocol
  | params | parameters to build declaration
  | out_len | length output buffer in bytes

Returns:
  allocated and filled string or NULL on failure (coded in ucs2le charset )

static void tds7_put_query_params | ( | TDSSOCKET * | tds,
|  | const char * | query,
|  | int | query_len |
| ) |  |  | `[static]`

Output params types and query (required by sp_prepare/sp_executesql/sp_prepexec).

Parameters:

  | tds | state information for the socket and the TDS protocol
  | query | query (in ucs2le codings)
  | query_len | query length in bytes

static int tds_ascii_to_ucs2 | ( | char * | buffer,
|  | const char * | buf |
| ) |  |  | `[static]`

Accept an ASCII string, convert it to UCS2-LE The input is null-terminated, but the output excludes the null.

Parameters:

  | buffer | buffer where to store output
  | buf | string to write

Returns:
  bytes written

static const char* tds_convert_string | ( | TDSSOCKET * | tds,
|  | const TDSICONV * | char_conv,
|  | const char * | s,
|  | int | len,
|  | int * | out_len |
| ) |  |  | `[static]`

Convert a string in an allocated buffer.

Parameters:

  | tds | state information for the socket and the TDS protocol
  | char_conv | information about the encodings involved
  | s | input string
  | len | input string length (in bytes), -1 for null terminated
  | out_len | returned output length (in bytes)

Returns:
  string allocated (or input pointer if no conversion required) or NULL if error

static int tds_get_column_declaration | ( | TDSSOCKET * | tds,
|  | TDSCOLUMN * | curcol,
|  | char * | out |
| ) |  |  | `[static]`

Return declaration for column (like "varchar(20)").

Parameters:

  | tds | state information for the socket and the TDS protocol
  | curcol | column
  | out | buffer to hold declaration

Returns:
  TDS_FAIL or TDS_SUCCEED

const char* tds_next_placeholder | ( | const char * | start | ) |

Get position of next placeholder.

Parameters:

  | start | pointer to part of query to search

Returns:
  next placeholder or NULL if not found

static int tds_put_data | ( | TDSSOCKET * | tds,
|  | TDSCOLUMN * | curcol |
| ) |  |  | `[static]`

Write data to wire.

Parameters:

  | tds | state information for the socket and the TDS protocol
  | curcol | column where store column information

Returns:
  TDS_FAIL on error or TDS_SUCCEED

static int tds_put_data_info | ( | TDSSOCKET * | tds,
|  | TDSCOLUMN * | curcol,
|  | int | flags |
| ) |  |  | `[static]`

Put data information to wire.

Parameters:

  | tds | state information for the socket and the TDS protocol
  | curcol | column where to store information
  | flags | bit flags on how to send data (use TDS_PUT_DATA_USE_NAME for use name information)

Returns:
  TDS_SUCCEED or TDS_FAIL

static int tds_put_data_info_length | ( | TDSSOCKET * | tds,
|  | TDSCOLUMN * | curcol,
|  | int | flags |
| ) |  |  | `[static]`

Calc information length in bytes (useful for calculating full packet length).

Parameters:

  | tds | state information for the socket and the TDS protocol
  | curcol | column where to store information
  | flags | bit flags on how to send data (use TDS_PUT_DATA_USE_NAME for use name information)

Returns:
  TDS_SUCCEED or TDS_FAIL

int tds_quote_id | ( | TDSSOCKET * | tds,
|  | char * | buffer,
|  | const char * | id,
|  | int | idlen |
| ) |  |  |

Quote an id.

Parameters:

  | tds | state information for the socket and the TDS protocol
  | buffer | buffer to store quoted id. If NULL do not write anything (useful to compute quote length)
  | id | id to quote
  | idlen | id length

Returns:
  written chars (not including needed terminator)

int tds_quote_string | ( | TDSSOCKET * | tds,
|  | char * | buffer,
|  | const char * | str,
|  | int | len |
| ) |  |  |

Quote a string.

Parameters:

  | tds | state information for the socket and the TDS protocol
  | buffer | buffer to store quoted id. If NULL do not write anything (useful to compute quote length)
  | str | string to quote (not necessary null-terminated)
  | len | length of string (-1 for null terminated)

Returns:
  written chars (not including needed terminator)

int tds_send_cancel | ( | TDSSOCKET * | tds | ) |

tds_send_cancel() sends an empty packet (8 byte header only) tds_process_cancel should be called directly after this.

Parameters:

  | tds | state information for the socket and the TDS protocol

Remarks:
  tcp will either deliver the packet or time out. (TIME_WAIT determines how long it waits between retries.)

On sending the cancel, we may get EAGAIN. We then select(2) until we know either 1) it succeeded or 2) it didn't. On failure, close the socket, tell the app, and fail the function.

On success, we read(2) and wait for a reply with select(2). If we get one, great. If the client's timeout expires, we tell him, but all we can do is wait some more or give up and close the connection. If he tells us to cancel again, we wait some more.

const char* tds_skip_quoted | ( | const char * | s | ) |

Skip quoting string (like 'sfsf', "dflkdj" or [dfkjd]).

Parameters:

  | s | pointer to first quoting character (should be '," or [)

Returns:
  character after quoting

int tds_submit_execdirect | ( | TDSSOCKET * | tds,
|  | const char * | query,
|  | TDSPARAMINFO * | params |
| ) |  |  |

Submit a prepared query with parameters.

Parameters:

  | tds | state information for the socket and the TDS protocol
  | query | language query with given placeholders (?)
  | params | parameters to send

Returns:
  TDS_FAIL or TDS_SUCCEED

int tds_submit_execute | ( | TDSSOCKET * | tds,
|  | TDSDYNAMIC * | dyn |
| ) |  |  |

tds_submit_execute() sends a previously prepared dynamic statement to the server.

Parameters:

  | tds | state information for the socket and the TDS protocol
  | dyn | dynamic proc to execute. Must build from same tds.

int tds_submit_prepare | ( | TDSSOCKET * | tds,
|  | const char * | query,
|  | const char * | id,
|  | TDSDYNAMIC ** | dyn_out,
|  | TDSPARAMINFO * | params |
| ) |  |  |

tds_submit_prepare() creates a temporary stored procedure in the server.

Under TDS 4.2 dynamic statements are emulated building sql command

Parameters:

  | tds | state information for the socket and the TDS protocol
  | query | language query with given placeholders (?)
  | id | string to identify the dynamic query. Pass NULL for automatic generation.
  | dyn_out | will receive allocated TDSDYNAMIC*. Any older allocated dynamic won't be freed, Can be NULL.
  | params | parameters to use. It can be NULL even if parameters are present. Used only for TDS7+

Returns:
  TDS_FAIL or TDS_SUCCEED

int tds_submit_query | ( | TDSSOCKET * | tds,
|  | const char * | query |
| ) |  |  |

tds_submit_query() sends a language string to the database server for processing.

TDS 4.2 is a plain text message with a packet type of 0x01, TDS 7.0 is a unicode string with packet type 0x01, and TDS 5.0 uses a TDS_LANGUAGE_TOKEN to encapsulate the query and a packet type of 0x0f.

Parameters:

  | tds | state information for the socket and the TDS protocol
  | query | language query to submit

Returns:
  TDS_FAIL or TDS_SUCCEED

int tds_submit_query_params | ( | TDSSOCKET * | tds,
|  | const char * | query,
|  | TDSPARAMINFO * | params |
| ) |  |  |

tds_submit_query_params() sends a language string to the database server for processing.

TDS 4.2 is a plain text message with a packet type of 0x01, TDS 7.0 is a unicode string with packet type 0x01, and TDS 5.0 uses a TDS_LANGUAGE_TOKEN to encapsulate the query and a packet type of 0x0f.

Parameters:

  | tds | state information for the socket and the TDS protocol
  | query | language query to submit
  | params | parameters of query

Returns:
  TDS_FAIL or TDS_SUCCEED

int tds_submit_rpc | ( | TDSSOCKET * | tds,
|  | const char * | rpc_name,
|  | TDSPARAMINFO * | params |
| ) |  |  |

tds_submit_rpc() call a RPC from server.

Output parameters will be stored in tds->param_info

Parameters:

  | tds | state information for the socket and the TDS protocol
  | rpc_name | name of RPC
  | params | parameters informations. NULL for no parameters

int tds_submit_unprepare | ( | TDSSOCKET * | tds,
|  | TDSDYNAMIC * | dyn |
| ) |  |  |

Send a unprepare request for a prepared query.

Parameters:

  | tds | state information for the socket and the TDS protocol
  | dyn | dynamic query

Returns:
  TDS_SUCCEED or TDS_FAIL