builder_limit_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2018 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package builder
  5. /*
  6. func TestBuilder_Limit4Mssql(t *testing.T) {
  7. sqlFromFile, err := readPreparationSQLFromFile("testdata/mssql_fiddle_data.sql")
  8. assert.NoError(t, err)
  9. f, err := newFiddler("", MSSQL, sqlFromFile)
  10. assert.NoError(t, err)
  11. assert.NotEmpty(t, f.sessionCode)
  12. // simple -- MsSQL style
  13. sql, err := Dialect(MSSQL).Select("a", "b", "c").From("table1").
  14. OrderBy("a ASC").Limit(5).ToBoundSQL()
  15. assert.NoError(t, err)
  16. assert.EqualValues(t, "SELECT a,b,c FROM (SELECT TOP 5 a,b,c,ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS RN FROM table1 ORDER BY a ASC) at", sql)
  17. assert.NoError(t, f.executableCheck(sql))
  18. // simple with where -- MsSQL style
  19. sql, err = Dialect(MSSQL).Select("a", "b", "c").From("table1").
  20. Where(Neq{"a": "3"}).OrderBy("a ASC").Limit(5, 10).ToBoundSQL()
  21. assert.NoError(t, err)
  22. assert.EqualValues(t, "SELECT a,b,c FROM (SELECT TOP 15 a,b,c,ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS RN FROM table1 WHERE a<>'3' ORDER BY a ASC) at WHERE at.RN>10", sql)
  23. assert.NoError(t, f.executableCheck(sql))
  24. // union with limit -- MsSQL style
  25. sql, err = Dialect(MSSQL).Select("a", "b", "c").From(
  26. Dialect(MSSQL).Select("a", "b", "c").From("table1").Where(Neq{"a": "1"}).
  27. OrderBy("a ASC").Limit(5, 6).Union("ALL",
  28. Select("a", "b", "c").From("table1").Where(Neq{"b": "2"}).OrderBy("a DESC").Limit(10)), "at").
  29. OrderBy("b DESC").Limit(7, 9).ToBoundSQL()
  30. assert.NoError(t, err)
  31. assert.EqualValues(t, "SELECT a,b,c FROM (SELECT TOP 16 a,b,c,ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS RN FROM ((SELECT a,b,c FROM (SELECT TOP 11 a,b,c,ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS RN FROM table1 WHERE a<>'1' ORDER BY a ASC) at WHERE at.RN>6) UNION ALL (SELECT a,b,c FROM (SELECT TOP 10 a,b,c,ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS RN FROM table1 WHERE b<>'2' ORDER BY a DESC) at)) at ORDER BY b DESC) at WHERE at.RN>9", sql)
  32. assert.NoError(t, f.executableCheck(sql))
  33. }
  34. func TestBuilder_Limit4MysqlLike(t *testing.T) {
  35. sqlFromFile, err := readPreparationSQLFromFile("testdata/mysql_fiddle_data.sql")
  36. assert.NoError(t, err)
  37. f, err := newFiddler("", MYSQL, sqlFromFile)
  38. assert.NoError(t, err)
  39. assert.NotEmpty(t, f.sessionCode)
  40. // simple -- MySQL/SQLite/PostgreSQL style
  41. sql, err := Dialect(MYSQL).Select("a", "b", "c").From("table1").OrderBy("a ASC").
  42. Limit(5, 10).ToBoundSQL()
  43. assert.NoError(t, err)
  44. assert.EqualValues(t, "SELECT a,b,c FROM table1 ORDER BY a ASC LIMIT 5 OFFSET 10", sql)
  45. assert.NoError(t, f.executableCheck(sql))
  46. // simple -- MySQL/SQLite/PostgreSQL style
  47. sql, err = Dialect(MYSQL).Select("a", "b", "c").From("table1").
  48. OrderBy("a ASC").Limit(5).ToBoundSQL()
  49. assert.NoError(t, err)
  50. assert.EqualValues(t, "SELECT a,b,c FROM table1 ORDER BY a ASC LIMIT 5", sql)
  51. assert.NoError(t, f.executableCheck(sql))
  52. // simple with where -- MySQL/SQLite/PostgreSQL style
  53. sql, err = Dialect(MYSQL).Select("a", "b", "c").From("table1").
  54. Where(Eq{"a": "1", "b": "1"}).OrderBy("a ASC").Limit(5, 10).ToBoundSQL()
  55. assert.NoError(t, err)
  56. assert.EqualValues(t, "SELECT a,b,c FROM table1 WHERE a='1' AND b='1' ORDER BY a ASC LIMIT 5 OFFSET 10", sql)
  57. assert.NoError(t, f.executableCheck(sql))
  58. // union -- MySQL/SQLite/PostgreSQL style
  59. sql, err = Dialect(MYSQL).Select("a", "b", "c").From(
  60. Dialect(MYSQL).Select("a", "b", "c").From("table1").Where(Eq{"a": "1"}).OrderBy("a ASC").
  61. Limit(5, 9).Union("ALL",
  62. Select("a", "b", "c").From("table1").Where(Eq{"a": "2"}).OrderBy("a DESC").Limit(10)), "at").
  63. Limit(5, 10).ToBoundSQL()
  64. assert.NoError(t, err)
  65. assert.EqualValues(t, "SELECT a,b,c FROM ((SELECT a,b,c FROM table1 WHERE a='1' ORDER BY a ASC LIMIT 5 OFFSET 9) UNION ALL (SELECT a,b,c FROM table1 WHERE a='2' ORDER BY a DESC LIMIT 10)) at LIMIT 5 OFFSET 10", sql)
  66. assert.NoError(t, f.executableCheck(sql))
  67. }
  68. func TestBuilder_Limit4Oracle(t *testing.T) {
  69. sqlFromFile, err := readPreparationSQLFromFile("testdata/oracle_fiddle_data.sql")
  70. assert.NoError(t, err)
  71. f, err := newFiddler("", ORACLE, sqlFromFile)
  72. assert.NoError(t, err)
  73. assert.NotEmpty(t, f.sessionCode)
  74. // simple -- OracleSQL style
  75. sql, err := Dialect(ORACLE).Select("a", "b", "c").From("table1").OrderBy("a ASC").
  76. Limit(5, 10).ToBoundSQL()
  77. assert.NoError(t, err)
  78. assert.EqualValues(t, "SELECT a,b,c FROM (SELECT * FROM (SELECT a,b,c,ROWNUM RN FROM table1 ORDER BY a ASC) at WHERE at.RN<=15) att WHERE att.RN>10", sql)
  79. assert.NoError(t, f.executableCheck(sql))
  80. // simple with join -- OracleSQL style
  81. sql, err = Dialect(ORACLE).Select("a", "b", "c", "d").From("table1 t1").
  82. InnerJoin("table2 t2", "t1.id = t2.ref_id").OrderBy("a ASC").Limit(5, 10).ToBoundSQL()
  83. assert.NoError(t, err)
  84. assert.EqualValues(t, "SELECT a,b,c,d FROM (SELECT * FROM (SELECT a,b,c,d,ROWNUM RN FROM table1 t1 INNER JOIN table2 t2 ON t1.id = t2.ref_id ORDER BY a ASC) at WHERE at.RN<=15) att WHERE att.RN>10", sql)
  85. assert.NoError(t, f.executableCheck(sql))
  86. // simple -- OracleSQL style
  87. sql, err = Dialect(ORACLE).Select("a", "b", "c").From("table1").
  88. OrderBy("a ASC").Limit(5).ToBoundSQL()
  89. assert.NoError(t, err)
  90. assert.EqualValues(t, "SELECT a,b,c FROM (SELECT a,b,c,ROWNUM RN FROM table1 ORDER BY a ASC) at WHERE at.RN<=5", sql)
  91. assert.NoError(t, f.executableCheck(sql))
  92. // simple with where -- OracleSQL style
  93. sql, err = Dialect(ORACLE).Select("a", "b", "c").From("table1").Where(Neq{"a": "10", "b": "20"}).
  94. OrderBy("a ASC").Limit(5, 1).ToBoundSQL()
  95. assert.NoError(t, err)
  96. assert.EqualValues(t, "SELECT a,b,c FROM (SELECT * FROM (SELECT a,b,c,ROWNUM RN FROM table1 WHERE a<>'10' AND b<>'20' ORDER BY a ASC) at WHERE at.RN<=6) att WHERE att.RN>1", sql)
  97. assert.NoError(t, f.executableCheck(sql))
  98. // union with limit -- OracleSQL style
  99. sql, err = Dialect(ORACLE).Select("a", "b", "c").From(
  100. Dialect(ORACLE).Select("a", "b", "c").From("table1").
  101. Where(Neq{"a": "0"}).OrderBy("a ASC").Limit(5, 10).Union("ALL",
  102. Select("a", "b", "c").From("table1").Where(Neq{"b": "48"}).
  103. OrderBy("a DESC").Limit(10)), "at").
  104. Limit(3).ToBoundSQL()
  105. assert.NoError(t, err)
  106. assert.EqualValues(t, "SELECT a,b,c FROM (SELECT a,b,c,ROWNUM RN FROM ((SELECT a,b,c FROM (SELECT * FROM (SELECT a,b,c,ROWNUM RN FROM table1 WHERE a<>'0' ORDER BY a ASC) at WHERE at.RN<=15) att WHERE att.RN>10) UNION ALL (SELECT a,b,c FROM (SELECT a,b,c,ROWNUM RN FROM table1 WHERE b<>'48' ORDER BY a DESC) at WHERE at.RN<=10)) at) at WHERE at.RN<=3", sql)
  107. assert.NoError(t, f.executableCheck(sql))
  108. }*/