summaryrefslogtreecommitdiff
path: root/tvapi/libtv/tvutils/CSqlite.h (plain)
blob: 049f250eb661bd248a93b8b478c75b2a2301e4e5
1//
2//
3// amlogic 2015
4//
5// @ Project : tv
6// @ File Name : CSqlite
7// @ Date : 2015-5
8// @ Author :
9//
10//
11#if !defined(_CSQLITE_H_)
12#define _CSQLITE_H_
13#include <unistd.h>
14#include <stdlib.h>
15#include <utils/String8.h>
16#include <utils/Vector.h>
17#include <utils/RefBase.h>
18#include "CTvLog.h"
19#include <sqlite3.h>
20using namespace android;
21class CSqlite {
22public:
23 class Cursor {
24 public:
25 void Init(char **data, int cow, int col)
26 {
27 mData = data;
28 mCurRowIndex = 0;
29 mRowNums = cow;
30 mColNums = col;
31 mIsClosed = false;
32 }
33 Cursor()
34 {
35 mData = NULL;
36 mCurRowIndex = 0;
37 mRowNums = 0;
38 mColNums = 0;
39 mIsClosed = false;
40 }
41
42 /*Cursor(Cursor& c)
43 {
44 data = c.data;
45 mCurRowIndex = 0;
46 mRowNums = c.mRowNums;
47 mColNums = c.mColNums;
48 mIsClosed = false;
49 }
50
51 Cursor& operator = (const Cursor& c)
52 {
53 data = c.data;
54 mCurRowIndex = 0;
55 mRowNums = c.mRowNums;
56 mColNums = c.mColNums;
57 mIsClosed = false;
58 return *this;
59 }*/
60 ~Cursor()
61 {
62 close();
63 }
64 //Row nums
65 int getCount()
66 {
67 return mRowNums;
68 }
69
70 int getPosition();
71
72 bool move(int offset);
73
74 bool moveToPosition(int position);
75
76 bool moveToFirst()
77 {
78 //LOGD("moveToFirst mRowNums = %d", mRowNums);
79 if (mRowNums <= 0) return false;
80 mCurRowIndex = 0;
81 return true;
82 }
83
84 bool moveToLast();
85
86 bool moveToNext()
87 {
88 if (mCurRowIndex >= mRowNums - 1)return false;
89 mCurRowIndex++;
90 return true;
91 }
92
93 bool moveToPrevious();
94
95 int getColumnIndex(const char *columnName)
96 {
97 int index = 0;
98 for (int i = 0; i < mColNums; i++) {
99 if (strcmp(columnName, mData[i]) == 0)
100 return index;
101 index++;
102 }
103
104 return -1;
105 }
106
107 //String getColumnName(int columnIndex);
108 //String[] getColumnNames();
109 int getColumnCount();
110 //字符串长度问题,弃用
111 int getString(char *str, int columnIndex)
112 {
113 if (columnIndex >= mColNums || str == NULL) return -1;
114 strcpy(str, mData[mColNums * (mCurRowIndex + 1) + columnIndex]);
115 return 0;
116 }
117 //不限长度,依赖于String8
118 String8 getString(int columnIndex)
119 {
120 if (columnIndex >= mColNums) return String8("");
121 return String8(mData[mColNums * (mCurRowIndex + 1) + columnIndex]);
122 }
123
124 int getInt(int columnIndex)
125 {
126 //if(columnIndex >= mColNums || str == NULL) return -1;
127 return atoi(mData[mColNums * (mCurRowIndex + 1) + columnIndex]);
128 }
129 unsigned long int getUInt(int columnIndex)
130 {
131 return strtoul(mData[mColNums * (mCurRowIndex + 1) + columnIndex], NULL, 10);
132 }
133 double getF(int columnIndex)
134 {
135 return atof(mData[mColNums * (mCurRowIndex + 1) + columnIndex]);
136 }
137 int getType(int columnIndex);
138 void close()
139 {
140 if (mData != NULL)
141 sqlite3_free_table(mData);
142
143 mData = NULL;
144 mCurRowIndex = 0;
145 mRowNums = 0;
146 mIsClosed = true;
147 }
148 bool isClosed()
149 {
150 return mIsClosed;
151 }
152 private:
153 char **mData;
154 int mCurRowIndex;
155 int mRowNums;
156 int mColNums;
157 bool mIsClosed;
158 };
159public:
160 CSqlite();
161 virtual ~CSqlite();
162 int openDb(const char *path);
163 int closeDb();
164 void setHandle(sqlite3 *h);
165 sqlite3 *getHandle();
166 bool integrityCheck();
167 int select(const char *sql, Cursor &);
168 bool exeSql(const char *sql);
169 void insert();
170 void del();
171 void update();
172 void xxtable();
173 bool beginTransaction();
174 bool commitTransaction();
175 bool rollbackTransaction();
176 void dbsync()
177 {
178 sync();
179 };
180private:
181 static int sqlite3_exec_callback(void *data, int nColumn, char **colValues, char **colNames);
182 sqlite3 *mHandle;
183};
184#endif //CSQLITE
185