| 1 | // vtest build: present_sqlite3? && !sanitize-memory-clang |
| 2 | import db.sqlite |
| 3 | |
| 4 | pub enum MessageStatus as u8 { |
| 5 | ready |
| 6 | } |
| 7 | |
| 8 | pub struct Message[T] { |
| 9 | message_id int @[primary; sql: serial] |
| 10 | status MessageStatus |
| 11 | } |
| 12 | |
| 13 | pub struct Queue[T] { |
| 14 | conn &sqlite.DB |
| 15 | } |
| 16 | |
| 17 | pub struct Queue_config { |
| 18 | path string |
| 19 | db ?sqlite.DB @[omitempty] |
| 20 | } |
| 21 | |
| 22 | pub fn new[T](config Queue_config) !Queue[T] { |
| 23 | mut conn := if db := config.db { |
| 24 | db |
| 25 | } else { |
| 26 | sqlite.connect(config.path) or { return error('Failed to connect to database: ${err}') } |
| 27 | } |
| 28 | mut queue := Queue[T]{ |
| 29 | conn: &conn |
| 30 | } |
| 31 | return queue |
| 32 | } |
| 33 | |
| 34 | pub fn (mut self Queue[T]) take() !Message[T] { |
| 35 | messages := sql self.conn { |
| 36 | select from Message[T] where status == MessageStatus.ready order by message_id limit 1 |
| 37 | } or { return error('') } |
| 38 | message := messages.first() |
| 39 | return message |
| 40 | } |
| 41 | |
| 42 | struct Payload {} |
| 43 | |
| 44 | fn test_main() { |
| 45 | _ := new[Payload](path: ':memory:')! |
| 46 | } |
| 47 | |