@@ -1574,6 +1574,124 @@ func TestEscapeBytes(t *testing.T) {
15741574 }
15751575}
15761576
1577+ func TestQuoteIdentifier (t * testing.T ) {
1578+ c := qt .New (t )
1579+
1580+ c .Assert (quoteIdentifier ("simple" ), qt .Equals , "`simple`" )
1581+ c .Assert (quoteIdentifier ("customer_report`$probe" ), qt .Equals , "`customer_report``$probe`" )
1582+ c .Assert (quoteIdentifier ("display`name" ), qt .Equals , "`display``name`" )
1583+ }
1584+
1585+ func TestQuoteStringLiteral (t * testing.T ) {
1586+ c := qt .New (t )
1587+
1588+ c .Assert (quoteStringLiteral ("simple" ), qt .Equals , "'simple'" )
1589+ c .Assert (quoteStringLiteral ("test'db" ), qt .Equals , "'test''db'" )
1590+ }
1591+
1592+ func TestDumperEscapesDiscoveredIdentifiers (t * testing.T ) {
1593+ c := qt .New (t )
1594+
1595+ log := xlog .NewStdLog (xlog .Level (xlog .INFO ))
1596+ fakedbs := driver .NewTestHandler (log )
1597+ server , err := driver .MockMysqlServer (log , fakedbs )
1598+ c .Assert (err , qt .IsNil )
1599+ c .Cleanup (func () { server .Close () })
1600+
1601+ address := server .Addr ()
1602+ database := "test`db"
1603+ table := "customer_report`$probe"
1604+ column := "display`name"
1605+
1606+ selectResult := & sqltypes.Result {
1607+ Fields : []* querypb.Field {
1608+ {Name : column , Type : querypb .Type_VARCHAR },
1609+ },
1610+ Rows : [][]sqltypes.Value {
1611+ {sqltypes .MakeTrusted (querypb .Type_VARCHAR , []byte ("ok" ))},
1612+ },
1613+ }
1614+
1615+ schemaResult := & sqltypes.Result {
1616+ Fields : []* querypb.Field {
1617+ {Name : "Table" , Type : querypb .Type_VARCHAR },
1618+ {Name : "Create Table" , Type : querypb .Type_VARCHAR },
1619+ },
1620+ Rows : [][]sqltypes.Value {
1621+ {
1622+ sqltypes .MakeTrusted (querypb .Type_VARCHAR , []byte (table )),
1623+ sqltypes .MakeTrusted (querypb .Type_VARCHAR , []byte ("CREATE TABLE `customer_report``$probe` (`display``name` varchar(255)) ENGINE=InnoDB" )),
1624+ },
1625+ },
1626+ }
1627+
1628+ tablesResult := & sqltypes.Result {
1629+ Fields : []* querypb.Field {
1630+ {Name : "Tables_in_test`db" , Type : querypb .Type_VARCHAR },
1631+ },
1632+ Rows : [][]sqltypes.Value {
1633+ {sqltypes .MakeTrusted (querypb .Type_VARCHAR , []byte (table ))},
1634+ },
1635+ }
1636+
1637+ viewsResult := & sqltypes.Result {
1638+ Fields : []* querypb.Field {
1639+ {Name : "TABLE_NAME" , Type : querypb .Type_VARCHAR },
1640+ },
1641+ Rows : [][]sqltypes.Value {},
1642+ }
1643+
1644+ fieldsResult := & sqltypes.Result {
1645+ Fields : []* querypb.Field {
1646+ {Name : "Field" , Type : querypb .Type_VARCHAR },
1647+ {Name : "Type" , Type : querypb .Type_VARCHAR },
1648+ {Name : "Null" , Type : querypb .Type_VARCHAR },
1649+ {Name : "Key" , Type : querypb .Type_VARCHAR },
1650+ {Name : "Default" , Type : querypb .Type_VARCHAR },
1651+ {Name : "Extra" , Type : querypb .Type_VARCHAR },
1652+ },
1653+ Rows : [][]sqltypes.Value {
1654+ testRow (column , "" ),
1655+ },
1656+ }
1657+
1658+ fakedbs .AddQueryPattern ("use .*" , & sqltypes.Result {})
1659+ fakedbs .AddQueryPattern ("show create table .*" , schemaResult )
1660+ fakedbs .AddQueryPattern ("show tables from .*" , tablesResult )
1661+ fakedbs .AddQueryPattern ("select table_name .* from information_schema.tables .*" , viewsResult )
1662+ fakedbs .AddQueryPattern ("show fields from .*" , fieldsResult )
1663+ fakedbs .AddQueryPattern ("select .* from .*" , selectResult )
1664+ fakedbs .AddQueryPattern ("set .*" , & sqltypes.Result {})
1665+
1666+ cfg := & Config {
1667+ Database : database ,
1668+ Outdir : c .TempDir (),
1669+ User : "mock" ,
1670+ Password : "mock" ,
1671+ Address : address ,
1672+ ChunksizeInMB : 1 ,
1673+ Threads : 16 ,
1674+ StmtSize : 10000 ,
1675+ IntervalMs : 500 ,
1676+ SessionVars : []string {"SET @@radon_streaming_fetch='ON'" },
1677+ }
1678+
1679+ d , err := NewDumper (cfg )
1680+ c .Assert (err , qt .IsNil )
1681+
1682+ err = d .Run (context .Background ())
1683+ c .Assert (err , qt .IsNil )
1684+
1685+ c .Assert (fakedbs .GetQueryCalledNum ("SHOW TABLES FROM `test``db`" ), qt .Equals , 1 )
1686+ c .Assert (fakedbs .GetQueryCalledNum ("SHOW CREATE TABLE `test``db`.`customer_report``$probe`" ), qt .Equals , 1 )
1687+ c .Assert (fakedbs .GetQueryCalledNum ("SHOW FIELDS FROM `customer_report``$probe`" ), qt .Equals , 1 )
1688+ c .Assert (fakedbs .GetQueryCalledNum ("SELECT `display``name` FROM `test``db`.`customer_report``$probe` " ), qt .Equals , 1 )
1689+
1690+ dat , err := os .ReadFile (cfg .Outdir + "/" + database + "." + table + ".00001.sql" )
1691+ c .Assert (err , qt .IsNil )
1692+ c .Assert (string (dat ), qt .Contains , "INSERT INTO `customer_report``$probe`(`display``name`) VALUES" )
1693+ }
1694+
15771695func TestDumperColumnIncludes (t * testing.T ) {
15781696 c := qt .New (t )
15791697
0 commit comments