SQL Service
Description
SQL service stores all its data in database. It can use one of these SQL backends
(configuration option Driver in smsd section):
native_mysqlfor MySQL Backendnative_pgsqlfor PostgreSQL Backendodbcfor ODBC Backend- drivers supported by DBI for DBI Backend, which include:
sqlite3- for SQLite 3mysql- for MySQLpgsql- for PostgeSQLfreetds- for MS SQL Server or Sybase
SQL connection parameters
Common for all backends:
User- user connecting to databasePassword- password for connecting to databaseHost- database host or data source nameDatabase- database nameDriver-native_mysql,native_pgsql,odbcor DBI oneSQL- SQL dialect to use
Specific for DBI:
DriversPath- path to DBI driversDBDir- sqlite/sqlite3 directory with database
See also
The variables are fully described in Gammu Configuration File documentation.
Tables
Added in version 1.37.1.
You can customize name of all tables in the [tables]. The SQL
queries will reflect this, so it’s enough to change table name in this section.
- outbox_multipart
Name of the outbox_multipart table.
You can change any table name using these:
[tables]
inbox = special_inbox
SQL Queries
Almost all queries are configurable. You can edit them in
[sql] section. There are several variables used in SQL
queries. We can separate them into three groups:
phone specific, which can be used in every query, see Phone Specific Parameters
SMS specific, which can be used in queries which works with SMS messages, see SMS Specific Parameters
query specific, which are numeric and are specific only for given query (or set of queries), see Configurable queries
Phone Specific Parameters
%IIMEI of phone
%SSIM IMSI
%PPHONE ID (hostname)
%Nclient name (eg. Gammu 1.12.3)
%Onetwork code
%Mnetwork name
SMS Specific Parameters
%Rremote number [1]
%Cdelivery datetime
%edelivery status on receiving or status error on sending
%tmessage reference
%dreceiving datetime for received sms
%Eencoded text of SMS
%cSMS coding (ie 8bit or UnicodeNoCompression)
%Fsms centre number
%uUDH header
%xclass
%Tdecoded SMS text
%ACreatorID of SMS (sending sms)
%Vrelative validity
Configurable queries
All configurable queries can be set in [sql] section. Sequence of rows in selects are mandatory.
All default queries noted here are noted for MySQL. Actual time and time addition are selected for default queries during initialization.
- delete_phone
Deletes phone from database.
Default value:
DELETE FROM phones WHERE IMEI = %I
- insert_phone
Inserts phone to database.
Default value:
INSERT INTO phones (IMEI, IMSI, ID, NetCode, NetName, Send, Receive, InsertIntoDB, TimeOut, Client, Battery, Signal) VALUES (%I, %S, %P, %O, %M, %1, %2, NOW(), (NOW() + INTERVAL 70 SECOND) + 0, %N, -1, -1)
The shown 70-second expiry uses the default 60-second
StatusFrequencyplus a ten-second grace period. The generated default query follows the configured frequency. Custom queries should provide an equivalent expiry interval.Query specific parameters:
%1enable send (yes or no) - configuration option Send
%2enable receive (yes or no) - configuration option Receive
- save_inbox_sms_select
Select message for update delivery status.
Default value:
SELECT ID, Status, SendingDateTime, DeliveryDateTime, SMSCNumber FROM sentitems WHERE DeliveryDateTime IS NULL AND SenderID = %P AND TPMR = %t AND DestinationNumber = %R
- save_inbox_sms_update_delivered
Update message delivery status if message was delivered.
Default value:
UPDATE sentitems SET DeliveryDateTime = %C, Status = %1, StatusError = %e WHERE ID = %2 AND TPMR = %t
Query specific parameters:
%1delivery status returned by GSM network
%2ID of message
- save_inbox_sms_update
Update message if there is an delivery error.
Default value:
UPDATE sentitems SET Status = %1, StatusError = %e WHERE ID = %2 AND TPMR = %t
Query specific parameters:
%1delivery status returned by GSM network
%2ID of message
- save_inbox_sms_insert
Insert received message.
Default value:
INSERT INTO inbox (ReceivingDateTime, InsertIntoDB, Text, SenderNumber, Coding, SMSCNumber, UDH, Class, TextDecoded, RecipientID, Status, MessageID, SequencePosition, PartCount, Processed) VALUES (%d, NOW(), %E, %R, %c, %F, %u, %x, %T, %P, %e, %1, %2, %3, %4)
Query specific parameters:
%1logical message identifier, or 0 until the first physical row ID is known
%2sequence position of this physical part
%3expected number of physical parts
%4initial processed state; received rows are staged as processed until their logical message identifier is finalized
Custom queries must store the database server’s current timestamp in
InsertIntoDBso incomplete multipart groups can be restored without extending their original timeout.
- save_inbox_sms_update_metadata
Finalize the logical message identifier and multipart metadata, then publish the received physical message to inbox consumers.
Default value:
UPDATE inbox SET MessageID = %1, SequencePosition = %2, PartCount = %3, Processed = %5 WHERE ID = %4
Query specific parameters:
%1ID of the first stored physical part of the logical message
%2sequence position of this physical part
%3expected number of physical parts
%4ID of this physical part
%5final processed state; false publishes the completed row
- restore_inbox_groups
Restore incomplete multipart groups after SMSD restarts. The default query selects recently inserted multipart rows for the current phone, including earlier rows sharing their logical message identifier. SMSD discards complete and expired groups after reading the result.
The selected columns must remain in the documented order. The final column is the row age in seconds, calculated by the database so that restoration does not depend on the SMSD host and database server sharing a timezone.
Default value:
SELECT MessageID, SenderNumber, SMSCNumber, UDH, SequencePosition, PartCount, TIMESTAMPDIFF(SECOND, InsertIntoDB, NOW()) FROM inbox WHERE PartCount > 1 AND RecipientID = %P AND MessageID IN (SELECT MessageID FROM inbox WHERE PartCount > 1 AND RecipientID = %P AND InsertIntoDB >= NOW() - INTERVAL 600 SECOND) ORDER BY InsertIntoDB ASC, ID ASC
- update_received
Update statistics after receiving message.
Default value:
UPDATE phones SET Received = Received + 1 WHERE IMEI = %I
- refresh_send_status
Update messages in outbox.
Default value:
UPDATE outbox SET SendingTimeOut = (NOW() + INTERVAL 60 SECOND) + 0 WHERE ID = %1 AND (SendingTimeOut < NOW() OR SendingTimeOut IS NULL)
The default query calculates sending timeout based on
LoopSleepvalue.Query specific parameters:
%1ID of message
- find_outbox_sms_id
Find sms messages for sending.
Default value:
SELECT ID, InsertIntoDB, SendingDateTime, SenderID FROM outbox WHERE SendingDateTime < NOW() AND SendingTimeOut < NOW() AND SendBefore >= CURTIME() AND SendAfter <= CURTIME() AND (SendDays & %2) <> 0 AND ( SenderID is NULL OR SenderID = '' OR SenderID = %P ) ORDER BY Priority DESC, InsertIntoDB ASC LIMIT %1
Query specific parameters:
%1limit of sms messages sended in one walk in loop
%2bit mask for the current weekday in the SMSD process local timezone, using Monday as 1 through Sunday as 64
Custom queries need to use
%2to enforce the outboxSendDaysrestriction. The default bitwise expression is adapted to the configured SQL dialect.
- find_outbox_body
Select body of message.
Default value:
SELECT Text, Coding, UDH, Class, TextDecoded, ID, DestinationNumber, MultiPart, RelativeValidity, DeliveryReport, CreatorID FROM outbox WHERE ID=%1
Query specific parameters:
%1ID of message
- find_outbox_multipart
Select remaining parts of sms message.
Default value:
SELECT Text, Coding, UDH, Class, TextDecoded, ID, SequencePosition FROM outbox_multipart WHERE ID=%1 AND SequencePosition=%2
Query specific parameters:
%1ID of message
%2Number of multipart message
- find_sent_item
Find an existing sent message before transmission. SMSD uses this to reconcile messages which were written to
sentitemsbut not removed fromoutbox, for example when the daemon stopped between these two operations.The selected columns and their order are mandatory. SMSD compares the stored message identity and payload with the outbox message. A matching successfully sent message is skipped; a different message using the same ID is left in the outbox and reported as a conflict.
Default value:
SELECT Text, Coding, UDH, Class, TextDecoded, DestinationNumber, InsertIntoDB, RelativeValidity, CreatorID, Status FROM sentitems WHERE ID=%1 AND SequencePosition=%2
Query specific parameters:
%1ID of message
%2Number of multipart message
- delete_outbox
Remove messages from outbox after threir successful send.
Default value:
DELETE FROM outbox WHERE ID=%1
Query specific parameters:
%1ID of message
- delete_outbox_multipart
Remove messages from outbox_multipart after threir successful send.
Default value:
DELETE FROM outbox_multipart WHERE ID=%1
Query specific parameters:
%1ID of message
- create_outbox
Create message (insert to outbox).
Default value:
INSERT INTO outbox (CreatorID, SenderID, DeliveryReport, MultiPart, InsertIntoDB, Text, DestinationNumber, RelativeValidity, Coding, UDH, Class, TextDecoded) VALUES (%1, %P, %2, %3, NOW(), %E, %R, %V, %c, %u, %x, %T)
Query specific parameters:
%1creator of message
%2delivery status report - yes/default
%3multipart - FALSE/TRUE
%4Part (part number)
%5ID of message
- create_outbox_multipart
Create message remaining parts.
Default value:
INSERT INTO outbox_multipart (SequencePosition, Text, Coding, UDH, Class, TextDecoded, ID) VALUES (%4, %E, %c, %u, %x, %T, %5)
Query specific parameters:
%1creator of message
%2delivery status report - yes/default
%3multipart - FALSE/TRUE
%4Part (part number)
%5ID of message
- add_sent_info
Insert to sentitems.
Default value:
INSERT INTO sentitems (CreatorID,ID,SequencePosition,Status,SendingDateTime, SMSCNumber, TPMR, SenderID,Text,DestinationNumber,Coding,UDH,Class,TextDecoded, InsertIntoDB,RelativeValidity) VALUES (%A, %1, %2, %3, NOW(), %F, %4, %P, %E, %R, %c, %u, %x, %T, %5, %V)
Query specific parameters:
%1ID of sms message
%2part number (for multipart sms)
%3message state (SendingError, Error, SendingOK, SendingOKNoReport)
%4message reference (TPMR)
%5time when inserted in db
- update_sent
Update sent statistics after sending message.
Default value:
UPDATE phones SET Sent= Sent + 1 WHERE IMEI = %I
- refresh_phone_status
Update phone status (battery, signal, and network).
Default value:
UPDATE phones SET TimeOut = (NOW() + INTERVAL 70 SECOND) + 0, Battery = %1, Signal = %2, NetCode = %O, NetName = %M WHERE IMEI = %I
The expiry interval is generated from
StatusFrequencyplus a ten-second grace period, as described forinsert_phone. Custom queries should keepTimeOutvalid through the next refresh.Query specific parameters:
%1battery percent
%2signal percent
- update_retries
Update number of retries for outbox message. The interval can be configured by
RetryTimeout.UPDATE outbox SET SendngTimeOut = (NOW() + INTERVAL 600 SECOND) + 0, Retries = %2 WHERE ID = %1
Query specific parameters:
%1message ID
%2number of retries