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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use cassandra_cpp_sys::cass_table_meta_materialized_view;
use cassandra_cpp_sys::cass_table_meta_materialized_view_by_name_n;
use cassandra_cpp_sys::cass_table_meta_materialized_view_count;

use crate::cassandra::iterator::ColumnIterator;
use crate::cassandra::iterator::FieldIterator;

use crate::cassandra::schema::column_meta::ColumnMeta;
use crate::cassandra::util::{Protected, ProtectedInner};
use crate::cassandra::value::Value;
use crate::cassandra_sys::cass_iterator_columns_from_table_meta;
use crate::cassandra_sys::cass_iterator_fields_from_table_meta;
use crate::cassandra_sys::cass_table_meta_clustering_key;
use crate::cassandra_sys::cass_table_meta_clustering_key_count;
use crate::cassandra_sys::cass_table_meta_column;
use crate::cassandra_sys::cass_table_meta_column_by_name;
use crate::cassandra_sys::cass_table_meta_column_count;
use crate::cassandra_sys::cass_table_meta_field_by_name;
use crate::cassandra_sys::cass_table_meta_name;
use crate::cassandra_sys::cass_table_meta_partition_key;
use crate::cassandra_sys::cass_table_meta_partition_key_count;
use crate::cassandra_sys::CassTableMeta as _CassTableMeta;
use std::mem;
use std::os::raw::c_char;
use std::slice;

use std::str;

use super::materialized_view_meta::MaterializedViewMeta;

/// Table metadata
#[derive(Debug)]
pub struct TableMeta(*const _CassTableMeta);

impl ProtectedInner<*const _CassTableMeta> for TableMeta {
    fn inner(&self) -> *const _CassTableMeta {
        self.0
    }
}

impl Protected<*const _CassTableMeta> for TableMeta {
    fn build(inner: *const _CassTableMeta) -> Self {
        if inner.is_null() {
            panic!("Unexpected null pointer")
        };
        TableMeta(inner)
    }
}

impl TableMeta {
    /// returns an iterator over the fields of this table
    pub fn field_iter(&mut self) -> FieldIterator {
        unsafe { FieldIterator::build(cass_iterator_fields_from_table_meta(self.0)) }
    }

    /// An iterator over the columns in this table
    pub fn columns_iter(&self) -> ColumnIterator {
        unsafe { ColumnIterator::build(cass_iterator_columns_from_table_meta(self.0)) }
    }

    /// Gets the column metadata for the provided column name.
    pub fn column_by_name(&self, name: &str) -> ColumnMeta {
        // TODO: can return NULL
        unsafe {
            ColumnMeta::build(cass_table_meta_column_by_name(
                self.0,
                name.as_ptr() as *const i8,
            ))
        }
    }

    /// Gets the name of the table.
    pub fn get_name(&self) -> String {
        unsafe {
            let mut name = mem::zeroed();
            let mut name_length = mem::zeroed();
            cass_table_meta_name(self.0, &mut name, &mut name_length);
            str::from_utf8(slice::from_raw_parts(
                name as *const u8,
                name_length as usize,
            ))
            .expect("must be utf8")
            .to_owned()
        }
    }

    /// Gets the total number of columns for the table.
    pub fn column_count(&self) -> usize {
        unsafe { cass_table_meta_column_count(self.0) }
    }

    /// Gets the column metadata for the provided index.
    pub fn column(&self, index: usize) -> ColumnMeta {
        // TODO: can return NULL
        unsafe { ColumnMeta::build(cass_table_meta_column(self.0, index)) }
    }

    /// Gets the number of columns for the table's partition key.
    pub fn partition_key_count(&self) -> usize {
        unsafe { cass_table_meta_partition_key_count(self.0) }
    }

    /// Gets the partition key column metadata for the provided index.
    pub fn partition_key(&self, index: usize) -> Option<ColumnMeta> {
        unsafe {
            let key = cass_table_meta_partition_key(self.0, index);
            if key.is_null() {
                None
            } else {
                Some(ColumnMeta::build(key))
            }
        }
    }

    /// Gets the number of columns for the table's clustering key
    pub fn clustering_key_count(&self) -> usize {
        unsafe { cass_table_meta_clustering_key_count(self.0) }
    }

    /// Gets the clustering key column metadata for the provided index.
    pub fn cluster_key(&self, index: usize) -> Option<ColumnMeta> {
        unsafe {
            let key = cass_table_meta_clustering_key(self.0, index);
            if key.is_null() {
                None
            } else {
                Some(ColumnMeta::build(key))
            }
        }
    }

    /// Gets a metadata field for the provided name. Metadata fields allow direct
    /// access to the column data found in the underlying "tables" metadata table.
    pub fn field_by_name(&self, name: &str) -> Option<Value> {
        // fixme replace CassValue with a custom type
        unsafe {
            let value = cass_table_meta_field_by_name(self.0, name.as_ptr() as *const i8);
            if value.is_null() {
                None
            } else {
                Some(Value::build(value))
            }
        }
    }

    /// Gets the total number of materialized views for the table.
    pub fn materialized_view_count(&self) -> usize {
        unsafe { cass_table_meta_materialized_view_count(self.0) }
    }

    /// Gets the materialized view metadata for the provided index.
    pub fn materialized_view(&self, index: usize) -> Option<MaterializedViewMeta> {
        unsafe {
            let value = cass_table_meta_materialized_view(self.0, index);
            if value.is_null() {
                None
            } else {
                Some(MaterializedViewMeta::build(value))
            }
        }
    }

    /// Gets the materialized view metadata for the provided name.
    pub fn materialized_view_by_name(&self, name: &str) -> Option<MaterializedViewMeta> {
        unsafe {
            let name_ptr = name.as_ptr() as *const c_char;
            let value = cass_table_meta_materialized_view_by_name_n(self.0, name_ptr, name.len());
            if value.is_null() {
                None
            } else {
                Some(MaterializedViewMeta::build(value))
            }
        }
    }
}