DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
SqlQuery.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.Generic;
19 using System.Collections.ObjectModel;
20 using System.Linq;
21 
23 
24 namespace Deveel.Data.Sql {
25  [Serializable]
26  public sealed class SqlQuery : ISerializable {
27  public SqlQuery(string text)
28  : this(text, QueryParameterStyle.Default) {
29  }
30 
31  public SqlQuery(string text, QueryParameterStyle parameterStyle) {
32  Text = text;
33  ParameterStyle = parameterStyle;
34  Parameters = new QueryParameterCollection(this);
35  }
36 
37  private SqlQuery(ObjectData data) {
38  Text = data.GetString("Text");
39  ParameterStyle = (QueryParameterStyle) data.GetInt32("ParameterStyle");
40 
41  Parameters = new QueryParameterCollection(this);
42 
43  var parameters = data.GetValue<QueryParameter[]>("Parameters");
44  if (parameters != null) {
45  foreach (var parameter in parameters) {
46  Parameters.Add(parameter);
47  }
48  }
49  }
50 
51  public string Text { get; private set; }
52 
53  public ICollection<QueryParameter> Parameters { get; private set; }
54 
55  public QueryParameterStyle ParameterStyle { get; private set; }
56 
58  data.SetValue("Text", Text);
59  data.SetValue("ParameterStyle", (int)ParameterStyle);
60 
61  var parameters = Parameters.ToArray();
62  if (parameters.Length > 0)
63  data.SetValue("Parameters", parameters);
64  }
65 
66  internal void ChangeStyle(QueryParameterStyle style) {
67  if (ParameterStyle != QueryParameterStyle.Default)
68  throw new InvalidOperationException();
69  if (style == QueryParameterStyle.Default)
70  throw new ArgumentException();
71 
72  ParameterStyle = style;
73  }
74 
75  #region QueryParameterCollection
76 
77  class QueryParameterCollection : Collection<QueryParameter> {
78  private SqlQuery SqlQuery { get; set; }
79 
81  SqlQuery = sqlQuery;
82  }
83 
84  private void ValidateParameter(QueryParameter item) {
85  if (item == null)
86  throw new ArgumentNullException("item");
87 
89  !String.Equals(item.Name, QueryParameter.Marker, StringComparison.Ordinal))
90  throw new ArgumentException(String.Format("The query accepts markers, but the parameter '{0}' is named.", item.Name));
92  if (item.Name.Equals(QueryParameter.Marker, StringComparison.Ordinal))
93  throw new ArgumentException("The query accepts named parameters, but a marker was set.");
94 
95  if (Items.Any(x => String.Equals(x.Name, item.Name)))
96  throw new ArgumentException(String.Format("A parameter named {0} was already inserted in the query.", item.Name));
97  }
98  }
99 
100  protected override void InsertItem(int index, QueryParameter item) {
101  ValidateParameter(item);
102  base.InsertItem(index, item);
103  }
104 
105  protected override void SetItem(int index, QueryParameter item) {
106  ValidateParameter(item);
107  base.SetItem(index, item);
108  }
109  }
110 
111  #endregion
112  }
113 }
void GetData(SerializeData data)
override void InsertItem(int index, QueryParameter item)
Definition: SqlQuery.cs:100
QueryParameterStyle ParameterStyle
Definition: SqlQuery.cs:55
SqlQuery(string text)
Definition: SqlQuery.cs:27
void SetValue(string key, Type type, object value)
void ValidateParameter(QueryParameter item)
Definition: SqlQuery.cs:84
No specific form of the parameter was given: this default to the system default parameter style confi...
void ChangeStyle(QueryParameterStyle style)
Definition: SqlQuery.cs:66
SqlQuery(string text, QueryParameterStyle parameterStyle)
Definition: SqlQuery.cs:31
override void SetItem(int index, QueryParameter item)
Definition: SqlQuery.cs:105
QueryParameterStyle
In a SQL query object, this is the form of parameters passed from the client side to the server side...
SqlQuery(ObjectData data)
Definition: SqlQuery.cs:37