DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
QueryParameter.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 
20 using Deveel.Data.Sql.Objects;
21 using Deveel.Data.Types;
22 
23 namespace Deveel.Data.Sql {
24  // TODO: Make it disposable?
25  [Serializable]
26  public sealed class QueryParameter : ISerializable {
27  public QueryParameter(SqlType sqlType)
28  : this(sqlType, null) {
29  }
30 
31  public QueryParameter(SqlType sqlType, ISqlObject value)
32  : this(Marker, sqlType, value) {
33  }
34 
35  public QueryParameter(string name, SqlType sqlType)
36  : this(name, sqlType, null) {
37  }
38 
39  public QueryParameter(string name, SqlType sqlType, ISqlObject value) {
40  if (sqlType == null)
41  throw new ArgumentNullException("sqlType");
42 
43  if (String.IsNullOrEmpty(name))
44  throw new ArgumentNullException("name");
45 
46  if (!String.Equals(name, Marker, StringComparison.Ordinal) &&
47  name[0] != NamePrefix)
48  throw new ArgumentException(String.Format("The parameter name '{0}' is invalid: must be '{1}' or starting with '{2}'", name, Marker, NamePrefix));
49 
50  Name = name;
51  SqlType = sqlType;
52  Value = value;
53  Direction = QueryParameterDirection.In;
54  }
55 
56  private QueryParameter(ObjectData data) {
57  Name = data.GetString("Name");
58  SqlType = data.GetValue<SqlType>("Type");
59  Value = data.GetValue<ISqlObject>("Value");
60  Direction = (QueryParameterDirection) data.GetInt32("Direction");
61  }
62 
63  public const char NamePrefix = ':';
64  public const string Marker = "?";
65 
66  public string Name { get; private set; }
67 
68  public SqlType SqlType { get; private set; }
69 
70  public QueryParameterDirection Direction { get; set; }
71 
72  public ISqlObject Value { get; set; }
73 
75  data.SetValue("Name", Name);
76  data.SetValue("Type", SqlType);
77  data.SetValue("Direction", Direction);
78  data.SetValue("Value", Value);
79  }
80  }
81 }
void GetData(SerializeData data)
A long string in the system.
void SetValue(string key, Type type, object value)
Defines the contract for a valid SQL Object
Definition: ISqlObject.cs:23
QueryParameter(SqlType sqlType, ISqlObject value)
Defines the properties of a specific SQL Type and handles the values compatible.
Definition: SqlType.cs:33
QueryParameter(string name, SqlType sqlType)
QueryParameter(string name, SqlType sqlType, ISqlObject value)
Parameters that are replaced on a zero-based index of the input parameters of a query. These parameters are not identified by a unique name, but by a ? character that acts as a place-holder for an input parameter.