Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.DataControl / Tizen.Applications.DataControl.Core / CloneCursorCore.cs
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 using System;
18
19 using System.IO;
20 using System.Text;
21 using System.Collections.Generic;
22 using System.Threading;
23
24 namespace Tizen.Applications.DataControl.Core
25 {
26     internal class CloneCursorCore : ICursor
27     {
28         internal const int MaxColumnNameSize = 1024;
29         private const string LogTag = "Tizen.Applications.DataControl";
30         private long _rowCount;
31         private int _columnCount;
32         private const int ResultNoData = -1;
33         private Interop.DataControl.SafeCursorHandle _cursor;
34         internal CloneCursorCore(Interop.DataControl.SafeCursorHandle cursor)
35         {
36             _cursor = cursor;
37             _columnCount = Interop.DataControl.GetColumnCount(cursor);
38
39             if (_columnCount == ResultNoData)
40             {
41                 _rowCount = 0;
42                 return;
43             }
44
45             Interop.DataControl.First(cursor);
46
47             do
48             {
49                 _rowCount++;
50             }
51             while (Interop.DataControl.Next(cursor) == (int)ResultType.Success);
52             Interop.DataControl.First(cursor);
53         }
54
55         public int GetColumnCount()
56         {
57             return Interop.DataControl.GetColumnCount(_cursor);
58         }
59
60         public ColumnType GetColumnType(int index)
61         {
62             int type;
63             ResultType ret;
64
65             if (index < 0 || index >= _columnCount)
66             {
67                 ErrorFactory.ThrowException(ResultType.InvalidParameter, false);
68             }
69
70             ret = Interop.DataControl.GetItemType(_cursor, index, out type);
71             if (ret != ResultType.Success)
72             {
73                 ErrorFactory.ThrowException(ret, false, "Column Index " + index.ToString());
74             }
75
76             return (ColumnType)type;
77         }
78
79         public string GetColumnName(int index)
80         {
81             string retStr;
82             ResultType ret;
83             StringBuilder columnName = new StringBuilder();
84
85             if (index < 0 || index >= _columnCount)
86             {
87                 ErrorFactory.ThrowException(ResultType.InvalidParameter, false);
88             }
89
90             columnName.Length = MaxColumnNameSize;
91             ret = Interop.DataControl.GetColumnName(_cursor, index, columnName);
92             if (ret != ResultType.Success)
93             {
94                 ErrorFactory.ThrowException(ret, false, "Column Index " + index.ToString());
95             }
96
97             retStr = columnName.ToString();
98             columnName.Clear();
99             columnName = null;
100
101             return retStr;
102         }
103
104         public long GetRowCount()
105         {
106             return _rowCount;
107         }
108
109         public bool Next()
110         {
111             ResultType type = Interop.DataControl.Next(_cursor);
112
113             if (type != ResultType.Success)
114             {
115                 return false;
116             }
117
118             return true;
119         }
120
121         public bool Prev()
122         {
123             ResultType type = Interop.DataControl.Prev(_cursor);
124
125             if (type != ResultType.Success)
126             {
127                 return false;
128             }
129
130             return true;
131         }
132
133         public bool Reset()
134         {
135             ResultType type = Interop.DataControl.First(_cursor);
136
137             if (type != ResultType.Success)
138             {
139                 return false;
140             }
141
142             return true;
143         }
144
145         public int GetIntValue(int index)
146         {
147             ResultType ret;
148             int value;
149
150             if (index < 0 || index >= _columnCount)
151             {
152                 ErrorFactory.ThrowException(ResultType.InvalidParameter, false);
153             }
154
155             ret = Interop.DataControl.GetInt(_cursor, index, out value);
156             if (ret != ResultType.Success)
157             {
158                 ErrorFactory.ThrowException(ret, false, "Column Index " + index.ToString());
159             }
160
161             return value;
162         }
163
164         public Int64 GetInt64Value(int index)
165         {
166             ResultType ret;
167             Int64 value;
168
169             if (index < 0 || index >= _columnCount)
170             {
171                 ErrorFactory.ThrowException(ResultType.InvalidParameter, false);
172             }
173
174             ret = Interop.DataControl.GetInt64(_cursor, index, out value);
175             if (ret != ResultType.Success)
176             {
177                 ErrorFactory.ThrowException(ret, false, "Column Index " + index.ToString());
178             }
179
180             return value;
181         }
182
183         public double GetDoubleValue(int index)
184         {
185             ResultType ret;
186             double value;
187
188             if (index < 0 || index >= _columnCount)
189             {
190                 ErrorFactory.ThrowException(ResultType.InvalidParameter, false);
191             }
192
193             ret = Interop.DataControl.Getdouble(_cursor, index, out value);
194             if (ret != ResultType.Success)
195             {
196                 ErrorFactory.ThrowException(ret, false, "Column Index " + index.ToString());
197             }
198
199             return value;
200         }
201
202         public string GetStringValue(int index)
203         {
204             ResultType ret;
205             int size;
206             byte[] value;
207             string text;
208
209             if (index < 0 || index >= _columnCount)
210             {
211                 ErrorFactory.ThrowException(ResultType.InvalidParameter, false);
212             }
213
214             size = Interop.DataControl.GetItemSize(_cursor, index);
215             if (size < 0)
216             {
217                 ErrorFactory.ThrowException(ResultType.IoError, false, "Invalid size");
218             }
219
220             value = new byte[size + 1];
221             ret = Interop.DataControl.GetText(_cursor, index, value);
222             if (ret != ResultType.Success)
223             {
224                 ErrorFactory.ThrowException(ret, false);
225             }
226
227             text = Encoding.UTF8.GetString(value);
228
229             return text;
230         }
231
232         public byte[] GetBlobValue(int index)
233         {
234             ResultType ret;
235             int size;
236             byte[] byte_array;
237
238             if (index < 0 || index >= _columnCount)
239             {
240                 ErrorFactory.ThrowException(ResultType.InvalidParameter, false);
241             }
242
243             size = Interop.DataControl.GetItemSize(_cursor, index);
244             if (size < 0)
245             {
246                 ErrorFactory.ThrowException(ResultType.IoError, false, "Invalid size");
247             }
248
249             byte_array = new byte[size];
250             ret = Interop.DataControl.GetBlob(_cursor, index, byte_array, size);
251             if (ret != ResultType.Success)
252             {
253                 ErrorFactory.ThrowException(ret, false);
254             }
255
256             return byte_array;
257         }
258     }
259 }