2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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 HmGenericDatabaseManager.cpp
19 * @brief Keeps the Implementation of GenericDatabaseManager and related interfaces
20 * declaration of the various functions which may be needed for various database operations
24 #include "HmGenericDatabaseManager.h"
27 using namespace Tizen::Base;
28 using namespace Tizen::Base::Collection;
29 using namespace Tizen::Io;
31 GenericDatabaseManager* GenericDatabaseManager::__pDatabaseManager = null;
34 GenericDatabaseManager::DestroyInstance(void)
36 if (__pDatabaseManager != null)
38 delete __pDatabaseManager;
39 __pDatabaseManager = null;
43 GenericDatabaseManager*
44 GenericDatabaseManager::GetInstance(void)
46 if (__pDatabaseManager == null)
50 return __pDatabaseManager;
54 GenericDatabaseManager::CreateDataBase(const String& databaseName)
56 result r = E_INVALID_ARG;
57 String folderName(databaseName);
58 int indexOfDirectory = 0;
60 TryReturn(!databaseName.IsEmpty(), r, "Invalid argument passed");
62 if (__pDatabase != null)
64 if (__pDatabase->GetName().Equals(databaseName, true))
75 folderName.LastIndexOf(L'/', databaseName.GetLength() - 1, indexOfDirectory);
76 folderName.Remove(indexOfDirectory, (folderName.GetLength() - indexOfDirectory));
77 Directory::Create(folderName, true);
78 __pDatabase = new (std::nothrow) Database();
79 TryReturn(__pDatabase != null, E_OUT_OF_MEMORY, "unable to allocate memory for Database");
80 r = __pDatabase->Construct(databaseName, "a+");
81 TryCatch(r == E_SUCCESS, , "__pDatabase->Construct(databaseName, a+) failed with error %s", GetErrorMessage(r));
87 if (__pDatabase != null)
97 GenericDatabaseManager::CreateTable(const String& tableName, ArrayList* pColumnNames, ArrayList* pColumnTypes)
99 result r = E_INVALID_ARG;
101 TryReturn((!tableName.IsEmpty() || pColumnNames != null || pColumnTypes != null), r, "Invalid arguments");
103 if (__pDatabase != null)
105 String queryText(CREATE_TABLE);
106 queryText.Append(tableName);
107 queryText.Append(L"(");
109 for (int paramCount = 0; paramCount < pColumnNames->GetCount(); paramCount++)
111 String* name = static_cast<String*>(pColumnNames->GetAt(paramCount));
112 String* type = static_cast<String*>(pColumnTypes->GetAt(paramCount));
114 queryText.Append(*name);
115 queryText.Append(L" ");
116 queryText.Append(*type);
118 if (paramCount == pColumnNames->GetCount() - 1)
120 queryText.Append(L")");
124 queryText.Append(L",");
128 r = __pDatabase->BeginTransaction();
132 r = __pDatabase->ExecuteSql(queryText, true);
136 __pDatabase->RollbackTransaction();
138 TryReturn(r == E_SUCCESS, r,
139 "__pDatabase->ExecuteSql(queryStr, true) failed %s", GetErrorMessage(r));
140 r = __pDatabase->CommitTransaction();
141 TryReturn(r == E_SUCCESS, r,
142 "__pDatabase->CommitTransaction() failed %s", GetErrorMessage(r));
149 GenericDatabaseManager::DeleteFromTable(const String& tableName, const String& whereCondition)
151 result r = E_INVALID_ARG;
153 TryReturn(!tableName.IsEmpty() || !whereCondition.IsEmpty(), r, "Invalid argument passed");
154 String query(DELETE_TABLE);
155 query.Append(tableName);
157 query.Append(L"WHERE ");
158 query.Append(whereCondition);
160 if (__pDatabase != null)
162 __pDatabase->BeginTransaction();
163 r = __pDatabase->ExecuteSql(query, true);
167 AppLogException("__pDatabase->ExecuteSql(query,true); returned with %s", GetErrorMessage(r));
168 __pDatabase->RollbackTransaction();
172 __pDatabase->CommitTransaction();
179 GenericDatabaseManager::GetDataRowFromTable(const String& tableName, const String& whereCondition, Collection::HashMap& rowDataHashMap)
181 result r = E_SUCCESS;
182 // Prepare Column info
183 DbEnumerator* pDbEnumerator = null;
184 String query = SELECT_TABLE;
186 query.Append(tableName);
188 query.Append(L"WHERE ");
189 query.Append(whereCondition);
190 pDbEnumerator = __pDatabase->QueryN(query);
192 if (pDbEnumerator == null)
194 AppLogException("GetDataRowFromTable returning error %s", GetErrorMessage(GetLastResult()));
195 return GetLastResult();
198 pDbEnumerator->MoveNext();
200 for (int paramCount = 0; paramCount < pDbEnumerator->GetColumnCount(); paramCount++)
202 Object* pValue = null;
203 DbColumnType columnType = pDbEnumerator->GetColumnType(paramCount);
204 String* pColumnName = null;
206 if (columnType == DB_COLUMNTYPE_INT)
209 r = pDbEnumerator->GetIntAt(paramCount, tempIntVal);
210 // TryContinue(retVal, pValue,
211 // "pDbEnumerator->GetIntAt failed with error %s",GetErrorMessage(retVal));
212 pValue = new (std::nothrow) Integer(tempIntVal);
219 else if (columnType == DB_COLUMNTYPE_INT64)
221 long long tempLongVal = 0;
222 r = pDbEnumerator->GetInt64At(paramCount, tempLongVal);
223 // TryContinue(retVal, pLongValue,
224 // "pDbEnumerator->GetInt64At failed with error %s",GetErrorMessage(retVal));
225 pValue = new (std::nothrow) LongLong(tempLongVal);
232 else if (columnType == DB_COLUMNTYPE_DOUBLE)
234 double tempDoubleVal;
235 r = pDbEnumerator->GetDoubleAt(paramCount, tempDoubleVal);
236 // TryContinue(retVal, pDoubleValue,
237 // "pDbEnumerator->GetDoubleAt failed with error %s",GetErrorMessage(retVal));
238 pValue = new (std::nothrow) Double(tempDoubleVal);
246 else if (columnType == DB_COLUMNTYPE_TEXT)
248 String* pTextValue = new (std::nothrow) String();
250 if (pTextValue == null)
254 r = pDbEnumerator->GetStringAt(paramCount, *pTextValue);
264 else if (columnType == DB_COLUMNTYPE_BLOB)
266 ByteBuffer* pBlobValue = new (std::nothrow) ByteBuffer();
268 if (pBlobValue == null)
273 r = pDbEnumerator->GetBlobAt(paramCount, *pBlobValue);
287 pColumnName = new (std::nothrow) String(pDbEnumerator->GetColumnName(paramCount));
289 if (pColumnName == null)
297 r = rowDataHashMap.Add(pColumnName, pValue);
301 delete pDbEnumerator;
302 pDbEnumerator = null;
308 GenericDatabaseManager::GetLastAddedRowID(const String& tableName)
311 TryReturn(!tableName.IsEmpty(), E_INVALID_ARG, "Invalid arguments");
313 if (__pDatabase != null)
315 String query = GET_LAST_RECORD_ID;
316 DbEnumerator* pDbEnumerator = null;
317 query.Append(tableName);
318 pDbEnumerator = __pDatabase->QueryN(query);
320 if (pDbEnumerator != null)
322 while (pDbEnumerator->MoveNext() == E_SUCCESS)
324 for (int paramCount = 0; paramCount < pDbEnumerator->GetColumnCount(); paramCount++)
326 DbColumnType columnType = pDbEnumerator->GetColumnType(paramCount);
328 if (columnType == DB_COLUMNTYPE_INT)
330 pDbEnumerator->GetIntAt(paramCount, rowId);
336 delete pDbEnumerator;
337 pDbEnumerator = null;
345 GenericDatabaseManager::InsertToTable(const String& tableName, ArrayList* pColumnNames, ArrayList* pColumnTypes, ArrayList* pColumnValues)
347 result r = E_SUCCESS;
348 TryReturn((!tableName.IsEmpty() || pColumnNames != null || pColumnValues != null), E_INVALID_ARG, "Invalid arguments");
350 if (__pDatabase != null)
352 DbEnumerator* pDbEnum = null;
353 DbStatement* pDbStatement = null;
354 String query(INSERT_TABLE);
355 query.Append(tableName);
357 //Prepare Transaction
358 r = __pDatabase->BeginTransaction();
362 for (int paramCount = 0; paramCount < pColumnNames->GetCount(); paramCount++)
364 String* name = static_cast<String*>(pColumnNames->GetAt(paramCount));
367 if (paramCount == pColumnNames->GetCount() - 1)
378 query.Append(L"VALUES");
381 for (int paramCount = 0; paramCount < pColumnValues->GetCount(); paramCount++)
383 query.Append(L" ? ");
385 if (paramCount == pColumnNames->GetCount() - 1)
395 pDbStatement = __pDatabase->CreateStatementN(query);
397 if (pDbStatement != null)
399 for (int colIndex = 0; colIndex < pColumnValues->GetCount(); colIndex++)
402 Integer* columnInttype = static_cast<Integer*>(pColumnTypes->GetAt(colIndex));
404 DbColumnType columnType = (DbColumnType) columnInttype->ToInt();
406 if (columnType == DB_COLUMNTYPE_INT)
408 Integer* pIntValue = static_cast<Integer*>(pColumnValues->GetAt(colIndex));
410 if (pIntValue != null && pIntValue->ToInt() == -1)
412 pDbStatement->BindNull(colIndex);
416 pDbStatement->BindInt(colIndex, pIntValue->ToInt());
419 else if (columnType == DB_COLUMNTYPE_INT64)
421 LongLong* pLongValue = static_cast<LongLong*>(pColumnValues->GetAt(colIndex));
422 pDbStatement->BindInt64(colIndex, pLongValue->ToLong());
424 else if (columnType == DB_COLUMNTYPE_DOUBLE)
426 Double* pDoubleValue = static_cast<Double*>(pColumnValues->GetAt(colIndex));
427 pDbStatement->BindDouble(colIndex, pDoubleValue->ToDouble());
429 else if (columnType == DB_COLUMNTYPE_TEXT)
431 String* pTextValue = static_cast<String*>(pColumnValues->GetAt(colIndex));
432 pDbStatement->BindString(colIndex, *pTextValue);
434 else if (columnType == DB_COLUMNTYPE_BLOB)
436 ByteBuffer* pBlobValue = static_cast<ByteBuffer*>(pColumnValues->GetAt(colIndex));
437 pDbStatement->BindBlob(colIndex, *pBlobValue);
445 pDbEnum = __pDatabase->ExecuteStatementN(*pDbStatement);
447 if (GetLastResult() == E_SUCCESS)
449 r = __pDatabase->CommitTransaction();
453 r = __pDatabase->RollbackTransaction();
458 AppLogDebug("__pDatabase->CommitTransaction() failed result = %s", GetErrorMessage(r));
459 __pDatabase->RollbackTransaction();
462 if (pDbStatement != null)
477 r = __pDatabase->RollbackTransaction();
482 r = __pDatabase->RollbackTransaction();
490 GenericDatabaseManager::UpdateTable(const String& tableName, ArrayList* pColumnsToModify, ArrayList* pColumnTypes, ArrayList* pColumnsNewData, const String* pWhereCondition)
492 TryReturn((!tableName.IsEmpty() || pColumnsToModify != null || pColumnsNewData != null || pWhereCondition != null), E_INVALID_ARG,
493 "Invalid arguments");
495 result r = E_SUCCESS;
496 String query = UPDATE_TABLE;
497 query.Append(tableName);
498 query.Append(L" SET ");
500 for (int columnNumber = 0; columnNumber < pColumnsToModify->GetCount(); columnNumber++)
502 String* pColName = static_cast<String*>(pColumnsToModify->GetAt(columnNumber));
503 query.Append(*pColName);
504 query.Append(L" = ");
507 if (columnNumber == pColumnsToModify->GetCount() - 1)
517 query.Append(L"WHERE ");
518 query.Append(*pWhereCondition);
520 if (__pDatabase != null)
522 DbStatement* pDbStatement = null;
524 r = __pDatabase->BeginTransaction();
525 pDbStatement = __pDatabase->CreateStatementN(query);
527 if (pDbStatement != null)
529 DbEnumerator* pDbEnum = null;
531 for (int colIndex = 0; colIndex < pColumnsNewData->GetCount(); colIndex++)
533 Integer* columnInttype = static_cast<Integer*>(pColumnTypes->GetAt(colIndex));
535 DbColumnType columnType = (DbColumnType) columnInttype->ToInt();
538 if (columnType == DB_COLUMNTYPE_INT)
540 Integer* pIntValue = static_cast<Integer*>(pColumnsNewData->GetAt(colIndex));
541 pDbStatement->BindInt(colIndex, pIntValue->ToInt());
543 else if (columnType == DB_COLUMNTYPE_INT64)
545 LongLong* pLongValue = static_cast<LongLong*>(pColumnsNewData->GetAt(colIndex));
546 pDbStatement->BindInt64(colIndex, pLongValue->ToLong());
548 else if (columnType == DB_COLUMNTYPE_DOUBLE)
550 Double* pDoubleValue = static_cast<Double*>(pColumnsNewData->GetAt(colIndex));
551 pDbStatement->BindDouble(colIndex, pDoubleValue->ToDouble());
553 else if (columnType == DB_COLUMNTYPE_TEXT)
555 String* pTextValue = static_cast<String*>(pColumnsNewData->GetAt(colIndex));
556 if (pTextValue != null)
558 if (pTextValue->IsEmpty())
560 pDbStatement->BindNull(colIndex);
564 pDbStatement->BindString(colIndex, *pTextValue);
568 else if (columnType == DB_COLUMNTYPE_BLOB)
570 ByteBuffer* pBlobValue = static_cast<ByteBuffer*>(pColumnsNewData->GetAt(colIndex));
571 pDbStatement->BindBlob(colIndex, *pBlobValue);
579 pDbEnum = __pDatabase->ExecuteStatementN(*pDbStatement);
584 AppLogException("__pDatabase->ExecuteStatementN returned error %s", GetErrorMessage(r));
589 r = __pDatabase->CommitTransaction();
594 if (pDbEnum->GetColumnCount() < 1)
601 if (pDbStatement != null)
610 AppLogException("Create statement failed with error %s", GetErrorMessage(r));
611 __pDatabase->RollbackTransaction();
619 GenericDatabaseManager::GenericDatabaseManager(void)
622 //no implementation required
625 GenericDatabaseManager::~GenericDatabaseManager(void)
627 if (__pDatabase != null)
634 GenericDatabaseManager::CreateInstance(void)
636 __pDatabaseManager = new (std::nothrow) GenericDatabaseManager();
638 if (__pDatabaseManager != null)
640 std::atexit(DestroyInstance);
645 GenericDatabaseManager::GetColumnType(DbEnumerator* dbEnumerator, String* pColumnName)
647 TryReturn(dbEnumerator != null || pColumnName != null, DB_COLUMNTYPE_UNDEFINED, "Invalid parameters passed");
649 for (int columnNumber = 0; columnNumber < dbEnumerator->GetColumnCount(); columnNumber++)
651 String colName = dbEnumerator->GetColumnName(columnNumber);
653 if (pColumnName->Equals(colName, false))
655 return dbEnumerator->GetColumnType(columnNumber);
659 return DB_COLUMNTYPE_UNDEFINED;
663 GenericDatabaseManager::GetColumnIndex(DbEnumerator* dbEnumerator, String* pColumnName)
665 TryReturn(dbEnumerator != null || pColumnName != null, -1, "Invalid parameters passed");
667 for (int columnNumber = 0; columnNumber < dbEnumerator->GetColumnCount(); columnNumber++)
669 String columnName = dbEnumerator->GetColumnName(columnNumber);
671 if (pColumnName->Equals(columnName, false))