_smsgateway_smslog() should return generated id.

ardas - January 29, 2007 - 15:40
Project:SMS Gateway
Version:4.7.x-1.x-dev
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:needs review
Description

Greetings,

We are using your smsgateway module. I found that you had removed id generation in the last 4.7 release in a _smsgateway_smslog() function because you decide to count on auto_increment feature. The previous code was:

<?php
function _smsgateway_smslog($destination, $message, $response, $incoming = false) {
 
$id = db_next_id('{smslog}_sid');
 
db_query('INSERT INTO {smslog} (sid, direction, number, message, response, timestamp) VALUES ( \'%d\', \'%s\', \'%s\', \'%s\', \'%s\', \'%d\')', $id, $incoming ? 'IN' : 'OUT', $destination, $message, $response, time());
  return
$id;
}
?>

The new one is:
<?php
function _smsgateway_smslog($destination, $message, $response, $incoming = false) {
 
db_query('INSERT INTO {smslog} (direction, number, message, response, timestamp) VALUES ( \'%s\', \'%s\', \'%s\', \'%s\', \'%d\')', $incoming ? 'IN' : 'OUT', $destination, $message, $response, time());
}
?>

In the solution we create now we need this function to return an ID of the smslog entry. This is very important because we have a table were we store relations between our special nodes and smslog entries of all SMS messages which are associated with these nodes. So, we can receive all SMS messages connected with a specific node.

Anyway, this is always very useful when it is possible to get an primary ID of the record which is going to be inserted. This is why Drupal authors decided to make db_next_id() function which is implemented for different SQL servers (btw, when using it in PostgreSQL or Oracle you have more power than just generating an ID).

I would like to propose you to continue using your previous approach - generating via sequences (using db_next_id()) and to avoid autoincrement in MySQL and serial in PostgreSQL. What do you think about it?

#1

ardas - January 29, 2007 - 16:02

By the way, I would also like to propose you an improvement to be able to get and save a generated id which we did in our version of smsgateway.module:
Call

$message['sid'] = _smsgateway_smslog( $message['sender_number'], $message['body'], $message['timestamp'], true);

Instead of just
_smsgateway_smslog( $message['sender_number'], $message['body'], $message['timestamp'], true);

The same for functions smsgateway_sendmessage() and smsgateway_sendmessageset().

Here is a patch fixing the problenm I've discussed in the previous post. Please review it and give me your feedback. Thank you.

AttachmentSize
smsgateway_id.patch 2.11 KB

#2

Bèr Kessels - March 10, 2007 - 16:41

This patch adds not only the next_id magic, it also provides the proper upgrade path.

AttachmentSize
smsgateway_no_auto_increment.patch 2.26 KB

#3

Bèr Kessels - March 14, 2007 - 11:23
Status:active» needs review

changing status to patch.

#4

Bèr Kessels - March 14, 2007 - 11:24

changing status to patch.

 
 

Drupal is a registered trademark of Dries Buytaert.