DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
DeveelDbExecutor.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 
5 using Deveel.Data.Sql;
6 using Deveel.Data.Sql.Objects;
8 using Deveel.Data.Sql.Tables;
9 using Deveel.Data.Types;
10 
11 using IQToolkit;
12 using IQToolkit.Data.Common;
13 
14 using QueryParameter = IQToolkit.Data.Common.QueryParameter;
15 
16 namespace Deveel.Data.Linq {
17  class DeveelDbExecutor : QueryExecutor {
18  private IQuery context;
19  private int rowsAffected;
20 
21  public DeveelDbExecutor(IQuery context) {
22  this.context = context;
23  }
24 
25  public override object Convert(object value, Type type) {
26  if (value == null) {
27  return TypeHelper.GetDefault(type);
28  }
29 
30  type = TypeHelper.GetNonNullableType(type);
31  var vtype = value.GetType();
32 
33  if (type != vtype) {
34  if (type.IsEnum) {
35  if (vtype == typeof (string))
36  return Enum.Parse(type, (string) value);
37 
38  Type utype = Enum.GetUnderlyingType(type);
39  if (utype != vtype)
40  value = System.Convert.ChangeType(value, utype);
41 
42  return Enum.ToObject(type, value);
43  }
44 
45  return System.Convert.ChangeType(value, type);
46  }
47 
48  return value;
49  }
50 
51  private SqlQuery GetQuery(QueryCommand command, object[] paramValues) {
52  var query = new SqlQuery(command.CommandText);
53  SetParameters(command, query, paramValues);
54  return query;
55  }
56 
57  private void SetParameters(QueryCommand command, SqlQuery query, object[] paramValues) {
58  if (command.Parameters.Count > 0 && query.Parameters.Count == 0) {
59  for (int i = 0, n = command.Parameters.Count; i < n; i++) {
60  AddParameter(query, command.Parameters[i], paramValues != null ? paramValues[i] : null);
61  }
62  } else if (paramValues != null) {
63  var queryParams = query.Parameters.ToList();
64  for (int i = 0, n = queryParams.Count; i < n; i++) {
65  var p = queryParams[i];
66  if (p.Direction == QueryParameterDirection.In
67  || p.Direction == QueryParameterDirection.InOut) {
68  p.Value = (ISqlObject) paramValues[i] ?? SqlNull.Value;
69  }
70  }
71  }
72  }
73 
74  private void AddParameter(SqlQuery query, QueryParameter queryParameter, object value) {
75  var sqlType = GetSqlType(queryParameter.Type);
76  var param = new Sql.QueryParameter(queryParameter.Name, sqlType, (ISqlObject)value);
77  param.Direction = QueryParameterDirection.In;
78  query.Parameters.Add(param);
79  }
80 
81  private SqlType GetSqlType(Type type) {
82  throw new NotImplementedException();
83  }
84 
85  public override IEnumerable<T> Execute<T>(QueryCommand command, Func<FieldReader, T> fnProjector, MappingEntity entity, object[] paramValues) {
86  var query = GetQuery(command, paramValues);
87  var queryResult = context.ExecuteQuery(query);
88 
89  return Project(queryResult[0], fnProjector, entity);
90  }
91 
92  private IEnumerable<T> Project<T>(ITable result, Func<FieldReader, T> fnProjector, MappingEntity entity) {
93  var reader = new TableFieldReader(result);
94  while (reader.NextRow()) {
95  yield return fnProjector(reader);
96  }
97  }
98 
99  public override IEnumerable<int> ExecuteBatch(QueryCommand query, IEnumerable<object[]> paramSets, int batchSize, bool stream) {
100  throw new NotImplementedException();
101  }
102 
103  public override IEnumerable<T> ExecuteBatch<T>(QueryCommand query, IEnumerable<object[]> paramSets, Func<FieldReader, T> fnProjector, MappingEntity entity,
104  int batchSize, bool stream) {
105  throw new NotImplementedException();
106  }
107 
108  public override IEnumerable<T> ExecuteDeferred<T>(QueryCommand command, Func<FieldReader, T> fnProjector, MappingEntity entity, object[] paramValues) {
109  var query = GetQuery(command, paramValues);
110  var queryResult = context.ExecuteQuery(query);
111 
112  return Project(queryResult[0], fnProjector, entity);
113  }
114 
115  public override int ExecuteCommand(QueryCommand query, object[] paramValues) {
116  var sqlQuery = GetQuery(query, paramValues);
117  var result = context.ExecuteQuery(sqlQuery);
118  rowsAffected = result[0].RowCount;
119  return rowsAffected;
120  }
121 
122  public override int RowsAffected {
123  get { return rowsAffected; }
124  }
125 
126  #region TableFieldReader
127 
128  class TableFieldReader : FieldReader {
129  private readonly ITable table;
130  private int rowOffset = -1;
131  private Row row;
132 
133  public TableFieldReader(ITable table) {
134  this.table = table;
135  Init();
136  }
137 
138  public bool NextRow() {
139  if (++rowOffset >= table.RowCount)
140  return false;
141 
142  row = table.GetRow(rowOffset);
143  return true;
144  }
145 
146  protected override Type GetFieldType(int ordinal) {
147  var sqlType = row[ordinal].Type;
148  return sqlType.GetRuntimeType();
149  }
150 
151  protected override bool IsDBNull(int ordinal) {
152  return row[ordinal].IsNull;
153  }
154 
155  protected override T GetValue<T>(int ordinal) {
156  var value = row[ordinal];
157  return (T) value.Type.ConvertTo(value.Value, typeof (T));
158  }
159 
160  protected override byte GetByte(int ordinal) {
161  return GetValue<byte>(ordinal);
162  }
163 
164  protected override char GetChar(int ordinal) {
165  return GetValue<char>(ordinal);
166  }
167 
168  protected override DateTime GetDateTime(int ordinal) {
169  return GetValue<DateTime>(ordinal);
170  }
171 
172  protected override decimal GetDecimal(int ordinal) {
173  throw new NotImplementedException();
174  }
175 
176  protected override double GetDouble(int ordinal) {
177  return GetValue<double>(ordinal);
178  }
179 
180  protected override float GetSingle(int ordinal) {
181  return GetValue<float>(ordinal);
182  }
183 
184  protected override Guid GetGuid(int ordinal) {
185  throw new NotImplementedException();
186  }
187 
188  protected override short GetInt16(int ordinal) {
189  return GetValue<short>(ordinal);
190  }
191 
192  protected override int GetInt32(int ordinal) {
193  return GetValue<int>(ordinal);
194  }
195 
196  protected override long GetInt64(int ordinal) {
197  return GetValue<long>(ordinal);
198  }
199 
200  protected override string GetString(int ordinal) {
201  return GetValue<string>(ordinal);
202  }
203 
204  protected override int FieldCount {
205  get { return table.TableInfo.ColumnCount; }
206  }
207  }
208 
209  #endregion
210  }
211 }
override int ExecuteCommand(QueryCommand query, object[] paramValues)
override object Convert(object value, Type type)
Defines the contract to access the data contained into a table of a database.
Definition: ITable.cs:40
A single row in a table of a database.
Definition: Row.cs:44
static readonly SqlNull Value
Definition: SqlNull.cs:24
Defines the contract for a valid SQL Object
Definition: ISqlObject.cs:23
A user-defined TYPE that holds complex objects in a database column.
void SetParameters(QueryCommand command, SqlQuery query, object[] paramValues)
SqlQuery GetQuery(QueryCommand command, object[] paramValues)
Defines the properties of a specific SQL Type and handles the values compatible.
Definition: SqlType.cs:33
override IEnumerable< int > ExecuteBatch(QueryCommand query, IEnumerable< object[]> paramSets, int batchSize, bool stream)
void AddParameter(SqlQuery query, QueryParameter queryParameter, object value)
ICollection< QueryParameter > Parameters
Definition: SqlQuery.cs:53