Tizen 2.1 base
[platform/framework/web/download-provider.git] / provider / include / download-provider-db.h
1 /*
2  * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef DOWNLOAD_PROVIDER2_DB_H
18 #define DOWNLOAD_PROVIDER2_DB_H
19
20 #include "download-provider-config.h"
21 #include "download-provider-slots.h"
22
23 /*
24  * Memory ( sync with logging ) : id, state, errorcode, startcount, packagename
25  * DB TABLES
26  *                              logging                 : id, state, errorcode, startcount, createtime, accesstime, packagename
27  *                              requestinfo             : id, auto_download, network_type, filename, destination, url
28  *                              downloadinfo    : id, http_status, content_size, mimetype, contentname, saved_path, tmp_saved_path, etag
29  *                              httpheaders             : id, header_field, header_data
30  *                              notification    : id, noti_enable, extra_key, extra_data
31  */
32 /*
33 CREATE TABLE logging
34 (
35         id              INTEGER UNIQUE PRIMARY KEY,
36         state           INTEGER DEFAULT 0,
37         errorcode       INTEGER DEFAULT 0,
38         startcount      INTEGER DEFAULT 0,
39         packagename     TEXT DEFAULT NULL,
40         createtime      DATE,
41         accesstime      DATE
42 );
43
44 CREATE TABLE requestinfo
45 (
46         id              INTEGER UNIQUE PRIMARY KEY,
47         auto_download   BOOLEAN DEFAULT 0,
48         state_event     BOOLEAN DEFAULT 0,
49         progress_event  BOOLEAN DEFAULT 0,
50         network_type    TINYINT DEFAULT 0,
51         filename        TEXT DEFAULT NULL,
52         destination     TEXT DEFAULT NULL,
53         url             TEXT DEFAULT NULL,
54         FOREIGN KEY(id) REFERENCES logging(id) ON DELETE CASCADE
55 );
56
57 CREATE TABLE downloadinfo
58 (
59         id              INTEGER UNIQUE PRIMARY KEY,
60         http_status     INTEGER DEFAULT 0,
61         content_size    UNSIGNED BIG INT DEFAULT 0,
62         mimetype        VARCHAR(64) DEFAULT NULL,
63         content_name    TEXT DEFAULT NULL,
64         saved_path      TEXT DEFAULT NULL,
65         tmp_saved_path  TEXT DEFAULT NULL,
66         etag            TEXT DEFAULT NULL,
67         FOREIGN KEY(id) REFERENCES logging(id) ON DELETE CASCADE
68 );
69
70 CREATE TABLE httpheaders
71 (
72         id              INTEGER NOT NULL,
73         header_field    TEXT DEFAULT NULL,
74         header_data     TEXT DEFAULT NULL,
75         FOREIGN KEY(id) REFERENCES logging(id) ON DELETE CASCADE
76 );
77
78 CREATE TABLE notification
79 (
80         id              INTEGER UNIQUE PRIMARY KEY,
81         noti_enable     BOOLEAN DEFAULT 0,
82         extra_key       TEXT DEFAULT NULL,
83         extra_data      TEXT DEFAULT NULL,
84         FOREIGN KEY(id) REFERENCES logging(id) ON DELETE CASCADE
85 );
86
87 CREATE UNIQUE INDEX requests_index ON logging (id, state, errorcode, packagename, createtime, accesstime);
88 */
89
90 #define DP_DB_TABLE_LOG "logging"
91 #define DP_DB_TABLE_REQUEST_INFO "requestinfo"
92 #define DP_DB_TABLE_DOWNLOAD_INFO "downloadinfo"
93 #define DP_DB_TABLE_HTTP_HEADERS "httpheaders"
94 #define DP_DB_TABLE_NOTIFICATION "notification"
95
96 #define DP_DB_COL_ID "id"
97 #define DP_DB_COL_STATE "state"
98 #define DP_DB_COL_ERRORCODE "errorcode"
99 #define DP_DB_COL_NETWORK_TYPE "network_type"
100 #define DP_DB_COL_HTTP_STATUS "http_status"
101 #define DP_DB_COL_AUTO_DOWNLOAD "auto_download"
102 #define DP_DB_COL_STATE_EVENT "state_event"
103 #define DP_DB_COL_PROGRESS_EVENT "progress_event"
104 #define DP_DB_COL_CONTENT_SIZE "content_size"
105 #define DP_DB_COL_CREATE_TIME "createtime"
106 #define DP_DB_COL_ACCESS_TIME "accesstime"
107 #define DP_DB_COL_STARTCOUNT "startcount"
108 #define DP_DB_COL_PACKAGENAME "packagename"
109 #define DP_DB_COL_DESTINATION "destination"
110 #define DP_DB_COL_FILENAME "filename"
111 #define DP_DB_COL_CONTENT_NAME "content_name"
112 #define DP_DB_COL_MIMETYPE "mimetype"
113 #define DP_DB_COL_ETAG "etag"
114 #define DP_DB_COL_SAVED_PATH "saved_path"
115 #define DP_DB_COL_TMP_SAVED_PATH "tmp_saved_path"
116 #define DP_DB_COL_URL "url"
117 #define DP_DB_COL_HEADER_FIELD "header_field"
118 #define DP_DB_COL_HEADER_DATA "header_data"
119 #define DP_DB_COL_NOTIFICATION_ENABLE "noti_enable"
120 #define DP_DB_COL_EXTRA_KEY "extra_key"
121 #define DP_DB_COL_DISTINCT_EXTRA_KEY "DISTINCT extra_key"
122 #define DP_DB_COL_EXTRA_VALUE "extra_data"
123
124 typedef enum {
125         DP_DB_COL_TYPE_NONE = 0,
126         DP_DB_COL_TYPE_INT = 10,
127         DP_DB_COL_TYPE_INT64 = 20,
128         DP_DB_COL_TYPE_TEXT = 30
129 } db_column_data_type;
130
131 typedef struct {
132         char *column;
133         db_column_data_type type;
134         int is_like;
135         void *value;
136 } db_conds_list_fmt;
137
138 int dp_db_open();
139 void dp_db_close();
140
141 int dp_db_remove_all(int id);
142 int dp_db_remove(int id, char *table);
143 int dp_db_insert_column(int id, char *table, char *column,
144                                                 db_column_data_type datatype, void *value);
145 int dp_db_set_column(int id, char *table, char *column,
146                                                 db_column_data_type datatype, void *value);
147 int dp_db_replace_column(int id, char *table, char *column,
148                                                 db_column_data_type datatype, void *value);
149 char *dp_db_get_text_column(int id, char *table, char *column);
150 int dp_db_get_int_column(int id, char *table, char *column);
151 long long dp_db_get_int64_column(int id, char *table, char *column);
152 int dp_db_update_date(int id, char *table, char *column);
153
154 // cond : id & cond
155 int dp_db_cond_set_column(int id, char *table, char *column,
156                                                 db_column_data_type datatype, void *value,
157                                                 char *condcolumn, db_column_data_type condtype,
158                                                 void *condvalue);
159 char *dp_db_cond_get_text_column(int id, char *table, char *column,
160                                                 char *condcolumn, db_column_data_type condtype,
161                                                 void *condvalue);
162 int dp_db_cond_remove(int id, char *table,
163                                                 char *condcolumn, db_column_data_type condtype,
164                                                 void *condvalue);
165 int dp_db_get_cond_rows_count(int id, char *table,
166                                                 char *condcolumn, db_column_data_type condtype,
167                                                 void *condvalue);
168
169 // Special API for http headers
170 int dp_db_get_http_headers_list(int id, char **headers);
171
172 // For auto-download in booting time
173 int dp_db_crashed_list(dp_request_slots *requests, int limit);
174
175 // For loading to memory when no in memory
176 dp_request *dp_db_load_logging_request(int id);
177
178 // For limitation by 48 hours & 1000 rows
179 int dp_db_limit_rows(int limit);
180 int dp_db_get_count_by_limit_time();
181 int dp_db_get_list_by_limit_time(dp_request_slots *requests, int limit);
182
183 int dp_db_insert_columns(char *table, int column_count,
184                                                 db_conds_list_fmt *columns);
185
186 int dp_db_get_conds_rows_count(char *table, char *getcolumn, char *op,
187                                                 int conds_count, db_conds_list_fmt *conds);
188
189 int dp_db_get_conds_list(char *table, char *getcolumn,
190                                                 db_column_data_type gettype, void **list,
191                                                 int rowslimit, int rowsoffset,
192                                                 char *ordercolumn, char *ordering,
193                                                 char *op, int conds_count,
194                                                 db_conds_list_fmt *conds);
195 #endif