+++ /dev/null
-/*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd
- *
- * Licensed under the Flora License, Version 1.1 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://floralicense.org/license/
- *
- * 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.
- */
-
-using System;
-using System.Collections.Generic;
-using System.Runtime.InteropServices;
-
-using Tizen;
-
-using LibTVRefCommonPortable.DataModels;
-using LibTVRefCommonPortable.Utils;
-
-namespace LibTVRefCommonTizen.Ports
-{
- public class DBPort : IDBAPIs
- {
- internal class SQLite
- {
- private const string Library = "libsqlite3.so.0";
-
- [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
- internal delegate int ExecCallback(IntPtr notUsed, int numberOfColumn, IntPtr value, IntPtr column);
-
- [DllImport(Library, EntryPoint = "sqlite3_open", CallingConvention = CallingConvention.Cdecl)]
- internal static extern int Open(string fileName, out IntPtr handle);
-
- [DllImport(Library, EntryPoint = "sqlite3_close", CallingConvention = CallingConvention.Cdecl)]
- internal static extern int Close(IntPtr handle);
-
- [DllImport(Library, EntryPoint = "sqlite3_exec", CallingConvention = CallingConvention.Cdecl)]
- internal static extern int Exec(IntPtr handle, string sql, ExecCallback callback, IntPtr notUsed, IntPtr errorMessage);
- }
-
- private const int SQLITE_OK = 0;
- private const int SQLITE_ROW = 100;
-
- private const int FIRST_COLUMN = 0;
-
- private int ptrSize = Marshal.SizeOf<IntPtr>();
-
- public int PtrSize
- {
- get
- {
- return ptrSize;
- }
- }
-
- public String AppDataPath
- {
- get;
- private set;
- }
-
- public DBPort(String appDataPath)
- {
- this.AppDataPath = appDataPath;
-
- // TODO : make a unit test for this!!!
- DebuggingPort.D("DBPort-------------------------------------");
- DebuggingPort.D("version " + ExecSQL("csk", "SELECT SQLITE_VERSION()"));
- DebuggingPort.D("create " + ExecSQL("csk", "CREATE TABLE friends(Id INTEGER PRIMARY KEY, Name TEXT);"));
-
- DebuggingPort.D("insert 1 " + ExecSQL("csk", "INSERT INTO friends(Name) VALUES ('Tom');"));
- DebuggingPort.D("insert 2 " + ExecSQL("csk", "INSERT INTO friends(Name) VALUES ('Rebecca');"));
- DebuggingPort.D("insert 3 " + ExecSQL("csk", "INSERT INTO friends(Name) VALUES ('Jim');"));
- DebuggingPort.D("insert 4 " + ExecSQL("csk", "INSERT INTO friends(Name) VALUES ('Roger');"));
- DebuggingPort.D("insert 5 " + ExecSQL("csk", "INSERT INTO friends(Name) VALUES ('Robert');"));
-
- IEnumerable<DBItem> dbItems = new List<DBItem>();
- DebuggingPort.D("select " + ExecSQL("csk", "SELECT * FROM friends;", out dbItems));
- foreach (var item in dbItems)
- {
- foreach (var value in item.ItemProperties)
- {
- DebuggingPort.D("column[" + value.Key + "] = " + value.Value);
- }
- }
-
- DebuggingPort.D("insert 5 " + ExecSQL("csk", "DROP TABLE friends;"));
- }
-
- private static bool CheckResult(int res, string desc)
- {
- if (res != SQLITE_OK)
- {
- DebuggingPort.D(String.Format("DB Error [{0}], {1}", desc, res));
- return false;
- }
-
- return true;
- }
-
- private static bool Open(string appDataPath, string dbName, out IntPtr handle)
- {
- DebuggingPort.D("[DB Open]");
- if (dbName == null)
- {
- DebuggingPort.E("DB Name is NULL!!!");
- handle = IntPtr.Zero;
- return false;
- }
-
- string dbPath = appDataPath + dbName;
- Log.Debug("csk", dbPath);
- return CheckResult(SQLite.Open(appDataPath + dbName + ".db", out handle), "open");
-
- }
-
- private static void Close(IntPtr handle)
- {
- DebuggingPort.D("[DB Close]");
- if (handle == null)
- {
- return;
- }
-
- SQLite.Close(handle);
- }
-
- internal sealed class DBHandleRAII// : IDisposable
- {
- internal IntPtr handle;
- internal DBHandleRAII(String appDataPath, string fileName)
- {
- Open(appDataPath, fileName, out handle);
- }
-
- ~DBHandleRAII()
- {
- Close(handle);
- }
- // TODO : Change to use IDisposable!!!
- // Dispose is not calling!!!
- /*
- public void Dispose()
- {
- Close(handle);
- }
- */
- }
-
- public bool ExecSQL(string dbName, string sql)
- {
- if (dbName == null || sql == null)
- {
- DebuggingPort.E("Invalid argument!!!");
- return false;
- }
-
- DBHandleRAII db = new DBHandleRAII(AppDataPath, dbName);
- if (db.handle == IntPtr.Zero)
- {
- DebuggingPort.E("DB open failed!!!");
- return false;
- }
-
- return CheckResult(SQLite.Exec(db.handle, sql, null, IntPtr.Zero, IntPtr.Zero), "exec ");
- }
-
- // TODO : Make this thread safe!!!
- List<DBItem> givenItems = new List<DBItem>();
-
- private int ExecCallback(IntPtr notUsed, int numberOfColumn, IntPtr value, IntPtr column)
- {
- DebuggingPort.D("ExecCallback");
- for (int i = 1; i < numberOfColumn; i++)
- {
- string _value = Marshal.PtrToStringAnsi(Marshal.ReadIntPtr(value, i * PtrSize));
- DebuggingPort.E("" + i + " - " + _value + " / ");
- DBItem item = new DBItem();
- item.Add(i, _value);
- givenItems.Add(item);
- }
-
- return 0;
- }
-
- public bool ExecSQL(string dbName, string sql, out IEnumerable<DBItem> items)
- {
- givenItems.Clear();
- items = givenItems;
-
- DebuggingPort.D("ExecSQL");
- if (dbName == null || sql == null)
- {
- DebuggingPort.E("Invalid argument!!!");
- return false;
- }
-
- DBHandleRAII db = new DBHandleRAII(AppDataPath, dbName);
- if (db.handle == IntPtr.Zero)
- {
- DebuggingPort.E("DB open failed!!!");
- return false;
- }
-
- if (CheckResult(SQLite.Exec(db.handle, sql, ExecCallback, IntPtr.Zero, IntPtr.Zero), "exec") == false)
- {
- return false;
- }
-
- return true;
- }
-
- }
-
-}