Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/sqlite/def/column.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::DefaultType;
use crate::sqlx_types::SqlxRow;
use crate::{sqlite::def::ColumnVisibility, sqlx_types::SqlxRow};
use sea_query::{
Alias, ColumnType, Index, IndexCreateStatement,
foreign_key::ForeignKeyAction as SeaQueryForeignKeyAction,
Expand All @@ -9,12 +9,19 @@ use std::num::ParseIntError;
/// An SQLite column definition
#[derive(Debug, PartialEq, Clone)]
pub struct ColumnInfo {
/// Column id
pub cid: i64,
/// Column name.
pub name: String,
/// Declared type
pub r#type: ColumnType,
/// Whether a NOT NULL constraint is present.
pub not_null: bool,
pub default_value: DefaultType,
/// Column is part of the PRIMARY KEY.
pub primary_key: bool,
/// Hidden status
pub hidden: ColumnVisibility,
}

#[cfg(feature = "sqlx-sqlite")]
Expand All @@ -27,6 +34,7 @@ impl ColumnInfo {
let is_pk: i8 = row.get(5);
let default_value: Option<String> = row.get(4);
let default_value = default_value.unwrap_or_default();
let hidden: i8 = row.get(6);
Ok(ColumnInfo {
cid: row.get(0),
name: row.get(1),
Expand All @@ -50,15 +58,26 @@ impl ColumnInfo {
}
},
primary_key: is_pk != 0,
hidden: ColumnVisibility::from_hidden(hidden),
})
}

#[inline]
pub fn is_hidden(&self) -> bool {
self.hidden == ColumnVisibility::HiddenVirtual
}
}

#[cfg(not(feature = "sqlx-sqlite"))]
impl ColumnInfo {
pub fn to_column_def(_: SqlxRow) -> Result<ColumnInfo, ParseIntError> {
unimplemented!()
}

#[inline]
pub fn is_hidden(&self) -> bool {
unimplemented!()
}
}

/// Maps the index and all columns in the index which is the result of queries
Expand Down
18 changes: 10 additions & 8 deletions src/sqlite/def/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,17 +177,19 @@ impl TableDef {
Ok(self)
}

/// Get a list of all the columns in the table mapped as [ColumnInfo]
pub async fn get_column_info(&mut self, conn: &impl Connection) -> DiscoveryResult<&TableDef> {
let mut index_query = String::default();
index_query.push_str("PRAGMA table_info('");
index_query.push_str(&self.name);
index_query.push_str("')");
let mut query = String::with_capacity(64 + self.name.len());
query.push_str("PRAGMA table_xinfo('");
query.push_str(&self.name);
query.push_str("')");

let index_info_rows = conn.query_all_raw(index_query).await?;
let rows = conn.query_all_raw(query).await?;

for info in index_info_rows {
let column = ColumnInfo::to_column_def(info)?;
for row in rows {
let column = ColumnInfo::to_column_def(row)?;
if column.is_hidden() {
continue; // Skip hidden columns
}
self.columns.push(column);
}

Expand Down
24 changes: 24 additions & 0 deletions src/sqlite/def/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,30 @@ pub fn parse_type(data_type: String) -> Result<ColumnType, ParseIntError> {
})
}

/// The `hidden` field returned by `PRAGMA table_xinfo`.
#[derive(Debug, PartialEq, Clone)]
pub enum ColumnVisibility {
/// `0` - ordinary column
Visible,
/// `1` - hidden column in a virtual table
HiddenVirtual,
/// `2` - generated VIRTUAL column
GeneratedVirtual,
/// `3` - generated STORED column
GeneratedStored,
}

impl ColumnVisibility {
pub fn from_hidden(value: i8) -> Self {
match value {
1 => Self::HiddenVirtual,
2 => Self::GeneratedVirtual,
3 => Self::GeneratedStored,
_ => Self::Visible,
}
}
}

/// The default types for an SQLite `dflt_value`
#[derive(Debug, PartialEq, Clone)]
pub enum DefaultType {
Expand Down
Loading
Loading