Skip to main content

mysql_handler/dd/
table.rs

1// Copyright (C) 2026 ren-yamanashi
2//
3// This program is free software; you can redistribute it and/or modify
4// it under the terms of the GNU General Public License, version 2.0,
5// as published by the Free Software Foundation.
6//
7// This program is designed to work with certain software (including
8// but not limited to OpenSSL) that is licensed under separate terms,
9// as designated in a particular file or component or in included license
10// documentation. The authors of this program hereby grant you an additional
11// permission to link the program and your derivative works with the
12// separately licensed software that they have either included with
13// the program or referenced in the documentation.
14//
15// This program is distributed in the hope that it will be useful,
16// but WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with this program; if not, see <https://www.gnu.org/licenses/>.
22
23//! [`DdTable`] safe accessors over the column and index collections.
24
25#![allow(unsafe_code)]
26
27use crate::dd::ffi;
28use crate::sys::{DdColumn, DdIndex, DdTable};
29
30impl DdTable {
31    /// Number of columns in the table.
32    #[must_use]
33    pub fn column_count(&self) -> usize {
34        let p: *const DdTable = self;
35        // SAFETY: `self` is a valid borrow.
36        unsafe { ffi::mysql__DdTable__column_count(p) }
37    }
38
39    /// Borrow the `i`th column (0-based). Returns `None` past the end.
40    #[must_use]
41    pub fn column_at(&self, i: usize) -> Option<&DdColumn> {
42        let p: *const DdTable = self;
43        // SAFETY: `self` is a valid borrow; the FFI returns null when `i`
44        // is out of range or `p` is null.
45        let raw = unsafe { ffi::mysql__DdTable__column_at(p, i) };
46        // SAFETY: a non-null result references a column owned by the same
47        // dd::Table tree as `self` and is valid for `self`'s lifetime.
48        unsafe { raw.as_ref() }
49    }
50
51    /// Number of indexes in the table.
52    #[must_use]
53    pub fn index_count(&self) -> usize {
54        let p: *const DdTable = self;
55        // SAFETY: `self` is a valid borrow.
56        unsafe { ffi::mysql__DdTable__index_count(p) }
57    }
58
59    /// Borrow the `i`th index (0-based). Returns `None` past the end.
60    #[must_use]
61    pub fn index_at(&self, i: usize) -> Option<&DdIndex> {
62        let p: *const DdTable = self;
63        // SAFETY: `self` is a valid borrow; the FFI returns null when `i`
64        // is out of range or `p` is null.
65        let raw = unsafe { ffi::mysql__DdTable__index_at(p, i) };
66        // SAFETY: a non-null result references an index owned by the same
67        // dd::Table tree as `self` and is valid for `self`'s lifetime.
68        unsafe { raw.as_ref() }
69    }
70}