| | |
| | | */ |
| | | Datum owa_util_showsource(PG_FUNCTION_ARGS) |
| | | { |
| | | char *cname = text_to_cstring(PG_GETARG_TEXT_P(0)); |
| | | |
| | | char *proc_name, *owner_name; |
| | | |
| | | char *dot_loc = strchr(cname, '.'); |
| | | if (dot_loc != nullptr) |
| | | { |
| | | *dot_loc = '\0'; |
| | | owner_name = cname; |
| | | proc_name = dot_loc + 1; |
| | | } |
| | | else |
| | | { |
| | | // TODO |
| | | } |
| | | |
| | | int res = SPI_connect(); |
| | | |
| | | std::string query = "SELECT line, text FROM \"SYS\".\"ALL_SOURCE\" WHERE \"NAME\" = upper("; |
| | | query.append(proc_name); |
| | | query.append(") AND \"OWNER\" = upper("); |
| | | query.append(owner_name); |
| | | query.append(" ORDER BY \"TYPE\", \"LINE\""); |
| | | |
| | | res = SPI_execute(query.c_str(), true, 0); |
| | | |
| | | if (res < 0) |
| | | { |
| | | ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| | | errmsg("SPI execute failed"))); |
| | | } |
| | | |
| | | { |
| | | std::string header = "Source code for "; |
| | | header.append(cname); |
| | | text *header_txt = cstring_to_text(header.c_str()); |
| | | Datum header_lvl = DirectFunctionCall1(int4_numeric, Int32GetDatum(1)); |
| | | DirectFunctionCall6(htp_header, header_lvl, |
| | | PointerGetDatum(header_txt), |
| | | static_cast<Datum>(0), |
| | | static_cast<Datum>(0), |
| | | static_cast<Datum>(0), |
| | | static_cast<Datum>(0)); |
| | | pfree(header_txt); |
| | | } |
| | | |
| | | DirectFunctionCall3(htp_preopen, static_cast<Datum>(0), |
| | | static_cast<Datum>(0), static_cast<Datum>(0)); |
| | | |
| | | int line1s = 1; |
| | | |
| | | for (size_t i = 0; i < SPI_tuptable->numvals; i++) |
| | | { |
| | | bool isnull; |
| | | Datum line_datum = GetAttributeByNum(SPI_tuptable->vals[i]->t_data, 0, &isnull); |
| | | int32_t line = DatumGetInt32(DirectFunctionCall1(numeric_int4, line_datum)); |
| | | if (line == 1) |
| | | { |
| | | line1s++; |
| | | if (line1s == 2) |
| | | { |
| | | DirectFunctionCall1(htp_print_ovr1, static_cast<Datum>(0)); |
| | | } |
| | | } |
| | | |
| | | Datum text_datum = GetAttributeByNum(SPI_tuptable->vals[i]->t_data, 1, &isnull); |
| | | char *txt = text_to_cstring(DatumGetTextP(text_datum)); |
| | | for (char *symbol = txt; *symbol != '\0'; symbol++) |
| | | { |
| | | if (*symbol == '\n') |
| | | { |
| | | *symbol = ' '; |
| | | } |
| | | } |
| | | text *out_line = cstring_to_text(txt); |
| | | DirectFunctionCall1(htp_print_ovr1, PointerGetDatum(out_line)); |
| | | pfree(out_line); |
| | | } |
| | | |
| | | DirectFunctionCall3(htp_preclose, static_cast<Datum>(0), |
| | | static_cast<Datum>(0), static_cast<Datum>(0)); |
| | | ereport(NOTICE, (errmsg("Функция owa_util.showsource еще не реализована."))); |
| | | |
| | | PG_RETURN_VOID(); |
| | | } |
| | |
| | | pfree(ccol_aliases); |
| | | } |
| | | |
| | | int32_t nrow_min = DatumGetInt32(DirectFunctionCall1(numeric_int4, PG_GETARG_DATUM(6))); |
| | | int32_t nrow_max; |
| | | int32_t nrow_min; |
| | | if (!PG_ARGISNULL(6)) |
| | | { |
| | | nrow_min = 0; |
| | | } |
| | | else |
| | | { |
| | | nrow_min = DatumGetInt32(DirectFunctionCall1(numeric_int4, PG_GETARG_DATUM(6))); |
| | | } |
| | | int32_t nrow_max; |
| | | if (!PG_ARGISNULL(7)) |
| | | { |
| | | nrow_max = DatumGetInt32(DirectFunctionCall1(numeric_int4, PG_GETARG_DATUM(7))); |
| | | } |
| | | else |
| | |
| | | pfree(ctable); |
| | | pfree(cclauses); |
| | | |
| | | ereport(DEBUG1, |
| | | errmsg_internal("OWA_UTIL.TABLEPRINT: query: %s", query.c_str())); |
| | | |
| | | int query_res = SPI_execute(query.c_str(), true, 0); |
| | | |
| | | if (query_res < 0) |
| | |
| | | Oid out_func; |
| | | bool output_is_varlena; |
| | | getTypeOutputInfo(target_type, &out_func, &output_is_varlena); |
| | | out_functions.push_back(out_func); |
| | | out_functions[i] = out_func; |
| | | } |
| | | |
| | | std::vector<size_t> column_widths(headings.size()); |
| | |
| | | } |
| | | } |
| | | |
| | | std::vector<std::vector<std::string>> entries(SPI_tuptable->numvals); |
| | | std::vector<std::vector<char*>> entries(SPI_tuptable->numvals, std::vector<char*>(headings.size())); |
| | | |
| | | for (size_t i = 0; i < SPI_tuptable->numvals; i++) |
| | | { |
| | | for (size_t j = 0; j < headings.size(); i++) |
| | | std::vector<Datum> datums(headings.size()); |
| | | bool *nulls = (bool*)palloc(headings.size() * sizeof(bool)); |
| | | heap_deform_tuple(SPI_tuptable->vals[i], SPI_tuptable->tupdesc, |
| | | datums.data(), nulls); |
| | | for (size_t j = 0; j < headings.size(); j++) |
| | | { |
| | | bool isnull; |
| | | Datum value = GetAttributeByNum(SPI_tuptable->vals[i]->t_data, j, &isnull); |
| | | if (!isnull) |
| | | if (!nulls[j]) |
| | | { |
| | | char *out_value = OidOutputFunctionCall(out_functions[j], value); |
| | | char *out_value = OidOutputFunctionCall(out_functions[j], datums[j]); |
| | | if (table_type == OWA_UTIL_PRE_TABLE) |
| | | { |
| | | column_widths[i] = Max(column_widths[i], strlen(out_value)); |
| | | } |
| | | entries[i].push_back(out_value); |
| | | entries[i][j] = out_value; |
| | | pfree(out_value); |
| | | } |
| | | else |
| | | { |
| | | entries[i].push_back(""); |
| | | entries[i][j] = pstrdup(""); |
| | | } |
| | | } |
| | | pfree(nulls); |
| | | } |
| | | |
| | | bool too_many_rows = SPI_tuptable->numvals > static_cast<uint64_t>(nrow_max - nrow_min + 1); |
| | |
| | | // start |
| | | if (table_type == OWA_UTIL_HTML_TABLE) |
| | | { |
| | | DirectFunctionCall5(htp_tableopen, |
| | | static_cast<Datum>(0), |
| | | static_cast<Datum>(0), |
| | | static_cast<Datum>(0), |
| | | static_cast<Datum>(0), |
| | | cattributes_datum); |
| | | { |
| | | LOCAL_FCINFO(fcinfo2, 5); |
| | | InitFunctionCallInfoData(*fcinfo2, NULL, 5, PG_GET_COLLATION(), NULL, NULL); |
| | | fcinfo2->args[0].isnull = true; |
| | | fcinfo2->args[1].isnull = true; |
| | | fcinfo2->args[2].isnull = true; |
| | | fcinfo2->args[3].isnull = true; |
| | | fcinfo2->args[4].isnull = cattributes_datum == 0; |
| | | fcinfo2->args[4].value = cattributes_datum; |
| | | htp_tableopen(fcinfo2); |
| | | } |
| | | |
| | | DirectFunctionCall5(htp_tablerowopen, |
| | | static_cast<Datum>(0), |
| | | static_cast<Datum>(0), |
| | | static_cast<Datum>(0), |
| | | static_cast<Datum>(0), |
| | | static_cast<Datum>(0)); |
| | | { |
| | | LOCAL_FCINFO(fcinfo2, 5); |
| | | InitFunctionCallInfoData(*fcinfo2, NULL, 5, PG_GET_COLLATION(), NULL, NULL); |
| | | fcinfo2->args[0].isnull = true; |
| | | fcinfo2->args[1].isnull = true; |
| | | fcinfo2->args[2].isnull = true; |
| | | fcinfo2->args[3].isnull = true; |
| | | fcinfo2->args[4].isnull = true; |
| | | htp_tablerowopen(fcinfo2); |
| | | } |
| | | |
| | | for (auto &heading: headings) |
| | | { |
| | | text *heading_txt = cstring_to_text(heading.c_str()); |
| | | DirectFunctionCall6(htp_tabledata, |
| | | PointerGetDatum(heading_txt), |
| | | static_cast<Datum>(0), |
| | | static_cast<Datum>(0), |
| | | static_cast<Datum>(0), |
| | | static_cast<Datum>(0), |
| | | static_cast<Datum>(0)); |
| | | |
| | | LOCAL_FCINFO(fcinfo2, 6); |
| | | InitFunctionCallInfoData(*fcinfo2, NULL, 6, PG_GET_COLLATION(), NULL, NULL); |
| | | fcinfo2->args[0].isnull = false; |
| | | fcinfo2->args[1].value = PointerGetDatum(heading_txt); |
| | | fcinfo2->args[1].isnull = true; |
| | | fcinfo2->args[2].isnull = true; |
| | | fcinfo2->args[3].isnull = true; |
| | | fcinfo2->args[4].isnull = true; |
| | | fcinfo2->args[5].isnull = true; |
| | | htp_tabledata(fcinfo2); |
| | | |
| | | pfree(heading_txt); |
| | | } |
| | | |
| | | DirectFunctionCall5(htp_tablerowclose, |
| | | static_cast<Datum>(0), |
| | | static_cast<Datum>(0), |
| | | static_cast<Datum>(0), |
| | | static_cast<Datum>(0), |
| | | static_cast<Datum>(0)); |
| | | { |
| | | LOCAL_FCINFO(fcinfo2, 5); |
| | | InitFunctionCallInfoData(*fcinfo2, NULL, 5, PG_GET_COLLATION(), NULL, NULL); |
| | | fcinfo2->args[0].isnull = true; |
| | | fcinfo2->args[1].isnull = true; |
| | | fcinfo2->args[2].isnull = true; |
| | | fcinfo2->args[3].isnull = true; |
| | | fcinfo2->args[4].isnull = true; |
| | | htp_tablerowclose(fcinfo2); |
| | | } |
| | | |
| | | for (auto &row: entries) |
| | | { |
| | | DirectFunctionCall5(htp_tablerowopen, |
| | | static_cast<Datum>(0), |
| | | static_cast<Datum>(0), |
| | | static_cast<Datum>(0), |
| | | static_cast<Datum>(0), |
| | | static_cast<Datum>(0)); |
| | | { |
| | | LOCAL_FCINFO(fcinfo2, 5); |
| | | InitFunctionCallInfoData(*fcinfo2, NULL, 5, PG_GET_COLLATION(), NULL, NULL); |
| | | fcinfo2->args[0].isnull = true; |
| | | fcinfo2->args[1].isnull = true; |
| | | fcinfo2->args[2].isnull = true; |
| | | fcinfo2->args[3].isnull = true; |
| | | fcinfo2->args[4].isnull = true; |
| | | htp_tablerowopen(fcinfo2); |
| | | } |
| | | |
| | | for (auto &cell: row) |
| | | { |
| | | text *cell_txt = cstring_to_text(cell.c_str()); |
| | | DirectFunctionCall6(htp_tabledata, |
| | | PointerGetDatum(cell_txt), |
| | | static_cast<Datum>(0), |
| | | static_cast<Datum>(0), |
| | | static_cast<Datum>(0), |
| | | static_cast<Datum>(0), |
| | | static_cast<Datum>(0)); |
| | | text *cell_txt = cstring_to_text(cell); |
| | | |
| | | LOCAL_FCINFO(fcinfo2, 6); |
| | | InitFunctionCallInfoData(*fcinfo2, NULL, 6, PG_GET_COLLATION(), NULL, NULL); |
| | | fcinfo2->args[0].isnull = false; |
| | | fcinfo2->args[1].value = PointerGetDatum(cell_txt); |
| | | fcinfo2->args[1].isnull = true; |
| | | fcinfo2->args[2].isnull = true; |
| | | fcinfo2->args[3].isnull = true; |
| | | fcinfo2->args[4].isnull = true; |
| | | fcinfo2->args[5].isnull = true; |
| | | htp_tabledata(fcinfo2); |
| | | pfree(cell_txt); |
| | | } |
| | | |
| | | DirectFunctionCall5(htp_tablerowclose, |
| | | static_cast<Datum>(0), |
| | | static_cast<Datum>(0), |
| | | static_cast<Datum>(0), |
| | | static_cast<Datum>(0), |
| | | static_cast<Datum>(0)); |
| | | { |
| | | LOCAL_FCINFO(fcinfo2, 5); |
| | | InitFunctionCallInfoData(*fcinfo2, NULL, 5, PG_GET_COLLATION(), NULL, NULL); |
| | | fcinfo2->args[0].isnull = true; |
| | | fcinfo2->args[1].isnull = true; |
| | | fcinfo2->args[2].isnull = true; |
| | | fcinfo2->args[3].isnull = true; |
| | | fcinfo2->args[4].isnull = true; |
| | | htp_tablerowclose(fcinfo2); |
| | | } |
| | | } |
| | | } |
| | | else // table_type == OWA_UTIL_PRE_TABLE |
| | |
| | | pfree(result); |
| | | } |
| | | |
| | | for (auto &row: entries) |
| | | { |
| | | for (char *cell: row) |
| | | { |
| | | pfree(cell); |
| | | } |
| | | } |
| | | |
| | | SPI_finish(); |
| | | PG_RETURN_BOOL(too_many_rows); |
| | | } |