@@ -1617,4 +1617,96 @@ describe('test/client.test.ts', () => {
16171617 } ) ;
16181618
16191619 } ) ;
1620+
1621+ describe ( 'execute()' , ( ) => {
1622+ it ( 'should execute sql with parameters' , async ( ) => {
1623+ const result = await db . execute ( 'INSERT INTO `myrds-test-user` (name, email, gmt_create, gmt_modified) VALUES(?, ?, now(), now())' ,
1624+ [ prefix + 'execute-test' , prefix + 'm@execute-test.com' ] ) ;
1625+ assert . equal ( result . affectedRows , 1 ) ;
1626+ assert ( result . insertId > 0 ) ;
1627+ } ) ;
1628+
1629+ it ( 'should execute select query' , async ( ) => {
1630+ await db . execute ( 'INSERT INTO `myrds-test-user` (name, email, gmt_create, gmt_modified) VALUES(?, ?, now(), now())' ,
1631+ [ prefix + 'execute-select-test' , prefix + 'm@execute-select-test.com' ] ) ;
1632+
1633+ const rows = await db . execute ( 'SELECT * FROM `myrds-test-user` WHERE email = ?' ,
1634+ [ prefix + 'm@execute-select-test.com' ] ) ;
1635+ assert ( Array . isArray ( rows ) ) ;
1636+ assert . equal ( rows . length , 1 ) ;
1637+ assert . equal ( rows [ 0 ] . name , prefix + 'execute-select-test' ) ;
1638+ } ) ;
1639+
1640+ it ( 'should execute in transaction' , async ( ) => {
1641+ const tran = await db . beginTransaction ( ) ;
1642+ try {
1643+ const result = await db . execute ( 'INSERT INTO `myrds-test-user` (name, email, gmt_create, gmt_modified) VALUES(?, ?, now(), now())' ,
1644+ [ prefix + 'execute-transaction-test' , prefix + 'm@execute-transaction-test.com' ] ,
1645+ { conn : tran } ) ;
1646+ assert . equal ( result . affectedRows , 1 ) ;
1647+ await tran . commit ( ) ;
1648+ } catch ( err ) {
1649+ await tran . rollback ( ) ;
1650+ throw err ;
1651+ }
1652+
1653+ const rows = await db . execute ( 'SELECT * FROM `myrds-test-user` WHERE email = ?' ,
1654+ [ prefix + 'm@execute-transaction-test.com' ] ) ;
1655+ assert . equal ( rows . length , 1 ) ;
1656+ } ) ;
1657+
1658+ it ( 'should execute in transaction scope' , async ( ) => {
1659+ await db . beginTransactionScope ( async ( tran ) => {
1660+ const result = await db . execute ( 'INSERT INTO `myrds-test-user` (name, email, gmt_create, gmt_modified) VALUES(?, ?, now(), now())' ,
1661+ [ prefix + 'execute-scope-test' , prefix + 'm@execute-scope-test.com' ] ,
1662+ { conn : tran } ) ;
1663+ assert . equal ( result . affectedRows , 1 ) ;
1664+ } ) ;
1665+
1666+ const rows = await db . execute ( 'SELECT * FROM `myrds-test-user` WHERE email = ?' ,
1667+ [ prefix + 'm@execute-scope-test.com' ] ) ;
1668+ assert . equal ( rows . length , 1 ) ;
1669+ } ) ;
1670+
1671+ it ( 'should execute with connection' , async ( ) => {
1672+ const conn = await db . getConnection ( ) ;
1673+ try {
1674+ const result = await conn . execute ( 'INSERT INTO `myrds-test-user` (name, email, gmt_create, gmt_modified) VALUES(?, ?, now(), now())' ,
1675+ [ prefix + 'execute-conn-test' , prefix + 'm@execute-conn-test.com' ] ) ;
1676+ assert . equal ( result . affectedRows , 1 ) ;
1677+ } finally {
1678+ conn . release ( ) ;
1679+ }
1680+
1681+ const rows = await db . execute ( 'SELECT * FROM `myrds-test-user` WHERE email = ?' ,
1682+ [ prefix + 'm@execute-conn-test.com' ] ) ;
1683+ assert . equal ( rows . length , 1 ) ;
1684+ } ) ;
1685+
1686+ it ( 'should execute update query' , async ( ) => {
1687+ await db . execute ( 'INSERT INTO `myrds-test-user` (name, email, gmt_create, gmt_modified) VALUES(?, ?, now(), now())' ,
1688+ [ prefix + 'execute-update-test' , prefix + 'm@execute-update-test.com' ] ) ;
1689+
1690+ const result = await db . execute ( 'UPDATE `myrds-test-user` SET email = ? WHERE name = ?' ,
1691+ [ prefix + 'm@execute-updated.com' , prefix + 'execute-update-test' ] ) ;
1692+ assert . equal ( result . affectedRows , 1 ) ;
1693+
1694+ const rows = await db . execute ( 'SELECT * FROM `myrds-test-user` WHERE email = ?' ,
1695+ [ prefix + 'm@execute-updated.com' ] ) ;
1696+ assert . equal ( rows . length , 1 ) ;
1697+ } ) ;
1698+
1699+ it ( 'should execute delete query' , async ( ) => {
1700+ await db . execute ( 'INSERT INTO `myrds-test-user` (name, email, gmt_create, gmt_modified) VALUES(?, ?, now(), now())' ,
1701+ [ prefix + 'execute-delete-test' , prefix + 'm@execute-delete-test.com' ] ) ;
1702+
1703+ const result = await db . execute ( 'DELETE FROM `myrds-test-user` WHERE name = ?' ,
1704+ [ prefix + 'execute-delete-test' ] ) ;
1705+ assert . equal ( result . affectedRows , 1 ) ;
1706+
1707+ const rows = await db . execute ( 'SELECT * FROM `myrds-test-user` WHERE name = ?' ,
1708+ [ prefix + 'execute-delete-test' ] ) ;
1709+ assert . equal ( rows . length , 0 ) ;
1710+ } ) ;
1711+ } ) ;
16201712} ) ;
0 commit comments