2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file FIo_DataControlResultSetImpl.cpp
19 * @brief This is the implementation for the %_DataControlResultSetImpl class.
24 #include <sys/types.h>
26 #include <unique_ptr.h>
28 #include <FBaseInteger.h>
29 #include <FBaseDouble.h>
30 #include <FBaseString.h>
31 #include <FBaseByteBuffer.h>
32 #include <FBaseDateTime.h>
33 #include <FBaseUtilStringUtil.h>
34 #include <FBaseResult.h>
35 #include <FIoDatabase.h>
36 #include <FIoDbEnumerator.h>
37 #include <FBaseColAllElementsDeleter.h>
38 #include <FBaseSysLog.h>
40 #include <FBase_StringConverter.h>
41 #include <FBase_NativeError.h>
42 #include <FApp_AppControlManager.h>
43 #include <FApp_AppArg.h>
44 #include <FIo_FileImpl.h>
45 #include <FIo_DataControlResultSetImpl.h>
47 using namespace Tizen::Base;
48 using namespace Tizen::Base::Utility;
49 using namespace Tizen::Base::Collection;
50 using namespace Tizen::App;
52 const char* _DATACONTROL_RESULT_DIR = "/tmp/osp/data-control/result/\0";
53 const char* _DATACONTROL_RESULT_COMPAT_DIR = "/tmp/osp/DataControlResult/\0";
55 namespace Tizen { namespace Io
58 static const int _DATACONTROL_PACKET_INDEX_COLUMNCOUNT = 1;
60 _DataControlResultSetImpl::_DataControlResultSetImpl(RequestId reqId)
70 _DataControlResultSetImpl::~_DataControlResultSetImpl(void)
72 __constructed = false;
76 // [sizeof(int)] row count
77 // [sizeof(int)] column count
78 // [sieeof(int)] total size of column names
81 // [sizeof(int)] column type x N
82 // [ variant ] column name x N
87 // [ varient ] content
89 _DataControlResultSetImpl::FillWith(IDbEnumerator* pDbEnum, String& version)
97 String* pCount = null;
98 String* pColumnCount = null;
100 _DataControlRequestType requestType = _DATACONTROL_REQUEST_TYPE_UNDEFINED;
103 int totalSizeOfColumnTypes = 0;
104 int totalSizeOfColumnNames = 0;
105 int cursorOffset = 0;
107 int countPerPage = 0;
112 SysAssertf(__constructed == true, "Not yet constructed. Construct() should be called before use.\n\n");
114 _AppControlManager* pAppMgr = _AppControlManager::GetInstance();
115 SysTryReturnResult(NID_IO, pAppMgr, E_SYSTEM, "Failed to get instance.");
117 _ResultInfo* pResultInfo = pAppMgr->__resultManager.FindItem(static_cast< int >(__reqId)); // provider reqId
118 SysTryReturnResult(NID_IO, pResultInfo, E_OBJ_NOT_FOUND,
119 "The data control request specified with the req (%ld) did not exist.", __reqId);
121 const _AppArg& arg = pResultInfo->arg;
124 reqType = arg.GetValue(OSP_K_DATACONTROL_REQUEST_TYPE);
125 Integer::Parse(reqType, type);
126 requestType = static_cast< _DataControlRequestType >(type);
127 SysTryReturnResult(NID_IO, requestType == _DATACONTROL_REQUEST_TYPE_SQL_QUERY, E_INVALID_ARG,
128 "The reqId should be for the data control query request.");
130 callerAppId = arg.GetCallerAppId();
131 callerReqId = arg.GetValue(OSP_K_REQUEST_ID);
133 // list-based request
134 std::unique_ptr< ArrayList, AllElementsDeleter > pList(_AppArg::GetListN(arg.GetBundle(), OSP_K_ARG));
135 SysTryReturnResult(NID_IO, pList, E_SYSTEM, "Invalid result object.");
137 pColumnCount = dynamic_cast< String* >(pList->GetAt(_DATACONTROL_PACKET_INDEX_COLUMNCOUNT));
138 SysTryReturnResult(NID_IO, pColumnCount, E_SYSTEM, "Invalid result object.");
139 Integer::Parse(*pColumnCount, columnCount);
141 pageNoIndex = _DATACONTROL_PACKET_INDEX_COLUMNCOUNT + columnCount + 2 + 1;
143 pNo = dynamic_cast< String* >(pList->GetAt(pageNoIndex));
144 SysTryReturnResult(NID_IO, pNo, E_SYSTEM, "Invalid result object.");
145 Integer::Parse(*pNo, pageNo);
147 pCount = dynamic_cast< String* >(pList->GetAt(pageNoIndex + 1));
148 SysTryReturnResult(NID_IO, pCount, E_SYSTEM, "Invalid result object.");
149 Integer::Parse(*pCount, countPerPage);
151 this->SetCapacity(pageNo, countPerPage);
152 if (version == L"ver_2.1.0.3")
154 tempFilePath.Append(_DATACONTROL_RESULT_DIR);
158 tempFilePath.Append(_DATACONTROL_RESULT_COMPAT_DIR);
160 tempFilePath.Append(callerAppId);
161 tempFilePath.Append(callerReqId);
162 __tmpPath = tempFilePath;
163 SysSecureLog(NID_IO, "[DC_PROV_SEND] temp file path: %ls", tempFilePath.GetPointer());
166 r = pDbEnum->Reset();
167 SysTryReturnResult(NID_IO, r == E_SUCCESS, E_DATABASE,
168 "The database engine has failed to execute reset.");
170 cursorOffset = (__pageNo - 1) * __countPerPage;
171 for (i = 0; i < cursorOffset; i++) // move cursor to the specific page to be requested
173 r = pDbEnum->MoveNext();
174 if (r == E_OUT_OF_RANGE) // the previous pages do not exist
176 __result = E_OUT_OF_RANGE;
177 SysTryReturnResult(NID_IO, false, E_SUCCESS, "Out of range error.");
179 SysTryReturnResult(NID_IO, !IsFailed(r), E_DATABASE,
180 "The database engine has failed to enumerator result set.");
183 // XXX: must call MoveNext() before calling GetColumnCount()
184 r = pDbEnum->MoveNext();
185 if (r == E_OUT_OF_RANGE) // the specific page is empty
187 __result = E_OUT_OF_RANGE;
188 SysTryReturnResult(NID_IO, false, E_SUCCESS, "Out of range error.");
190 SysTryReturnResult(NID_IO, !IsFailed(r), E_DATABASE,
191 "The database engine has failed to enumerate the result set.");
193 // if has the result set
194 std::unique_ptr<File> pFile(new (std::nothrow) File());
196 SysTryReturnResult(NID_IO, pFile != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
198 r = pFile->Construct(__tmpPath, L"w+", true);
199 SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] Failed to create temp file (%ls) for result set.",
200 GetErrorMessage(r), __tmpPath.GetPointer());
202 columnCount = pDbEnum->GetColumnCount();
203 SysSecureLog(NID_IO, "column count is %d.\n", columnCount);
205 r = pFile->Seek(FILESEEKPOSITION_BEGIN, sizeof(int));
206 SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] System error was occurred.", GetErrorMessage(r));
208 r = pFile->Write(&columnCount, sizeof(int)); // pack column count
209 SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] System error was occurred.", GetErrorMessage(r));
211 r = pFile->Seek(FILESEEKPOSITION_CURRENT, sizeof(int));
212 SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] System error was occurred.", GetErrorMessage(r));
215 for (i = 0; i < columnCount; i++)
218 DbColumnType columnType = pDbEnum->GetColumnType(i);
221 case DB_COLUMNTYPE_INT:
223 type = 1; // int64 type
227 case DB_COLUMNTYPE_DOUBLE:
229 type = 2; // double type
233 case DB_COLUMNTYPE_TEXT:
235 type = 3; // text type
239 case DB_COLUMNTYPE_BLOB:
241 type = 4; // blob type
245 case DB_COLUMNTYPE_NULL:
247 type = 5; // null type
252 SysLog(NID_IO, "type: UNDEFINED (%d)", type);
255 r = pFile->Write(&type, sizeof(int));
256 SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] System error was occurred.", GetErrorMessage(r));
258 totalSizeOfColumnTypes += sizeof(int);
260 //totalSizeOfColumnTypes = columnCount * sizeof(int);
262 totalSizeOfColumnNames = 0;
263 for (i = 0; i < columnCount; i++)
265 char* pColumnName = null;
268 columnName = pDbEnum->GetColumnName(i);
269 columnName.Append('\n');
270 r = pFile->Write(columnName);
271 SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] System error was occurred.", GetErrorMessage(r));
273 pColumnName = _StringConverter::CopyToCharArrayN(columnName);
274 byte = strlen(pColumnName);
275 totalSizeOfColumnNames += byte;
277 SysSecureLog(NID_IO, "[%d] column name: %s", i, pColumnName);
278 delete[] pColumnName;
280 r = pFile->Seek(FILESEEKPOSITION_BEGIN, sizeof(int) * 2);
281 SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] System error was occurred.", GetErrorMessage(r));
283 r = pFile->Write(&totalSizeOfColumnNames, sizeof(int)); // pack
284 SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] System error was occurred.", GetErrorMessage(r));
286 r = pFile->Seek(FILESEEKPOSITION_CURRENT, totalSizeOfColumnTypes + totalSizeOfColumnNames);
287 SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] System error was occurred.", GetErrorMessage(r));
291 for (i = 0; i < columnCount; i++)
293 DbColumnType columnType = pDbEnum->GetColumnType(i);
296 case DB_COLUMNTYPE_INT:
298 long long int64Value = 0;
299 int type = 1; // int64
302 r = pDbEnum->GetInt64At(i, int64Value);
303 SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] System error was occurred.", GetErrorMessage(r));
305 size = sizeof(long long);
307 r = pFile->Write(&type, sizeof(int));
308 SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] System error was occurred.", GetErrorMessage(r));
310 r = pFile->Write(&size, sizeof(int));
311 SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] System error was occurred.", GetErrorMessage(r));
312 r = pFile->Write(&int64Value, sizeof(long long));
313 SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] System error was occurred.", GetErrorMessage(r));
317 case DB_COLUMNTYPE_DOUBLE:
319 double doubleValue = 0;
320 int type = 2; // double
323 r = pDbEnum->GetDoubleAt(i, doubleValue);
324 SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] System error was occurred.", GetErrorMessage(r));
326 size = sizeof(double);
328 r = pFile->Write(&type, sizeof(int));
329 SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] System error was occurred.", GetErrorMessage(r));
331 r = pFile->Write(&size, sizeof(int));
332 SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] System error was occurred.", GetErrorMessage(r));
334 r = pFile->Write(&doubleValue, sizeof(double));
335 SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] System error was occurred.", GetErrorMessage(r));
339 case DB_COLUMNTYPE_TEXT:
342 int type = 3; // text
345 r = pDbEnum->GetStringAt(i, textValue);
346 SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] System error was occurred.", GetErrorMessage(r));
348 std::unique_ptr<char[]> pContent(_StringConverter::CopyToCharArrayN(textValue));
349 size = strlen(pContent.get());
351 r = pFile->Write(&type, sizeof(int));
352 SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] System error was occurred.", GetErrorMessage(r));
354 r = pFile->Write(&size, sizeof(int));
355 SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] System error was occurred.", GetErrorMessage(r));
359 r = pFile->Write(pContent.get(), size);
360 SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] System error was occurred.", GetErrorMessage(r));
365 case DB_COLUMNTYPE_BLOB: // XXX: need test !!
367 ByteBuffer blobValue;
368 int type = 4; // blob
371 size = pDbEnum->GetColumnSize(i);
372 r = blobValue.Construct(size);
373 SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] System error was occurred.", GetErrorMessage(r));
375 r = pDbEnum->GetBlobAt(i, blobValue);
376 SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] System error was occurred.", GetErrorMessage(r));
378 r = pFile->Write(&type, sizeof(int));
379 SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] System error was occurred.", GetErrorMessage(r));
381 r = pFile->Write(&size, sizeof(int));
382 SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] System error was occurred.", GetErrorMessage(r));
386 r = pFile->Write(blobValue);
387 SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] System error was occurred.", GetErrorMessage(r));
392 case DB_COLUMNTYPE_NULL:
394 int type = 5; // null
397 r = pFile->Write(&type, sizeof(int));
398 SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] System error was occurred.", GetErrorMessage(r));
400 r = pFile->Write(&size, sizeof(int));
401 SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] System error was occurred.", GetErrorMessage(r));
406 SysTryReturnResult(NID_IO, false, E_DATABASE,
407 "The column type is invalid.");
413 while (pDbEnum->MoveNext() == E_SUCCESS && rowCount < __countPerPage);
415 SysLog(NID_IO, "row count is %d.\n", rowCount);
416 r = pFile->Seek(FILESEEKPOSITION_BEGIN, 0);
417 SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] System error was occurred.", GetErrorMessage(r));
419 r = pFile->Write(&rowCount, sizeof(int)); // pack row count
420 SysTryReturn(NID_IO, r == E_SUCCESS, r, r, "[%s] System error was occurred.", GetErrorMessage(r));
426 _DataControlResultSetImpl::SetCapacity(int pageNo, int countPerPage)
428 result r = E_SUCCESS;
430 SysAssertf(__constructed == true, "Not yet constructed. Construct() should be called before use.\n\n");
433 __countPerPage = countPerPage;