A clean, Markdown-based publishing platform made for writers. Write together, and build a community. https://writefreely.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

73 lines
1.8 KiB

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