Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/en_US/search_objects.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ have not enabled those object types in the :ref:`preferences <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
Expand Down
22 changes: 22 additions & 0 deletions web/pgadmin/tools/search_objects/static/js/SearchObjects.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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',
Expand Down Expand Up @@ -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,
Comment thread
dpage marked this conversation as resolved.
Resolved
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;
Expand Down Expand Up @@ -431,6 +450,9 @@ export default function SearchObjects({nodeData}) {
</Box>
<Box className='SearchObjects-footer1'>
<Box>{footerText}</Box>
<PgIconButton data-test="copy-results" onClick={()=>copyResultsAsCsv(sortedItems)}
icon={<ContentCopyIcon />} title={gettext('Copy results as CSV')}
disabled={sortedItems.length == 0} />
</Box>
<FormFooterMessage type={MESSAGE_TYPE.ERROR} message={errorMsg} closable onClose={()=>setErrorMsg('')} />
<Box className='SearchObjects-footer'>
Expand Down
44 changes: 43 additions & 1 deletion web/regression/javascript/search_objects/SearchObject.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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'
}
]});

Expand Down Expand Up @@ -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;
}
});
});
});
Loading