cassandra_cpp/cassandra/schema/
table_meta.rs

1use crate::cassandra::iterator::ColumnIterator;
2use crate::cassandra::iterator::FieldIterator;
3
4use crate::cassandra::schema::column_meta::ColumnMeta;
5use crate::cassandra::util::{Protected, ProtectedInner};
6use crate::cassandra::value::Value;
7use crate::cassandra_sys::cass_iterator_columns_from_table_meta;
8use crate::cassandra_sys::cass_iterator_fields_from_table_meta;
9use crate::cassandra_sys::cass_table_meta_clustering_key;
10use crate::cassandra_sys::cass_table_meta_clustering_key_count;
11use crate::cassandra_sys::cass_table_meta_column;
12use crate::cassandra_sys::cass_table_meta_column_by_name;
13use crate::cassandra_sys::cass_table_meta_column_count;
14use crate::cassandra_sys::cass_table_meta_field_by_name;
15use crate::cassandra_sys::cass_table_meta_name;
16use crate::cassandra_sys::cass_table_meta_partition_key;
17use crate::cassandra_sys::cass_table_meta_partition_key_count;
18use crate::cassandra_sys::CassTableMeta as _CassTableMeta;
19
20use std::marker::PhantomData;
21
22use std::os::raw::c_char;
23use std::slice;
24
25use std::str;
26
27/// Table metadata
28//
29// Borrowed from wherever the value is borrowed from.
30#[derive(Debug)]
31pub struct TableMeta<'a>(*const _CassTableMeta, PhantomData<&'a _CassTableMeta>);
32
33impl ProtectedInner<*const _CassTableMeta> for TableMeta<'_> {
34    fn inner(&self) -> *const _CassTableMeta {
35        self.0
36    }
37}
38
39impl Protected<*const _CassTableMeta> for TableMeta<'_> {
40    fn build(inner: *const _CassTableMeta) -> Self {
41        if inner.is_null() {
42            panic!("Unexpected null pointer")
43        };
44        TableMeta(inner, PhantomData)
45    }
46}
47
48impl<'a> TableMeta<'a> {
49    /// returns an iterator over the fields of this table
50    pub fn field_iter(&self) -> FieldIterator<'a> {
51        unsafe { FieldIterator::build(cass_iterator_fields_from_table_meta(self.0)) }
52    }
53
54    /// An iterator over the columns in this table
55    pub fn columns_iter(&self) -> ColumnIterator<'a> {
56        unsafe { ColumnIterator::build(cass_iterator_columns_from_table_meta(self.0)) }
57    }
58
59    /// Gets the column metadata for the provided column name.
60    pub fn column_by_name(&self, name: &str) -> ColumnMeta<'a> {
61        // TODO: can return NULL
62        unsafe {
63            ColumnMeta::build(cass_table_meta_column_by_name(
64                self.0,
65                name.as_ptr() as *const c_char,
66            ))
67        }
68    }
69
70    /// Gets the name of the table.
71    pub fn get_name(&self) -> String {
72        let mut name = std::ptr::null();
73        let mut name_length = 0;
74        unsafe {
75            cass_table_meta_name(self.0, &mut name, &mut name_length);
76            str::from_utf8(slice::from_raw_parts(name as *const u8, name_length))
77                .expect("must be utf8")
78                .to_owned()
79        }
80    }
81
82    /// Gets the total number of columns for the table.
83    pub fn column_count(&self) -> usize {
84        unsafe { cass_table_meta_column_count(self.0) }
85    }
86
87    /// Gets the column metadata for the provided index.
88    pub fn column(&self, index: usize) -> ColumnMeta<'a> {
89        // TODO: can return NULL
90        unsafe { ColumnMeta::build(cass_table_meta_column(self.0, index)) }
91    }
92
93    /// Gets the number of columns for the table's partition key.
94    pub fn partition_key_count(&self) -> usize {
95        unsafe { cass_table_meta_partition_key_count(self.0) }
96    }
97
98    /// Gets the partition key column metadata for the provided index.
99    pub fn partition_key(&self, index: usize) -> Option<ColumnMeta<'a>> {
100        unsafe {
101            let key = cass_table_meta_partition_key(self.0, index);
102            if key.is_null() {
103                None
104            } else {
105                Some(ColumnMeta::build(key))
106            }
107        }
108    }
109
110    /// Gets the number of columns for the table's clustering key
111    pub fn clustering_key_count(&self) -> usize {
112        unsafe { cass_table_meta_clustering_key_count(self.0) }
113    }
114
115    /// Gets the clustering key column metadata for the provided index.
116    pub fn cluster_key(&self, index: usize) -> Option<ColumnMeta<'a>> {
117        unsafe {
118            let key = cass_table_meta_clustering_key(self.0, index);
119            if key.is_null() {
120                None
121            } else {
122                Some(ColumnMeta::build(key))
123            }
124        }
125    }
126
127    /// Gets a metadata field for the provided name. Metadata fields allow direct
128    /// access to the column data found in the underlying "tables" metadata table.
129    pub fn field_by_name(&self, name: &str) -> Option<Value<'a>> {
130        // fixme replace CassValule with a custom type
131        unsafe {
132            let value = cass_table_meta_field_by_name(self.0, name.as_ptr() as *const c_char);
133            if value.is_null() {
134                None
135            } else {
136                Some(Value::build(value))
137            }
138        }
139    }
140}