How to backport db_merge() to drupal 6? I am converting a module from drupal 7 to drupal 6. I have a query in drupal 7.
The query contains function db_merge()

   db_merge('facebook_rules_user_tokens')
    -&gt;key(array('uid' =&gt; $form_state['user']-&gt;uid))
    -&gt;fields(array(
      'user_grant' =&gt; $form_state['values']['facebook_rules_allow'],
    ))


Which function should I use in d6 for the above sql?
From the MergeQuery docs

An ANSI SQL:2003 compatible database would run the following query:

MERGE INTO table_name_1 USING table_name_2 ON (condition)
  WHEN MATCHED THEN
  UPDATE SET column1 = value1 [, column2 = value2 ...]
  WHEN NOT MATCHED THEN
  INSERT (column1 [, column2 ...]) VALUES (value1 [, value2 ...

Other databases (most notably MySQL, PostgreSQL and SQLite) will emulate this statement by running a SELECT and then INSERT or UPDATE.



You need to do a SELECT using db_query first, to find out if your data already exists.
Then you can use drupal_write_record, which will do either an UPDATE or an INSERT depending on whether or not you populate the tables primary key(s).