FreeTDS API
|
Function to handle query. More...
Classes | |
struct | tds_quoteout_stream |
Macros | |
#define | TDS_PUT_N_AS_UCS2(tds, s) |
Utility to convert a constant ascii string to ucs2 and send to server. | |
Typedefs | |
typedef struct tds_quoteout_stream | TDSQUOTEOUTSTREAM |
Enumerations | |
enum | { MUL_STARTED = 1 } |
Functions | |
static TDSRET | tds4_send_emulated_rpc (TDSSOCKET *tds, const char *rpc_name, TDSPARAMINFO *params) |
Send RPC as string query. | |
static const char * | tds50_char_declaration_from_usertype (TDSSOCKET *tds, TDS_INT usertype, unsigned int *p_size) |
static char * | tds5_fix_dot_query (const char *query, size_t *query_len, TDSPARAMINFO *params) |
Substitute ?-style placeholders with named (@param) ones. | |
static TDSRET | tds5_put_params (TDSSOCKET *tds, TDSPARAMINFO *info, int flags) |
Send parameters to server. | |
TDSRET | tds71_submit_prepexec (TDSSOCKET *tds, const char *query, const char *id, TDSDYNAMIC **dyn_out, TDSPARAMINFO *params) |
Creates a temporary stored procedure in the server and execute it. | |
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_query_params (TDSSOCKET *tds, const char *query, size_t query_len) |
Output params types and query (required by sp_prepare/sp_executesql/sp_prepexec) | |
static TDSRET | tds7_send_execute (TDSSOCKET *tds, TDSDYNAMIC *dyn) |
Send dynamic request on TDS 7+ to be executed. | |
static TDSRET | tds7_write_param_def_from_params (TDSSOCKET *tds, const char *query, size_t query_len, TDSPARAMINFO *params) |
Write string with parameters definition, useful for TDS7+. | |
static TDSRET | tds7_write_param_def_from_query (TDSSOCKET *tds, const char *converted_query, size_t converted_query_len, TDSPARAMINFO *params) |
Write string with parameters definition, useful for TDS7+. | |
static size_t | tds_ascii_to_ucs2 (char *buffer, const char *buf) |
Accept an ASCII string, convert it to UCS2-LE The input is NUL-terminated, but the output does not contains the NUL. | |
const char * | tds_convert_string (TDSSOCKET *tds, TDSICONV *char_conv, const char *s, int len, size_t *out_len) |
Convert a string in an allocated buffer. | |
int | tds_count_placeholders (const char *query) |
Count the number of placeholders ('?') in a query. | |
static int | tds_count_placeholders_ucs2le (const char *query, const char *query_end) |
Count the number of placeholders ('?') in a query. | |
static bool | tds_cursor_check_allocated (TDSCONNECTION *conn, TDSCURSOR *cursor) |
Check if a cursor is allocated into the server. | |
TDSRET | tds_cursor_close (TDSSOCKET *tds, TDSCURSOR *cursor) |
TDSRET | tds_cursor_dealloc (TDSSOCKET *tds, TDSCURSOR *cursor) |
Send a deallocation request to server. | |
TDSRET | tds_cursor_declare (TDSSOCKET *tds, TDSCURSOR *cursor, bool *something_to_send) |
TDSRET | tds_cursor_fetch (TDSSOCKET *tds, TDSCURSOR *cursor, TDS_CURSOR_FETCH fetch_type, TDS_INT i_row) |
TDSRET | tds_cursor_get_cursor_info (TDSSOCKET *tds, TDSCURSOR *cursor, TDS_UINT *prow_number, TDS_UINT *prow_count) |
TDSRET | tds_cursor_open (TDSSOCKET *tds, TDSCURSOR *cursor, TDSPARAMINFO *params, bool *something_to_send) |
TDSRET | tds_cursor_setname (TDSSOCKET *tds, TDSCURSOR *cursor) |
TDSRET | tds_cursor_setrows (TDSSOCKET *tds, TDSCURSOR *cursor, bool *something_to_send) |
TDSRET | tds_cursor_update (TDSSOCKET *tds, TDSCURSOR *cursor, TDS_CURSOR_OPERATION op, TDS_INT i_row, TDSPARAMINFO *params) |
TDSRET | tds_deferred_cursor_dealloc (TDSCONNECTION *conn, TDSCURSOR *cursor) |
Deallocate cursor on idle. | |
TDSRET | tds_deferred_unprepare (TDSCONNECTION *conn, TDSDYNAMIC *dyn) |
Unprepare dynamic on idle. | |
TDSRET | tds_disconnect (TDSSOCKET *tds) |
size_t | tds_fix_column_size (TDSSOCKET *tds TDS_UNUSED, TDSCOLUMN *curcol) |
Get column size for wire. | |
TDSRET | tds_get_column_declaration (TDSSOCKET *tds, TDSCOLUMN *curcol, char *out) |
Return declaration for column (like "varchar(20)"). | |
TDSRET | tds_multiple_done (TDSSOCKET *tds, TDSMULTIPLE *multiple) |
TDSRET | tds_multiple_execute (TDSSOCKET *tds, TDSMULTIPLE *multiple, TDSDYNAMIC *dyn) |
TDSRET | tds_multiple_init (TDSSOCKET *tds, TDSMULTIPLE *multiple, TDS_MULTIPLE_TYPE type, TDSHEADERS *head) |
TDSRET | tds_multiple_query (TDSSOCKET *tds, TDSMULTIPLE *multiple, const char *query, TDSPARAMINFO *params) |
int | tds_needs_unprepare (TDSCONNECTION *conn, TDSDYNAMIC *dyn) |
Check if dynamic request must be unprepared. | |
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) |
Found the next placeholder (? or @param) in a string. | |
static TDSRET | tds_put_char_param_as_string (TDSSOCKET *tds, const TDSCOLUMN *curcol) |
static TDSRET | tds_put_data (TDSSOCKET *tds, TDSCOLUMN *curcol) |
Write data to wire. | |
static TDSRET | tds_put_data_info (TDSSOCKET *tds, TDSCOLUMN *curcol, int flags) |
Put data information to wire. | |
static TDSRET | tds_put_param_as_string (TDSSOCKET *tds, TDSPARAMINFO *params, int n) |
Send a parameter to server. | |
static TDSRET | tds_query_flush_packet (TDSSOCKET *tds) |
Flush query packet. | |
static size_t | tds_quote (char *buffer, char quoting, const char *id, size_t len) |
Quote a string properly. | |
static void | tds_quote_and_put (TDSSOCKET *tds, const char *s, const char *end) |
Send a string to server while quoting it. | |
size_t | tds_quote_id (TDSSOCKET *tds, char *buffer, const char *id, int idlen) |
Quote an id. | |
size_t | tds_quote_id_rpc (TDSSOCKET *tds, char *buffer, const char *id, int idlen) |
Quote an id for a RPC call. | |
size_t | tds_quote_string (TDSSOCKET *tds TDS_UNUSED, char *buffer, const char *str, int len) |
Quote a string. | |
static void | tds_quoteout_stream_init (TDSQUOTEOUTSTREAM *stream, TDSSOCKET *tds) |
static int | tds_quoteout_stream_write (TDSOUTSTREAM *stream, size_t len) |
TDSRET | 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 TDSRET | 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) |
Set current cursor. | |
void | tds_set_cur_dyn (TDSSOCKET *tds, TDSDYNAMIC *dyn) |
Set current dynamic. | |
const char * | tds_skip_comment (const char *s) |
Skip a comment in a query. | |
static const char * | tds_skip_comment_ucs2le (const char *s, const char *end) |
Skip a comment in a query. | |
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) |
Return pointer to end of a quoted string. | |
void | tds_start_query (TDSSOCKET *tds, unsigned char packet_type) |
Start query packet of a given type. | |
static TDSRET | tds_start_query_head (TDSSOCKET *tds, unsigned char packet_type, TDSHEADERS *head) |
Start query packet of a given type. | |
TDSRET | tds_submit_begin_tran (TDSSOCKET *tds) |
Send a rollback request. | |
TDSRET | tds_submit_commit (TDSSOCKET *tds, bool cont) |
Send a commit request. | |
TDSRET | tds_submit_execdirect (TDSSOCKET *tds, const char *query, TDSPARAMINFO *params, TDSHEADERS *head) |
Submit a prepared query with parameters. | |
TDSRET | tds_submit_execute (TDSSOCKET *tds, TDSDYNAMIC *dyn) |
Sends a previously prepared dynamic statement to the server. | |
TDSRET | tds_submit_optioncmd (TDSSOCKET *tds, TDS_OPTION_CMD command, TDS_OPTION option, TDS_OPTION_ARG *param, TDS_INT param_size) |
Send option commands to server. | |
TDSRET | tds_submit_prepare (TDSSOCKET *tds, const char *query, const char *id, TDSDYNAMIC **dyn_out, TDSPARAMINFO *params) |
Creates a temporary stored procedure in the server. | |
TDSRET | tds_submit_query (TDSSOCKET *tds, const char *query) |
Sends a language string to the database server for processing. | |
TDSRET | tds_submit_query_params (TDSSOCKET *tds, const char *query, TDSPARAMINFO *params, TDSHEADERS *head) |
Sends a language string to the database server for processing. | |
TDSRET | tds_submit_queryf (TDSSOCKET *tds, const char *queryf,...) |
Format and submit a query. | |
TDSRET | tds_submit_rollback (TDSSOCKET *tds, bool cont) |
Send a rollback request. | |
TDSRET | tds_submit_rpc (TDSSOCKET *tds, const char *rpc_name, TDSPARAMINFO *params, TDSHEADERS *head) |
Calls a RPC from server. | |
TDSRET | tds_submit_unprepare (TDSSOCKET *tds, TDSDYNAMIC *dyn) |
Send a unprepare request for a prepared query. | |
Variables | |
static const TDSCONTEXT | empty_ctx = {0} |
Function to handle query.
#define TDS_PUT_N_AS_UCS2 | ( | tds, | |
s | |||
) |
Utility to convert a constant ascii string to ucs2 and send to server.
Used to send internal store procedure names to server.
tds | A pointer to the TDSSOCKET structure managing a client/server operation. |
s | constanst string to send |
|
static |
Send RPC as string query.
This function is used on old protocol which does not support RPC queries.
tds | A pointer to the TDSSOCKET structure managing a client/server operation. |
rpc_name | name of RPC to invoke |
params | parameters to send to server |
|
static |
Substitute ?-style placeholders with named (@param) ones.
Sybase does not support ?-style placeholders so convert them. Also the function replace parameter names.
query | query string | |
[in,out] | query_len | pointer to query length. On input length of input query, on output length of output query |
params | parameters to send to server |
|
static |
Send parameters to server.
tds | A pointer to the TDSSOCKET structure managing a client/server operation. |
info | parameters to send |
flags | 0 or TDS_PUT_DATA_USE_NAME |
TDSRET tds71_submit_prepexec | ( | TDSSOCKET * | tds, |
const char * | query, | ||
const char * | id, | ||
TDSDYNAMIC ** | dyn_out, | ||
TDSPARAMINFO * | params | ||
) |
Creates a temporary stored procedure in the server and execute it.
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. |
|
static |
Output params types and query (required by sp_prepare/sp_executesql/sp_prepexec)
tds | state information for the socket and the TDS protocol |
query | query (encoded in ucs2le) |
query_len | query length in bytes |
|
static |
Send dynamic request on TDS 7+ to be executed.
tds | A pointer to the TDSSOCKET structure managing a client/server operation. |
dyn | dynamic query to execute |
|
static |
Write string with parameters definition, useful for TDS7+.
Looks like "@P1 INT, @P2 VARCHAR(100)"
tds | state information for the socket and the TDS protocol |
query | query to send to server encoded in ucs2le |
query_len | query length in bytes |
params | parameters to build declaration |
|
static |
Write string with parameters definition, useful for TDS7+.
Looks like "@P1 INT, @P2 VARCHAR(100)"
tds | state information for the socket and the TDS protocol |
converted_query | query to send to server in ucs2le encoding |
converted_query_len | query length in bytes |
params | parameters to build declaration |
|
static |
Accept an ASCII string, convert it to UCS2-LE The input is NUL-terminated, but the output does not contains the NUL.
buffer | buffer where to store output |
buf | string to write |
const char * tds_convert_string | ( | TDSSOCKET * | tds, |
TDSICONV * | char_conv, | ||
const char * | s, | ||
int | len, | ||
size_t * | out_len | ||
) |
Convert a string in an allocated buffer.
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 NUL-terminated |
out_len | returned output length (in bytes) |
int tds_count_placeholders | ( | const char * | query | ) |
Count the number of placeholders ('?') in a query.
query | query string |
|
static |
Count the number of placeholders ('?') in a query.
query | query encoded in ucs2le |
query_end | end of query |
|
static |
Check if a cursor is allocated into the server.
If is not allocated it assures is removed from the connection list
tds | A pointer to the TDSSOCKET structure managing a client/server operation. |
TDSRET tds_deferred_cursor_dealloc | ( | TDSCONNECTION * | conn, |
TDSCURSOR * | cursor | ||
) |
Deallocate cursor on idle.
This let libTDS close the cursor when possible.
tds | A pointer to the TDSSOCKET structure managing a client/server operation. |
cursor | cursor to close |
TDSRET tds_deferred_unprepare | ( | TDSCONNECTION * | conn, |
TDSDYNAMIC * | dyn | ||
) |
Unprepare dynamic on idle.
This let libTDS close the prepared statement when possible.
tds | A pointer to the TDSSOCKET structure managing a client/server operation. |
dyn | dynamic request to close |
Return declaration for column (like "varchar(20)").
This depends on:
tds | A pointer to the TDSSOCKET structure managing a client/server operation. |
curcol | column |
out | buffer to hold declaration |
int tds_needs_unprepare | ( | TDSCONNECTION * | conn, |
TDSDYNAMIC * | dyn | ||
) |
Check if dynamic request must be unprepared.
Depending on status and protocol version request should be unprepared or not.
tds | A pointer to the TDSSOCKET structure managing a client/server operation. |
dyn | dynamic request to check |
const char * tds_next_placeholder | ( | const char * | start | ) |
Get position of next placeholder.
start | pointer to part of query to search |
|
static |
Found the next placeholder (? or @param) in a string.
String must be encoded in ucs2le.
start | start of the string (or part of it) |
end | end of string |
named | true if named parameters should be returned |
Write data to wire.
tds | A pointer to the TDSSOCKET structure managing a client/server operation. |
curcol | column where store column information |
Put data information to wire.
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) |
|
static |
Send a parameter to server.
Parameters are converted to string and sent to server.
tds | A pointer to the TDSSOCKET structure managing a client/server operation. |
params | parameters structure |
n | number of parameter to send |
|
static |
Flush query packet.
Used at the end of packet write to really send packet to server. This also changes the state to TDS_PENDING.
tds | A pointer to the TDSSOCKET structure managing a client/server operation. |
|
static |
Quote a string properly.
Output string is always NUL-terminated
buffer | output buffer. If NULL function will just return required bytes |
quoting | quote character (should be one of '\'', '"', ']') |
id | string to quote |
len | length of string to quote |
|
static |
Send a string to server while quoting it.
tds | A pointer to the TDSSOCKET structure managing a client/server operation. |
s | string start |
end | string end |
size_t tds_quote_id | ( | TDSSOCKET * | tds, |
char * | buffer, | ||
const char * | id, | ||
int | idlen | ||
) |
Quote an id.
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 (< 0 for NUL terminated) |
size_t tds_quote_id_rpc | ( | TDSSOCKET * | tds, |
char * | buffer, | ||
const char * | id, | ||
int | idlen | ||
) |
Quote an id for a RPC call.
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 (< 0 for NUL terminated) |
size_t tds_quote_string | ( | TDSSOCKET *tds | TDS_UNUSED, |
char * | buffer, | ||
const char * | str, | ||
int | len | ||
) |
Quote a string.
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 NUL-terminated) |
len | length of string (-1 for NUL-terminated) |
TDSRET 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.
tds | state information for the socket and the TDS protocol |
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.
Set current cursor.
Current cursor is the one will receive output from server.
tds | A pointer to the TDSSOCKET structure managing a client/server operation. |
cursor | cursor to set as current |
void tds_set_cur_dyn | ( | TDSSOCKET * | tds, |
TDSDYNAMIC * | dyn | ||
) |
Set current dynamic.
tds | A pointer to the TDSSOCKET structure managing a client/server operation. |
dyn | dynamic to set |
const char * tds_skip_comment | ( | const char * | s | ) |
Skip a comment in a query.
s | start of the string (or part of it) |
|
static |
Skip a comment in a query.
s | start of the string (or part of it). Encoded in ucs2le |
end | end of string |
const char * tds_skip_quoted | ( | const char * | s | ) |
Skip quoting string (like 'sfsf', "dflkdj" or [dfkjd])
s | pointer to first quoting character. Should be ', " or [. |
|
static |
Return pointer to end of a quoted string.
At the beginning pointer should point to delimiter.
s | start of string to skip encoded in ucs2le |
end | pointer to end of string |
void tds_start_query | ( | TDSSOCKET * | tds, |
unsigned char | packet_type | ||
) |
Start query packet of a given type.
tds | A pointer to the TDSSOCKET structure managing a client/server operation. |
packet_type | packet type |
|
static |
Start query packet of a given type.
tds | A pointer to the TDSSOCKET structure managing a client/server operation. |
packet_type | packet type |
head | extra information to put in a TDS7 header |
TDSRET tds_submit_begin_tran | ( | TDSSOCKET * | tds | ) |
Send a rollback request.
TDS 7.2+ need this in order to handle transactions correctly if MARS is used.
tds | A pointer to the TDSSOCKET structure managing a client/server operation. |
TDSRET tds_submit_commit | ( | TDSSOCKET * | tds, |
bool | cont | ||
) |
Send a commit request.
TDS 7.2+ need this in order to handle transactions correctly if MARS is used.
tds | A pointer to the TDSSOCKET structure managing a client/server operation. |
cont | true to start a new transaction |
TDSRET tds_submit_execdirect | ( | TDSSOCKET * | tds, |
const char * | query, | ||
TDSPARAMINFO * | params, | ||
TDSHEADERS * | head | ||
) |
Submit a prepared query with parameters.
tds | state information for the socket and the TDS protocol |
query | language query with given placeholders (?) |
params | parameters to send |
TDSRET tds_submit_execute | ( | TDSSOCKET * | tds, |
TDSDYNAMIC * | dyn | ||
) |
Sends a previously prepared dynamic statement to the server.
tds | state information for the socket and the TDS protocol |
dyn | dynamic proc to execute. Must build from same tds. |
TDSRET tds_submit_optioncmd | ( | TDSSOCKET * | tds, |
TDS_OPTION_CMD | command, | ||
TDS_OPTION | option, | ||
TDS_OPTION_ARG * | param, | ||
TDS_INT | param_size | ||
) |
Send option commands to server.
Option commands are used to change server options.
tds | A pointer to the TDSSOCKET structure managing a client/server operation. |
command | command type. |
option | option to set/get. |
param | parameter value |
param_size | length of parameter value in bytes |
TDSRET tds_submit_prepare | ( | TDSSOCKET * | tds, |
const char * | query, | ||
const char * | id, | ||
TDSDYNAMIC ** | dyn_out, | ||
TDSPARAMINFO * | params | ||
) |
Creates a temporary stored procedure in the server.
Under TDS 4.2 dynamic statements are emulated building sql command. TDS 5 does not uses parameters type. TDS 7+ uses parameter types to prepare the query. You should prepare again the query if parameters changes.
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+ |
TDSRET tds_submit_query | ( | TDSSOCKET * | tds, |
const char * | 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.
tds | A pointer to the TDSSOCKET structure managing a client/server operation. |
query | language query to submit |
TDSRET tds_submit_query_params | ( | TDSSOCKET * | tds, |
const char * | query, | ||
TDSPARAMINFO * | params, | ||
TDSHEADERS * | head | ||
) |
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.
tds | A pointer to the TDSSOCKET structure managing a client/server operation. |
query | language query to submit |
params | parameters of query |
TDSRET tds_submit_queryf | ( | TDSSOCKET * | tds, |
const char * | queryf, | ||
... | |||
) |
Format and submit a query.
tds | A pointer to the TDSSOCKET structure managing a client/server operation. |
queryf | query format. printf like expansion is performed on this query. |
TDSRET tds_submit_rollback | ( | TDSSOCKET * | tds, |
bool | cont | ||
) |
Send a rollback request.
TDS 7.2+ need this in order to handle transactions correctly if MARS is used.
tds | A pointer to the TDSSOCKET structure managing a client/server operation. |
cont | true to start a new transaction |
TDSRET tds_submit_rpc | ( | TDSSOCKET * | tds, |
const char * | rpc_name, | ||
TDSPARAMINFO * | params, | ||
TDSHEADERS * | head | ||
) |
Calls a RPC from server.
Output parameters will be stored in tds->param_info.
tds | state information for the socket and the TDS protocol |
rpc_name | name of RPC |
params | parameters information. NULL for no parameters |
TDSRET tds_submit_unprepare | ( | TDSSOCKET * | tds, |
TDSDYNAMIC * | dyn | ||
) |
Send a unprepare request for a prepared query.
tds | state information for the socket and the TDS protocol |
dyn | dynamic query |