summaryrefslogtreecommitdiff
path: root/common/src/vp-db-util.c
blob: 56225175a69c9529334c3f0bfcf9274f0cf1df76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* Copyright (c) 2000-2015 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#include "vp-db-common.h"
#include "vp-db-util.h"

#define VIDEO_THUMB_DB_NAME "/opt/usr/apps/org.tizen.videos/data/.video-thumb.db"

static sqlite3 *g_sqlite3_handle = NULL;
static int g_nRefCount = 0;

/* callback functions */
static int __vp_db_util_busy_handler(void *pData, int count)
{
	usleep(50000);

	vp_dbgW("__vp_db_util_busy_handler called : %d", count);

	return 100 - count;
}

/* internal functions */
static sqlite3 *_vp_db_util_connect_db()
{
	sqlite3 *pSqlite = NULL;
	int ret = SQLITE_OK;

	/*Connect DB */
	ret = sqlite3_open(VIDEO_THUMB_DB_NAME, &pSqlite);
	if (SQLITE_OK != ret) {
		vp_dbgE("sqlite3_open fail [0x%x] : %s", ret,
			sqlite3_errmsg(pSqlite));
		return NULL;
	}

	ret = sqlite3_busy_handler(pSqlite, __vp_db_util_busy_handler, NULL);
	if (SQLITE_OK != ret) {
		vp_dbgE("sqlite3_busy_handler fail [0x%x]", ret);
		sqlite3_close(pSqlite);
		return NULL;
	}

	return pSqlite;
}

static void _vp_db_util_disconnect_db(sqlite3 *db_handle)
{
	if (db_handle == NULL) {
		vp_dbgE("db_handle is NULL");
		return;
	}
	int ret = SQLITE_OK;
	ret = sqlite3_close(db_handle);
	if (SQLITE_OK != ret) {
		vp_dbgE("sqlite3_close fail [0x%x]", ret);
		db_handle = NULL;
	}
}


/* external functions */
bool vp_db_util_connect()
{
	if (g_sqlite3_handle) {
		vp_dbgW("database is already connect");
		g_nRefCount++;
		return TRUE;
	}

	g_sqlite3_handle = _vp_db_util_connect_db();
	if (g_sqlite3_handle == NULL) {
		vp_dbgE("database connect is fail");
		return FALSE;
	}

	g_nRefCount++;

	return TRUE;
}

void vp_db_util_disconnect()
{
	g_nRefCount--;

	if (g_nRefCount < 0) {
		g_nRefCount = 0;
	}

	if (g_nRefCount == 0) {
		if (g_sqlite3_handle == NULL) {
			vp_dbgW("database is already disconnect");
			return;
		}
		_vp_db_util_disconnect_db(g_sqlite3_handle);

		g_sqlite3_handle = NULL;
	}

	return;

}

bool vp_db_util_query_prepare(char *szQuery, sqlite3_stmt **stmt)
{
	if (g_sqlite3_handle == NULL) {
		vp_dbgE("database is not connect");
		return FALSE;
	}

	int err = -1;

	sqlite3 *handle = g_sqlite3_handle;
	vp_dbg("SQL = %s\n", szQuery);

	err = sqlite3_prepare_v2(handle, szQuery, -1, stmt, NULL);
	if (SQLITE_OK != err) {
		vp_dbgE("Query fails [0x%x]: query_string[%s] ", err, szQuery);
		return FALSE;
	}

	vp_dbg("query success\n");

	return TRUE;
}