limit.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package main
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "log"
  6. "os"
  7. "strings"
  8. "github.com/mattn/go-sqlite3"
  9. )
  10. func createBulkInsertQuery(n int, start int) (query string, args []interface{}) {
  11. values := make([]string, n)
  12. args = make([]interface{}, n*2)
  13. pos := 0
  14. for i := 0; i < n; i++ {
  15. values[i] = "(?, ?)"
  16. args[pos] = start + i
  17. args[pos+1] = fmt.Sprintf("こんにちわ世界%03d", i)
  18. pos += 2
  19. }
  20. query = fmt.Sprintf(
  21. "insert into foo(id, name) values %s",
  22. strings.Join(values, ", "),
  23. )
  24. return
  25. }
  26. func bukInsert(db *sql.DB, query string, args []interface{}) (err error) {
  27. stmt, err := db.Prepare(query)
  28. if err != nil {
  29. return
  30. }
  31. _, err = stmt.Exec(args...)
  32. if err != nil {
  33. return
  34. }
  35. return
  36. }
  37. func main() {
  38. var sqlite3conn *sqlite3.SQLiteConn
  39. sql.Register("sqlite3_with_limit", &sqlite3.SQLiteDriver{
  40. ConnectHook: func(conn *sqlite3.SQLiteConn) error {
  41. sqlite3conn = conn
  42. return nil
  43. },
  44. })
  45. os.Remove("./foo.db")
  46. db, err := sql.Open("sqlite3_with_limit", "./foo.db")
  47. if err != nil {
  48. log.Fatal(err)
  49. }
  50. defer db.Close()
  51. sqlStmt := `
  52. create table foo (id integer not null primary key, name text);
  53. delete from foo;
  54. `
  55. _, err = db.Exec(sqlStmt)
  56. if err != nil {
  57. log.Printf("%q: %s\n", err, sqlStmt)
  58. return
  59. }
  60. if sqlite3conn == nil {
  61. log.Fatal("not set sqlite3 connection")
  62. }
  63. limitVariableNumber := sqlite3conn.GetLimit(sqlite3.SQLITE_LIMIT_VARIABLE_NUMBER)
  64. log.Printf("default SQLITE_LIMIT_VARIABLE_NUMBER: %d", limitVariableNumber)
  65. num := 400
  66. query, args := createBulkInsertQuery(num, 0)
  67. err = bukInsert(db, query, args)
  68. if err != nil {
  69. log.Fatal(err)
  70. }
  71. smallLimitVariableNumber := 100
  72. sqlite3conn.SetLimit(sqlite3.SQLITE_LIMIT_VARIABLE_NUMBER, smallLimitVariableNumber)
  73. limitVariableNumber = sqlite3conn.GetLimit(sqlite3.SQLITE_LIMIT_VARIABLE_NUMBER)
  74. log.Printf("updated SQLITE_LIMIT_VARIABLE_NUMBER: %d", limitVariableNumber)
  75. query, args = createBulkInsertQuery(num, num)
  76. err = bukInsert(db, query, args)
  77. if err != nil {
  78. if err != nil {
  79. log.Printf("expect failed since SQLITE_LIMIT_VARIABLE_NUMBER is too small: %v", err)
  80. }
  81. }
  82. bigLimitVariableNumber := 999999
  83. sqlite3conn.SetLimit(sqlite3.SQLITE_LIMIT_VARIABLE_NUMBER, bigLimitVariableNumber)
  84. limitVariableNumber = sqlite3conn.GetLimit(sqlite3.SQLITE_LIMIT_VARIABLE_NUMBER)
  85. log.Printf("set SQLITE_LIMIT_VARIABLE_NUMBER: %d", bigLimitVariableNumber)
  86. log.Printf("updated SQLITE_LIMIT_VARIABLE_NUMBER: %d", limitVariableNumber)
  87. query, args = createBulkInsertQuery(500, num+num)
  88. err = bukInsert(db, query, args)
  89. if err != nil {
  90. if err != nil {
  91. log.Fatal(err)
  92. }
  93. }
  94. log.Println("no error if SQLITE_LIMIT_VARIABLE_NUMBER > 999")
  95. }