Mercurial > hg > Papers > 2016 > tatsuki-prosym
changeset 38:0bb8c8489f2c
add src in compareDatabase
author | tatsuki |
---|---|
date | Thu, 01 Dec 2016 18:56:10 +0900 |
parents | eea008c85b3b |
children | 9d0f50febed1 |
files | compareDatabase.tex |
diffstat | 1 files changed, 10 insertions(+), 6 deletions(-) [+] |
line wrap: on
line diff
--- a/compareDatabase.tex Thu Dec 01 18:53:43 2016 +0900 +++ b/compareDatabase.tex Thu Dec 01 18:56:10 2016 +0900 @@ -33,8 +33,12 @@ このテーブルuserに対して、名前が{\tt kono} タイプが{\tt Teacher}のユーザー以下のノードを全て取得したい。 木構造の深さがわかっている場合、以下のコードのように、副問合せを複数回使用することで一回のSQLで取得可能である。 -select * from user where parent in (select id from user where parent = (select id from user where name = "kono" AND type = "Teacher")) union select * from user where parent = (select id from user where name = "kono" AND type = "Teacher") union select * from user where name = "kono" AND type = "Teacher"; - +\begin{lstlisting}[frame=lrbt,label=index,numbers=left] +ResultSet set = stmt.executeQuery("select * from user where parent_id in (select id from user where parent_id = (select id from user where name = \"kono\" AND type = \"Teacher\")) union select * from user where parent_id = (select id from user where name = \"kono\" AND type = \"Teacher\") union select * from user where name = \"kono\" AND type = \"Teacher\";"); +while (set.next()) { + System.out.println("user -> " + set.getString(2)); +} +\end{lstlisting} しかし、深さが変わると上記のSQLは使用できないうえ、SQL文が長くわかりづらくなる。 なので、特定のユーザー以下のノードを全て取得する場合は、プログラム内で再帰的に複数回SQLを発行する必要がある。 @@ -45,10 +49,10 @@ tree = jungle.getTreeByName("user"); InterfaceTraverser traverser = tree.getTraverser(true); Iterator<TreeNode> iterator = traverser.find((TreeNode node) -> { - String value = node.getAttributes().getString("Name"); - if (value == null) return false; - return value.equals("Kono"); -},"Type", "Teacher"); + String value = node.getAttributes().getString("Name"); + if (value == null) return false; + return value.equals("Kono"); + },"Type", "Teacher"); while (iterator.hasNext()) { TreeNode node = iterator.next();