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
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::marker::PhantomData;

use std::os::raw::c_char;
use std::slice;

use std::str;

/// Table metadata
//
// Borrowed from wherever the value is borrowed from.
#[derive(Debug)]
pub struct TableMeta<'a>(*const _CassTableMeta, PhantomData<&'a _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, PhantomData)
    }
}

impl<'a> TableMeta<'a> {
    /// returns an iterator over the fields of this table
    pub fn field_iter(&self) -> FieldIterator<'a> {
        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<'a> {
        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<'a> {
        // TODO: can return NULL
        unsafe {
            ColumnMeta::build(cass_table_meta_column_by_name(
                self.0,
                name.as_ptr() as *const c_char,
            ))
        }
    }

    /// Gets the name of the table.
    pub fn get_name(&self) -> String {
        let mut name = std::ptr::null();
        let mut name_length = 0;
        unsafe {
            cass_table_meta_name(self.0, &mut name, &mut name_length);
            str::from_utf8(slice::from_raw_parts(name as *const u8, name_length))
                .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<'a> {
        // 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<'a>> {
        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<'a>> {
        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<'a>> {
        // fixme replace CassValule with a custom type
        unsafe {
            let value = cass_table_meta_field_by_name(self.0, name.as_ptr() as *const c_char);
            if value.is_null() {
                None
            } else {
                Some(Value::build(value))
            }
        }
    }
}