| 1 | import type { DbEngineAdapter, DbHostConn } from "./index"; |
| 2 | |
| 3 | function quoteIdent(name: |
| 4 | return `\`${name |
| 5 | } |
| 6 | |
| 7 | function connectionConfig(conn: |
| 8 | return { |
| 9 | host: conn.host |
| 10 | port: conn.port |
| 11 | user: conn.username |
| 12 | password: conn.password |
| 13 | ssl: conn.ssl |
| 14 | }; |
| 15 | } |
| 16 | |
| 17 | export const mysqlAdapter: |
| 18 | defaultPort: 3306, |
| 19 | |
| 20 | async testConnection(conn |
| 21 | try { |
| 22 | const mysql = |
| 23 | const connection = |
| 24 | await connection.ping |
| 25 | await connection.end |
| 26 | return { ok: |
| 27 | } catch (err) |
| 28 | return { ok: |
| 29 | } |
| 30 | }, |
| 31 | |
| 32 | async createDatabase(conn |
| 33 | const mysql = |
| 34 | const connection = |
| 35 | const remote = |
| 36 | // CREATE/ALTER/DROP USER don't accept bind parameters ($1-style `?`) for the |
| 37 | // user/host/password literals, so they're inlined via the driver's own |
| 38 | // escaping (connection.escape quotes + escapes) instead. Identifiers (the |
| 39 | // database name) use backtick-quoting with doubled internal backticks. |
| 40 | try { |
| 41 | await connection.execute |
| 42 | await connection.execute |
| 43 | `CREATE USER IF NOT EXISTS ${connection.escape |
| 44 | ); |
| 45 | await connection.execute |
| 46 | `GRANT ALL PRIVILEGES ON ${quoteIdent(opts |
| 47 | ); |
| 48 | await connection.execute |
| 49 | } finally { |
| 50 | await connection.end |
| 51 | } |
| 52 | }, |
| 53 | |
| 54 | async rotatePassword(conn |
| 55 | const mysql = |
| 56 | const connection = |
| 57 | const remote = |
| 58 | try { |
| 59 | await connection.execute |
| 60 | `ALTER USER ${connection.escape |
| 61 | ); |
| 62 | await connection.execute |
| 63 | } finally { |
| 64 | await connection.end |
| 65 | } |
| 66 | }, |
| 67 | |
| 68 | async dropDatabase(conn |
| 69 | const mysql = |
| 70 | const connection = |
| 71 | const remote = |
| 72 | try { |
| 73 | await connection.execute |
| 74 | await connection.execute |
| 75 | await connection.execute |
| 76 | } finally { |
| 77 | await connection.end |
| 78 | } |
| 79 | }, |
| 80 | |
| 81 | formatConnectionString(opts) { |
| 82 | return `mysql://${encodeURIComponent |
| 83 | }, |
| 84 | }; |