---
name: "Mail::SpamAssassin::BayesStore::SQL"
section: "3pm"
platform: "linux"
package: "spamassassin"
version: "4.0.2-3"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/Mail::SpamAssassin::BayesStore::SQL"
---

# Mail::SpamAssassin::BayesStore::SQL(3pm)

SQL Bayesian Storage Module Implementation

## NAME

Mail::SpamAssassin::BayesStore::SQL - SQL Bayesian Storage Module Implementation

## DESCRIPTION

This module implements a SQL based bayesian storage module.  It's compatible with SQLite and possibly other standard SQL servers.

Do not use this for MySQL/MariaDB or PgSQL, they have their own specific modules.

## METHODS

### new

public class (Mail::SpamAssassin::BayesStore::SQL) new (Mail::Spamassassin::Plugin::Bayes `$bayes`)

Description: This methods creates a new instance of the Mail::SpamAssassin::BayesStore::SQL object.  It expects to be passed an instance of the Mail::SpamAssassin:Bayes object which is passed into the Mail::SpamAssassin::BayesStore parent object.

This method sets up the database connection and determines the username to use in queries.

### tie\_db\_readonly

public instance (Boolean) tie\_db\_readonly ();

Description: This method ensures that the database connection is properly setup and working.  If necessary it will initialize a user's bayes variables so that they can begin using the database immediately.

### tie\_db\_writable

public instance (Boolean) tie\_db\_writable ()

Description: This method ensures that the database connection is properly setup and working. If necessary it will initialize a users bayes variables so that they can begin using the database immediately.

### untie\_db

public instance () untie\_db ()

Description: Disconnects from an SQL server.

### calculate\_expire\_delta

public instance (%) calculate\_expire\_delta (Integer `$newest_atime`,\
                                              Integer `$start`,\
                                              Integer `$max_expire_mult`)

Description: This method performs a calculation on the data to determine the optimum atime for token expiration.

### token\_expiration

public instance (Integer, Integer,\
                  Integer, Integer) token\_expiration(\\% `$opts`,\
                                                     Integer `$newdelta`,\
                                                     @ `@vars`)

Description: This method performs the database specific expiration of tokens based on the passed in `$newdelta` and `@vars`.

### sync\_due

public instance (Boolean) sync\_due ()

Description: This method determines if a database sync is currently required.

Unused for SQL based implementation.

### seen\_get

public instance (String) seen\_get (string `$msgid`)

Description: This method retrieves the stored value, if any, for `$msgid`.  The return value is the stored string ('s' for spam and 'h' for ham) or undef if `$msgid` is not found.

### seen\_put

public (Boolean) seen\_put (string `$msgid`, char `$flag`)

Description: This method records `$msgid` as the type given by `$flag`.  `$flag` is one of two values 's' for spam and 'h' for ham.

### seen\_delete

public instance (Boolean) seen\_delete (string `$msgid`)

Description: This method removes `$msgid` from the database.

### get\_storage\_variables

public instance (@) get\_storage\_variables ()

Description: This method retrieves the various administrative variables used by the Bayes process and database.

The values returned in the array are in the following order:

0: scan count base

1: number of spam

2: number of ham

3: number of tokens in db

4: last expire atime

5: oldest token in db atime

6: db version value

7: last journal sync

8: last atime delta

9: last expire reduction count

10: newest token in db atime

### dump\_db\_toks

public instance () dump\_db\_toks (String `$template`, String `$regex`, Array `@vars`)

Description: This method loops over all tokens, computing the probability for the token and then printing it out according to the passed in token.

### set\_last\_expire

public instance (Boolean) set\_last\_expire (Integer `$time`)

Description: This method sets the last expire time.

### get\_running\_expire\_tok

public instance (String `$time`) get\_running\_expire\_tok ()

Description: This method determines if an expire is currently running and returns the last time set.

There can be multiple times, so we just pull the greatest (most recent) value.

### set\_running\_expire\_tok

public instance (String `$time`) set\_running\_expire\_tok ()

Description: This method sets the time that an expire starts running.

### remove\_running\_expire\_tok

public instance (Boolean) remove\_running\_expire\_tok ()

Description: This method removes the row in the database that indicates that and expire is currently running.

### tok\_get

public instance (Integer, Integer, Integer) tok\_get (String `$token`)

Description: This method retrieves a specified token (`$token`) from the database and returns it's spam\_count, ham\_count and last access time.

### tok\_get\_all

public instance (\\@) tok\_get (@ `$tokens`)

Description: This method retrieves the specified tokens (`$tokens`) from storage and returns an array ref of arrays spam count, ham count and last access time.

### tok\_count\_change

public instance (Boolean) tok\_count\_change (Integer `$spam_count`,\
 					    Integer `$ham_count`,\
 					    String `$token`,\
 					    String `$atime`)

Description: This method takes a `$spam_count` and `$ham_count` and adds it to `$tok` along with updating `$tok`s atime with `$atime`.

### multi\_tok\_count\_change

public instance (Boolean) multi\_tok\_count\_change (Integer `$spam_count`,\
  					          Integer `$ham_count`,\
 				 	          \\% `$tokens`,\
 					          String `$atime`)

Description: This method takes a `$spam_count` and `$ham_count` and adds it to all of the tokens in the `$tokens` hash ref along with updating each token's atime with `$atime`.

### nspam\_nham\_get

public instance ($spam\_count, `$ham_count`) nspam\_nham\_get ()

Description: This method retrieves the total number of spam and the total number of ham learned.

### nspam\_nham\_change

public instance (Boolean) nspam\_nham\_change (Integer `$num_spam`,\
                                              Integer `$num_ham`)

Description: This method updates the number of spam and the number of ham in the database.

### tok\_touch

public instance (Boolean) tok\_touch (String `$token`,\
                                      String `$atime`)

Description: This method updates the given tokens (`$token`) atime.

The assumption is that the token already exists in the database.

### tok\_touch\_all

public instance (Boolean) tok\_touch (\\@ `$tokens`\
                                      String `$atime`)

Description: This method does a mass update of the given list of tokens `$tokens`, if the existing token atime is \< `$atime`.

The assumption is that the tokens already exist in the database.

We should never be touching more than N\_SIGNIFICANT\_TOKENS, so we can make some assumptions about how to handle the data (ie no need to batch like we do in tok\_get\_all)

### cleanup

public instance (Boolean) cleanup ()

Description: This method performs any cleanup necessary before moving onto the next operation.

### get\_magic\_re

public instance get\_magic\_re (String)

Description: This method returns a regexp which indicates a magic token.

Unused in SQL implementation.

### sync

public instance (Boolean) sync (\\% `$opts`)

Description: This method performs a sync of the database

### perform\_upgrade

public instance (Boolean) perform\_upgrade (\\% `$opts`);

Description: Performs an upgrade of the database from one version to another, not currently used in this implementation.

### clear\_database

public instance (Boolean) clear\_database ()

Description: This method deletes all records for a particular user.

Callers should be aware that any errors returned by this method could cause the database to be inconsistent for the given user.

### backup\_database

public instance (Boolean) backup\_database ()

Description: This method will dump the users database in a machine readable format.

### restore\_database

public instance (Boolean) restore\_database (String `$filename`, Boolean `$showdots`)

Description: This method restores a database from the given filename, `$filename`.

Callers should be aware that any errors returned by this method could causes the database to be inconsistent for the given user.

### db\_readable

public instance (Boolean) **db\_readable()**

Description: This method returns a boolean value indicating if the database is in a readable state.

### db\_writable

public instance (Boolean) **db\_writeable()**

Description: This method returns a boolean value indicating if the database is in a writable state.

## Private Methods

### \_connect\_db

private instance (Boolean) \_connect\_db ()

Description: This method connects to the SQL database.

### \_get\_db\_version

private instance (Integer) \_get\_db\_version ()

Description: Gets the current version of the database from the special global vars tables.

### \_initialize\_db

private instance (Boolean) \_initialize\_db ()

Description: This method will check to see if a user has had their bayes variables initialized. If not then it will perform this initialization.

### \_put\_token

private instance (Boolean) \_put\_token (string `$token`,\
                                        integer `$spam_count`,\
                                        integer `$ham_count`,\
 				       string `$atime`)

Description: This method performs the work of either inserting or updating a token in the database.

### \_put\_tokens

private instance (Boolean) \_put\_tokens (\\% `$tokens`,\
                                         integer `$spam_count`,\
                                         integer `$ham_count`,\
 	 			        string `$atime`)

Description: This method performs the work of either inserting or updating tokens in the database.

### \_get\_oldest\_token\_age

private instance (Integer) \_get\_oldest\_token\_age ()

Description: This method finds the atime of the oldest token in the database.

The use of min(atime) in the SQL is ugly and but really the most efficient way of getting the oldest\_token\_age after we've done a mass expire.  It should only be called at expire time.

### \_get\_num\_hapaxes

private instance (Integer) \_get\_num\_hapaxes ()

Description: This method gets the total number of hapaxes (spam\_count + ham\_count == 1) in the token database for a user.

### \_get\_num\_lowfreq

private instance (Integer) \_get\_num\_lowfreq ()

Description: This method gets the total number of lowfreq tokens (spam\_count \< 8 and ham\_count \< 8) in the token database for a user

### \_token\_select\_string

private instance (String) \_token\_select\_string

Description: This method returns the string to be used in SELECT statements to represent the token column.

The default is to use the SUBSTR function to pad the token out to 5 characters.
