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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//! Provides PostgreSQL row metadata structures for the `carbon-core` framework.
//!
//! This module defines database row metadata types that bridge between the
//! core framework's metadata structures and PostgreSQL table schemas. These
//! types are designed to work seamlessly with `sqlx` for database operations,
//! providing type-safe conversion from framework metadata to database rows.
//!
//! # Overview
//!
//! The metadata module includes:
//! - **AccountRowMetadata**: PostgreSQL row structure for account metadata,
//! including public key and slot information.
//! - **InstructionRowMetadata**: PostgreSQL row structure for instruction
//! metadata, including signature, index, stack height, and slot information.
//!
//! # Key Components
//!
//! ## AccountRowMetadata
//!
//! Represents account metadata as stored in PostgreSQL tables:
//! - **pubkey**: The account's public key (stored as `__pubkey`)
//! - **slot**: The slot when the account was last updated (stored as `__slot`)
//!
//! ## InstructionRowMetadata
//!
//! Represents instruction metadata as stored in PostgreSQL tables:
//! - **signature**: The transaction signature (stored as `__signature`)
//! - **index**: The instruction index within the transaction (stored as `__instruction_index`)
//! - **stack_height**: The call stack height for nested instructions (stored as `__stack_height`)
//! - **slot**: The slot when the instruction was executed (stored as `__slot`)
//!
//! # Database Schema
//!
//! These types are designed to work with PostgreSQL tables that include
//! metadata columns with double-underscore prefixes to distinguish them
//! from application-specific data columns:
//!
//! ```sql
//! -- Example account table schema
//! CREATE TABLE accounts (
//! __pubkey BYTEA NOT NULL,
//! __slot BIGINT,
//! -- application-specific columns...
//! );
//!
//! -- Example instruction table schema
//! CREATE TABLE instructions (
//! __signature TEXT NOT NULL,
//! __instruction_index INTEGER NOT NULL,
//! __stack_height INTEGER NOT NULL,
//! __slot BIGINT,
//! -- application-specific columns...
//! );
//! ```
//!
//! # Usage Examples
//!
//! ## Converting from framework metadata
//!
//! ```ignore
//! use carbon_core::{
//! account::AccountMetadata,
//! instruction::InstructionMetadata,
//! postgres::metadata::{AccountRowMetadata, InstructionRowMetadata},
//! };
//!
//! // Convert account metadata
//! let account_metadata = AccountMetadata {
//! pubkey: solana_pubkey::Pubkey::new_unique(),
//! slot: 12345,
//! };
//! let row_metadata: AccountRowMetadata = account_metadata.into();
//!
//! // Convert instruction metadata
//! let instruction_metadata = InstructionMetadata {
//! transaction_metadata: TransactionMetadata {
//! signature: solana_signature::Signature::new_unique(),
//! slot: 12345,
//! fee_payer: solana_pubkey::Pubkey::new_unique(),
//! },
//! index: 0,
//! stack_height: 0,
//! };
//! let row_metadata: InstructionRowMetadata = instruction_metadata.into();
//! ```
//!
//! ## Using with SQLx queries
//!
//! ```ignore
//! use carbon_core::postgres::metadata::{AccountRowMetadata, InstructionRowMetadata};
//! use sqlx::PgPool;
//!
//! async fn get_account_metadata(pool: &PgPool, pubkey: Pubkey) -> Result<Option<AccountRowMetadata>, sqlx::Error> {
//! sqlx::query_as!(
//! AccountRowMetadata,
//! "SELECT __pubkey, __slot FROM accounts WHERE __pubkey = $1",
//! pubkey
//! )
//! .fetch_optional(pool)
//! .await
//! }
//!
//! async fn get_instruction_metadata(pool: &PgPool, signature: &str, index: u32) -> Result<Option<InstructionRowMetadata>, sqlx::Error> {
//! sqlx::query_as!(
//! InstructionRowMetadata,
//! "SELECT __signature, __instruction_index, __stack_height, __slot FROM instructions WHERE __signature = $1 AND __instruction_index = $2",
//! signature,
//! index
//! )
//! .fetch_optional(pool)
//! .await
//! }
//! ```
//!
//! # Notes
//!
//! - All metadata fields use PostgreSQL primitive types from the `primitives` module
//! - The `From` implementations provide seamless conversion from framework metadata
//! - Column names use double-underscore prefixes to avoid conflicts with application data
//! - Optional fields (like `slot`) allow for partial metadata storage
//! - These types implement `sqlx::FromRow` for direct use in SQLx queries
use crate::;
/// PostgreSQL row metadata for account information.
///
/// This type represents account metadata as stored in PostgreSQL tables,
/// providing a bridge between the framework's `AccountMetadata` and database
/// row structures. It includes essential account identification and timing
/// information.
///
/// # Database Schema
///
/// ```sql
/// CREATE TABLE accounts (
/// __pubkey BYTEA NOT NULL,
/// __slot BIGINT,
/// -- application-specific columns...
/// );
/// ```
///
/// # Fields
///
/// - **pubkey**: The account's public key, stored as `BYTEA` in PostgreSQL
/// - **slot**: The slot when the account was last updated (optional)
///
/// # Example
///
/// ```ignore
/// use carbon_core::postgres::metadata::AccountRowMetadata;
/// use carbon_core::account::AccountMetadata;
///
/// let account_metadata = AccountMetadata {
/// pubkey: solana_pubkey::Pubkey::new_unique(),
/// slot: 12345,
/// };
/// let row_metadata: AccountRowMetadata = account_metadata.into();
/// ```
/// PostgreSQL row metadata for instruction information.
///
/// This type represents instruction metadata as stored in PostgreSQL tables,
/// providing a bridge between the framework's `InstructionMetadata` and database
/// row structures. It includes transaction identification, instruction positioning,
/// and execution context information.
///
/// # Database Schema
///
/// ```sql
/// CREATE TABLE instructions (
/// __signature TEXT NOT NULL,
/// __instruction_index INTEGER NOT NULL,
/// __stack_height INTEGER NOT NULL,
/// __slot BIGINT,
/// -- application-specific columns...
/// );
/// ```
///
/// # Fields
///
/// - **signature**: The transaction signature that contains this instruction
/// - **index**: The zero-based index of this instruction within the transaction
/// - **stack_height**: The call stack height for nested instruction calls
/// - **slot**: The slot when the instruction was executed (optional)
///
/// # Example
///
/// ```ignore
/// use carbon_core::postgres::metadata::InstructionRowMetadata;
/// use carbon_core::instruction::InstructionMetadata;
///
/// let instruction_metadata = InstructionMetadata {
/// transaction_metadata: TransactionMetadata {
/// signature: solana_signature::Signature::new_unique(),
/// slot: 12345,
/// fee_payer: solana_pubkey::Pubkey::new_unique(),
/// },
/// index: 0,
/// stack_height: 0,
/// };
/// let row_metadata: InstructionRowMetadata = instruction_metadata.into();
/// ```