## CamelDB CamelDB |

## Synopsis

    #define             CAMEL_DB_FILE
    #define             CAMEL_DB_FREE_CACHE_SIZE
    #define             CAMEL_DB_SLEEP_INTERVAL
    #define             CAMEL_DB_RELEASE_SQLITE_MEMORY
    #define             CAMEL_DB_USE_SHARED_CACHE
    int                 (*CamelDBCollate)                   (void *,
                                                             ...,
                                                             const void *,
                                                             ...,
                                                             const void *);
                        CamelDB;
                        CamelMIRecord;
                        CamelFIRecord;
    int                 (*CamelDBSelectCB)                  (gpointer data,
                                                             int ncol,
                                                             char **colvalues,
                                                             char **colnames);
    CamelDB *           camel_db_open                       (const char *path,
                                                             CamelException *ex);
    void                camel_db_close                      (CamelDB *cdb);
    int                 camel_db_command                    (CamelDB *cdb,
                                                             const char *stmt,
                                                             CamelException *ex);
    int                 camel_db_transaction_command        (CamelDB *cdb,
                                                             GSList *qry_list,
                                                             CamelException *ex);
    int                 camel_db_begin_transaction          (CamelDB *cdb,
                                                             CamelException *ex);
    int                 camel_db_add_to_transaction         (CamelDB *cdb,
                                                             const char *query,
                                                             CamelException *ex);
    int                 camel_db_end_transaction            (CamelDB *cdb,
                                                             CamelException *ex);
    int                 camel_db_abort_transaction          (CamelDB *cdb,
                                                             CamelException *ex);
    int                 camel_db_clear_folder_summary       (CamelDB *cdb,
                                                             char *folder,
                                                             CamelException *ex);
    int                 camel_db_rename_folder              (CamelDB *cdb,
                                                             const char *old_folder,
                                                             const char *new_folder,
                                                             CamelException *ex);
    int                 camel_db_delete_folder              (CamelDB *cdb,
                                                             const char *folder,
                                                             CamelException *ex);
    int                 camel_db_delete_uid                 (CamelDB *cdb,
                                                             const char *folder,
                                                             const char *uid,
                                                             CamelException *ex);
    int                 camel_db_delete_uids                (CamelDB *cdb,
                                                             const char *folder_name,
                                                             GSList *uids,
                                                             CamelException *ex);
    int                 camel_db_create_folders_table       (CamelDB *cdb,
                                                             CamelException *ex);
    int                 camel_db_select                     (CamelDB *cdb,
                                                             const char *stmt,
                                                             CamelDBSelectCB callback,
                                                             gpointer data,
                                                             CamelException *ex);
    int                 camel_db_write_folder_info_record   (CamelDB *cdb,
                                                             CamelFIRecord *record,
                                                             CamelException *ex);
    int                 camel_db_read_folder_info_record    (CamelDB *cdb,
                                                             const char *folder_name,
                                                             CamelFIRecord **record,
                                                             CamelException *ex);
    int                 camel_db_prepare_message_info_table (CamelDB *cdb,
                                                             const char *folder_name,
                                                             CamelException *ex);
    int                 camel_db_write_message_info_record  (CamelDB *cdb,
                                                             const char *folder_name,
                                                             CamelMIRecord *record,
                                                             CamelException *ex);
    int                 camel_db_read_message_info_records  (CamelDB *cdb,
                                                             const char *folder_name,
                                                             gpointer p,
                                                             CamelDBSelectCB read_mir_callback,
                                                             CamelException *ex);
    int                 camel_db_read_message_info_record_with_uid
                                                            (CamelDB *cdb,
                                                             const char *folder_name,
                                                             const char *uid,
                                                             gpointer p,
                                                             CamelDBSelectCB read_mir_callback,
                                                             CamelException *ex);
    int                 camel_db_count_junk_message_info    (CamelDB *cdb,
                                                             const char *table_name,
                                                             guint32 *count,
                                                             CamelException *ex);
    int                 camel_db_count_unread_message_info  (CamelDB *cdb,
                                                             const char *table_name,
                                                             guint32 *count,
                                                             CamelException *ex);
    int                 camel_db_count_deleted_message_info (CamelDB *cdb,
                                                             const char *table_name,
                                                             guint32 *count,
                                                             CamelException *ex);
    int                 camel_db_count_total_message_info   (CamelDB *cdb,
                                                             const char *table_name,
                                                             guint32 *count,
                                                             CamelException *ex);
    int                 camel_db_count_visible_message_info (CamelDB *cdb,
                                                             const char *table_name,
                                                             guint32 *count,
                                                             CamelException *ex);
    int                 camel_db_count_visible_unread_message_info
                                                            (CamelDB *cdb,
                                                             const char *table_name,
                                                             guint32 *count,
                                                             CamelException *ex);
    int                 camel_db_count_junk_not_deleted_message_info
                                                            (CamelDB *cdb,
                                                             const char *table_name,
                                                             guint32 *count,
                                                             CamelException *ex);
    void                camel_db_camel_mir_free             (CamelMIRecord *record);
    int                 camel_db_create_vfolder             (CamelDB *db,
                                                             const char *folder_name,
                                                             CamelException *ex);
    int                 camel_db_recreate_vfolder           (CamelDB *db,
                                                             const char *folder_name,
                                                             CamelException *ex);
    int                 camel_db_delete_uid_from_vfolder    (CamelDB *db,
                                                             char *folder_name,
                                                             char *vuid,
                                                             CamelException *ex);
    int                 camel_db_delete_uid_from_vfolder_transaction
                                                            (CamelDB *db,
                                                             char *folder_name,
                                                             char *vuid,
                                                             CamelException *ex);
    GPtrArray *         camel_db_get_vuids_from_vfolder     (CamelDB *db,
                                                             char *folder_name,
                                                             char *filter,
                                                             CamelException *ex);
    int                 camel_db_add_to_vfolder             (CamelDB *db,
                                                             char *folder_name,
                                                             char *vuid,
                                                             CamelException *ex);
    int                 camel_db_add_to_vfolder_transaction (CamelDB *db,
                                                             char *folder_name,
                                                             char *vuid,
                                                             CamelException *ex);
    int                 camel_db_get_folder_uids            (CamelDB *db,
                                                             char *folder_name,
                                                             char *sort_by,
                                                             char *collate,
                                                             GPtrArray *array,
                                                             CamelException *ex);
    GPtrArray *         camel_db_get_folder_junk_uids       (CamelDB *db,
                                                             char *folder_name,
                                                             CamelException *ex);
    GPtrArray *         camel_db_get_folder_deleted_uids    (CamelDB *db,
                                                             char *folder_name,
                                                             CamelException *ex);
    char *              camel_db_sqlize_string              (const char *string);
    void                camel_db_free_sqlized_string        (char *string);
    char *              camel_db_get_column_name            (const char *raw_name);
    int                 camel_db_set_collate                (CamelDB *cdb,
                                                             const char *col,
                                                             const char *collate,
                                                             CamelDBCollate func);
    int                 camel_db_migrate_vfolders_to_14     (CamelDB *cdb,
                                                             const char *folder,
                                                             CamelException *ex);

## Description

## Details

### CAMEL_DB_FILE

    #define CAMEL_DB_FILE "folders.db"

### CAMEL_DB_FREE_CACHE_SIZE

    #define CAMEL_DB_FREE_CACHE_SIZE 2 * 1024 * 1024

### CAMEL_DB_SLEEP_INTERVAL

    #define CAMEL_DB_SLEEP_INTERVAL 1*10*10

### CAMEL_DB_RELEASE_SQLITE_MEMORY

    #define CAMEL_DB_RELEASE_SQLITE_MEMORY if(!g_getenv("CAMEL_SQLITE_FREE_CACHE")) sqlite3_release_memory(CAMEL_DB_FREE_CACHE_SIZE);

### CAMEL_DB_USE_SHARED_CACHE

    #define CAMEL_DB_USE_SHARED_CACHE if(g_getenv("CAMEL_SQLITE_SHARED_CACHE")) sqlite3_enable_shared_cache(TRUE);

### CamelDBCollate ()

    int                 (*CamelDBCollate)                   (void *,
                                                             ...,
                                                             const void *,
                                                             ...,
                                                             const void *);

`Param1` : |
`...` : |
`Param3` : |
`...` : |
`Param5` : |
Returns : |

### CamelDB

    typedef struct {
    	sqlite3 *db;
    	GMutex *lock;
    	CamelDBPrivate *priv;
    } CamelDB;

### CamelMIRecord

    typedef struct {
    	char *uid;
    	guint32 flags;
    	guint32 msg_type;
    	guint32 dirty;
    	gboolean read;
    	gboolean deleted;
    	gboolean replied;
    	gboolean important;
    	gboolean junk;
    	gboolean attachment;
    	guint32 size;
    	time_t dsent;
    	time_t dreceived;
    	char *subject;
    	char *from;
    	char *to;
    	char *cc;
    	char *mlist;
    	char *followup_flag;
    	char *followup_completed_on;
    	char *followup_due_by;
    	char *part;
    	char *labels;
    	char *usertags;
    	char *cinfo;
    	char *bdata;
    } CamelMIRecord;

### CamelFIRecord

    typedef struct {
    	char *folder_name;
    	guint32 version;
    	guint32 flags;
    	guint32 nextuid;
    	time_t time;
    	guint32 saved_count;
    	guint32 unread_count;
    	guint32 deleted_count;
    	guint32 junk_count;
    	guint32 visible_count;
    	guint32 jnd_count;  /* Junked not deleted */
    	char *bdata;
    } CamelFIRecord;

### CamelDBSelectCB ()

    int                 (*CamelDBSelectCB)                  (gpointer data,
                                                             int ncol,
                                                             char **colvalues,
                                                             char **colnames);

`data` : |
`ncol` : |
`colvalues` : |
`colnames` : |
Returns : |

### camel_db_open ()

    CamelDB *           camel_db_open                       (const char *path,
                                                             CamelException *ex);

`path` : |
`ex` : |
Returns : |

### camel_db_close ()

    void                camel_db_close                      (CamelDB *cdb);

`cdb` : |

### camel_db_command ()

    int                 camel_db_command                    (CamelDB *cdb,
                                                             const char *stmt,
                                                             CamelException *ex);

`cdb` : |
`stmt` : |
`ex` : |
Returns : |

### camel_db_transaction_command ()

    int                 camel_db_transaction_command        (CamelDB *cdb,
                                                             GSList *qry_list,
                                                             CamelException *ex);

`cdb` : |
`qry_list` : |
`ex` : |
Returns : |

### camel_db_begin_transaction ()

    int                 camel_db_begin_transaction          (CamelDB *cdb,
                                                             CamelException *ex);

`cdb` : |
`ex` : |
Returns : |

### camel_db_add_to_transaction ()

    int                 camel_db_add_to_transaction         (CamelDB *cdb,
                                                             const char *query,
                                                             CamelException *ex);

`cdb` : |
`query` : |
`ex` : |
Returns : |

### camel_db_end_transaction ()

    int                 camel_db_end_transaction            (CamelDB *cdb,
                                                             CamelException *ex);

`cdb` : |
`ex` : |
Returns : |

### camel_db_abort_transaction ()

    int                 camel_db_abort_transaction          (CamelDB *cdb,
                                                             CamelException *ex);

`cdb` : |
`ex` : |
Returns : |

### camel_db_clear_folder_summary ()

    int                 camel_db_clear_folder_summary       (CamelDB *cdb,
                                                             char *folder,
                                                             CamelException *ex);

`cdb` : |
`folder` : |
`ex` : |
Returns : |

### camel_db_rename_folder ()

    int                 camel_db_rename_folder              (CamelDB *cdb,
                                                             const char *old_folder,
                                                             const char *new_folder,
                                                             CamelException *ex);

`cdb` : |
`old_folder` : |
`new_folder` : |
`ex` : |
Returns : |

### camel_db_delete_folder ()

    int                 camel_db_delete_folder              (CamelDB *cdb,
                                                             const char *folder,
                                                             CamelException *ex);

`cdb` : |
`folder` : |
`ex` : |
Returns : |

### camel_db_delete_uid ()

    int                 camel_db_delete_uid                 (CamelDB *cdb,
                                                             const char *folder,
                                                             const char *uid,
                                                             CamelException *ex);

`cdb` : |
`folder` : |
`uid` : |
`ex` : |
Returns : |

### camel_db_delete_uids ()

    int                 camel_db_delete_uids                (CamelDB *cdb,
                                                             const char *folder_name,
                                                             GSList *uids,
                                                             CamelException *ex);

`cdb` : |
`folder_name` : |
`uids` : |
`ex` : |
Returns : |

### camel_db_create_folders_table ()

    int                 camel_db_create_folders_table       (CamelDB *cdb,
                                                             CamelException *ex);

`cdb` : |
`ex` : |
Returns : |

### camel_db_select ()

    int                 camel_db_select                     (CamelDB *cdb,
                                                             const char *stmt,
                                                             CamelDBSelectCB callback,
                                                             gpointer data,
                                                             CamelException *ex);

`cdb` : |
`stmt` : |
`callback` : |
`data` : |
`ex` : |
Returns : |

### camel_db_write_folder_info_record ()

    int                 camel_db_write_folder_info_record   (CamelDB *cdb,
                                                             CamelFIRecord *record,
                                                             CamelException *ex);

`cdb` : |
`record` : |
`ex` : |
Returns : |

### camel_db_read_folder_info_record ()

    int                 camel_db_read_folder_info_record    (CamelDB *cdb,
                                                             const char *folder_name,
                                                             CamelFIRecord **record,
                                                             CamelException *ex);

`cdb` : |
`folder_name` : |
`record` : |
`ex` : |
Returns : |

### camel_db_prepare_message_info_table ()

    int                 camel_db_prepare_message_info_table (CamelDB *cdb,
                                                             const char *folder_name,
                                                             CamelException *ex);

`cdb` : |
`folder_name` : |
`ex` : |
Returns : |

### camel_db_write_message_info_record ()

    int                 camel_db_write_message_info_record  (CamelDB *cdb,
                                                             const char *folder_name,
                                                             CamelMIRecord *record,
                                                             CamelException *ex);

`cdb` : |
`folder_name` : |
`record` : |
`ex` : |
Returns : |

### camel_db_read_message_info_records ()

    int                 camel_db_read_message_info_records  (CamelDB *cdb,
                                                             const char *folder_name,
                                                             gpointer p,
                                                             CamelDBSelectCB read_mir_callback,
                                                             CamelException *ex);

`cdb` : |
`folder_name` : |
`p` : |
`read_mir_callback` : |
`ex` : |
Returns : |

### camel_db_read_message_info_record_with_uid ()

    int                 camel_db_read_message_info_record_with_uid
                                                            (CamelDB *cdb,
                                                             const char *folder_name,
                                                             const char *uid,
                                                             gpointer p,
                                                             CamelDBSelectCB read_mir_callback,
                                                             CamelException *ex);

`cdb` : |
`folder_name` : |
`uid` : |
`p` : |
`read_mir_callback` : |
`ex` : |
Returns : |

### camel_db_count_junk_message_info ()

    int                 camel_db_count_junk_message_info    (CamelDB *cdb,
                                                             const char *table_name,
                                                             guint32 *count,
                                                             CamelException *ex);

`cdb` : |
`table_name` : |
`count` : |
`ex` : |
Returns : |

### camel_db_count_unread_message_info ()

    int                 camel_db_count_unread_message_info  (CamelDB *cdb,
                                                             const char *table_name,
                                                             guint32 *count,
                                                             CamelException *ex);

`cdb` : |
`table_name` : |
`count` : |
`ex` : |
Returns : |

### camel_db_count_deleted_message_info ()

    int                 camel_db_count_deleted_message_info (CamelDB *cdb,
                                                             const char *table_name,
                                                             guint32 *count,
                                                             CamelException *ex);

`cdb` : |
`table_name` : |
`count` : |
`ex` : |
Returns : |

### camel_db_count_total_message_info ()

    int                 camel_db_count_total_message_info   (CamelDB *cdb,
                                                             const char *table_name,
                                                             guint32 *count,
                                                             CamelException *ex);

`cdb` : |
`table_name` : |
`count` : |
`ex` : |
Returns : |

### camel_db_count_visible_message_info ()

    int                 camel_db_count_visible_message_info (CamelDB *cdb,
                                                             const char *table_name,
                                                             guint32 *count,
                                                             CamelException *ex);

`cdb` : |
`table_name` : |
`count` : |
`ex` : |
Returns : |

### camel_db_count_visible_unread_message_info ()

    int                 camel_db_count_visible_unread_message_info
                                                            (CamelDB *cdb,
                                                             const char *table_name,
                                                             guint32 *count,
                                                             CamelException *ex);

`cdb` : |
`table_name` : |
`count` : |
`ex` : |
Returns : |

### camel_db_count_junk_not_deleted_message_info ()

    int                 camel_db_count_junk_not_deleted_message_info
                                                            (CamelDB *cdb,
                                                             const char *table_name,
                                                             guint32 *count,
                                                             CamelException *ex);

`cdb` : |
`table_name` : |
`count` : |
`ex` : |
Returns : |

### camel_db_camel_mir_free ()

    void                camel_db_camel_mir_free             (CamelMIRecord *record);

`record` : |

### camel_db_create_vfolder ()

    int                 camel_db_create_vfolder             (CamelDB *db,
                                                             const char *folder_name,
                                                             CamelException *ex);

`db` : |
`folder_name` : |
`ex` : |
Returns : |

### camel_db_recreate_vfolder ()

    int                 camel_db_recreate_vfolder           (CamelDB *db,
                                                             const char *folder_name,
                                                             CamelException *ex);

`db` : |
`folder_name` : |
`ex` : |
Returns : |

### camel_db_delete_uid_from_vfolder ()

    int                 camel_db_delete_uid_from_vfolder    (CamelDB *db,
                                                             char *folder_name,
                                                             char *vuid,
                                                             CamelException *ex);

`db` : |
`folder_name` : |
`vuid` : |
`ex` : |
Returns : |

### camel_db_delete_uid_from_vfolder_transaction ()

    int                 camel_db_delete_uid_from_vfolder_transaction
                                                            (CamelDB *db,
                                                             char *folder_name,
                                                             char *vuid,
                                                             CamelException *ex);

`db` : |
`folder_name` : |
`vuid` : |
`ex` : |
Returns : |

### camel_db_get_vuids_from_vfolder ()

    GPtrArray *         camel_db_get_vuids_from_vfolder     (CamelDB *db,
                                                             char *folder_name,
                                                             char *filter,
                                                             CamelException *ex);

`db` : |
`folder_name` : |
`filter` : |
`ex` : |
Returns : |

### camel_db_add_to_vfolder ()

    int                 camel_db_add_to_vfolder             (CamelDB *db,
                                                             char *folder_name,
                                                             char *vuid,
                                                             CamelException *ex);

`db` : |
`folder_name` : |
`vuid` : |
`ex` : |
Returns : |

### camel_db_add_to_vfolder_transaction ()

    int                 camel_db_add_to_vfolder_transaction (CamelDB *db,
                                                             char *folder_name,
                                                             char *vuid,
                                                             CamelException *ex);

`db` : |
`folder_name` : |
`vuid` : |
`ex` : |
Returns : |

### camel_db_get_folder_uids ()

    int                 camel_db_get_folder_uids            (CamelDB *db,
                                                             char *folder_name,
                                                             char *sort_by,
                                                             char *collate,
                                                             GPtrArray *array,
                                                             CamelException *ex);

`db` : |
`folder_name` : |
`sort_by` : |
`collate` : |
`array` : |
`ex` : |
Returns : |

### camel_db_get_folder_junk_uids ()

    GPtrArray *         camel_db_get_folder_junk_uids       (CamelDB *db,
                                                             char *folder_name,
                                                             CamelException *ex);

`db` : |
`folder_name` : |
`ex` : |
Returns : |

### camel_db_get_folder_deleted_uids ()

    GPtrArray *         camel_db_get_folder_deleted_uids    (CamelDB *db,
                                                             char *folder_name,
                                                             CamelException *ex);

`db` : |
`folder_name` : |
`ex` : |
Returns : |

### camel_db_sqlize_string ()

    char *              camel_db_sqlize_string              (const char *string);

`string` : |
Returns : |

### camel_db_free_sqlized_string ()

    void                camel_db_free_sqlized_string        (char *string);

`string` : |

### camel_db_get_column_name ()

    char *              camel_db_get_column_name            (const char *raw_name);

`raw_name` : |
Returns : |

### camel_db_set_collate ()

    int                 camel_db_set_collate                (CamelDB *cdb,
                                                             const char *col,
                                                             const char *collate,
                                                             CamelDBCollate func);

`cdb` : |
`col` : |
`collate` : |
`func` : |
Returns : |

### camel_db_migrate_vfolders_to_14 ()

    int                 camel_db_migrate_vfolders_to_14     (CamelDB *cdb,
                                                             const char *folder,
                                                             CamelException *ex);

`cdb` : |
`folder` : |
`ex` : |
Returns : |