2 * Copyright (c) 2023 Samsung Electronics Co., Ltd All Rights Reserved
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 using System.ComponentModel;
19 using System.Threading.Tasks;
21 namespace Tizen.Data.Tdbc.Driver.Sqlite
23 [EditorBrowsable(EditorBrowsableState.Never)]
24 public class Statement : IStatement
28 private bool disposedValue;
32 if (_stmt != IntPtr.Zero)
34 Interop.Sqlite.Finalize(_stmt);
39 private bool Prepare(Sql sql)
42 Console.WriteLine("Prepare: " + sql.Command);
43 int ret = Interop.Sqlite.Prepare(_conn.GetHandle(), sql.Command, -1, out _stmt, IntPtr.Zero);
44 if (ret != (int)Interop.Sqlite.ResultCode.SQLITE_OK)
46 Console.WriteLine("Prepare: failed " + ret);
50 foreach (var i in sql.Bindings)
54 int pos = Interop.Sqlite.GetParameterIndex(_stmt, key);
56 throw new InvalidOperationException("Invalid binding");
58 // TODO: consider null binding
62 Type type = obj.GetType();
63 if (typeof(int) == type)
65 Interop.Sqlite.BindInt(_stmt, pos, (int)obj);
67 else if (typeof(bool) == type)
70 Interop.Sqlite.BindInt(_stmt, pos, val ? 1 : 0);
72 else if (typeof(double) == type)
74 Interop.Sqlite.BindDouble(_stmt, pos, (double)obj);
76 else if (typeof(string) == type)
78 Interop.Sqlite.BindText(_stmt, pos, (string)obj, -1, (IntPtr)(-1));
80 else if (typeof(byte[]) == type)
82 Interop.Sqlite.BindData(_stmt, pos, (byte[])obj, ((byte[])obj).Length, IntPtr.Zero);
89 internal Statement(Connection conn)
94 public bool Execute(Sql sql)
98 int ret = Interop.Sqlite.Step(_stmt);
99 Console.WriteLine("Execute: " + ret);
100 if (ret != (int)Interop.Sqlite.ResultCode.SQLITE_ROW &&
101 ret != (int)Interop.Sqlite.ResultCode.SQLITE_DONE)
106 public int ExecuteUpdate(Sql sql)
110 int ret = Interop.Sqlite.Step(_stmt);
111 if (ret != (int)Interop.Sqlite.ResultCode.SQLITE_ROW &&
112 ret != (int)Interop.Sqlite.ResultCode.SQLITE_DONE)
114 return Interop.Sqlite.Changes(_conn.GetHandle());
117 public IResultSet ExecuteQuery(Sql sql)
119 bool prepared = Prepare(sql);
122 throw new InvalidOperationException("Couldn't prepare");
124 var set = new ResultSet(_stmt, _conn);
130 public async Task<IResultSet> ExecuteQueryAsync(Sql sql)
132 return await Task.Run(() =>
134 return ExecuteQuery(sql);
135 }).ConfigureAwait(false);
138 public async Task<int> ExecuteUpdateAsync(Sql sql)
140 return await Task.Run(() =>
142 return ExecuteUpdate(sql);
143 }).ConfigureAwait(false);
146 public async Task<bool> ExecuteAsync(Sql sql)
148 return await Task.Run(() =>
151 }).ConfigureAwait(false);
154 protected virtual void Dispose(bool disposing)
163 disposedValue = true;
169 Dispose(disposing: false);
172 public void Dispose()
174 Dispose(disposing: true);
175 GC.SuppressFinalize(this);