A clean, Markdown-based publishing platform made for writers. Write together, and build a community. https://writefreely.org
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

74 lines
1.8 KiB

  1. //go:build sqlite && !wflib
  2. // +build sqlite,!wflib
  3. /*
  4. * Copyright © 2019-2020 Musing Studio LLC.
  5. *
  6. * This file is part of WriteFreely.
  7. *
  8. * WriteFreely is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, included
  10. * in the LICENSE file in this source code package.
  11. */
  12. package writefreely
  13. import (
  14. "database/sql"
  15. "github.com/go-sql-driver/mysql"
  16. "github.com/mattn/go-sqlite3"
  17. "github.com/writeas/web-core/log"
  18. "regexp"
  19. )
  20. func init() {
  21. SQLiteEnabled = true
  22. regex := func(re, s string) (bool, error) {
  23. return regexp.MatchString(re, s)
  24. }
  25. sql.Register("sqlite3_with_regex", &sqlite3.SQLiteDriver{
  26. ConnectHook: func(conn *sqlite3.SQLiteConn) error {
  27. return conn.RegisterFunc("regexp", regex, true)
  28. },
  29. })
  30. }
  31. func (db *datastore) isDuplicateKeyErr(err error) bool {
  32. if db.driverName == driverSQLite {
  33. if err, ok := err.(sqlite3.Error); ok {
  34. return err.Code == sqlite3.ErrConstraint
  35. }
  36. } else if db.driverName == driverMySQL {
  37. if mysqlErr, ok := err.(*mysql.MySQLError); ok {
  38. return mysqlErr.Number == mySQLErrDuplicateKey
  39. }
  40. } else {
  41. log.Error("isDuplicateKeyErr: failed check for unrecognized driver '%s'", db.driverName)
  42. }
  43. return false
  44. }
  45. func (db *datastore) isIgnorableError(err error) bool {
  46. if db.driverName == driverMySQL {
  47. if mysqlErr, ok := err.(*mysql.MySQLError); ok {
  48. return mysqlErr.Number == mySQLErrCollationMix
  49. }
  50. } else {
  51. log.Error("isIgnorableError: failed check for unrecognized driver '%s'", db.driverName)
  52. }
  53. return false
  54. }
  55. func (db *datastore) isHighLoadError(err error) bool {
  56. if db.driverName == driverMySQL {
  57. if mysqlErr, ok := err.(*mysql.MySQLError); ok {
  58. return mysqlErr.Number == mySQLErrMaxUserConns || mysqlErr.Number == mySQLErrTooManyConns
  59. }
  60. }
  61. return false
  62. }