convert.c
来自「postgresql-odbc,跨平台应用」· C语言 代码 · 共 2,560 行 · 第 1/5 页
C
2,560 行
for (endpos -= backstep, wstmt -= backstep; endpos >= 0 && isspace((UCHAR) *wstmt); endpos--, wstmt--) ; if (endpos < 0) return FALSE; for (stapos = endpos; stapos >= 0 && !isspace((UCHAR) *wstmt); stapos--, wstmt--) ; if (stapos < 0) return FALSE; wstmt++; switch (tokenwd = endpos - stapos) { case 4: if (strnicmp(wstmt, "FULL", tokenwd) == 0 || strnicmp(wstmt, "LEFT", tokenwd) == 0) break; return FALSE; case 5: if (strnicmp(wstmt, "OUTER", tokenwd) == 0 || strnicmp(wstmt, "RIGHT", tokenwd) == 0) break; if (strnicmp(wstmt, "INNER", tokenwd) == 0 || strnicmp(wstmt, "CROSS", tokenwd) == 0) { outerj = FALSE; break; } return FALSE; default: return FALSE; } if (stmt) { if (outerj) SC_set_outer_join(stmt); else SC_set_inner_join(stmt); } return TRUE;}/*---------- * Check if the statement is * INSERT INTO ... () VALUES () * This isn't really a strict check but ... *---------- */static BOOLinsert_without_target(const char *stmt, int *endpos){ const char *wstmt = stmt; while (isspace((UCHAR) *(++wstmt))); if (!*wstmt) return FALSE; if (strnicmp(wstmt, "VALUES", 6)) return FALSE; wstmt += 6; if (!wstmt[0] || !isspace((UCHAR) wstmt[0])) return FALSE; while (isspace((UCHAR) *(++wstmt))); if (*wstmt != '(' || *(++wstmt) != ')') return FALSE; wstmt++; *endpos = wstmt - stmt; return !wstmt[0] || isspace((UCHAR) wstmt[0]) || ';' == wstmt[0];}staticRETCODE prep_params(StatementClass *stmt, QueryParse *qp, QueryBuild *qb);static intPrepare_and_convert(StatementClass *stmt, QueryParse *qp, QueryBuild *qb){ CSTR func = "Prepare_and_convert"; char *new_statement, *exe_statement = NULL; int retval; ConnectionClass *conn = SC_get_conn(stmt); ConnInfo *ci = &(conn->connInfo); BOOL discardOutput, outpara; if (PROTOCOL_74(ci)) { switch (stmt->prepared) { case NOT_YET_PREPARED: case ONCE_DESCRIBED: break; default: return SQL_SUCCESS; } } if (QB_initialize(qb, qp->stmt_len, stmt, NULL) < 0) return SQL_ERROR; if (PROTOCOL_74(ci)) return prep_params(stmt, qp, qb); discardOutput = (0 != (qb->flags & FLGB_DISCARD_OUTPUT)); if (NOT_YET_PREPARED == stmt->prepared) /* not yet prepared */ { ssize_t elen; int i, oc; SQLSMALLINT marker_count; const IPDFields *ipdopts = qb->ipdopts; char plan_name[32]; qb->flags |= FLGB_BUILDING_PREPARE_STATEMENT; sprintf(plan_name, "_PLAN%p", stmt);#ifdef NOT_USED new_statement = qb->query_statement; sprintf(new_statement, "PREPARE \"%s\"", plan_name); qb->npos = strlen(new_statement);#endif /* NOT_USED */ CVT_APPEND_STR(qb, "PREPARE \""); CVT_APPEND_STR(qb, plan_name); CVT_APPEND_CHAR(qb, '"'); marker_count = stmt->num_params - qb->num_discard_params; if (!ipdopts || ipdopts->allocated < marker_count) { SC_set_error(stmt, STMT_COUNT_FIELD_INCORRECT, "The # of binded parameters < the # of parameter markers", func); return SQL_ERROR; } if (marker_count > 0) { CVT_APPEND_CHAR(qb, '('); for (i = qb->proc_return, oc = 0; i < stmt->num_params; i++) { outpara = FALSE; if (i < ipdopts->allocated && SQL_PARAM_OUTPUT == ipdopts->parameters[i].paramType) { outpara = TRUE; if (discardOutput) continue; } if (oc > 0) CVT_APPEND_STR(qb, ", "); if (outpara) CVT_APPEND_STR(qb, "void"); else CVT_APPEND_STR(qb, pgtype_to_name(stmt, ipdopts->parameters[i].PGType, FALSE)); oc++; } CVT_APPEND_CHAR(qb, ')'); } CVT_APPEND_STR(qb, " as "); for (qp->opos = 0; qp->opos < qp->stmt_len; qp->opos++) { retval = inner_process_tokens(qp, qb); if (SQL_ERROR == retval) { QB_replace_SC_error(stmt, qb, func); QB_Destructor(qb); return retval; } } CVT_APPEND_CHAR(qb, ';'); /* build the execute statement */ exe_statement = malloc(30 + 2 * marker_count); sprintf(exe_statement, "EXECUTE \"%s\"", plan_name); if (marker_count > 0) { elen = strlen(exe_statement); exe_statement[elen++] = '('; for (i = 0; i < marker_count; i++) { if (i > 0) exe_statement[elen++] = ','; exe_statement[elen++] = '?'; } exe_statement[elen++] = ')'; exe_statement[elen] = '\0'; }inolog("exe_statement=%s\n", exe_statement); stmt->execute_statement = exe_statement; QP_initialize(qp, stmt); SC_set_planname(stmt, plan_name); } qb->flags &= FLGB_DISCARD_OUTPUT; qb->flags |= FLGB_EXECUTE_PREPARED; qb->param_number = -1; for (qp->opos = 0; qp->opos < qp->stmt_len; qp->opos++) { retval = inner_process_tokens(qp, qb); if (SQL_ERROR == retval) { QB_replace_SC_error(stmt, qb, func); if (exe_statement) { free(exe_statement); stmt->execute_statement = NULL; } QB_Destructor(qb); return retval; } } /* make sure new_statement is always null-terminated */ CVT_TERMINATE(qb); if (exe_statement) SC_set_concat_prepare_exec(stmt); stmt->stmt_with_params = qb->query_statement; qb->query_statement = NULL; return SQL_SUCCESS;}#define my_strchr(conn, s1,c1) pg_mbschr(conn->ccsc, s1,c1)staticRETCODE prep_params(StatementClass *stmt, QueryParse *qp, QueryBuild *qb){ CSTR func = "prep_params"; RETCODE retval; BOOL ret; ConnectionClass *conn = SC_get_conn(stmt); QResultClass *res; char plan_name[32], multi; int func_cs_count = 0; const char *orgquery, *srvquery; Int4 endp1, endp2; SQLSMALLINT num_pa = 0, num_p1, num_p2;inolog("prep_params\n"); qb->flags |= FLGB_BUILDING_PREPARE_STATEMENT; for (qp->opos = 0; qp->opos < qp->stmt_len; qp->opos++) { retval = inner_process_tokens(qp, qb); if (SQL_ERROR == retval) { QB_replace_SC_error(stmt, qb, func); QB_Destructor(qb); return retval; } } CVT_TERMINATE(qb); retval = SQL_ERROR;#define return DONT_CALL_RETURN_FROM_HERE??? ENTER_INNER_CONN_CS(conn, func_cs_count); if (NAMED_PARSE_REQUEST == SC_get_prepare_method(stmt)) sprintf(plan_name, "_PLAN%p", stmt); else strcpy(plan_name, NULL_STRING); stmt->current_exec_param = 0; multi = stmt->multi_statement; if (multi > 0) { orgquery = stmt->statement; SC_scanQueryAndCountParams(orgquery, conn, &endp1, &num_p1, NULL, NULL); srvquery = qb->query_statement; SC_scanQueryAndCountParams(srvquery, conn, &endp2, NULL, NULL, NULL); mylog("%s:SendParseRequest for the first command length=%d(%d) num_p=%d\n", func, endp2, endp1, num_p1); ret = SendParseRequest(stmt, plan_name, srvquery, endp2, num_p1); } else ret = SendParseRequest(stmt, plan_name, qb->query_statement, SQL_NTS, -1); if (!ret) goto cleanup; if (!SendDescribeRequest(stmt, plan_name)) goto cleanup; SC_set_planname(stmt, plan_name); if (!(res = SendSyncAndReceive(stmt, NULL, "prepare_and_describe"))) { SC_set_error(stmt, STMT_EXEC_ERROR, "commnication error while preapreand_describe", func); CC_on_abort(conn, CONN_DEAD); goto cleanup; } SC_set_Result(stmt, res); if (!QR_command_maybe_successful(res)) { SC_set_error(stmt, STMT_EXEC_ERROR, "Error while preparing parameters", func); goto cleanup; } if (stmt->multi_statement <= 0) { retval = SQL_SUCCESS; goto cleanup; } while (multi > 0) { orgquery += (endp1 + 1); srvquery += (endp2 + 1); num_pa += num_p1; SC_scanQueryAndCountParams(orgquery, conn, &endp1, &num_p1, &multi, NULL); SC_scanQueryAndCountParams(srvquery, conn, &endp2, &num_p2, NULL, NULL); mylog("%s:SendParseRequest for the subsequent command length=%d(%d) num_p=%d\n", func, endp2, endp1, num_p1); if (num_p2 > 0) { stmt->current_exec_param = num_pa; ret = SendParseRequest(stmt, plan_name, srvquery, endp2 < 0 ? SQL_NTS : endp2, num_p1); if (!ret) goto cleanup; if (!SendDescribeRequest(stmt, plan_name)) goto cleanup; if (!(res = SendSyncAndReceive(stmt, NULL, "prepare_and_describe"))) { SC_set_error(stmt, STMT_EXEC_ERROR, "commnication error while preapreand_describe", func); CC_on_abort(conn, CONN_DEAD); goto cleanup; } QR_Destructor(res); } } retval = SQL_SUCCESS;cleanup:#undef return CLEANUP_FUNC_CONN_CS(func_cs_count, conn); stmt->current_exec_param = -1; QB_Destructor(qb); return retval;}RETCODE prepareParameters(StatementClass *stmt){ switch (stmt->prepared) { QueryParse query_org, *qp; QueryBuild query_crt, *qb; case NOT_YET_PREPARED: case ONCE_DESCRIBED:inolog("prepareParameters\n"); qp = &query_org; QP_initialize(qp, stmt); qb = &query_crt; if (QB_initialize(qb, qp->stmt_len, stmt, NULL) < 0) return SQL_ERROR; return prep_params(stmt, qp, qb); } return SQL_SUCCESS;}/* * This function inserts parameters into an SQL statements. * It will also modify a SELECT statement for use with declare/fetch cursors. * This function does a dynamic memory allocation to get rid of query size limit! */intcopy_statement_with_parameters(StatementClass *stmt, BOOL buildPrepareStatement){ CSTR func = "copy_statement_with_parameters"; RETCODE retval; QueryParse query_org, *qp; QueryBuild query_crt, *qb; char *new_statement; BOOL begin_first = FALSE, prepare_dummy_cursor = FALSE; ConnectionClass *conn = SC_get_conn(stmt); ConnInfo *ci = &(conn->connInfo); SQLLEN current_row; const char *bestitem = NULL;inolog("%s: enter prepared=%d\n", func, stmt->prepared); if (!stmt->statement) { SC_set_error(stmt, STMT_INTERNAL_ERROR, "No statement string", func); return SQL_ERROR; } current_row = stmt->exec_current_row < 0 ? 0 : stmt->exec_current_row; qp = &query_org; QP_initialize(qp, stmt); if (stmt->statement_type != STMT_TYPE_SELECT) { stmt->options.cursor_type = SQL_CURSOR_FORWARD_ONLY; stmt->options.scroll_concurrency = SQL_CONCUR_READ_ONLY; } else if (stmt->options.cursor_type == SQL_CURSOR_FORWARD_ONLY) stmt->options.scroll_concurrency = SQL_CONCUR_READ_ONLY; else if (stmt->options.scroll_concurrency != SQL_CONCUR_READ_ONLY) { if (SQL_CURSOR_KEYSET_DRIVEN == stmt->options.cursor_type) { if (0 == (ci->updatable_cursors & ALLOW_KEYSET_DRIVEN_CURSORS)) stmt->options.cursor_type = SQL_CURSOR_STATIC; else { if (SC_update_not_ready(stmt)) parse_statement(stmt, TRUE); if (stmt->updatable && stmt->ntab > 0) { if (bestitem = GET_NAME(stmt->ti[0]->bestitem), NULL == bestitem) stmt->options.cursor_type = SQL_CURSOR_STATIC; } } } if (SQL_CURSOR_STATIC == stmt->options.cursor_type) { if (0 == (ci->updatable_cursors & ALLOW_STATIC_CURSORS)) stmt->options.scroll_concurrency = SQL_CONCUR_READ_ONLY; else if (SC_update_not_ready(stmt)) parse_statement(stmt, TRUE); } if (SC_parsed_status(stmt) == STMT_PARSE_FATAL) { stmt->options.scroll_concurrency = SQL_CONCUR_READ_ONLY; if (stmt->options.cursor_type == SQL_CURSOR_KEYSET_DRIVEN) stmt->options.cursor_type = SQL_CURSOR_STATIC; } else if (!stmt->updatable) { stmt->options.scroll_concurrency = SQL_CONCUR_READ_ONLY; stmt->options.cursor_type = SQL_CURSOR_STATIC; } else { qp->from_pos = stmt->from_pos; qp->where_pos = stmt->where_pos; }inolog("type=%d concur=%d\n", stmt->options.cursor_type, stmt->options.scroll_concurrency); } SC_miscinfo_clear(stmt); /* If the application hasn't set a cursor name, then generate one */ if (!SC_cursor_is_valid(stmt)) { char curname[32]; sprintf(curname, "SQL_CUR%p", stmt); STR_TO_NAME(stmt->cursor_name, curname); } if (stmt->stmt_with_params) { free(stmt->stmt_with_params); stmt->stmt_with_params = NULL; } SC_no_fetchcursor(stmt); SC_no_pre_executable(stmt); if (stmt->statement_type == STMT_TYPE_SELECT) SC_set_pre_executable(stmt); qb = &query_crt; qb->query_statement = NULL; if (PREPARED_PERMANENTLY == stmt->prepared || (buildPrepareStatement && stmt->options.scroll_concurrency == SQL_CONCUR_READ_ONLY)) { retval = Prepare_and_convert(stmt, qp, qb); return retval; } if (ci->disallow_premature) prepare_dummy_cursor = stmt->pre_executing; if (prepare_dummy_cursor) qp->flags |= FLGP_PREPARE_DUMMY_CURSOR; if (QB_initialize(qb, qp->stmt_len, stmt, NULL) < 0) return SQL_ERROR; new_statement = qb->query_statement; /* For selects, prepend a declare cursor to the statement */ if (stmt->statement_type == STMT_TYPE_SELECT && !stmt->internal) { const char *opt_scroll = NULL_STRING, *opt_hold = NULL_STRING; if (prepare_dummy_cursor) { SC_set_fetchcursor(stmt); if (PG_VERSION_GE(conn, 7.4)) opt_scroll = " scroll"; if (!CC_is_in_trans(conn) && PG_VERSION_GE(conn, 7.1)) { strcpy(new_statement, "BEGIN;"); begin_first = TRUE; } } else if (ci->drivers.use_declarefetch /** && SQL_CONCUR_READ_ONLY == stmt->options.scroll_concurrency **/ ) { SC_set_fetchcursor(stmt); if (SC_is_with_hold(stmt)) opt_hold = " with hold"; if (PG_VERSION_GE(conn, 7.4)) { if (SQL_CURSOR_FORWARD_ONLY != stmt->options.cursor_type) opt_scroll = " scroll"; } } if (SC_is_fetchcursor(stmt)) { sprintf(new_statement, "%sdeclare \"%s\"%s cursor%s for ", new_statement, SC_cursor_name(stmt), opt_scroll, opt_hold); qb->npos = strlen(new_statement); qp->flags |= FLGP_CURSOR_CHECK_OK; qp->declare_pos = qb->npos; } else if (SQL_CONCUR_READ_ONLY != stmt->options.scroll_concurrency) { qb->flags |= FLGB_CREATE_KEYSET; if (SQL_CURSOR_KEYSET_DRIVEN == stmt->options.cursor_type) qb->flags |= FLGB_KEYSET_DRIVEN; } } for (qp->opos = 0; qp->opos < qp->stmt_len; qp->opos++) { retval = inner_process_tokens(qp, qb); if (SQL_ERROR == retval) { QB_replace_SC_error(stmt, qb, func); QB_Destructor(qb); return retval; } } /* make sure new_statement is always null-terminated */ CVT_TERMINATE(qb); new_statement = qb->query_statement; stmt->statement_type = qp->statement_type; stmt->inaccurate_result = (0 != (qb->flags & FLGB_INACCURATE_RESULT)); if (0 != (qp->flags & FLGP_SELECT_INTO) || 0 !=
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?