diff --git a/docs/en_US/search_objects.rst b/docs/en_US/search_objects.rst index 3c501c27f52..ec6db820880 100644 --- a/docs/en_US/search_objects.rst +++ b/docs/en_US/search_objects.rst @@ -28,6 +28,14 @@ have not enabled those object types in the :ref:`preferences `, so you can't double click on it. You can click on the ellipsis appended to the function and procedure names to see their arguments. +Click the *Copy results as CSV* button, to the right of the match count +below the grid, to copy the results to the clipboard as CSV, in the order +currently shown, with a header row of column names. Every value is enclosed +in double quotes, so names and paths containing commas, quotes or line breaks +are kept intact. The arguments of functions and procedures are always +included in the copied object names, whether or not they have been expanded +in the grid. + You can filter based on a particular object type by selecting one from the object type dropdown. If the search button is hit when one of the object type diff --git a/web/pgadmin/tools/search_objects/static/js/SearchObjects.jsx b/web/pgadmin/tools/search_objects/static/js/SearchObjects.jsx index 936a7ed5ce3..80020f96dff 100644 --- a/web/pgadmin/tools/search_objects/static/js/SearchObjects.jsx +++ b/web/pgadmin/tools/search_objects/static/js/SearchObjects.jsx @@ -12,6 +12,7 @@ import React, { useState, useMemo, useCallback } from 'react'; import PropTypes from 'prop-types'; import HelpIcon from '@mui/icons-material/HelpRounded'; import SearchRoundedIcon from '@mui/icons-material/SearchRounded'; +import ContentCopyIcon from '@mui/icons-material/ContentCopy'; import pgAdmin from 'sources/pgadmin'; import gettext from 'sources/gettext'; import url_for from 'sources/url_for'; @@ -20,6 +21,7 @@ import getApiInstance, { parseApiError } from '../../../../static/js/api_instanc import { PrimaryButton, PgIconButton } from '../../../../static/js/components/Buttons'; import { FormFooterMessage, InputSelect, InputText, MESSAGE_TYPE } from '../../../../static/js/components/FormComponents'; import PgReactDataGrid from '../../../../static/js/components/PgReactDataGrid'; +import CopyData from 'tools/sqleditor/static/js/components/QueryToolDataGrid/CopyData'; const StyledBox = styled(Box)(({theme}) => ({ display:'flex', @@ -266,6 +268,23 @@ const translateSearchObjectsPath = (nodeData, path, catalog_level)=> { return [new_path, id_path]; }; +/* Copy the results, in the order shown, to the clipboard as CSV with the + * grid's column headers. Function arguments are included whenever the + * object has them, so overloaded functions can be told apart. + */ +const copyResultsAsCsv = (rows)=> { + const csvRows = rows.map((row)=>({ + name: row.other_info ? `${row.name}(${_.unescape(row.other_info)})` : row.name, + type: row.type_label, + path: row.path, + })); + new CopyData({ + field_separator: ',', + quote_char: '"', + quoting: 'all', + }).copyRowsToCsv(csvRows, columns, true); +}; + // This function is used to sort the column. function getComparator(sortColumn) { const key = sortColumn?.columnKey; @@ -431,6 +450,9 @@ export default function SearchObjects({nodeData}) { {footerText} + copyResultsAsCsv(sortedItems)} + icon={} title={gettext('Copy results as CSV')} + disabled={sortedItems.length == 0} /> setErrorMsg('')} /> diff --git a/web/regression/javascript/search_objects/SearchObject.spec.js b/web/regression/javascript/search_objects/SearchObject.spec.js index 12124ffc732..2452c19c779 100644 --- a/web/regression/javascript/search_objects/SearchObject.spec.js +++ b/web/regression/javascript/search_objects/SearchObject.spec.js @@ -8,7 +8,7 @@ ////////////////////////////////////////////////////////////// -import { act, render } from '@testing-library/react'; +import { act, render, waitFor } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import axios from 'axios'; import MockAdapter from 'axios-mock-adapter'; @@ -71,6 +71,15 @@ describe('SearchObjects', ()=>{ 'show_node': true, 'other_info': null, 'catalog_level': 'N' + }, + { + 'name': 'plp, "odd"\nname', + 'type': 'extension', + 'type_label': 'Extensions', + 'path': ':extension.13320:/plp, "odd"\nname', + 'show_node': true, + 'other_info': null, + 'catalog_level': 'N' } ]}); @@ -121,5 +130,38 @@ describe('SearchObjects', ()=>{ await user.type(ctrl.container.querySelector('input'), 'plp{Enter}'); expect(ctrl.container.querySelectorAll('[data-test="react-data-grid"]').length).toBe(1); }); + + it('copy_results_as_csv', async ()=>{ + /* Just enough node types for the result paths to be translated */ + const origNodes = pgAdmin.Browser.Nodes; + pgAdmin.Browser.Nodes = { + 'coll-database': {type: 'coll-database', label: 'Databases'}, + 'coll-extension': {type: 'coll-extension', label: 'Extensions'}, + 'coll-function': {type: 'coll-function', label: 'Functions'}, + 'coll-language': {type: 'coll-language', label: 'Languages'}, + 'catalog': {type: 'catalog'}, + }; + try { + /* userEvent.setup() installs its own clipboard stub on navigator */ + const writeText = jest.spyOn(navigator.clipboard, 'writeText'); + await ctrlMount(); + const copyBtn = ()=>ctrl.container.querySelector('button[data-test="copy-results"]'); + expect(copyBtn()).toBeDisabled(); + await user.type(ctrl.container.querySelector('input'), 'plp{Enter}'); + await waitFor(()=>expect(copyBtn()).toBeEnabled()); + await user.click(copyBtn()); + expect(writeText).toHaveBeenCalledWith([ + '"Object name","Type","Object path"', + '"plpgsql","Extensions","Extensions/plpgsql"', + '"plpgsql_call_handler","Functions","PostgreSQL Catalog (pg_catalog)/Functions/plpgsql_call_handler"', + '"plpgsql_inline_handler(internal)","Functions","PostgreSQL Catalog (pg_catalog)/Functions/plpgsql_inline_handler"', + '"plpgsql_validator(oid)","Functions","PostgreSQL Catalog (pg_catalog)/Functions/plpgsql_validator"', + '"plpgsql","Languages","Languages/plpgsql"', + '"plp, ""odd""\nname","Extensions","Extensions/plp, ""odd""\nname"', + ].join('\n')); + } finally { + pgAdmin.Browser.Nodes = origNodes; + } + }); }); });