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 if (typeof(int) == obj.GetType())
60 Interop.Sqlite.BindInt(_stmt, pos, (int)obj);
62 else if (typeof(bool) == obj.GetType())
65 Interop.Sqlite.BindInt(_stmt, pos, val ? 1 : 0);
67 else if (typeof(double) == obj.GetType())
69 Interop.Sqlite.BindDouble(_stmt, pos, (double)obj);
71 else if (typeof(string) == obj.GetType())
73 Interop.Sqlite.BindText(_stmt, pos, (string)obj, -1, (IntPtr)(-1));
75 else if (typeof(byte[]) == obj.GetType())
78 Interop.Sqlite.BindData(_stmt, pos, (byte[])obj, ((byte[])obj).Length, IntPtr.Zero);
85 internal Statement(Connection conn)
90 public bool Execute(Sql sql)
94 int ret = Interop.Sqlite.Step(_stmt);
95 Console.WriteLine("Execute: " + ret);
96 if (ret != (int)Interop.Sqlite.ResultCode.SQLITE_ROW &&
97 ret != (int)Interop.Sqlite.ResultCode.SQLITE_DONE)
102 public int ExecuteUpdate(Sql sql)
106 int ret = Interop.Sqlite.Step(_stmt);
107 if (ret != (int)Interop.Sqlite.ResultCode.SQLITE_ROW &&
108 ret != (int)Interop.Sqlite.ResultCode.SQLITE_DONE)
110 return Interop.Sqlite.Changes(_conn.GetHandle());
113 public IResultSet ExecuteQuery(Sql sql)
115 bool prepared = Prepare(sql);
118 throw new InvalidOperationException("Couldn't prepare");
120 var set = new ResultSet(_stmt, _conn);
126 public async Task<IResultSet> ExecuteQueryAsync(Sql sql)
128 return await Task.Run(() =>
130 return ExecuteQuery(sql);
131 }).ConfigureAwait(false);
134 public async Task<int> ExecuteUpdateAsync(Sql sql)
136 return await Task.Run(() =>
138 return ExecuteUpdate(sql);
139 }).ConfigureAwait(false);
142 public async Task<bool> ExecuteAsync(Sql sql)
144 return await Task.Run(() =>
147 }).ConfigureAwait(false);
150 protected virtual void Dispose(bool disposing)
159 disposedValue = true;
165 Dispose(disposing: false);
168 public void Dispose()
170 Dispose(disposing: true);
171 GC.SuppressFinalize(this);