2022-03-30 01:36:59 -05:00
|
|
|
/////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// pgAdmin 4 - PostgreSQL Tools
|
|
|
|
//
|
2024-01-01 02:43:48 -06:00
|
|
|
// Copyright (C) 2013 - 2024, The pgAdmin Development Team
|
2022-03-30 01:36:59 -05:00
|
|
|
// This software is released under the PostgreSQL Licence
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
import React, { useEffect } from 'react';
|
2024-06-06 06:43:12 -05:00
|
|
|
import { styled } from '@mui/material/styles';
|
2022-03-30 01:36:59 -05:00
|
|
|
import { generateNodeUrl } from '../../../../browser/static/js/node_ajax';
|
|
|
|
import gettext from 'sources/gettext';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import getApiInstance from 'sources/api_instance';
|
2024-02-20 23:45:25 -06:00
|
|
|
import CodeMirror from '../../../../static/js/components/ReactCodeMirror';
|
2022-04-22 05:12:04 -05:00
|
|
|
import Loader from 'sources/components/Loader';
|
2023-10-23 07:13:17 -05:00
|
|
|
import withStandardTabInfo from '../../../../static/js/helpers/withStandardTabInfo';
|
|
|
|
import { BROWSER_PANELS } from '../../../../browser/static/js/constants';
|
|
|
|
import { usePgAdmin } from '../../../../static/js/BrowserComponent';
|
2022-03-30 01:36:59 -05:00
|
|
|
|
2024-06-06 06:43:12 -05:00
|
|
|
|
|
|
|
const Root = styled('div')(({theme}) => ({
|
|
|
|
'& .SQL-textArea': {
|
2022-03-30 01:36:59 -05:00
|
|
|
height: '100% !important',
|
|
|
|
width: '100% !important',
|
|
|
|
background: theme.palette.grey[400],
|
|
|
|
minHeight: '100%',
|
|
|
|
minWidth: '100%',
|
2024-06-06 06:43:12 -05:00
|
|
|
}
|
2022-03-30 01:36:59 -05:00
|
|
|
}));
|
|
|
|
|
2023-10-23 07:13:17 -05:00
|
|
|
function SQL({nodeData, node, treeNodeInfo, isActive, isStale, setIsStale}) {
|
|
|
|
const did = ((!_.isUndefined(treeNodeInfo)) && (!_.isUndefined(treeNodeInfo['database']))) ? treeNodeInfo['database']._id: 0;
|
|
|
|
const dbConnected = !_.isUndefined(treeNodeInfo) && !_.isUndefined(treeNodeInfo['database']) ? treeNodeInfo.database.connected: false;
|
2022-03-30 01:36:59 -05:00
|
|
|
const [nodeSQL, setNodeSQL] = React.useState('');
|
2022-04-22 05:12:04 -05:00
|
|
|
const [loaderText, setLoaderText] = React.useState('');
|
2023-10-23 07:13:17 -05:00
|
|
|
const pgAdmin = usePgAdmin();
|
2022-03-30 01:36:59 -05:00
|
|
|
|
|
|
|
useEffect(() => {
|
2023-10-23 07:13:17 -05:00
|
|
|
if(!isStale || !isActive) {
|
|
|
|
return;
|
|
|
|
}
|
2022-09-08 04:46:48 -05:00
|
|
|
let sql = '-- ' + gettext('Please select an object in the tree view.');
|
2023-10-23 07:13:17 -05:00
|
|
|
if(node) {
|
2022-03-30 01:36:59 -05:00
|
|
|
let url = generateNodeUrl.call(
|
|
|
|
node,
|
2023-10-23 07:13:17 -05:00
|
|
|
treeNodeInfo,
|
2022-03-30 01:36:59 -05:00
|
|
|
'sql',
|
|
|
|
nodeData,
|
|
|
|
true,
|
|
|
|
node.url_jump_after_node
|
|
|
|
);
|
2023-10-23 07:13:17 -05:00
|
|
|
if (did && !dbConnected){
|
2022-06-13 04:20:17 -05:00
|
|
|
return;
|
|
|
|
}
|
2022-03-30 01:36:59 -05:00
|
|
|
sql =
|
|
|
|
'-- ' + gettext('No SQL could be generated for the selected object.');
|
|
|
|
|
|
|
|
if (node.hasSQL) {
|
|
|
|
const api = getApiInstance();
|
2023-10-23 07:13:17 -05:00
|
|
|
setLoaderText('Loading...');
|
2022-03-30 01:36:59 -05:00
|
|
|
api({
|
|
|
|
url: url,
|
|
|
|
type: 'GET',
|
|
|
|
})
|
|
|
|
.then((res) => {
|
|
|
|
if (res.data.length > 0) {
|
|
|
|
setNodeSQL(res.data);
|
2022-04-22 05:12:04 -05:00
|
|
|
setLoaderText('');
|
2022-03-30 01:36:59 -05:00
|
|
|
} else {
|
2023-10-23 07:13:17 -05:00
|
|
|
setNodeSQL(sql);
|
2022-03-30 01:36:59 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
2023-10-23 07:13:17 -05:00
|
|
|
pgAdmin.Browser.notifier.alert(
|
2022-11-29 06:45:46 -06:00
|
|
|
gettext('Error'),
|
|
|
|
gettext(e.response.data.errormsg)
|
2022-03-30 01:36:59 -05:00
|
|
|
);
|
|
|
|
// show failed message.
|
2023-11-20 06:15:17 -06:00
|
|
|
setNodeSQL(gettext('Failed to retrieve data from the server.'));
|
2023-10-23 07:13:17 -05:00
|
|
|
setLoaderText('');
|
|
|
|
}).then(()=>{
|
2022-04-22 05:12:04 -05:00
|
|
|
setLoaderText('');
|
2022-03-30 01:36:59 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sql != '') {
|
2023-10-23 07:13:17 -05:00
|
|
|
setNodeSQL(sql);
|
2022-03-30 01:36:59 -05:00
|
|
|
}
|
2023-10-23 07:13:17 -05:00
|
|
|
setIsStale(false);
|
2023-10-27 05:21:45 -05:00
|
|
|
}, [isStale, isActive, nodeData?.id]);
|
2022-03-30 01:36:59 -05:00
|
|
|
|
|
|
|
return (
|
2024-06-06 06:43:12 -05:00
|
|
|
(<Root style={{height: '100%'}} >
|
2022-05-30 02:28:24 -05:00
|
|
|
<Loader message={loaderText}/>
|
2022-04-22 05:12:04 -05:00
|
|
|
<CodeMirror
|
2024-06-06 06:43:12 -05:00
|
|
|
className='SQL-textArea'
|
2023-10-23 07:13:17 -05:00
|
|
|
value={nodeSQL}
|
2022-04-22 05:12:04 -05:00
|
|
|
readonly={true}
|
2024-02-20 23:45:25 -06:00
|
|
|
showCopyBtn
|
2022-04-22 05:12:04 -05:00
|
|
|
/>
|
2024-06-06 06:43:12 -05:00
|
|
|
</Root>)
|
2022-03-30 01:36:59 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
SQL.propTypes = {
|
|
|
|
nodeData: PropTypes.object,
|
|
|
|
treeNodeInfo: PropTypes.object,
|
|
|
|
node: PropTypes.func,
|
2023-10-23 07:13:17 -05:00
|
|
|
isActive: PropTypes.bool,
|
|
|
|
isStale: PropTypes.bool,
|
|
|
|
setIsStale: PropTypes.func,
|
2022-03-30 01:36:59 -05:00
|
|
|
};
|
2023-10-23 07:13:17 -05:00
|
|
|
|
|
|
|
export default withStandardTabInfo(SQL, BROWSER_PANELS.SQL);
|