| 1 | // vtest build: !(macos || windows) |
| 2 | import db.mysql |
| 3 | import pool |
| 4 | import time |
| 5 | |
| 6 | // Define your connection factory function |
| 7 | fn create_conn() !&pool.ConnectionPoolable { |
| 8 | config := mysql.Config{ |
| 9 | host: '127.0.0.1' |
| 10 | port: 3306 |
| 11 | username: 'root' |
| 12 | password: '12345678' |
| 13 | dbname: 'mysql' |
| 14 | } |
| 15 | db := mysql.connect(config)! |
| 16 | return &db |
| 17 | } |
| 18 | |
| 19 | fn main() { |
| 20 | // Configure pool parameters |
| 21 | config := pool.ConnectionPoolConfig{ |
| 22 | max_conns: 50 |
| 23 | min_idle_conns: 5 |
| 24 | max_lifetime: 2 * time.hour |
| 25 | idle_timeout: 30 * time.minute |
| 26 | get_timeout: 5 * time.second |
| 27 | } |
| 28 | |
| 29 | // Create connection pool |
| 30 | mut my_pool := pool.new_connection_pool(create_conn, config)! |
| 31 | defer { |
| 32 | // When application exits |
| 33 | my_pool.close() |
| 34 | } |
| 35 | |
| 36 | // Acquire connection |
| 37 | mut conn := my_pool.get()! |
| 38 | defer { |
| 39 | // Return connection to pool |
| 40 | my_pool.put(conn) or { println(err) } |
| 41 | } |
| 42 | |
| 43 | // Convert `conn` to a `mysql.DB` object |
| 44 | mut db := conn as mysql.DB |
| 45 | |
| 46 | assert db.validate()! |
| 47 | |
| 48 | mut response := db.exec('drop table if exists users')! |
| 49 | assert response == []mysql.Row{} |
| 50 | |
| 51 | response = db.exec('create table if not exists users ( |
| 52 | id INT PRIMARY KEY AUTO_INCREMENT, |
| 53 | username TEXT, |
| 54 | last_name TEXT NULL DEFAULT NULL |
| 55 | )')! |
| 56 | assert response == []mysql.Row{} |
| 57 | |
| 58 | mut result_code := db.exec_none('insert into users (username) values ("jackson")') |
| 59 | assert result_code == 0 |
| 60 | result_code = db.exec_none('insert into users (username) values ("shannon")') |
| 61 | assert result_code == 0 |
| 62 | result_code = db.exec_none('insert into users (username) values ("bailey")') |
| 63 | assert result_code == 0 |
| 64 | result_code = db.exec_none('insert into users (username) values ("blaze")') |
| 65 | assert result_code == 0 |
| 66 | rows := db.exec_param('insert into users (username) values (?)', 'Hi')! |
| 67 | assert rows == []mysql.Row{} |
| 68 | |
| 69 | // Regression testing to ensure the query and exec return the same values |
| 70 | res := db.query('select * from users')! |
| 71 | response = res.rows() |
| 72 | assert response[0].vals[1] == 'jackson' |
| 73 | response = db.exec('select * from users')! |
| 74 | assert response[0].vals[1] == 'jackson' |
| 75 | |
| 76 | response = db.exec('select * from users where id = 400')! |
| 77 | assert response.len == 0 |
| 78 | |
| 79 | single_row := db.exec_one('select * from users')! |
| 80 | assert single_row.vals[1] == 'jackson' |
| 81 | |
| 82 | response = db.exec_param_many('select * from users where username = ?', [ |
| 83 | 'jackson', |
| 84 | ])! |
| 85 | assert response[0] == mysql.Row{ |
| 86 | vals: ['1', 'jackson', ''] |
| 87 | } |
| 88 | |
| 89 | response = db.exec_param_many('select * from users where username = ? and id = ?', [ |
| 90 | 'bailey', |
| 91 | '3', |
| 92 | ])! |
| 93 | assert response[0] == mysql.Row{ |
| 94 | vals: ['3', 'bailey', ''] |
| 95 | } |
| 96 | |
| 97 | response = db.exec_param_many('select * from users', [''])! |
| 98 | assert response == [ |
| 99 | mysql.Row{ |
| 100 | vals: ['1', 'jackson', ''] |
| 101 | }, |
| 102 | mysql.Row{ |
| 103 | vals: ['2', 'shannon', ''] |
| 104 | }, |
| 105 | mysql.Row{ |
| 106 | vals: ['3', 'bailey', ''] |
| 107 | }, |
| 108 | mysql.Row{ |
| 109 | vals: ['4', 'blaze', ''] |
| 110 | }, |
| 111 | mysql.Row{ |
| 112 | vals: ['5', 'Hi', ''] |
| 113 | }, |
| 114 | ] |
| 115 | |
| 116 | response = db.exec_param('select * from users where username = ?', 'blaze')! |
| 117 | assert response[0] == mysql.Row{ |
| 118 | vals: ['4', 'blaze', ''] |
| 119 | } |
| 120 | |
| 121 | // transaction test |
| 122 | // turn off `autocommit` first |
| 123 | db.autocommit(false)! |
| 124 | // begin a new transaction |
| 125 | db.begin()! |
| 126 | result_code = db.exec_none('insert into users (username) values ("tom")') |
| 127 | assert result_code == 0 |
| 128 | // make a savepoint |
| 129 | db.savepoint('savepoint1')! |
| 130 | result_code = db.exec_none('insert into users (username) values ("kitty")') |
| 131 | assert result_code == 0 |
| 132 | // rollback to `savepoint1` |
| 133 | db.rollback_to('savepoint1')! |
| 134 | result_code = db.exec_none('insert into users (username) values ("mars")') |
| 135 | assert result_code == 0 |
| 136 | db.commit()! |
| 137 | response = db.exec_param_many('select * from users', [''])! |
| 138 | assert response == [ |
| 139 | mysql.Row{ |
| 140 | vals: ['1', 'jackson', ''] |
| 141 | }, |
| 142 | mysql.Row{ |
| 143 | vals: ['2', 'shannon', ''] |
| 144 | }, |
| 145 | mysql.Row{ |
| 146 | vals: ['3', 'bailey', ''] |
| 147 | }, |
| 148 | mysql.Row{ |
| 149 | vals: ['4', 'blaze', ''] |
| 150 | }, |
| 151 | mysql.Row{ |
| 152 | vals: ['5', 'Hi', ''] |
| 153 | }, |
| 154 | mysql.Row{ |
| 155 | vals: ['6', 'tom', ''] |
| 156 | }, |
| 157 | mysql.Row{ |
| 158 | vals: ['8', 'mars', ''] |
| 159 | }, |
| 160 | ] |
| 161 | } |
| 162 | |