2021-09-27 06:14:26 -05:00
|
|
|
/////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// pgAdmin 4 - PostgreSQL Tools
|
|
|
|
//
|
2024-01-01 02:43:48 -06:00
|
|
|
// Copyright (C) 2013 - 2024, The pgAdmin Development Team
|
2021-09-27 06:14:26 -05:00
|
|
|
// This software is released under the PostgreSQL Licence
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
|
2023-03-16 06:52:26 -05:00
|
|
|
import * as BrowserFS from 'browserfs';
|
2021-09-27 06:14:26 -05:00
|
|
|
import url_for from 'sources/url_for';
|
|
|
|
import pgAdmin from 'sources/pgadmin';
|
2022-09-08 04:46:48 -05:00
|
|
|
import _ from 'lodash';
|
2023-03-16 06:52:26 -05:00
|
|
|
import { FileType } from 'react-aspen';
|
2021-09-27 06:14:26 -05:00
|
|
|
import { findInTree } from './tree';
|
2023-05-09 04:44:14 -05:00
|
|
|
import gettext from 'sources/gettext';
|
2021-09-27 06:14:26 -05:00
|
|
|
|
2022-01-20 05:28:21 -06:00
|
|
|
import { unix } from 'path-fx';
|
2023-05-23 04:07:16 -05:00
|
|
|
import getApiInstance, { parseApiError } from '../api_instance';
|
2021-09-27 06:14:26 -05:00
|
|
|
|
|
|
|
export class ManageTreeNodes {
|
2023-03-16 06:52:26 -05:00
|
|
|
constructor() {
|
|
|
|
this.tree = {};
|
|
|
|
this.tempTree = new TreeNode(undefined, {});
|
2021-09-27 06:14:26 -05:00
|
|
|
}
|
|
|
|
|
2023-03-16 06:52:26 -05:00
|
|
|
public init = (_root: string) => new Promise((res) => {
|
|
|
|
const node = {parent: null, children: [], data: null};
|
2021-09-27 06:14:26 -05:00
|
|
|
this.tree = {};
|
|
|
|
this.tree[_root] = {name: 'root', type: FileType.Directory, metadata: node};
|
|
|
|
res();
|
2023-03-29 06:52:50 -05:00
|
|
|
});
|
2021-09-27 06:14:26 -05:00
|
|
|
|
2023-03-16 06:52:26 -05:00
|
|
|
public updateNode = (_path, _data) => new Promise((res) => {
|
2021-11-09 23:47:51 -06:00
|
|
|
const item = this.findNode(_path);
|
|
|
|
if (item) {
|
2023-06-29 00:04:20 -05:00
|
|
|
item.data = {...item.data, ..._data};
|
2021-09-27 06:14:26 -05:00
|
|
|
item.name = _data.label;
|
|
|
|
item.metadata.data = _data;
|
|
|
|
}
|
|
|
|
res(true);
|
2023-03-29 06:52:50 -05:00
|
|
|
});
|
2021-09-27 06:14:26 -05:00
|
|
|
|
2023-03-16 06:52:26 -05:00
|
|
|
public removeNode = async (_path) => {
|
2021-09-27 06:14:26 -05:00
|
|
|
const item = this.findNode(_path);
|
|
|
|
|
2024-06-11 07:37:22 -05:00
|
|
|
if (item?.parentNode) {
|
2021-09-27 06:14:26 -05:00
|
|
|
item.children = [];
|
|
|
|
item.parentNode.children.splice(item.parentNode.children.indexOf(item), 1);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
findNode(path) {
|
|
|
|
if (path === null || path === undefined || path.length === 0 || path == '/browser') {
|
|
|
|
return this.tempTree;
|
|
|
|
}
|
|
|
|
return findInTree(this.tempTree, path);
|
|
|
|
}
|
|
|
|
|
2023-03-16 06:52:26 -05:00
|
|
|
public addNode = (_parent: string, _path: string, _data: []) => new Promise((res) => {
|
2021-09-27 06:14:26 -05:00
|
|
|
_data.type = _data.inode ? FileType.Directory : FileType.File;
|
|
|
|
_data._label = _data.label;
|
|
|
|
_data.label = _.escape(_data.label);
|
|
|
|
|
|
|
|
_data.is_collection = isCollectionNode(_data._type);
|
2023-03-16 06:52:26 -05:00
|
|
|
const nodeData = {parent: _parent, children: [], data: _data};
|
2021-09-27 06:14:26 -05:00
|
|
|
|
2023-03-16 06:52:26 -05:00
|
|
|
const tmpParentNode = this.findNode(_parent);
|
|
|
|
const treeNode = new TreeNode(_data.id, _data, {}, tmpParentNode, nodeData, _data.type);
|
2021-09-27 06:14:26 -05:00
|
|
|
|
|
|
|
if (tmpParentNode !== null && tmpParentNode !== undefined) tmpParentNode.children.push(treeNode);
|
|
|
|
|
|
|
|
res(treeNode);
|
2023-03-29 06:52:50 -05:00
|
|
|
});
|
2021-09-27 06:14:26 -05:00
|
|
|
|
2023-05-23 04:07:16 -05:00
|
|
|
public readNode = async (_path: string) => {
|
2023-03-16 06:52:26 -05:00
|
|
|
let temp_tree_path = _path;
|
|
|
|
const node = this.findNode(_path);
|
|
|
|
const base_url = pgAdmin.Browser.URL;
|
2023-05-23 04:07:16 -05:00
|
|
|
const api = getApiInstance();
|
2021-09-27 06:14:26 -05:00
|
|
|
|
2021-10-14 01:05:11 -05:00
|
|
|
if (node && node.children.length > 0) {
|
2023-05-23 04:07:16 -05:00
|
|
|
if (node.type !== FileType.File) {
|
|
|
|
console.error(node, 'It\'s a leaf node');
|
|
|
|
return [];
|
2021-09-27 06:14:26 -05:00
|
|
|
}
|
2024-06-11 07:37:22 -05:00
|
|
|
else if (node.children.length != 0) {
|
|
|
|
return node.children;
|
2021-10-14 01:05:11 -05:00
|
|
|
}
|
|
|
|
}
|
2021-09-27 06:14:26 -05:00
|
|
|
|
2023-03-16 06:52:26 -05:00
|
|
|
const self = this;
|
2023-05-23 04:07:16 -05:00
|
|
|
let url = '';
|
|
|
|
if (_path == '/browser') {
|
|
|
|
url = url_for('browser.nodes');
|
|
|
|
} else {
|
|
|
|
const _parent_url = self.generate_url(_path);
|
|
|
|
if (node.metadata.data._pid == null ) {
|
|
|
|
url = node.metadata.data._type + '/children/' + node.metadata.data._id;
|
|
|
|
}
|
2024-06-11 07:37:22 -05:00
|
|
|
else if (node.metadata.data._type.includes('coll-')) {
|
|
|
|
const _type = node.metadata.data._type.replace('coll-', '');
|
|
|
|
url = _type + '/nodes/' + _parent_url + '/';
|
|
|
|
}
|
2023-05-23 04:07:16 -05:00
|
|
|
else {
|
2024-06-11 07:37:22 -05:00
|
|
|
url = node.metadata.data._type + '/children/' + _parent_url + '/' + node.metadata.data._id;
|
2023-05-23 04:07:16 -05:00
|
|
|
}
|
2021-09-27 06:14:26 -05:00
|
|
|
|
2023-05-23 04:07:16 -05:00
|
|
|
url = base_url + url;
|
2021-10-14 01:05:11 -05:00
|
|
|
|
2023-05-23 04:07:16 -05:00
|
|
|
temp_tree_path = node.path;
|
2021-09-27 06:14:26 -05:00
|
|
|
|
2023-05-23 04:07:16 -05:00
|
|
|
if (node.metadata.data._type == 'server' && !node.metadata.data.connected) {
|
|
|
|
url = null;
|
2021-09-27 06:14:26 -05:00
|
|
|
}
|
2023-05-23 04:07:16 -05:00
|
|
|
}
|
2021-09-27 06:14:26 -05:00
|
|
|
|
2023-05-23 04:07:16 -05:00
|
|
|
let treeData = [];
|
|
|
|
if (url) {
|
|
|
|
try {
|
|
|
|
const res = await api.get(url);
|
|
|
|
treeData = res.data.data;
|
|
|
|
} catch (error) {
|
|
|
|
/* react-aspen does not handle reject case */
|
|
|
|
console.error(error);
|
2023-10-23 07:13:17 -05:00
|
|
|
pgAdmin.Browser.notifier.error(parseApiError(error)||'Node Load Error...');
|
2023-05-23 04:07:16 -05:00
|
|
|
return [];
|
2021-09-27 06:14:26 -05:00
|
|
|
}
|
2023-05-23 04:07:16 -05:00
|
|
|
}
|
2021-09-27 06:14:26 -05:00
|
|
|
|
2023-05-23 04:07:16 -05:00
|
|
|
const Path = BrowserFS.BFSRequire('path');
|
|
|
|
for (const idx in treeData) {
|
|
|
|
const _node: any = treeData[idx];
|
|
|
|
const _pathl = Path.join(_path, _node.id);
|
|
|
|
await self.addNode(temp_tree_path, _pathl, _node);
|
|
|
|
}
|
|
|
|
if (node.children.length > 0) return node.children;
|
|
|
|
else {
|
|
|
|
if (node.data && node.data._type == 'server' && node.data.connected) {
|
2023-10-23 07:13:17 -05:00
|
|
|
pgAdmin.Browser.notifier.info(gettext('Server children are not available.'
|
2023-05-23 04:07:16 -05:00
|
|
|
+' Please check these nodes are not hidden through the preferences setting `Browser > Nodes`.'), null);
|
2023-05-09 04:44:14 -05:00
|
|
|
}
|
2023-05-23 04:07:16 -05:00
|
|
|
return [];
|
2021-09-27 06:14:26 -05:00
|
|
|
}
|
2023-05-23 04:07:16 -05:00
|
|
|
};
|
2021-09-27 06:14:26 -05:00
|
|
|
|
|
|
|
public generate_url = (path: string) => {
|
|
|
|
let _path = path;
|
2023-03-16 06:52:26 -05:00
|
|
|
const _parent_path = [];
|
2021-10-04 05:45:49 -05:00
|
|
|
let _partitions = [];
|
2021-09-27 06:14:26 -05:00
|
|
|
while(_path != '/') {
|
2023-03-16 06:52:26 -05:00
|
|
|
const node = this.findNode(_path);
|
|
|
|
const _parent = unix.dirname(_path);
|
2021-09-27 06:14:26 -05:00
|
|
|
if(node.parentNode && node.parentNode.path == _parent) {
|
|
|
|
if (node.parentNode.metadata.data !== null && !node.parentNode.metadata.data._type.includes('coll-'))
|
2021-10-04 05:45:49 -05:00
|
|
|
if(node.parentNode.metadata.data._type.includes('partition')) {
|
|
|
|
_partitions.push(node.parentNode.metadata.data._id);
|
|
|
|
} else {
|
|
|
|
_parent_path.push(node.parentNode.metadata.data._id);
|
|
|
|
}
|
2023-03-16 06:52:26 -05:00
|
|
|
}
|
|
|
|
_path = _parent;
|
2021-09-27 06:14:26 -05:00
|
|
|
}
|
2022-01-12 04:59:13 -06:00
|
|
|
_partitions = _partitions.reverse();
|
2021-10-04 05:45:49 -05:00
|
|
|
// Replace the table with the last partition as in reality partition node is not child of the table
|
|
|
|
if(_partitions.length > 0) _parent_path[0] = _partitions[_partitions.length-1];
|
|
|
|
|
2024-06-11 07:37:22 -05:00
|
|
|
_parent_path.reverse();
|
|
|
|
return _parent_path.join('/');
|
2023-03-29 06:52:50 -05:00
|
|
|
};
|
2021-09-27 06:14:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export class TreeNode {
|
|
|
|
constructor(id, data, domNode, parent, metadata, type) {
|
|
|
|
this.id = id;
|
|
|
|
this.data = data;
|
|
|
|
this.setParent(parent);
|
|
|
|
this.children = [];
|
|
|
|
this.domNode = domNode;
|
|
|
|
this.metadata = metadata;
|
2023-03-16 06:52:26 -05:00
|
|
|
this.name = metadata ? metadata.data.label : '';
|
2024-06-11 07:37:22 -05:00
|
|
|
this.type = type || undefined;
|
2021-09-27 06:14:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
hasParent() {
|
|
|
|
return this.parentNode !== null && this.parentNode !== undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
parent() {
|
|
|
|
return this.parentNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
setParent(parent) {
|
|
|
|
this.parentNode = parent;
|
|
|
|
this.path = this.id;
|
|
|
|
if (this.id)
|
|
|
|
if (parent !== null && parent !== undefined && parent.path !== undefined) {
|
|
|
|
this.path = parent.path + '/' + this.id;
|
|
|
|
} else {
|
|
|
|
this.path = '/browser/' + this.id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getData() {
|
|
|
|
if (this.data === undefined) {
|
|
|
|
return undefined;
|
|
|
|
} else if (this.data === null) {
|
|
|
|
return null;
|
|
|
|
}
|
2024-06-11 07:37:22 -05:00
|
|
|
return {...this.data};
|
2021-09-27 06:14:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
getHtmlIdentifier() {
|
|
|
|
return this.domNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the ancestor with matches this condition
|
|
|
|
*/
|
|
|
|
ancestorNode(condition) {
|
|
|
|
let node = this;
|
|
|
|
|
|
|
|
while (node.hasParent()) {
|
|
|
|
node = node.parent();
|
|
|
|
if (condition(node)) {
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a condition returns true if the current node
|
|
|
|
* or any of the parent nodes condition result is true
|
|
|
|
*/
|
|
|
|
anyFamilyMember(condition) {
|
|
|
|
if(condition(this)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.ancestorNode(condition) !== null;
|
|
|
|
}
|
|
|
|
anyParent(condition) {
|
|
|
|
return this.ancestorNode(condition) !== null;
|
|
|
|
}
|
|
|
|
|
|
|
|
reload(tree) {
|
|
|
|
return new Promise((resolve)=>{
|
|
|
|
this.unload(tree)
|
|
|
|
.then(()=>{
|
|
|
|
tree.setInode(this.domNode);
|
|
|
|
tree.deselect(this.domNode);
|
|
|
|
setTimeout(() => {
|
|
|
|
tree.selectNode(this.domNode);
|
|
|
|
}, 0);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
unload(tree) {
|
|
|
|
return new Promise((resolve, reject)=>{
|
|
|
|
this.children = [];
|
|
|
|
tree.unload(this.domNode)
|
|
|
|
.then(
|
|
|
|
()=>{
|
|
|
|
resolve(true);
|
|
|
|
},
|
|
|
|
()=>{
|
2024-06-11 07:37:22 -05:00
|
|
|
reject(new Error());
|
2021-09-27 06:14:26 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
open(tree, suppressNoDom) {
|
|
|
|
return new Promise((resolve, reject)=>{
|
|
|
|
if(suppressNoDom && (this.domNode == null || typeof(this.domNode) === 'undefined')) {
|
|
|
|
resolve(true);
|
|
|
|
} else if(tree.isOpen(this.domNode)) {
|
|
|
|
resolve(true);
|
|
|
|
} else {
|
2024-06-11 07:37:22 -05:00
|
|
|
tree.open(this.domNode).then(() => resolve(true), () => reject(new Error(true)));
|
2021-09-27 06:14:26 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isCollectionNode(node) {
|
2023-03-16 06:52:26 -05:00
|
|
|
if (pgAdmin.Browser.Nodes && node in pgAdmin.Browser.Nodes) {
|
|
|
|
if (pgAdmin.Browser.Nodes[node].is_collection !== undefined) return pgAdmin.Browser.Nodes[node].is_collection;
|
|
|
|
else return false;
|
|
|
|
}
|
|
|
|
return false;
|
2021-09-27 06:14:26 -05:00
|
|
|
}
|