cassandra_cpp/cassandra/schema/
schema_meta.rs

1use crate::cassandra::iterator::KeyspaceIterator;
2
3use crate::cassandra::schema::keyspace_meta::KeyspaceMeta;
4use crate::cassandra::util::{Protected, ProtectedInner};
5use crate::cassandra_sys::cass_iterator_keyspaces_from_schema_meta;
6use crate::cassandra_sys::cass_schema_meta_free;
7use crate::cassandra_sys::cass_schema_meta_keyspace_by_name_n;
8use crate::cassandra_sys::cass_schema_meta_snapshot_version;
9use crate::cassandra_sys::CassSchemaMeta as _CassSchemaMeta;
10use std::os::raw::c_char;
11
12/// A snapshot of the schema's metadata. Owned.
13#[derive(Debug)]
14pub struct SchemaMeta(*const _CassSchemaMeta);
15
16impl Drop for SchemaMeta {
17    fn drop(&mut self) {
18        unsafe {
19            cass_schema_meta_free(self.0);
20        }
21    }
22}
23
24impl ProtectedInner<*const _CassSchemaMeta> for SchemaMeta {
25    fn inner(&self) -> *const _CassSchemaMeta {
26        self.0
27    }
28}
29
30impl Protected<*const _CassSchemaMeta> for SchemaMeta {
31    fn build(inner: *const _CassSchemaMeta) -> Self {
32        if inner.is_null() {
33            panic!("Unexpected null pointer")
34        };
35        SchemaMeta(inner)
36    }
37}
38
39impl SchemaMeta {
40    /// Gets the version of the schema metadata snapshot.
41    pub fn snapshot_version(&self) -> u32 {
42        unsafe { cass_schema_meta_snapshot_version(self.0) }
43    }
44
45    /// Gets the keyspace metadata for the provided keyspace name.
46    pub fn get_keyspace_by_name(&self, keyspace: &str) -> KeyspaceMeta {
47        // TODO: can return NULL
48        unsafe {
49            let keyspace_ptr = keyspace.as_ptr() as *const c_char;
50            KeyspaceMeta::build(cass_schema_meta_keyspace_by_name_n(
51                self.0,
52                keyspace_ptr,
53                keyspace.len(),
54            ))
55        }
56    }
57
58    /// Returns an iterator over the keyspaces in this schema
59    pub fn keyspace_iter(&self) -> KeyspaceIterator {
60        unsafe { KeyspaceIterator::build(cass_iterator_keyspaces_from_schema_meta(self.0)) }
61    }
62}