Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
SQL.jsx117 linesDownload Raw Back to js
1/////////////////////////////////////////////////////////////2//3// pgAdmin 4 - PostgreSQL Tools4//5// Copyright (C) 2013 - 2024, The pgAdmin Development Team6// This software is released under the PostgreSQL Licence7//8//////////////////////////////////////////////////////////////9 10import React, { useEffect } from 'react';11import { generateNodeUrl } from '../../../../browser/static/js/node_ajax';12import gettext from 'sources/gettext';13import PropTypes from 'prop-types';14import getApiInstance from 'sources/api_instance';15import { makeStyles } from '@mui/styles';16import CodeMirror from '../../../../static/js/components/ReactCodeMirror';17import Loader from 'sources/components/Loader';18import withStandardTabInfo from '../../../../static/js/helpers/withStandardTabInfo';19import { BROWSER_PANELS } from '../../../../browser/static/js/constants';20import { usePgAdmin } from '../../../../static/js/BrowserComponent';21 22const useStyles = makeStyles((theme) => ({23  textArea: {24    height: '100% !important',25    width: '100% !important',26    background: theme.palette.grey[400],27    minHeight: '100%',28    minWidth: '100%',29  },30}));31 32function SQL({nodeData, node, treeNodeInfo, isActive, isStale, setIsStale}) {33  const classes = useStyles();34  const did = ((!_.isUndefined(treeNodeInfo)) && (!_.isUndefined(treeNodeInfo['database']))) ? treeNodeInfo['database']._id: 0;35  const dbConnected = !_.isUndefined(treeNodeInfo) && !_.isUndefined(treeNodeInfo['database']) ? treeNodeInfo.database.connected: false;36  const [nodeSQL, setNodeSQL] = React.useState('');37  const [loaderText, setLoaderText] = React.useState('');38  const pgAdmin = usePgAdmin();39 40  useEffect(() => {41    if(!isStale || !isActive) {42      return;43    }44    let sql = '-- ' + gettext('Please select an object in the tree view.');45    if(node) {46      let url = generateNodeUrl.call(47        node,48        treeNodeInfo,49        'sql',50        nodeData,51        true,52        node.url_jump_after_node53      );54      if (did && !dbConnected){55        return;56      }57      sql =58        '-- ' + gettext('No SQL could be generated for the selected object.');59 60      if (node.hasSQL) {61        const api = getApiInstance();62        setLoaderText('Loading...');63        api({64          url: url,65          type: 'GET',66        })67          .then((res) => {68            if (res.data.length > 0) {69              setNodeSQL(res.data);70              setLoaderText('');71            } else {72              setNodeSQL(sql);73            }74          })75          .catch((e) => {76            pgAdmin.Browser.notifier.alert(77              gettext('Error'),78              gettext(e.response.data.errormsg)79            );80            // show failed message.81            setNodeSQL(gettext('Failed to retrieve data from the server.'));82            setLoaderText('');83          }).then(()=>{84            setLoaderText('');85          });86      }87    }88    if (sql != '') {89      setNodeSQL(sql);90    }91    setIsStale(false);92  }, [isStale, isActive, nodeData?.id]);93 94  return (95    <>96      <Loader message={loaderText}/>97      <CodeMirror98        className={classes.textArea}99        value={nodeSQL}100        readonly={true}101        showCopyBtn102      />103    </>104  );105}106 107SQL.propTypes = {108  nodeData: PropTypes.object,109  treeNodeInfo: PropTypes.object,110  node: PropTypes.func,111  isActive: PropTypes.bool,112  isStale: PropTypes.bool,113  setIsStale: PropTypes.func,114};115 116export default withStandardTabInfo(SQL, BROWSER_PANELS.SQL);117 
codekingpro/portable-devtools · Team Ai