1// +build sqlite,!wflib 2 3/* 4 * Copyright © 2019-2020 A Bunch Tell 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 13package writefreely 14 15import ( 16 "database/sql" 17 "github.com/go-sql-driver/mysql" 18 "github.com/mattn/go-sqlite3" 19 "github.com/writeas/web-core/log" 20 "regexp" 21) 22 23func init() { 24 SQLiteEnabled = true 25 26 regex := func(re, s string) (bool, error) { 27 return regexp.MatchString(re, s) 28 } 29 sql.Register("sqlite3_with_regex", &sqlite3.SQLiteDriver{ 30 ConnectHook: func(conn *sqlite3.SQLiteConn) error { 31 return conn.RegisterFunc("regexp", regex, true) 32 }, 33 }) 34} 35 36func (db *datastore) isDuplicateKeyErr(err error) bool { 37 if db.driverName == driverSQLite { 38 if err, ok := err.(sqlite3.Error); ok { 39 return err.Code == sqlite3.ErrConstraint 40 } 41 } else if db.driverName == driverMySQL { 42 if mysqlErr, ok := err.(*mysql.MySQLError); ok { 43 return mysqlErr.Number == mySQLErrDuplicateKey 44 } 45 } else { 46 log.Error("isDuplicateKeyErr: failed check for unrecognized driver '%s'", db.driverName) 47 } 48 49 return false 50} 51 52func (db *datastore) isIgnorableError(err error) bool { 53 if db.driverName == driverMySQL { 54 if mysqlErr, ok := err.(*mysql.MySQLError); ok { 55 return mysqlErr.Number == mySQLErrCollationMix 56 } 57 } else { 58 log.Error("isIgnorableError: failed check for unrecognized driver '%s'", db.driverName) 59 } 60 61 return false 62} 63 64func (db *datastore) isHighLoadError(err error) bool { 65 if db.driverName == driverMySQL { 66 if mysqlErr, ok := err.(*mysql.MySQLError); ok { 67 return mysqlErr.Number == mySQLErrMaxUserConns || mysqlErr.Number == mySQLErrTooManyConns 68 } 69 } 70 71 return false 72} 73