use crate::column::{ColumnDef, ColumnType};
use crate::table::{TableDef, TableKind};
use super::options::Fts5Options;
pub const FTS5_MODULE: &str = "fts5";
#[derive(Debug, Clone, Default)]
pub struct Fts5Table {
name: String,
columns: Vec<ColumnDef>,
options: Fts5Options,
}
impl Fts5Table {
#[must_use]
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
columns: Vec::new(),
options: Fts5Options::default(),
}
}
#[must_use]
pub fn column(self, name: impl Into<String>, column_type: ColumnType) -> Self {
self.push_column(name.into(), column_type, false)
}
#[must_use]
pub fn unindexed_column(self, name: impl Into<String>, column_type: ColumnType) -> Self {
self.push_column(name.into(), column_type, true)
}
#[must_use]
pub fn tokenize(mut self, spec: impl Into<String>) -> Self {
self.options.tokenize = Some(spec.into());
self
}
#[must_use]
pub fn prefix(mut self, spec: impl Into<String>) -> Self {
self.options.prefix = Some(spec.into());
self
}
#[must_use]
pub fn content(mut self, table: impl Into<String>) -> Self {
self.options.content = Some(table.into());
self
}
#[must_use]
pub fn content_rowid(mut self, column: impl Into<String>) -> Self {
self.options.content_rowid = Some(column.into());
self
}
#[must_use]
pub fn columnsize(mut self, value: u8) -> Self {
self.options.columnsize = Some(value);
self
}
#[must_use]
pub fn detail(mut self, value: impl Into<String>) -> Self {
self.options.detail = Some(value.into());
self
}
#[must_use]
pub fn build(self) -> TableDef {
let mut args: Vec<String> = self.columns.iter().map(column_arg).collect();
args.extend(self.options.render());
TableDef {
name: self.name,
columns: self.columns,
indexes: Vec::new(),
strict: false,
kind: TableKind::virtual_table(FTS5_MODULE, args),
}
}
fn push_column(mut self, name: String, column_type: ColumnType, unindexed: bool) -> Self {
self.columns.push(ColumnDef {
name,
column_type,
primary_key: false,
not_null: false,
default: None,
unique: false,
references: None,
on_delete: None,
on_update: None,
check: None,
unindexed,
});
self
}
}
fn column_arg(column: &ColumnDef) -> String {
if column.unindexed {
format!("\"{}\" UNINDEXED", column.name)
} else {
format!("\"{}\"", column.name)
}
}