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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
use crate::internal::category::Category;
use crate::internal::column::Column;
use crate::internal::streamname;
use crate::internal::stringpool::StringPool;
use crate::internal::value::{Value, ValueRef};
use std::io::{self, Read, Seek, SeekFrom, Write};
use std::ops::Index;
use std::rc::Rc;

// ========================================================================= //

/// A database table.
#[derive(Clone)]
pub struct Table {
    name: String,
    columns: Vec<Column>,
    long_string_refs: bool,
}

impl Table {
    /// Creates a new table object with the given name and columns.  The
    /// `long_string_refs` argument indicates the size of any encoded string
    /// refs.
    pub(crate) fn new(
        name: String,
        columns: Vec<Column>,
        long_string_refs: bool,
    ) -> Rc<Table> {
        Rc::new(Table { name, columns, long_string_refs })
    }

    /// Returns the name of the table.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Returns the name of the CFB stream that holds this table's data.
    pub(crate) fn stream_name(&self) -> String {
        streamname::encode(&self.name, true)
    }

    /// Returns true if the given string is a valid table name.
    pub(crate) fn is_valid_name(name: &str) -> bool {
        Category::Identifier.validate(name) && streamname::is_valid(name, true)
    }

    pub(crate) fn long_string_refs(&self) -> bool {
        self.long_string_refs
    }

    /// Returns the list of columns in this table.
    pub fn columns(&self) -> &[Column] {
        &self.columns
    }

    /// Returns true if this table has a column with the given name.
    pub fn has_column(&self, column_name: &str) -> bool {
        self.index_for_column_name(column_name).is_some()
    }

    /// Returns the column with the given name, if any.
    pub fn get_column(&self, column_name: &str) -> Option<&Column> {
        match self.index_for_column_name(column_name) {
            Some(index) => Some(&self.columns[index]),
            None => None,
        }
    }

    /// Returns the indices of table's primary key columns.
    pub fn primary_key_indices(&self) -> Vec<usize> {
        self.columns
            .iter()
            .enumerate()
            .filter_map(|(index, column)| {
                if column.is_primary_key() {
                    Some(index)
                } else {
                    None
                }
            })
            .collect()
    }

    pub(crate) fn index_for_column_name(
        &self,
        column_name: &str,
    ) -> Option<usize> {
        for (index, column) in self.columns.iter().enumerate() {
            if column.name() == column_name {
                return Some(index);
            }
        }
        None
    }

    /// Parses row data from the given data source and returns an interator
    /// over the rows.
    pub(crate) fn read_rows<R: Read + Seek>(
        &self,
        mut reader: R,
    ) -> io::Result<Vec<Vec<ValueRef>>> {
        let data_length = reader.seek(SeekFrom::End(0))?;
        reader.rewind()?;
        let row_size = self
            .columns
            .iter()
            .map(|col| col.coltype().width(self.long_string_refs))
            .sum::<u64>();
        let num_columns = self.columns.len();
        let num_rows =
            if row_size > 0 { (data_length / row_size) as usize } else { 0 };
        let mut rows =
            vec![Vec::<ValueRef>::with_capacity(num_columns); num_rows];
        for column in self.columns.iter() {
            let coltype = column.coltype();
            for row in rows.iter_mut() {
                row.push(
                    coltype.read_value(&mut reader, self.long_string_refs)?,
                );
            }
        }
        Ok(rows)
    }

    pub(crate) fn write_rows<W: Write>(
        &self,
        mut writer: W,
        rows: Vec<Vec<ValueRef>>,
    ) -> io::Result<()> {
        for (index, column) in self.columns.iter().enumerate() {
            let coltype = column.coltype();
            for row in rows.iter() {
                coltype.write_value(
                    &mut writer,
                    row[index],
                    self.long_string_refs,
                )?;
            }
        }
        Ok(())
    }
}

// ========================================================================= //

/// One row from a database table.
#[derive(Clone)]
pub struct Row {
    table: Rc<Table>,
    values: Vec<Value>,
}

impl Row {
    pub(crate) fn new(table: Rc<Table>, values: Vec<Value>) -> Row {
        debug_assert_eq!(values.len(), table.columns().len());
        Row { table, values }
    }

    /// Returns the number of values in the row.
    pub fn len(&self) -> usize {
        self.values.len()
    }

    /// Returns values in the row is empty
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Returns the list of columns in this row.
    pub fn columns(&self) -> &[Column] {
        self.table.columns()
    }

    /// Returns true if this row has a column with the given name.
    pub fn has_column(&self, column_name: &str) -> bool {
        self.table.has_column(column_name)
    }
}

/// Gets the value of the column with the given index.  Panics if `index >=
/// self.len()`.
impl Index<usize> for Row {
    type Output = Value;

    fn index(&self, index: usize) -> &Value {
        debug_assert_eq!(self.values.len(), self.table.columns().len());
        if index < self.values.len() {
            &self.values[index]
        } else if self.table.name.is_empty() {
            panic!(
                "Anonymous table has only {} columns (index was {index})",
                self.values.len()
            );
        } else {
            panic!(
                "Table {:?} has only {} columns (index was {index})",
                self.table.name,
                self.values.len()
            );
        }
    }
}

/// Gets the value of the column with the given name.  Panics if
/// `!self.has_column(column_name)`.
impl<'a> Index<&'a str> for Row {
    type Output = Value;

    fn index(&self, column_name: &str) -> &Value {
        match self.table.index_for_column_name(column_name) {
            Some(index) => &self.values[index],
            None => {
                if self.table.name.is_empty() {
                    panic!(
                        "Anonymous table has no column named {column_name:?}"
                    );
                } else {
                    panic!(
                        "Table {:?} has no column named {column_name:?}",
                        self.table.name
                    );
                }
            }
        }
    }
}

// ========================================================================= //

/// An iterator over the rows in a database table.
pub struct Rows<'a> {
    string_pool: &'a StringPool,
    table: Rc<Table>,
    rows: Vec<Vec<ValueRef>>,
    next_row_index: usize,
}

impl<'a> Rows<'a> {
    pub(crate) fn new(
        string_pool: &'a StringPool,
        table: Rc<Table>,
        rows: Vec<Vec<ValueRef>>,
    ) -> Rows<'a> {
        Rows { table, string_pool, rows, next_row_index: 0 }
    }

    /// Returns the list of columns for these rows.
    pub fn columns(&self) -> &[Column] {
        self.table.columns()
    }

    pub(crate) fn into_table_and_values(
        self,
    ) -> (Rc<Table>, Vec<Vec<ValueRef>>) {
        (self.table, self.rows)
    }
}

impl<'a> Iterator for Rows<'a> {
    type Item = Row;

    fn next(&mut self) -> Option<Row> {
        if self.next_row_index < self.rows.len() {
            let values: Vec<Value> = self.rows[self.next_row_index]
                .iter()
                .map(|value_ref| value_ref.to_value(self.string_pool))
                .collect();
            self.next_row_index += 1;
            Some(Row::new(self.table.clone(), values))
        } else {
            None
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        debug_assert!(self.next_row_index <= self.rows.len());
        let remaining_rows = self.rows.len() - self.next_row_index;
        (remaining_rows, Some(remaining_rows))
    }
}

impl<'a> ExactSizeIterator for Rows<'a> {}

// ========================================================================= //

#[cfg(test)]
mod tests {
    use super::Table;

    #[test]
    fn valid_table_name() {
        assert!(Table::is_valid_name("fooBar"));
        assert!(Table::is_valid_name("_Validation"));
        assert!(Table::is_valid_name("Catch22"));
        assert!(Table::is_valid_name("Foo.Bar"));

        assert!(!Table::is_valid_name(""));
        assert!(!Table::is_valid_name("99Bottles"));
        assert!(!Table::is_valid_name(
            "ThisStringIsWayTooLongToBeATableNameIMeanSeriouslyWhoWouldTryTo\
             UseANameThatIsThisLongItWouldBePrettySilly"
        ));
    }
}

// ========================================================================= //