1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
use std::collections::HashMap;
use std::net::{TcpStream};
use std::io::{BufReader,BufRead,Write, Read};

pub struct WildDocClient{
    document_root:String
    ,dbname:Vec<u8>
    ,sock:TcpStream
}
impl WildDocClient{
    pub fn new(host:&str,port:&str,document_root:&str,dbname:&str)->Self{
        let sock=TcpStream::connect(&(host.to_owned()+":"+port)).expect("failed to connect server");
        sock.set_nonblocking(false).expect("out of service");
        let document_root=std::path::Path::new(&(document_root.to_owned()+dbname)).to_str().unwrap().to_owned();
        let mut dbname=dbname.as_bytes().to_vec();
        dbname.push(0);
        Self{
            document_root
            ,dbname
            ,sock
        }
    }
    pub fn exec(&mut self,xml:&str,input_json:Option<String>)->std::io::Result<Vec<u8>>{
        let mut include_cache=HashMap::new();
        let mut recv_response=Vec::new();

        self.sock.write_all(&self.dbname)?;
        if let Some(input_json)=input_json{
            self.sock.write_all(input_json.as_bytes())?;
        }
        self.sock.write_all(&[0])?;
        self.sock.write_all(xml.as_bytes())?;
        self.sock.write_all(&[0])?;

        let mut reader=BufReader::new(self.sock.try_clone().unwrap());
        loop{
            let mut recv_include = Vec::new();
            if reader.read_until(0,&mut recv_include)? > 0 {
                if recv_include.starts_with(b"include:"){
                    recv_include.remove(recv_include.len()-1);
                    if let Ok(str)=std::str::from_utf8(&recv_include){
                        let s: Vec<&str>=str.split(':').collect();
                        let path=self.document_root.to_owned()+s[1];
                        let path=path.trim().to_owned();
                        let include_xml=include_cache.entry(path).or_insert_with_key(|path|{
                            match std::fs::File::open(path){
                                Ok(mut f)=>{
                                    let mut contents=Vec::new();
                                    let _=f.read_to_end(&mut contents);
                                    contents
                                }
                                ,_=>{
                                    b"".to_vec()
                                }
                            }
                        });
                        include_xml.push(0);
                        self.sock.write_all(&include_xml)?;
                    }else{
                        break;
                    }
                }else{
                    break;
                }
            }else{
                break;
            }
        }
        reader.read_until(0,&mut recv_response)?;
        Ok(recv_response)
    }
}
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_works() {
        let mut client=WildDocClient::new("localhost","51818","./test/","test");
        client.exec(r#"<wd><wd:session name="hoge">
            <wd:update commit="1">
                <collection name="person">
                    <field name="name">Noah</field>
                    <field name="country">US</field>
                </collection>
                <collection name="person">
                    <field name="name">Liam</field>
                    <field name="country">US</field>
                </collection>
                <collection name="person">
                    <field name="name">Olivia</field>
                    <field name="country">UK</field>
                </collection>
            </wd:update>
        </wd:session></wd>"#,None).unwrap();
        
        /*
        client.exec(r#"<wd>
            include-test:<wd:include src="hoge.xml" />
            <wd:search name="p" collection="person">
            </wd:search>
            OK
            <wd:result var="q" search="p">
                <div>
                    find <wd:print wd:value="wd.v('q').length" /> persons.
                </div>
                <ul>
                    <wd:for var="r" index="i" wd:in="wd.v('q')"><li>
                        <wd:print wd:value="wd.v('r').row" /> : <wd:print wd:value="wd.v('r').field('name')" /> : <wd:print wd:value="wd.v('r').field('country')" />
                    </li></wd:for>
                </ul>
            </wd:result>
        </wd>"#);

        
        client.exec(r#"<wd>
            <wd:search name="p" collection="person">
                <field name="country" method="match" value="US" />
            </wd:search>
            <wd:result var="q" search="p">
                <div>
                    find <wd:print wd:value="wd.v('q').length" /> persons from the US.
                </div>
                <ul>
                    <wd:for var="r" index="i" wd:in="wd.v('q')"><li>
                        <wd:print wd:value="wd.v('r').row" /> : <wd:print wd:value="wd.v('r').field('name')" /> : <wd:print wd:value="wd.v('r').field('country')" />
                    </li></wd:for>
                </ul>
            </wd:result>
        </wd>"#);
        client.exec(r#"<wd>
            <wd:script>
                const ymd=function(){
                    const now=new Date();
                    return now.getFullYear()+"-"+(now.getMonth()+1)+"-"+now.getDate();
                };
                const uk="UK";
            </wd:script>
            <wd:search name="p" collection="person">
                <field name="country" method="match" wd:value="uk" />
            </wd:search>
            <wd:result var="q" search="p">
                <div>
                    <wd:print wd:value="ymd()" />
                </div>
                <div>
                    find <wd:print wd:value="wd.v('q').length" /> persons from the <wd:print wd:value="uk" />.
                </div>
                <ul>
                    <wd:for var="r" index="i" wd:in="wd.v('q')"><li>
                        <wd:print wd:value="wd.v('r').row" /> : <wd:print wd:value="wd.v('r').field('name')" /> : <wd:print wd:value="wd.v('r').field('country')" />
                    </li></wd:for>
                </ul>
            </wd:result>
        </wd>"#);
        */
        client.exec(r#"<wd><wd:session name="hoge">
            <wd:update commit="1">
                <wd:search name="person" collection="person"></wd:search>
                <wd:result var="q" search="person">
                    <wd:for var="r" index="i" wd:in="wd.v('q')">
                        <collection name="person" wd:row="wd.v('r').row">
                            <field name="name">Renamed <wd:print wd:value="wd.v('r').field('name')" /></field>
                            <field name="country"><wd:print wd:value="wd.v('r').field('country')" /></field>
                        </collection>
                    </wd:for>
                </wd:result>
            </wd:update>
        </wd:session></wd>"#,None).unwrap();
        client.exec(r#"<wd>
            <wd:search name="p" collection="person"></wd:search>
            <wd:result var="q" search="p">
                <div>
                    find <wd:print wd:value="wd.v('q').length" /> persons.
                </div>
                <ul>
                    <wd:for var="r" index="i" wd:in="wd.v('q')"><li>
                        <wd:print wd:value="wd.v('r').row" /> : <wd:print wd:value="wd.v('r').field('name')" /> : <wd:print wd:value="wd.v('r').field('country')" />
                    </li></wd:for>
                </ul>
            </wd:result>
        </wd>"#,None).unwrap();
    }
}