pub struct Catalog<'conn> { /* private fields */ }Expand description
Provides catalog operations for database metadata.
§Example
use hyperdb_api::{Connection, Catalog, CreateMode, Result};
fn main() -> Result<()> {
let conn = Connection::connect("localhost:7483", "example.hyper", CreateMode::CreateIfNotExists)?;
let catalog = Catalog::new(&conn);
// Check if a schema exists
if !catalog.has_schema("my_schema")? {
catalog.create_schema("my_schema")?;
}
// List tables
let tables = catalog.get_table_names("my_schema")?;
for table in tables {
println!("Table: {}", table);
}
Ok(())
}Implementations§
Source§impl<'conn> Catalog<'conn>
impl<'conn> Catalog<'conn>
Sourcepub fn new(connection: &'conn Connection) -> Self
pub fn new(connection: &'conn Connection) -> Self
Creates a new Catalog for the given connection.
Sourcepub fn create_database(&self, path: &str) -> Result<()>
pub fn create_database(&self, path: &str) -> Result<()>
Creates a new database file (delegates to Connection).
§Errors
Forwards the error from Connection::create_database.
Sourcepub fn drop_database(&self, path: &str) -> Result<()>
pub fn drop_database(&self, path: &str) -> Result<()>
Drops (deletes) a database file (delegates to Connection).
§Errors
Forwards the error from Connection::drop_database.
Sourcepub fn attach_database(&self, path: &str, alias: Option<&str>) -> Result<()>
pub fn attach_database(&self, path: &str, alias: Option<&str>) -> Result<()>
Attaches a database file to the connection.
Once attached, the database can be queried and modified. The database is identified by its alias (or by its path if no alias is provided).
§Arguments
path- The path to the database file to attach.alias- Optional alias for the database. IfNone, the database is attached without an explicit alias (typically using its filename).
§Errors
Returns an error if the database file doesn’t exist or if attachment fails.
Sourcepub fn detach_database(&self, alias: &str) -> Result<()>
pub fn detach_database(&self, alias: &str) -> Result<()>
Detaches a database from the connection.
After detaching, the database file is released and can be accessed externally (e.g., copied, moved, etc.). All pending updates are written to disk before detaching.
§Arguments
alias- The alias of the database to detach.
§Errors
Returns an error if the database is not attached or if detachment fails.
Sourcepub fn detach_all_databases(&self) -> Result<()>
pub fn detach_all_databases(&self) -> Result<()>
Detaches all databases from the connection.
This is useful for cleanup before closing a connection or when you need to release all database files.
§Errors
Returns an error if the databases could not be detached.
Sourcepub fn create_schema<T>(&self, schema_name: T) -> Result<()>
pub fn create_schema<T>(&self, schema_name: T) -> Result<()>
Creates a schema.
§Errors
- Returns an error if
schema_namecannot be converted to aSchemaName. - Returns
Error::Clientif the server rejectsCREATE SCHEMA IF NOT EXISTS.
Sourcepub fn get_table_names<T>(&self, schema: T) -> Result<Vec<String>>
pub fn get_table_names<T>(&self, schema: T) -> Result<Vec<String>>
Sourcepub fn has_schema<T>(&self, schema: T) -> Result<bool>
pub fn has_schema<T>(&self, schema: T) -> Result<bool>
Checks whether a schema exists.
§Arguments
schema- The schema name (can include database qualifier).
§Returns
true if the schema exists, false otherwise.
§Errors
- Returns an error if
schemacannot be converted to aSchemaName. - Returns
Error::Clientif thepg_catalog.pg_namespacelookup query fails.
Sourcepub fn has_table<T>(&self, table_name: T) -> Result<bool>
pub fn has_table<T>(&self, table_name: T) -> Result<bool>
Checks whether a table exists.
§Arguments
table_name- The table name (can include database and schema qualifiers).
§Returns
true if the table exists, false otherwise.
§Errors
- Returns an error if
table_namecannot be converted to aTableName. - Returns
Error::Clientif thepg_catalog.pg_tableslookup query fails.
Sourcepub fn get_table_definition<T>(&self, table_name: T) -> Result<TableDefinition>
pub fn get_table_definition<T>(&self, table_name: T) -> Result<TableDefinition>
Retrieves the table definition for an existing table.
§Arguments
table_name- The table name (can include database and schema qualifiers).
§Returns
A TableDefinition representing the table’s schema.
§Errors
Returns an error if the table does not exist or if retrieval fails.
§Example
use hyperdb_api::{Connection, Catalog, Result};
fn main() -> Result<()> {
let conn = Connection::without_database("localhost:7483")?;
let catalog = Catalog::new(&conn);
let table_def = catalog.get_table_definition("public.products")?;
println!("Columns: {}", table_def.column_count());
for col in table_def.columns() {
println!(" - {}: {}", col.name, col.type_name());
}
Ok(())
}Sourcepub fn create_table(&self, table_def: &TableDefinition) -> Result<()>
pub fn create_table(&self, table_def: &TableDefinition) -> Result<()>
Sourcepub fn create_table_if_not_exists(
&self,
table_def: &TableDefinition,
) -> Result<()>
pub fn create_table_if_not_exists( &self, table_def: &TableDefinition, ) -> Result<()>
Creates a table from a definition if it doesn’t exist.
Unlike create_table, this method does not fail
if the table already exists.
§Errors
- Returns
Error::InvalidTableDefinitioniftable_defcannot be rendered as valid SQL (zero columns, bad identifiers). - Returns
Error::Clientif the server rejectsCREATE TABLE IF NOT EXISTS.
Sourcepub fn drop_table<T>(&self, table_name: T) -> Result<()>
pub fn drop_table<T>(&self, table_name: T) -> Result<()>
Sourcepub fn drop_table_if_exists<T>(&self, table_name: T) -> Result<()>
pub fn drop_table_if_exists<T>(&self, table_name: T) -> Result<()>
Drops a table if it exists.
Unlike drop_table, this method does not fail
if the table doesn’t exist.
§Errors
- Returns an error if
table_namecannot be converted to aTableName. - Returns
Error::Clientif the server rejectsDROP TABLE IF EXISTS.
Sourcepub fn drop_schema<T>(&self, schema_name: T, cascade: bool) -> Result<()>
pub fn drop_schema<T>(&self, schema_name: T, cascade: bool) -> Result<()>
Sourcepub fn drop_schema_if_exists<T>(
&self,
schema_name: T,
cascade: bool,
) -> Result<()>
pub fn drop_schema_if_exists<T>( &self, schema_name: T, cascade: bool, ) -> Result<()>
Drops a schema if it exists.
§Errors
- Returns an error if
schema_namecannot be converted to aSchemaName. - Returns
Error::Clientif the server rejectsDROP SCHEMA IF EXISTS— typically becausecascadewasfalseand the schema is not empty.
Sourcepub fn get_row_count<T>(&self, table_name: T) -> Result<i64>
pub fn get_row_count<T>(&self, table_name: T) -> Result<i64>
Returns the approximate row count for a table.
This executes SELECT COUNT(*) FROM table_name.
§Example
let catalog = Catalog::new(&conn);
let count = catalog.get_row_count("public.users")?;
println!("Users: {}", count);§Errors
- Returns an error if
table_namecannot be converted to aTableName. - Returns
Error::Clientif theSELECT COUNT(*)query fails (e.g. table does not exist).
Sourcepub fn get_column_names<T>(&self, table_name: T) -> Result<Vec<String>>
pub fn get_column_names<T>(&self, table_name: T) -> Result<Vec<String>>
Returns the column names for a table.
§Example
let catalog = Catalog::new(&conn);
let columns = catalog.get_column_names("public.users")?;
for col in &columns {
println!("Column: {}", col);
}§Errors
Forwards the error from
get_table_definition — invalid
table_name, missing table, or a failed catalog query.
Sourcepub fn get_database_names(&self) -> Result<Vec<String>>
pub fn get_database_names(&self) -> Result<Vec<String>>
Returns a list of attached database names.
§Example
let catalog = Catalog::new(&conn);
let databases = catalog.get_database_names()?;
for db in &databases {
println!("Database: {}", db);
}§Errors
Returns Error::Client if the
SELECT datname FROM pg_catalog.pg_database query fails or a
streaming error occurs while draining the result.
Trait Implementations§
Auto Trait Implementations§
impl<'conn> Freeze for Catalog<'conn>
impl<'conn> !RefUnwindSafe for Catalog<'conn>
impl<'conn> Send for Catalog<'conn>
impl<'conn> Sync for Catalog<'conn>
impl<'conn> Unpin for Catalog<'conn>
impl<'conn> UnsafeUnpin for Catalog<'conn>
impl<'conn> !UnwindSafe for Catalog<'conn>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request