|
1 | 1 | # Server and Client
|
2 | 2 |
|
3 |
| -Now that we've gone over the individual components, we'll tie them all together with the server and |
4 |
| -client. |
5 |
| - |
6 |
| -## Server |
7 |
| - |
8 |
| -in the toyDB |
| 3 | +Now that we've gone over the individual components, we'll tie them all together in the toyDB |
9 | 4 | server `toydb::Server`, located in the [`server`](https://github.com/erikgrinaker/toydb/blob/0839215770e31f1e693d5cccf20a68210deaaa3f/src/server.rs) module.
|
10 | 5 |
|
11 | 6 | The server wraps an inner Raft node `raft::Node`, which manages the SQL state machine, and is
|
@@ -113,60 +108,8 @@ https://github.com/erikgrinaker/toydb/blob/8f8eae0dcf70b1a0df2e853b1f6600e0c7075
|
113 | 108 |
|
114 | 109 | toyDB is now up and running!
|
115 | 110 |
|
116 |
| -## Client Library |
117 |
| - |
118 |
| -The main client library `toydb::Client` in the [`client`](https://github.com/erikgrinaker/toydb/blob/213e5c02b09f1a3cac6a8bbd0a81773462f367f5/src/client.rs) |
119 |
| -module is used to connect to a toyDB server: |
120 |
| - |
121 |
| -https://github.com/erikgrinaker/toydb/blob/213e5c02b09f1a3cac6a8bbd0a81773462f367f5/src/client.rs#L15-L24 |
122 |
| - |
123 |
| -When initialized, it connects to a toyDB server over TCP: |
124 |
| - |
125 |
| -https://github.com/erikgrinaker/toydb/blob/213e5c02b09f1a3cac6a8bbd0a81773462f367f5/src/client.rs#L27-L33 |
126 |
| - |
127 |
| -It can then send Bincode-encoded `toydb::Request` to the server, and receives `toydb::Response` |
128 |
| -back. |
129 |
| - |
130 |
| -https://github.com/erikgrinaker/toydb/blob/213e5c02b09f1a3cac6a8bbd0a81773462f367f5/src/client.rs#L35-L40 |
131 |
| - |
132 |
| - |
133 |
| -In particular, `Client::execute` can be used to execute arbitrary SQL statements in the client's |
134 |
| -session: |
135 |
| - |
136 |
| -https://github.com/erikgrinaker/toydb/blob/213e5c02b09f1a3cac6a8bbd0a81773462f367f5/src/client.rs#L42-L56 |
137 |
| - |
138 |
| -## `toysql` Binary |
139 |
| - |
140 |
| -However, `toydb::Client` is a programmatic API, and we want a more convenient user interface. |
141 |
| -The `toysql` client in [`src/bin/toysql.rs`](https://github.com/erikgrinaker/toydb/blob/0839215770e31f1e693d5cccf20a68210deaaa3f/src/bin/toysql.rs) |
142 |
| -provides a typical [REPL](https://en.wikipedia.org/wiki/Read–eval–print_loop) (read-evaluate-print loop) where users can enter SQL statements and view the results. |
143 |
| - |
144 |
| -Like `toydb`, `toysql` is a tiny [`clap`](https://docs.rs/clap/latest/clap/) command that takes a |
145 |
| -toyDB server address to connect to and starts an interactive shell: |
146 |
| - |
147 |
| -https://github.com/erikgrinaker/toydb/blob/0839215770e31f1e693d5cccf20a68210deaaa3f/src/bin/toysql.rs#L29-L53 |
148 |
| - |
149 |
| -This first attempts to connect to the toyDB server using the `toydb::Client` client, and then starts |
150 |
| -an interactive shell using the [Rustyline](https://docs.rs/rustyline/latest/rustyline/) library. |
151 |
| - |
152 |
| -https://github.com/erikgrinaker/toydb/blob/0839215770e31f1e693d5cccf20a68210deaaa3f/src/bin/toysql.rs#L55-L81 |
153 |
| - |
154 |
| -The shell is simply a loop that prompts the user to input a SQL statement: |
155 |
| - |
156 |
| -https://github.com/erikgrinaker/toydb/blob/0839215770e31f1e693d5cccf20a68210deaaa3f/src/bin/toysql.rs#L216-L250 |
157 |
| - |
158 |
| -Each statement is the executed against the server via `toydb::Client::execute`, and the response |
159 |
| -is formatted and printed as output: |
160 |
| - |
161 |
| -https://github.com/erikgrinaker/toydb/blob/0839215770e31f1e693d5cccf20a68210deaaa3f/src/bin/toysql.rs#L83-L92 |
162 |
| - |
163 |
| -https://github.com/erikgrinaker/toydb/blob/0839215770e31f1e693d5cccf20a68210deaaa3f/src/bin/toysql.rs#L175-L204 |
164 |
| - |
165 |
| -And with that, we have a fully functional SQL database system and can run queries to our heart's |
166 |
| -content. Have fun! |
167 |
| - |
168 | 111 | ---
|
169 | 112 |
|
170 | 113 | <p align="center">
|
171 |
| -← <a href="sql-execution.md">SQL Execution</a> |
| 114 | +← <a href="sql-execution.md">SQL Execution</a> | <a href="client.md">Client</a> → |
172 | 115 | </p>
|
0 commit comments