mockgres 0.0.25

An in-memory database that replicates a reasonable subset of Postgres functionality to make unit tests that rely on a database to run.
Documentation
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
use std::sync::Arc;
use std::sync::atomic::{AtomicI32, Ordering};
use std::time::Duration;

use dashmap::DashMap;
use parking_lot::{Mutex, MutexGuard, RwLock};
use time::OffsetDateTime;

use crate::catalog::{SchemaId, TableId};
use crate::db::Db;
use crate::storage::RowKey;
use crate::txn::TxId;

pub type SessionId = i32;

#[allow(dead_code)]
#[derive(Clone, Debug)]
pub struct RowPointer {
    pub table_id: TableId,
    pub key: RowKey,
}

#[allow(dead_code)]
#[derive(Clone, Debug, Default)]
pub struct TxnChanges {
    pub inserted: Vec<RowPointer>,
    pub updated_old: Vec<RowPointer>,
}

#[derive(Clone, Debug, Default)]
pub enum SessionTimeZone {
    #[default]
    Utc,
    FixedOffset {
        seconds: i32,
        display: String,
    },
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TransactionIsolation {
    ReadCommitted,
}

impl TransactionIsolation {
    pub fn parse(input: &str) -> Result<Self, String> {
        let normalized = input.trim().to_ascii_lowercase().replace(['_', '-'], " ");
        match normalized.as_str() {
            "read committed" => Ok(TransactionIsolation::ReadCommitted),
            other => Err(format!("isolation level {other} not supported")),
        }
    }

    pub fn as_str(&self) -> &'static str {
        match self {
            TransactionIsolation::ReadCommitted => "read committed",
        }
    }
}

impl SessionTimeZone {
    pub fn parse(input: &str) -> Result<Self, String> {
        let trimmed = input.trim();
        if trimmed.eq_ignore_ascii_case("utc") || trimmed.eq_ignore_ascii_case("z") {
            return Ok(SessionTimeZone::Utc);
        }
        let mut chars = trimmed.chars();
        let Some(sign_char) = chars.next() else {
            return Err("invalid time zone value".to_string());
        };
        if sign_char != '+' && sign_char != '-' {
            return Err("invalid time zone offset".to_string());
        }
        let sign = if sign_char == '+' { 1 } else { -1 };
        let rest = chars.as_str();
        let (hour_part, minute_part) = if let Some(colon_idx) = rest.find(':') {
            (&rest[..colon_idx], Some(&rest[colon_idx + 1..]))
        } else {
            (rest, None)
        };
        if hour_part.is_empty() {
            return Err("invalid time zone hour".to_string());
        }
        let hours: i32 = hour_part
            .parse()
            .map_err(|_| "invalid time zone hour".to_string())?;
        if hours.abs() > 15 {
            return Err("time zone hour out of range".to_string());
        }
        let minutes: i32 = match minute_part {
            Some(part) if !part.is_empty() => part
                .parse()
                .map_err(|_| "invalid time zone minute".to_string())?,
            Some(_) => return Err("invalid time zone minute".to_string()),
            None => 0,
        };
        if !(0..60).contains(&minutes) {
            return Err("time zone minute out of range".to_string());
        }
        if hours == 15 && minutes > 0 {
            return Err("time zone offset out of range".to_string());
        }
        let seconds = sign * (hours * 3600 + minutes * 60);
        let display = format!(
            "{}{:02}:{:02}",
            if sign >= 0 { '+' } else { '-' },
            hours.abs(),
            minutes.abs()
        );
        Ok(SessionTimeZone::FixedOffset { seconds, display })
    }

    pub fn offset_seconds(&self) -> i32 {
        match self {
            SessionTimeZone::Utc => 0,
            SessionTimeZone::FixedOffset { seconds, .. } => *seconds,
        }
    }

    pub fn display_value(&self) -> &str {
        match self {
            SessionTimeZone::Utc => "UTC",
            SessionTimeZone::FixedOffset { display, .. } => display.as_str(),
        }
    }

    pub fn offset_string(&self) -> String {
        match self {
            SessionTimeZone::Utc => "+00:00".to_string(),
            SessionTimeZone::FixedOffset { display, .. } => display.clone(),
        }
    }
}

#[derive(Debug)]
pub struct SessionState {
    pub current_tx: Option<TxId>,
    pub statement_xid: Option<TxId>,
    #[allow(dead_code)]
    pub changes: TxnChanges,
    pub next_epoch: u64,
    pub txn_epoch: Option<u64>,
    pub statement_epoch: Option<u64>,
    pub search_path: Vec<SchemaId>,
    pub current_database: Option<String>,
    pub statement_time_micros: Option<i64>,
    pub txn_start_micros: Option<i64>,
    pub time_zone: SessionTimeZone,
    pub db_override: Option<Arc<RwLock<Db>>>,
    pub default_txn_isolation: TransactionIsolation,
    pub txn_isolation: Option<TransactionIsolation>,
    pub lock_timeout: Option<Duration>,
}

impl Default for SessionState {
    fn default() -> Self {
        Self {
            current_tx: None,
            statement_xid: None,
            changes: TxnChanges::default(),
            next_epoch: 1,
            txn_epoch: None,
            statement_epoch: None,
            search_path: Vec::new(),
            current_database: None,
            statement_time_micros: None,
            txn_start_micros: None,
            time_zone: SessionTimeZone::default(),
            db_override: None,
            default_txn_isolation: TransactionIsolation::ReadCommitted,
            txn_isolation: None,
            lock_timeout: None,
        }
    }
}

#[derive(Debug)]
pub struct Session {
    id: SessionId,
    state: Mutex<SessionState>,
}

impl Session {
    pub fn new(id: SessionId) -> Self {
        Self {
            id,
            state: Mutex::new(SessionState::default()),
        }
    }

    pub fn id(&self) -> SessionId {
        self.id
    }

    #[allow(dead_code)]
    pub fn state(&self) -> MutexGuard<'_, SessionState> {
        self.state.lock()
    }

    pub fn set_statement_xid(&self, xid: TxId) {
        let mut guard = self.state.lock();
        guard.statement_xid = Some(xid);
    }

    #[allow(dead_code)]
    pub fn statement_xid(&self) -> Option<TxId> {
        self.state.lock().statement_xid
    }

    pub fn current_tx(&self) -> Option<TxId> {
        self.state.lock().current_tx
    }

    pub fn set_current_tx(&self, tx: Option<TxId>) {
        let mut guard = self.state.lock();
        guard.current_tx = tx;
    }

    pub fn reset_changes(&self) {
        let mut guard = self.state.lock();
        guard.changes = TxnChanges::default();
    }

    pub fn record_inserts(&self, mut ptrs: Vec<RowPointer>) {
        if ptrs.is_empty() {
            return;
        }
        let mut guard = self.state.lock();
        guard.changes.inserted.append(&mut ptrs);
    }

    pub fn record_touched(&self, mut ptrs: Vec<RowPointer>) {
        if ptrs.is_empty() {
            return;
        }
        let mut guard = self.state.lock();
        guard.changes.updated_old.append(&mut ptrs);
    }

    pub fn take_changes(&self) -> TxnChanges {
        let mut guard = self.state.lock();
        std::mem::take(&mut guard.changes)
    }

    pub fn enter_statement(&self) -> bool {
        let mut guard = self.state.lock();
        if guard.txn_epoch.is_some() {
            return false;
        }
        let epoch = guard.next_epoch;
        guard.next_epoch += 1;
        guard.statement_epoch = Some(epoch);
        true
    }

    pub fn exit_statement(&self) -> Option<u64> {
        let mut guard = self.state.lock();
        guard.statement_time_micros = None;
        guard.statement_epoch.take()
    }

    pub fn current_epoch(&self) -> Option<u64> {
        let guard = self.state.lock();
        guard.txn_epoch.or(guard.statement_epoch)
    }

    pub fn begin_transaction_epoch(&self) -> u64 {
        let mut guard = self.state.lock();
        let epoch = guard.next_epoch;
        guard.next_epoch += 1;
        guard.txn_epoch = Some(epoch);
        epoch
    }

    pub fn end_transaction_epoch(&self) -> Option<u64> {
        let mut guard = self.state.lock();
        guard.txn_epoch.take()
    }

    pub fn search_path(&self) -> Vec<SchemaId> {
        self.state.lock().search_path.clone()
    }

    pub fn set_search_path(&self, path: Vec<SchemaId>) {
        let mut guard = self.state.lock();
        guard.search_path = path;
    }

    pub fn set_database_name(&self, name: String) {
        let mut guard = self.state.lock();
        guard.current_database = Some(name);
    }

    pub fn database_name(&self) -> Option<String> {
        self.state.lock().current_database.clone()
    }

    pub fn set_time_zone(&self, tz: SessionTimeZone) {
        let mut guard = self.state.lock();
        guard.time_zone = tz;
    }

    pub fn time_zone(&self) -> SessionTimeZone {
        self.state.lock().time_zone.clone()
    }

    pub fn set_default_txn_isolation(&self, iso: TransactionIsolation) {
        let mut guard = self.state.lock();
        guard.default_txn_isolation = iso;
        if guard.txn_isolation.is_none() {
            guard.txn_isolation = Some(iso);
        }
    }

    pub fn default_txn_isolation(&self) -> TransactionIsolation {
        self.state.lock().default_txn_isolation
    }

    pub fn set_txn_isolation(&self, iso: TransactionIsolation) {
        let mut guard = self.state.lock();
        guard.txn_isolation = Some(iso);
    }

    pub fn clear_txn_isolation(&self) {
        let mut guard = self.state.lock();
        guard.txn_isolation = None;
    }

    pub fn txn_isolation(&self) -> Option<TransactionIsolation> {
        self.state.lock().txn_isolation
    }

    pub fn set_lock_timeout(&self, timeout: Option<Duration>) {
        let mut guard = self.state.lock();
        guard.lock_timeout = timeout;
    }

    pub fn lock_timeout(&self) -> Option<Duration> {
        self.state.lock().lock_timeout
    }

    pub fn set_statement_time_micros(&self, micros: i64) {
        let mut guard = self.state.lock();
        guard.statement_time_micros = Some(micros);
    }

    pub fn statement_time_micros(&self) -> Option<i64> {
        self.state.lock().statement_time_micros
    }

    pub fn set_txn_start_micros(&self, micros: i64) {
        let mut guard = self.state.lock();
        guard.txn_start_micros = Some(micros);
    }

    pub fn txn_start_micros(&self) -> Option<i64> {
        self.state.lock().txn_start_micros
    }

    pub fn clear_txn_start_micros(&self) {
        let mut guard = self.state.lock();
        guard.txn_start_micros = None;
    }

    pub fn set_db_override(&self, db: Option<Arc<RwLock<Db>>>) {
        let mut guard = self.state.lock();
        guard.db_override = db;
    }

    pub fn db_override(&self) -> Option<Arc<RwLock<Db>>> {
        self.state.lock().db_override.clone()
    }
}

#[derive(Debug, Default)]
pub struct SessionManager {
    next_id: AtomicI32,
    sessions: DashMap<SessionId, Arc<Session>>,
}

impl SessionManager {
    pub fn new() -> Self {
        Self {
            next_id: AtomicI32::new(1),
            sessions: DashMap::new(),
        }
    }

    pub fn create_session(&self) -> Arc<Session> {
        let id = self.next_id.fetch_add(1, Ordering::SeqCst);
        let session = Arc::new(Session::new(id));
        self.sessions.insert(id, session.clone());
        session
    }

    pub fn get(&self, id: SessionId) -> Option<Arc<Session>> {
        self.sessions.get(&id).map(|entry| entry.clone())
    }

    #[allow(dead_code)]
    pub fn remove(&self, id: SessionId) {
        self.sessions.remove(&id);
    }
}

pub fn now_utc_micros() -> i64 {
    let now = OffsetDateTime::now_utc();
    (now.unix_timestamp_nanos() / 1_000) as i64
}