DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
RuledNamingConvention.cs
Go to the documentation of this file.
1 using System;
2 using System.Text;
3 
4 namespace Deveel.Data.Mapping {
6  public static RuledNamingConvention SqlNaming = new RuledNamingConvention(DefaultNamingRules.LowerCase | DefaultNamingRules.UnderscoreSeparator);
7 
9  ValidateCaseRule(rules);
10  Rules = rules;
11  }
12 
13  private void ValidateCaseRule(DefaultNamingRules rules) {
14  if ((rules & DefaultNamingRules.CamelCase) != 0 &&
15  ((rules & DefaultNamingRules.LowerCase) != 0 ||
16  (rules & DefaultNamingRules.UpperCase) != 0))
17  throw new ArgumentException("Invalid casing rule.", "rules");
18 
19  // TODO: do the same for other casing rules
20  }
21 
22  public DefaultNamingRules Rules { get; private set; }
23 
24  public string FormatName(string inputName) {
25  var input = inputName.ToCharArray();
26  var sb = new StringBuilder();
27 
28  bool upperSeen = false;
29  for (int i = 0; i < input.Length; i++) {
30  var c = input[i];
31  if (Char.IsUpper(c)) {
32  if ((Rules & DefaultNamingRules.LowerCase) != 0) {
33  c = Char.ToLower(c);
34  } else if ((Rules & DefaultNamingRules.CamelCase) != 0) {
35  if (upperSeen) {
36  c = Char.ToLower(c);
37  }
38  }
39 
40  upperSeen = true;
41  } else if (Char.IsLower(c)) {
42  if ((Rules & DefaultNamingRules.UpperCase) != 0) {
43  c = Char.ToUpper(c);
44  }
45 
46  upperSeen = false;
47  }
48 
49  sb.Append(c);
50 
51  if (upperSeen && i > 0 && ((Rules & DefaultNamingRules.UnderscoreSeparator) != 0))
52  sb.Append('_');
53  }
54 
55  return sb.ToString();
56  }
57  }
58 }
void ValidateCaseRule(DefaultNamingRules rules)