cassandra_cpp/cassandra/schema/
aggregate_meta.rs

1use crate::cassandra::data_type::ConstDataType;
2use crate::cassandra::iterator::FieldIterator;
3
4use crate::cassandra::schema::function_meta::FunctionMeta;
5use crate::cassandra::util::{Protected, ProtectedInner};
6use crate::cassandra::value::Value;
7
8use crate::cassandra_sys::cass_aggregate_meta_argument_count;
9use crate::cassandra_sys::cass_aggregate_meta_argument_type;
10use crate::cassandra_sys::cass_aggregate_meta_field_by_name_n;
11use crate::cassandra_sys::cass_aggregate_meta_final_func;
12use crate::cassandra_sys::cass_aggregate_meta_full_name;
13use crate::cassandra_sys::cass_aggregate_meta_init_cond;
14use crate::cassandra_sys::cass_aggregate_meta_name;
15use crate::cassandra_sys::cass_aggregate_meta_return_type;
16use crate::cassandra_sys::cass_aggregate_meta_state_func;
17use crate::cassandra_sys::cass_aggregate_meta_state_type;
18use crate::cassandra_sys::cass_iterator_fields_from_aggregate_meta;
19use crate::cassandra_sys::raw2utf8;
20use crate::cassandra_sys::CassAggregateMeta as _CassAggregateMeta;
21use std::marker::PhantomData;
22
23use std::os::raw::c_char;
24
25/// Metadata about a Cassandra aggregate
26//
27// Borrowed immutably.
28#[derive(Debug)]
29pub struct AggregateMeta<'a>(
30    *const _CassAggregateMeta,
31    PhantomData<&'a _CassAggregateMeta>,
32);
33
34impl ProtectedInner<*const _CassAggregateMeta> for AggregateMeta<'_> {
35    fn inner(&self) -> *const _CassAggregateMeta {
36        self.0
37    }
38}
39
40impl Protected<*const _CassAggregateMeta> for AggregateMeta<'_> {
41    fn build(inner: *const _CassAggregateMeta) -> Self {
42        if inner.is_null() {
43            panic!("Unexpected null pointer")
44        };
45        AggregateMeta(inner, PhantomData)
46    }
47}
48
49impl<'a> AggregateMeta<'a> {
50    /// An iterator over the fields of an aggregate
51    pub fn fields_iter(&self) -> FieldIterator<'a> {
52        unsafe { FieldIterator::build(cass_iterator_fields_from_aggregate_meta(self.0)) }
53    }
54
55    /// Gets the name of the aggregate.
56    pub fn get_name(&self) -> String {
57        let mut name = std::ptr::null();
58        let mut name_length = 0;
59        unsafe {
60            cass_aggregate_meta_name(self.0, &mut name, &mut name_length);
61            raw2utf8(name, name_length).expect("must be utf8")
62        }
63    }
64
65    /// Gets the full name of the aggregate.
66    pub fn full_name(&self) -> String {
67        let mut name = std::ptr::null();
68        let mut name_length = 0;
69        unsafe {
70            cass_aggregate_meta_full_name(self.0, &mut name, &mut name_length);
71            raw2utf8(name, name_length).expect("must be utf8")
72        }
73    }
74
75    /// Gets the number of arguments this aggregate takes.
76    pub fn argument_count(&self) -> usize {
77        unsafe { cass_aggregate_meta_argument_count(self.0) }
78    }
79
80    /// Gets the aggregate's argument type for the provided index.
81    pub fn argument_type(&self, index: usize) -> ConstDataType<'a> {
82        // TODO: can return NULL
83        unsafe { ConstDataType::build(cass_aggregate_meta_argument_type(self.0, index)) }
84    }
85
86    /// Gets the aggregate's argument return type.
87    pub fn return_type(&self) -> ConstDataType<'a> {
88        unsafe { ConstDataType::build(cass_aggregate_meta_return_type(self.0)) }
89    }
90
91    /// Gets the aggregate's argument state type.
92    pub fn state_type(&self) -> ConstDataType<'a> {
93        unsafe { ConstDataType::build(cass_aggregate_meta_state_type(self.0)) }
94    }
95
96    /// Gets the function metadata for the aggregate's state function.
97    pub fn state_func(&self) -> FunctionMeta<'a> {
98        unsafe { FunctionMeta::build(cass_aggregate_meta_state_func(self.0)) }
99    }
100
101    /// Gets the function metadata for the aggregates's final function.
102    pub fn final_func(&self) -> FunctionMeta<'a> {
103        unsafe { FunctionMeta::build(cass_aggregate_meta_final_func(self.0)) }
104    }
105
106    ///  Gets the initial condition value for the aggregate.
107    pub fn init_cond(&self) -> Value<'a> {
108        unsafe { Value::build(cass_aggregate_meta_init_cond(self.0)) }
109    }
110
111    ///  Gets a metadata field for the provided name. Metadata fields allow direct
112    /// access to the column data found in the underlying "aggregates" metadata table.
113    pub fn field_by_name(&self, name: &str) -> Option<Value<'a>> {
114        unsafe {
115            let name_ptr = name.as_ptr() as *const c_char;
116            let agg = cass_aggregate_meta_field_by_name_n(self.0, name_ptr, name.len());
117            if agg.is_null() {
118                None
119            } else {
120                Some(Value::build(agg))
121            }
122        }
123    }
124}