# SQLITEADMIN_QUERIES

NAME: sms_TRIGGER_BACKUP_1330822195
SQL:
  CREATE TRIGGER [sms_update_thread_on_insert] 
  AFTER INSERT ON [sms] 
  FOR EACH STATEMENT 
  BEGIN 

  TRIGGER sms_update_thread_on_insert AFTER INSERT ON sms BEGIN  UPDATE threads SET    date = (strftime('%s','now') * 1000),     snippet = new.body,     snippet_cs = 0  WHERE threads._id = new.thread_id;   UPDATE threads SET message_count =      (SELECT COUNT(sms._id) FROM sms LEFT JOIN threads       ON threads._id = thread_id      WHERE thread_id = new.thread_id        AND sms.type != 3) +      (SELECT COUNT(pdu._id) FROM pdu LEFT JOIN threads       ON threads._id = thread_id      WHERE thread_id = new.thread_id        AND (m_type=132 OR m_type=130 OR m_type=128)        AND msg_box != 3)   WHERE threads._id = new.thread_id;   UPDATE threads SET read =     CASE (SELECT COUNT(*)          FROM sms          WHERE read = 0            AND thread_id = threads._id)      WHEN 0 THEN 1      ELSE 0    END  WHERE threads._id = new.thread_id;

  END;CREATE TRIGGER [sms_update_thread_date_subject_on_update] 
  AFTER UPDATE OF [DATE] ,[BODY] ,[TYPE]  
  ON [sms] 
  FOR EACH STATEMENT 
  BEGIN 

  TRIGGER sms_update_thread_date_subject_on_update AFTER  UPDATE OF date, body, type  ON sms BEGIN  UPDATE threads SET    date = (strftime('%s','now') * 1000),     snippet = new.body,     snippet_cs = 0  WHERE threads._id = new.thread_id;   UPDATE threads SET message_count =      (SELECT COUNT(sms._id) FROM sms LEFT JOIN threads       ON threads._id = thread_id      WHERE thread_id = new.thread_id        AND sms.type != 3) +      (SELECT COUNT(pdu._id) FROM pdu LEFT JOIN threads       ON threads._id = thread_id      WHERE thread_id = new.thread_id        AND (m_type=132 OR m_type=130 OR m_type=128)        AND msg_box != 3)   WHERE threads._id = new.thread_id;   UPDATE threads SET read =     CASE (SELECT COUNT(*)          FROM sms          WHERE read = 0            AND thread_id = threads._id)      WHEN 0 THEN 1      ELSE 0    END  WHERE threads._id = new.thread_id;

  END;CREATE TRIGGER [sms_update_thread_read_on_update] 
  AFTER UPDATE OF [READ]  
  ON [sms] 
  FOR EACH STATEMENT 
  BEGIN 

  TRIGGER sms_update_thread_read_on_update AFTER  UPDATE OF read  ON sms BEGIN   UPDATE threads SET read =     CASE (SELECT COUNT(*)          FROM sms          WHERE read = 0            AND thread_id = threads._id)      WHEN 0 THEN 1      ELSE 0    END  WHERE threads._id = new.thread_id;

  END;CREATE TRIGGER [update_threads_error_on_update_sms] 
  AFTER UPDATE OF [TYPE]  
  ON [sms] 
  FOR EACH STATEMENT WHEN (OLD.TYPE != 5 AND NEW.TYPE = 5) OR (OLD.TYPE = 5 AND NEW.TYPE != 5) 
  BEGIN 

  UPDATE threads SET error =     CASE      WHEN NEW.type = 5 THEN error + 1      ELSE error - 1    END   WHERE _id = NEW.thread_id;

  END;CREATE TRIGGER [sms_words_update] 
  AFTER UPDATE ON [sms] 
  FOR EACH STATEMENT 
  BEGIN 

  UPDATE words  SET index_text = NEW.body WHERE (source_id=NEW._id AND table_to_use=1);

  END;CREATE TRIGGER [sms_words_delete] 
  AFTER DELETE ON [sms] 
  FOR EACH STATEMENT 
  BEGIN 

  DELETE FROM   words WHERE source_id = OLD._id AND table_to_use = 1;

  END;


