|
| 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', '3'])! |
| 91 | + assert response[0] == mysql.Row{ |
| 92 | + vals: ['3', 'bailey', ''] |
| 93 | + } |
| 94 | + |
| 95 | + response = db.exec_param_many('select * from users', [''])! |
| 96 | + assert response == [ |
| 97 | + mysql.Row{ |
| 98 | + vals: ['1', 'jackson', ''] |
| 99 | + }, |
| 100 | + mysql.Row{ |
| 101 | + vals: ['2', 'shannon', ''] |
| 102 | + }, |
| 103 | + mysql.Row{ |
| 104 | + vals: ['3', 'bailey', ''] |
| 105 | + }, |
| 106 | + mysql.Row{ |
| 107 | + vals: ['4', 'blaze', ''] |
| 108 | + }, |
| 109 | + mysql.Row{ |
| 110 | + vals: ['5', 'Hi', ''] |
| 111 | + }, |
| 112 | + ] |
| 113 | + |
| 114 | + response = db.exec_param('select * from users where username = ?', 'blaze')! |
| 115 | + assert response[0] == mysql.Row{ |
| 116 | + vals: ['4', 'blaze', ''] |
| 117 | + } |
| 118 | + |
| 119 | + // transaction test |
| 120 | + // turn off `autocommit` first |
| 121 | + db.autocommit(false)! |
| 122 | + // begin a new transaction |
| 123 | + db.begin()! |
| 124 | + result_code = db.exec_none('insert into users (username) values ("tom")') |
| 125 | + assert result_code == 0 |
| 126 | + // make a savepoint |
| 127 | + db.savepoint('savepoint1')! |
| 128 | + result_code = db.exec_none('insert into users (username) values ("kitty")') |
| 129 | + assert result_code == 0 |
| 130 | + // rollback to `savepoint1` |
| 131 | + db.rollback_to('savepoint1')! |
| 132 | + result_code = db.exec_none('insert into users (username) values ("mars")') |
| 133 | + assert result_code == 0 |
| 134 | + db.commit()! |
| 135 | + response = db.exec_param_many('select * from users', [''])! |
| 136 | + assert response == [ |
| 137 | + mysql.Row{ |
| 138 | + vals: ['1', 'jackson', ''] |
| 139 | + }, |
| 140 | + mysql.Row{ |
| 141 | + vals: ['2', 'shannon', ''] |
| 142 | + }, |
| 143 | + mysql.Row{ |
| 144 | + vals: ['3', 'bailey', ''] |
| 145 | + }, |
| 146 | + mysql.Row{ |
| 147 | + vals: ['4', 'blaze', ''] |
| 148 | + }, |
| 149 | + mysql.Row{ |
| 150 | + vals: ['5', 'Hi', ''] |
| 151 | + }, |
| 152 | + mysql.Row{ |
| 153 | + vals: ['6', 'tom', ''] |
| 154 | + }, |
| 155 | + mysql.Row{ |
| 156 | + vals: ['8', 'mars', ''] |
| 157 | + }, |
| 158 | + ] |
| 159 | +} |
0 commit comments