cassandra_cpp/cassandra/schema/
function_meta.rs

1use crate::cassandra::data_type::ConstDataType;
2use crate::cassandra::error::*;
3use crate::cassandra::iterator::FieldIterator;
4use crate::cassandra::util::{Protected, ProtectedInner};
5use crate::cassandra::value::Value;
6
7use crate::cassandra_sys::cass_function_meta_argument;
8use crate::cassandra_sys::cass_function_meta_argument_count;
9use crate::cassandra_sys::cass_function_meta_argument_type_by_name_n;
10use crate::cassandra_sys::cass_function_meta_body;
11use crate::cassandra_sys::cass_function_meta_called_on_null_input;
12use crate::cassandra_sys::cass_function_meta_field_by_name_n;
13use crate::cassandra_sys::cass_function_meta_full_name;
14use crate::cassandra_sys::cass_function_meta_language;
15use crate::cassandra_sys::cass_function_meta_name;
16use crate::cassandra_sys::cass_function_meta_return_type;
17use crate::cassandra_sys::cass_iterator_fields_from_function_meta;
18use crate::cassandra_sys::cass_true;
19use crate::cassandra_sys::CassFunctionMeta as _CassFunctionMeta;
20
21use std::marker::PhantomData;
22use std::os::raw::c_char;
23use std::{slice, str};
24
25/// The metadata for a function
26//
27// Borrowed immutably.
28#[derive(Debug)]
29pub struct FunctionMeta<'a>(*const _CassFunctionMeta, PhantomData<&'a _CassFunctionMeta>);
30
31impl ProtectedInner<*const _CassFunctionMeta> for FunctionMeta<'_> {
32    fn inner(&self) -> *const _CassFunctionMeta {
33        self.0
34    }
35}
36
37impl Protected<*const _CassFunctionMeta> for FunctionMeta<'_> {
38    fn build(inner: *const _CassFunctionMeta) -> Self {
39        if inner.is_null() {
40            panic!("Unexpected null pointer")
41        };
42        FunctionMeta(inner, PhantomData)
43    }
44}
45
46impl<'a> FunctionMeta<'a> {
47    /// Iterator over the fields in this function.
48    pub fn fields_iter(&self) -> FieldIterator<'a> {
49        unsafe { FieldIterator::build(cass_iterator_fields_from_function_meta(self.0)) }
50    }
51
52    /// Gets the name of the function.
53    pub fn get_name(&self) -> String {
54        let mut name = std::ptr::null();
55        let mut name_length = 0;
56        unsafe {
57            cass_function_meta_name(self.0, &mut name, &mut name_length);
58            str::from_utf8(slice::from_raw_parts(name as *const u8, name_length))
59                .expect("must be utf8")
60                .to_owned()
61        }
62    }
63
64    /// Gets the full name of the function. The full name includes the
65    /// function's name and the function's signature:
66    /// "name(type1 type2.. typeN)".
67    pub fn full_name(&self) -> String {
68        let mut name = std::ptr::null();
69        let mut name_length = 0;
70        unsafe {
71            cass_function_meta_full_name(self.0, &mut name, &mut name_length);
72            str::from_utf8(slice::from_raw_parts(name as *const u8, name_length))
73                .expect("must be utf8")
74                .to_owned()
75        }
76    }
77
78    /// Gets the body of the function.
79    pub fn body(&self) -> String {
80        let mut name = std::ptr::null();
81        let mut name_length = 0;
82        unsafe {
83            cass_function_meta_body(self.0, &mut name, &mut name_length);
84            str::from_utf8(slice::from_raw_parts(name as *const u8, name_length))
85                .expect("must be utf8")
86                .to_owned()
87        }
88    }
89
90    /// Gets the language of the function.
91    pub fn language(&self) -> String {
92        let mut name = std::ptr::null();
93        let mut name_length = 0;
94        unsafe {
95            cass_function_meta_language(self.0, &mut name, &mut name_length);
96            str::from_utf8(slice::from_raw_parts(name as *const u8, name_length))
97                .expect("must be utf8")
98                .to_owned()
99        }
100    }
101
102    /// Gets whether a function is called on "null".
103    pub fn called_on_null_input(&self) -> bool {
104        unsafe { cass_function_meta_called_on_null_input(self.0) == cass_true }
105    }
106
107    /// Gets the number of arguments this function takes.
108    pub fn argument_count(&self) -> usize {
109        unsafe { cass_function_meta_argument_count(self.0) }
110    }
111
112    /// Gets the function's argument name and type for the provided index.
113    pub fn argument(&self, index: usize) -> Result<(String, ConstDataType<'a>)> {
114        let mut name = std::ptr::null();
115        let mut name_length = 0;
116        let mut data_type = std::ptr::null();
117        unsafe {
118            cass_function_meta_argument(self.0, index, &mut name, &mut name_length, &mut data_type)
119                .to_result(())?;
120            let name = str::from_utf8(slice::from_raw_parts(name as *const u8, name_length))
121                .expect("must be utf8")
122                .to_owned();
123            let data_type = ConstDataType::build(data_type);
124            Ok((name, data_type))
125        }
126    }
127
128    /// Gets the function's argument and type for the provided name.
129    pub fn argument_type_by_name(&self, name: &str) -> ConstDataType<'a> {
130        unsafe {
131            let name_ptr = name.as_ptr() as *const c_char;
132            // TODO: can return NULL
133            ConstDataType::build(cass_function_meta_argument_type_by_name_n(
134                self.0,
135                name_ptr,
136                name.len(),
137            ))
138        }
139    }
140
141    /// Gets the return type of the function.
142    pub fn return_type(&self) -> ConstDataType<'a> {
143        unsafe { ConstDataType::build(cass_function_meta_return_type(self.0)) }
144    }
145
146    /// Gets a metadata field for the provided name. Metadata fields allow direct
147    /// access to the column data found in the underlying "functions" metadata table.
148    pub fn field_by_name(&self, name: &str) -> Value<'a> {
149        unsafe {
150            let name_ptr = name.as_ptr() as *const c_char;
151            // TODO: can return NULL
152            Value::build(cass_function_meta_field_by_name_n(
153                self.0,
154                name_ptr,
155                name.len(),
156            ))
157        }
158    }
159}