提交 1de9c636 编写于 作者: M Mars Liu

add pid; ensure node id if no

上级 2221abbc
{
"type": "code_options",
"author": "刘鑫",
"source": "children.md"
}
\ No newline at end of file
# 查找子节点
有一个表 node,有两列
```shell
# select item from node;
id | pid
------------------
1 | 0
2 | 1
3 | 1
4 | 3
5 | 2
...
```
其中id是自增主键,pid指向父节点的id。那么给定某一个id ,查找它的子节点的查询应该是:
## 答案
```sql
select * from node where pid = ?
```
## 选项
### 缺少 where
```sql
select * from node pid = ?
```
### 结构错误
```sql
from node select * where pid = ?
```
### select 不完整
```sql
select from node where pid = ?
```
......@@ -3,6 +3,7 @@
"node_id": "oceanbase-6f727423ee414b85b6ed90bebcdd88b8",
"title": "查询数据",
"export": [
"hello.json"
"hello.json",
"children.json"
]
}
\ No newline at end of file
{
"keywords": [],
"node_id": "oceanbase-641f612acf59456187b9573a4398e20b",
"title": "表查询"
"title": "表查询",
"export": []
}
\ No newline at end of file
{
"keywords": [],
"node_id": "oceanbase-d28015752f834bffacc6dfb9ab64b8c2",
"title": "连接查询"
"title": "连接查询",
"export": []
}
\ No newline at end of file
import logging
from genericpath import exists
import json
import os
......@@ -7,6 +8,9 @@ import re
id_set = set()
logger = logging.getLogger(__name__)
def load_json(p):
with open(p, 'r') as f:
return json.loads(f.read())
......@@ -18,13 +22,22 @@ def dump_json(p, j, exist_ok=False, override=False):
if not override:
return
else:
print(f"{p} already exist")
logger.error(f"{p} already exist")
sys.exit(0)
with open(p, 'w+') as f:
f.write(json.dumps(j, indent=2, ensure_ascii=False))
def ensure_config(path):
config_path = os.path.join(path, "config.json")
if not os.path.exists(config_path):
node = {"keywords": []}
dump_json(config_path, node, exist_ok=True, override=False)
return node
else:
return load_json(config_path)
def parse_no_name(d):
p = r'(\d+)\.(.*)'
m = re.search(p, d)
......@@ -37,6 +50,7 @@ def parse_no_name(d):
return no, dir_name
def check_export(base, cfg):
flag = False
exports = []
......@@ -114,8 +128,8 @@ def gen_tree(data_path):
return node, node_children
# 根节点
cfg = ensure_config(data_path)
cfg_path = os.path.join(data_path, 'config.json')
cfg = load_json(cfg_path)
if ensure_node_id(cfg):
dump_json(cfg_path, cfg, exist_ok=True, override=True)
......@@ -134,7 +148,7 @@ def gen_tree(data_path):
print(level_no_dir)
no, level_name = parse_no_name(level_no_name)
level_path = os.path.join(level_no_dir, 'config.json')
level_cfg = load_json(level_path)
level_cfg = ensure_config(level_no_dir)
if ensure_node_id(level_cfg) or check_export(level_no_dir, level_cfg):
dump_json(level_path, level_cfg, exist_ok=True, override=True)
if ensure_title(level_cfg, level_path):
......@@ -148,7 +162,7 @@ def gen_tree(data_path):
for chapter_no_dir, chapter_no_name in list_dir(level_no_dir):
no, chapter_name = parse_no_name(chapter_no_name)
chapter_path = os.path.join(chapter_no_dir, 'config.json')
chapter_cfg = load_json(chapter_path)
chapter_cfg = ensure_config(chapter_no_dir)
if ensure_node_id(chapter_cfg) or check_export(chapter_no_dir, chapter_cfg):
dump_json(chapter_path, chapter_cfg, exist_ok=True, override=True)
if ensure_title(chapter_cfg, chapter_path):
......@@ -162,7 +176,7 @@ def gen_tree(data_path):
for section_no_dir, section_no_name in list_dir(chapter_no_dir):
no, section_name = parse_no_name(section_no_name)
sec_path = os.path.join(section_no_dir, 'config.json')
sec_cfg = load_json(sec_path)
sec_cfg = ensure_config(section_no_dir)
flag = ensure_node_id(sec_cfg) or check_export(section_no_dir, sec_cfg)
section_node, section_node_children = make_node(
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册