DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
DeveelDbParameterCollection.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2015 Deveel
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 using System.Collections;
19 using System.Collections.Generic;
20 using System.Data;
21 using System.Data.Common;
22 using System.Linq;
23 using System.Runtime.CompilerServices;
24 
25 using Deveel.Data.Sql;
26 using Deveel.Data.Types;
27 
28 namespace Deveel.Data.Client {
29  public sealed class DeveelDbParameterCollection : DbParameterCollection {
30  private readonly List<DeveelDbParameter> parameters;
31 
33  if (command == null)
34  throw new ArgumentNullException("command");
35 
36  Command = command;
37  parameters = new List<DeveelDbParameter>();
38  }
39 
40  private DeveelDbCommand Command { get; set; }
41 
42  public new DeveelDbParameter this[int offset] {
43  get { return(DeveelDbParameter) GetParameter(offset); }
44  set { SetParameter(offset, value); }
45  }
46 
47  public new DeveelDbParameter this[string name] {
48  get { return (DeveelDbParameter) GetParameter(name); }
49  set { SetParameter(name, value); }
50  }
51 
52  private QueryParameterStyle ParameterStyle {
53  get { return Command.Connection.Settings.ParameterStyle; }
54  }
55 
56  private void AssertValidName(string paramName) {
57  if (ParameterStyle == QueryParameterStyle.Named) {
58  if (Contains(paramName))
59  throw new ArgumentException(String.Format("The parameter '{0}' already exists in the collection.", paramName));
60  } else {
61  if (!String.IsNullOrEmpty(paramName) ||
62  !String.Equals(paramName, QueryParameter.Marker))
63  throw new ArgumentException("Cannot specify the name of the parameter.");
64  }
65  }
66 
67  private DeveelDbParameter CreateParemeter(object value) {
68  var dbType = GetDbType(value);
69  var sqlType = GetSqlType(value);
70 
71  return new DeveelDbParameter {
72  DbType = dbType,
73  SqlType = sqlType,
74  Direction = ParameterDirection.Input,
75  Value = value,
76  ParameterName = QueryParameter.Marker,
77  };
78  }
79 
80  private SqlTypeCode GetSqlType(object value) {
81  throw new NotImplementedException();
82  }
83 
84  private DbType GetDbType(object value) {
85  throw new NotImplementedException();
86  }
87 
88  public override int Add(object value) {
89  if (value is DeveelDbParameter)
90  return AddParameter((DeveelDbParameter) value);
91  if (value is IDbDataParameter)
92  return AddDbDataParameter((IDbDataParameter) value);
93 
94  return AddValue(value);
95  }
96 
97  private int AddValue(object value) {
98  if (ParameterStyle != QueryParameterStyle.Marker)
99  throw new ArgumentException("Cannot add an unnamed parameter in this context.");
100 
101  var parameter = CreateParemeter(value);
102  parameters.Add(parameter);
103  return parameters.Count - 1;
104  }
105 
106  private int AddDbDataParameter(IDbDataParameter parameter) {
107  AssertValidName(parameter.ParameterName);
108 
109  DeveelDbParameter dbParameter;
110  if (parameter is DeveelDbParameter) {
111  dbParameter = (DeveelDbParameter) parameter;
112  } else {
113  dbParameter = new DeveelDbParameter();
114  }
115 
116  parameters.Add(dbParameter);
117  return parameters.Count - 1;
118  }
119 
120  private int AddParameter(DeveelDbParameter parameter) {
121  AssertValidName(parameter.ParameterName);
122  parameters.Add(parameter);
123  return parameters.Count - 1;
124  }
125 
126  public override bool Contains(object value) {
127  if (value is string)
128  return Contains((string) value);
129  if (value is IDbDataParameter)
130  return Contains(((IDbDataParameter) value).ParameterName);
131 
132  return false;
133  }
134 
135  public override void Clear() {
136  parameters.Clear();
137  }
138 
139  public override int IndexOf(object value) {
140  if (value is string)
141  return IndexOf((string) value);
142  if (value is IDbDataParameter)
143  return IndexOf(((IDbDataParameter) value).ParameterName);
144 
145  return -1;
146  }
147 
148  public override void Insert(int index, object value) {
149  throw new NotImplementedException();
150  }
151 
152  public override void Remove(object value) {
153  if (value is string) {
154  var paramName = (string) value;
155  RemoveAt(paramName);
156  } else if (value is int) {
157  var index = (int) value;
158  RemoveAt(index);
159  } else if (value is IDbDataParameter) {
160  var param = (IDbDataParameter) value;
161  throw new NotImplementedException();
162  }
163  }
164 
165  public override void RemoveAt(int index) {
166  if (index < 0 || index >= parameters.Count)
167  throw new ArgumentOutOfRangeException("index");
168 
169  parameters.RemoveAt(index);
170  }
171 
172  public override void RemoveAt(string parameterName) {
173  var index = IndexOf(parameterName);
174  if (index == -1)
175  return;
176 
177  RemoveAt(index);
178  }
179 
180  protected override void SetParameter(int index, DbParameter value) {
181  throw new NotImplementedException();
182  }
183 
184  protected override void SetParameter(string parameterName, DbParameter value) {
185  throw new NotImplementedException();
186  }
187 
188  public override int Count {
189  get { return parameters.Count; }
190  }
191 
192  public override object SyncRoot {
193  get { return null; }
194  }
195 
196  public override bool IsFixedSize {
197  get { return false; }
198  }
199 
200  public override bool IsReadOnly {
201  get { return false; }
202  }
203 
204  public override bool IsSynchronized {
205  get { return false; }
206  }
207 
208  public override int IndexOf(string parameterName) {
209  if (ParameterStyle != QueryParameterStyle.Named)
210  return -1;
211 
212  for (int i = 0; i < parameters.Count; i++) {
213  if (parameters[i].ParameterName == parameterName)
214  return i;
215  }
216 
217  return -1;
218  }
219 
220  public override IEnumerator GetEnumerator() {
221  return parameters.GetEnumerator();
222  }
223 
224  protected override DbParameter GetParameter(int index) {
225  throw new NotImplementedException();
226  }
227 
228  protected override DbParameter GetParameter(string parameterName) {
229  throw new NotImplementedException();
230  }
231 
232  public override bool Contains(string value) {
233  if (ParameterStyle != QueryParameterStyle.Named)
234  return false;
235 
236  return parameters.Any(x => x.ParameterName == value);
237  }
238 
239  public override void CopyTo(Array array, int index) {
240  throw new NotImplementedException();
241  }
242 
243  public override void AddRange(Array values) {
244  foreach (var value in values) {
245  Add(value);
246  }
247  }
248  }
249 }
override DbParameter GetParameter(string parameterName)
A long string in the system.
override void SetParameter(int index, DbParameter value)
ParameterDirection
The possible directions of a procedure parameter.
QueryParameterStyle
In a SQL query object, this is the form of parameters passed from the client side to the server side...
Defines the properties of a specific SQL Type and handles the values compatible.
Definition: SqlType.cs:33
SqlTypeCode
Enumerates the codes of all SQL types handled by the system.
Definition: SqlTypeCode.cs:23
override void SetParameter(string parameterName, DbParameter value)