📄 mapi.mx
字号:
#ifdef HAVE_OPENSSL int secure;#endif char *username; char *password; char *language; char *database; /* to obtain from server */ int languageId; int versionId; /* Monet 4 or 5 */ char *motd; /* welcome message from server */ int profile; /* profile Mapi interaction */ int trace; /* Trace Mapi interaction */ int auto_commit; char *noexplain; /* on error, don't explain, only print result */ MapiMsg error; /* Error occurred */ char *errorstr; /* error from server */ const char *action; /* pointer to constant string */ struct BlockCache blk; int connected; MapiHdl first; /* start of doubly-linked list */ MapiHdl active; /* set when not all rows have been received */ int cachelimit; /* default maximum number of rows to cache */ int redircnt; /* redirection count */ stream *tracelog; /* keep a log for inspection */ stream *from, *to;};struct MapiResultSet { struct MapiResultSet *next; struct MapiStatement *hdl; int tableid; /* SQL id of current result set */ int querytype; /* type of SQL query */ int row_count; int fieldcnt; int maxfields; char *errorstr; /* error from server */ struct MapiColumn *fields; struct MapiRowBuf cache;};@htypedef struct MapiStatement *MapiHdl;@cstruct MapiStatement { struct MapiStruct *mid; char *template; /* keep parameterized query text around */ char *query; int maxbindings; struct MapiBinding *bindings; int maxparams; struct MapiParam *params; struct MapiResultSet *result, *active, *lastresult; int needmore; /* need more input */ int *pending_close; int npending_close; MapiHdl prev, next;};@-All external calls to the library should pass the mapi-checkroutine. It assures a working connection and proper reset ofthe error status of the Mapi structure.@c#ifdef DEBUG#define debugprint(fmt,arg) printf(fmt,arg)#else#define debugprint(fmt,arg) ((void) 0)#endif#define mapi_check(X,C) \ do { \ debugprint("entering %s\n", (C)); \ assert(X); \ if ((X)->connected == 0) { \ mapi_setError((X), "Connection lost", (C), MERROR); \ return (X)->error; \ } \ mapi_clrError(X); \ } while (0)#define mapi_check0(X,C) \ do { \ debugprint("entering %s\n", (C)); \ assert(X); \ if ((X)->connected == 0) { \ mapi_setError((X), "Connection lost", (C), MERROR); \ return 0; \ } \ mapi_clrError(X); \ } while (0)#define mapi_hdl_check(X,C) \ do { \ debugprint("entering %s\n", (C)); \ assert(X); \ assert((X)->mid); \ if ((X)->mid->connected == 0) { \ mapi_setError((X)->mid, "Connection lost", (C), MERROR); \ return (X)->mid->error; \ } \ mapi_clrError((X)->mid); \ } while (0)#define mapi_hdl_check0(X,C) \ do { \ debugprint("entering %s\n", (C)); \ assert(X); \ assert((X)->mid); \ if ((X)->mid->connected == 0) { \ mapi_setError((X)->mid, "Connection lost", (C), MERROR); \ return 0; \ } \ mapi_clrError((X)->mid); \ } while (0)@h#ifdef __cplusplusextern "C" {#endif/* avoid using "#ifdef WIN32" so that this file does not need our config.h */#if defined(_MSC_VER) || defined(__CYGWIN__) || defined(__MINGW32__)#ifndef LIBMAPI#define mapi_export extern __declspec(dllimport)#else#define mapi_export extern __declspec(dllexport)#endif#else#define mapi_export extern#endif/* three structures used for communicating date/time information *//* these structs are deliberately compatible with the ODBC versions SQL_DATE_STRUCT, SQL_TIME_STRUCT, and SQL_TIMESTAMP_STRUCT */typedef struct { /* used by MAPI_DATE */ short year; unsigned short month; unsigned short day;} MapiDate;typedef struct { /* used by MAPI_TIME */ unsigned short hour; unsigned short minute; unsigned short second;} MapiTime;typedef struct { /* used by MAPI_DATETIME */ short year; unsigned short month; unsigned short day; unsigned short hour; unsigned short minute; unsigned short second; unsigned int fraction; /* in 1000 millionths of a second (10e-9) */} MapiDateTime;/* connection-oriented functions */mapi_export Mapi mapi_mapi(const char *host, int port, const char *username, const char *password, const char *lang, const char *dbname);mapi_export MapiMsg mapi_destroy(Mapi mid);mapi_export MapiMsg mapi_start_talking(Mapi mid);mapi_export Mapi mapi_connect(const char *host, int port, const char *username, const char *password, const char *lang, const char *dbname);mapi_export Mapi mapi_connect_ssl(const char *host, int port, const char *username, const char *password, const char *lang, const char *dbname);#ifdef ST_READ /* if stream.h was included */mapi_export stream **mapi_embedded_init(Mapi *midp, char *lang);#endifmapi_export MapiMsg mapi_disconnect(Mapi mid);mapi_export MapiMsg mapi_reconnect(Mapi mid);mapi_export MapiMsg mapi_ping(Mapi mid);mapi_export MapiMsg mapi_error(Mapi mid);mapi_export char *mapi_error_str(Mapi mid);mapi_export void mapi_noexplain(Mapi mid, char *errorprefix);mapi_export MapiMsg mapi_explain(Mapi mid, FILE *fd);mapi_export MapiMsg mapi_explain_query(MapiHdl hdl, FILE *fd);mapi_export MapiMsg mapi_explain_result(MapiHdl hdl, FILE *fd);mapi_export MapiMsg mapi_output(Mapi mid, char *output);mapi_export MapiMsg mapi_stream_into(Mapi mid, char *docname, char *colname);mapi_export MapiMsg mapi_profile(Mapi mid, int flag);mapi_export MapiMsg mapi_trace(Mapi mid, int flag);mapi_export int mapi_get_trace(Mapi mid);mapi_export MapiMsg mapi_trace_log(Mapi mid, const char *nme);mapi_export MapiMsg mapi_setAutocommit(Mapi mid, int autocommit);mapi_export char *mapi_result_error(MapiHdl hdl);mapi_export MapiMsg mapi_next_result(MapiHdl hdl);mapi_export MapiMsg mapi_needmore(MapiHdl hdl);mapi_export int mapi_more_results(MapiHdl hdl);mapi_export MapiHdl mapi_new_handle(Mapi mid);mapi_export MapiMsg mapi_close_handle(MapiHdl hdl);mapi_export MapiMsg mapi_bind(MapiHdl hdl, int fnr, char **ptr);mapi_export MapiMsg mapi_bind_var(MapiHdl hdl, int fnr, int type, void *ptr);mapi_export MapiMsg mapi_bind_numeric(MapiHdl hdl, int fnr, int scale, int precision, void *ptr);mapi_export MapiMsg mapi_clear_bindings(MapiHdl hdl);mapi_export MapiMsg mapi_param_type(MapiHdl hdl, int fnr, int ctype, int sqltype, void *ptr);mapi_export MapiMsg mapi_param_string(MapiHdl hdl, int fnr, int sqltype, char *ptr, int *sizeptr);mapi_export MapiMsg mapi_param(MapiHdl hdl, int fnr, char **ptr);mapi_export MapiMsg mapi_param_numeric(MapiHdl hdl, int fnr, int scale, int precision, void *ptr);mapi_export MapiMsg mapi_clear_params(MapiHdl hdl);mapi_export MapiHdl mapi_prepare(Mapi mid, const char *cmd);mapi_export MapiMsg mapi_prepare_handle(MapiHdl hdl, const char *cmd);mapi_export MapiMsg mapi_virtual_result(MapiHdl hdl, int columns, const char **columnnames, const char **columntypes, const int *columnlengths, int tuplecount, const char ***tuples);mapi_export MapiMsg mapi_execute(MapiHdl hdl);mapi_export MapiMsg mapi_execute_array(MapiHdl hdl, char **val);mapi_export MapiMsg mapi_fetch_reset(MapiHdl hdl);mapi_export MapiMsg mapi_finish(MapiHdl hdl);mapi_export MapiHdl mapi_prepare_array(Mapi mid, const char *cmd, char **val);mapi_export MapiHdl mapi_query(Mapi mid, const char *cmd);mapi_export MapiMsg mapi_query_handle(MapiHdl hdl, const char *cmd);mapi_export MapiHdl mapi_query_prep(Mapi mid);mapi_export MapiMsg mapi_query_part(MapiHdl hdl, const char *cmd, size_t size);mapi_export MapiMsg mapi_query_done(MapiHdl hdl);mapi_export MapiHdl mapi_quick_query(Mapi mid, const char *cmd, FILE *fd);mapi_export MapiHdl mapi_query_array(Mapi mid, const char *cmd, char **val);mapi_export MapiHdl mapi_quick_query_array(Mapi mid, const char *cmd, char **val, FILE *fd);mapi_export MapiHdl mapi_stream_query(Mapi mid, const char *cmd, int windowsize);mapi_export MapiMsg mapi_cache_limit(Mapi mid, int limit);mapi_export MapiMsg mapi_cache_shuffle(MapiHdl hdl, int percentage);mapi_export MapiMsg mapi_cache_freeup(MapiHdl hdl, int percentage);mapi_export MapiMsg mapi_quick_response(MapiHdl hdl, FILE *fd);mapi_export MapiMsg mapi_seek_row(MapiHdl hdl, int rowne, int whence);mapi_export MapiMsg mapi_timeout(Mapi mid, int time);mapi_export int mapi_fetch_row(MapiHdl hdl);mapi_export int mapi_fetch_all_rows(MapiHdl hdl);mapi_export int mapi_get_field_count(MapiHdl hdl);mapi_export int mapi_get_row_count(MapiHdl hdl);mapi_export int mapi_rows_affected(MapiHdl hdl);mapi_export char *mapi_fetch_field(MapiHdl hdl, int fnr);mapi_export MapiMsg mapi_store_field(MapiHdl hdl, int fnr, int outtype, void *outparam);mapi_export char **mapi_fetch_field_array(MapiHdl hdl);mapi_export char *mapi_fetch_line(MapiHdl hdl);mapi_export char *mapi_get_lang(Mapi mid);mapi_export char *mapi_get_dbname(Mapi mid);mapi_export char *mapi_get_host(Mapi mid);mapi_export char *mapi_get_user(Mapi mid);mapi_export char *mapi_get_mapi_version(Mapi mid);mapi_export char *mapi_get_monet_version(Mapi mid);mapi_export int mapi_get_monet_versionId(Mapi mid);mapi_export char *mapi_get_motd(Mapi mid);mapi_export int mapi_is_connected(Mapi mid);mapi_export char *mapi_get_table(MapiHdl hdl, int fnr);mapi_export char *mapi_get_name(MapiHdl hdl, int fnr);mapi_export char *mapi_get_type(MapiHdl hdl, int fnr);mapi_export int mapi_get_len(MapiHdl hdl, int fnr);mapi_export int mapi_get_querytype(MapiHdl hdl);mapi_export int mapi_get_tableid(MapiHdl hdl);mapi_export char *mapi_quote(const char *msg, int size);mapi_export char *mapi_unquote(char *msg);mapi_export MapiHdl mapi_get_active (Mapi mid);#ifdef __cplusplus}#endif#endif /* _MAPI_H_INCLUDED */@@- Mapi Functions.The application interface commands are described below.They have been developed to ease interaction.@cstatic Mapi mapi_new(void);static int mapi_extend_bindings(MapiHdl hdl, int minbindings);static int mapi_extend_params(MapiHdl hdl, int minparams);static MapiMsg mapi_setError(Mapi mid, const char *msg, const char *action, MapiMsg error);static void close_connection(Mapi mid);static MapiMsg read_into_cache(MapiHdl hdl, int lookahead);static int unquote(const char *msg, char **start, const char **next, int endchar);static int mapi_slice_row(struct MapiResultSet *result, int cr);static void mapi_store_bind(struct MapiResultSet *result, int cr);#ifdef HAVE_OPENSSLstatic SSL_CTX *mapi_ssl_ctx = 0;#endifstatic int mapi_initialized = 0;#ifdef HAVE_LONG_LONGtypedef unsigned long long mapi_uint64;typedef long long mapi_int64;#else#ifdef HAVE___INT64typedef unsigned __int64 mapi_uint64;typedef __int64 mapi_int64;#endif#endif#define check_stream(mid,s,msg,f,e) \ do { \ if ((s) == NULL || stream_errnr(s)) { \ close_connection(mid); \ mapi_setError((mid), (msg), (f), MTIMEOUT); \ return (e); \ } \ } while (0)#define REALLOC(p,c) ((p) = ((p) ? realloc((p),(c)*sizeof(*(p))) : malloc((c)*sizeof(*(p)))))@- BlockingThe server side code works with a common/stream package, a fast buffered IO scheme.However, this package is not always available for every language runtime system.For those cases a simple line-based protocol is also supported by the server.@- Error HandlingAll externally visible functions should first call mapi_clrError (usuallythough a call to one of the check macros above) to clear the error flag.When an error is detected, the library calls mapi_setError to set the errorflag. The application can call mapi_error or mapi_error_str to check forerrors, and mapi_explain or mapi_explain_query to print a formatted errorreport.@cstatic voidmapi_clrError(Mapi mid){ assert(mid); if (mid->errorstr) free(mid->errorstr); mid->action = 0; /* contains references to constants */ mid->error = 0; mid->errorstr = 0;}static MapiMsgmapi_setError(Mapi mid, const char *msg, const char *action, MapiMsg error){ assert(msg); REALLOC(mid->errorstr, strlen(msg) + 1); strcpy(mid->errorstr, msg); mid->error = error; mid->action = action; return mid->error;}MapiMsgmapi_error(Mapi mid){ assert(mid); return mid->error;}char *mapi_error_str(Mapi mid){ assert(mid); return mid->errorstr;}static voidclean_print(char *msg, const char *prefix, FILE *fd){ int len = (int) strlen(prefix); while (msg && *msg) { /* cut by line */ char *p = strchr(msg, '\n'); if (p) *p++ = 0; /* skip over prefix */ if (strncmp(msg, prefix, len) == 0) msg += len; /* output line */ fputs(msg, fd); fputc('\n', fd); msg = p; }}static voidindented_print(const char *msg, const char *prefix, FILE *fd){ /* for multiline error messages, indent all subsequent lines with the space it takes to print "ERROR = " */ const char *s, *p, *q; s = prefix; p = msg; while (p && *p) { fprintf(fd, "%s", s); s = " "; q = strchr(p, '\n'); if (q) { q++; /* also print the newline */ fprintf(fd, "%.*s", (int) (q - p), p); } else { /* print bit after last newline, adding one ourselves */ fprintf(fd, "%s\n", p); break; /* nothing more to do */ } p = q; }}voidmapi_noexplain(Mapi mid, char *errorprefix){ assert(mid); mid->noexplain = errorprefix;}MapiMsgmapi_explain(Mapi mid, FILE *fd){ assert(mid); if (mid->noexplain == NULL) { fprintf(fd, "MAPI = %s\@%s:%d\n", mid->username, mid->hostname, mid->port); if (mid->action) fprintf(fd, "ACTION= %s\n", mid->action); if (mid->errorstr) indented_print(mid->errorstr, "ERROR = ", fd); } else if (mid->errorstr) { clean_print(mid->errorstr, mid->noexplain, fd); } fflush(fd); mapi_clrError(mid); return MOK;}MapiMsgmapi_explain_query(MapiHdl hdl, FILE *fd){ Mapi mid;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -