1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
use cassandra::data_type::ConstDataType;
use cassandra::iterator::AggregateIterator;
use cassandra::iterator::FieldIterator;
use cassandra::iterator::FunctionIterator;
use cassandra::iterator::TableIterator;
use cassandra::iterator::UserTypeIterator;
use cassandra::schema::aggregate_meta::AggregateMeta;

use cassandra::schema::function_meta::FunctionMeta;
use cassandra::schema::table_meta::TableMeta;
use cassandra::util::Protected;

use cassandra_sys::CassKeyspaceMeta as _CassKeyspaceMeta;
use cassandra_sys::CassValue as _CassValue;
use cassandra_sys::cass_iterator_aggregates_from_keyspace_meta;
use cassandra_sys::cass_iterator_fields_from_keyspace_meta;
use cassandra_sys::cass_iterator_functions_from_keyspace_meta;

use cassandra_sys::cass_iterator_tables_from_keyspace_meta;
use cassandra_sys::cass_iterator_user_types_from_keyspace_meta;
use cassandra_sys::cass_keyspace_meta_aggregate_by_name;
use cassandra_sys::cass_keyspace_meta_field_by_name;
use cassandra_sys::cass_keyspace_meta_function_by_name;
use cassandra_sys::cass_keyspace_meta_name;
use cassandra_sys::cass_keyspace_meta_table_by_name;
use cassandra_sys::cass_keyspace_meta_user_type_by_name;
use cassandra_sys::raw2utf8;
use std::ffi::CString;
use std::mem;

/// A snapshot of the schema's metadata.
#[derive(Debug)]
pub struct KeyspaceMeta(*const _CassKeyspaceMeta);

impl Protected<*const _CassKeyspaceMeta> for KeyspaceMeta {
    fn inner(&self) -> *const _CassKeyspaceMeta { self.0 }
    fn build(inner: *const _CassKeyspaceMeta) -> Self { KeyspaceMeta(inner) }
}

#[derive(Debug)]
pub struct MetadataFieldValue(*const _CassValue);

impl KeyspaceMeta {
    /// Iterator over the aggregates in this keyspace
    pub fn aggregrates_iter(&self) -> AggregateIterator {
        unsafe { AggregateIterator::build(cass_iterator_aggregates_from_keyspace_meta(self.0)) }
    }

    /// Iterator over the field in this keyspace
    pub fn fields_iter(&self) -> FieldIterator {
        unsafe { FieldIterator::build(cass_iterator_fields_from_keyspace_meta(self.0)) }
    }

    /// Gets the table metadata for the provided table name.
    pub fn table_by_name(&self, name: &str) -> Option<TableMeta> {
        unsafe {
            let value = cass_keyspace_meta_table_by_name(self.0, CString::new(name).expect("must be utf8").as_ptr());
            if value.is_null() {
                None
            } else {
                Some(TableMeta::build(value))
            }
        }
    }

    /// Gets the data type for the provided type name.
    pub fn user_type_by_name(&self, name: &str) -> Option<ConstDataType> {
        unsafe {
            let value = cass_keyspace_meta_user_type_by_name(self.0,
                                                             CString::new(name).expect("must be utf8").as_ptr());
            if value.is_null() {
                None
            } else {
                Some(ConstDataType(value))
            }
        }
    }

    /// Gets the function metadata for the provided function name.
    pub fn get_function_by_name(&self, name: &str, arguments: Vec<&str>) -> Option<FunctionMeta> {
        unsafe {
            let value = cass_keyspace_meta_function_by_name(self.0,
                                                            CString::new(name).expect("must be utf8").as_ptr(),
                                                            CString::new(arguments.join(","))
                                                                .expect("must be utf8")
                                                                .as_ptr());
            if value.is_null() {
                None
            } else {
                Some(FunctionMeta::build(value))
            }
        }
    }

    /// Gets the aggregate metadata for the provided aggregate name.
    pub fn aggregate_by_name(&self, name: &str, arguments: Vec<&str>) -> Option<AggregateMeta> {
        unsafe {
            let agg = cass_keyspace_meta_aggregate_by_name(self.0,
                                                           CString::new(name).expect("must be utf8").as_ptr(),
                                                           CString::new(arguments.join(","))
                                                               .expect("must be utf8")
                                                               .as_ptr());
            if agg.is_null() {
                None
            } else {
                Some(AggregateMeta::build((agg)))
            }
        }
    }

    /// Iterator over the tables in this keyspaces
    pub fn table_iter(&mut self) -> TableIterator {
        unsafe { TableIterator::build(cass_iterator_tables_from_keyspace_meta(self.0)) }
    }

    /// Iterator over the functions in this keyspaces
    pub fn function_iter(&mut self) -> FunctionIterator {
        unsafe { FunctionIterator::build(cass_iterator_functions_from_keyspace_meta(self.0)) }
    }

    /// Iterator over the UDTs in this keyspaces
    pub fn user_type_iter(&mut self) -> UserTypeIterator {
        unsafe { UserTypeIterator::build(cass_iterator_user_types_from_keyspace_meta(self.0)) }
    }

    /// Gets the name of the keyspace.
    pub fn name(&self) -> String {
        unsafe {
            let mut name = mem::zeroed();
            let mut name_length = mem::zeroed();
            cass_keyspace_meta_name(self.0, &mut name, &mut name_length);
            raw2utf8(name, name_length).expect("must be utf8")
        }
    }

    /// Gets a metadata field for the provided name. Metadata fields allow direct
    /// access to the column data found in the underlying "keyspaces" metadata table.
    pub fn field_by_name(&self, name: &str) -> Option<MetadataFieldValue> {
        unsafe {
            let value = cass_keyspace_meta_field_by_name(self.0, CString::new(name).expect("must be utf8").as_ptr());
            if value.is_null() {
                None
            } else {
                Some(MetadataFieldValue(value))
            }
        }
    }
}