5 // Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne
\r
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
\r
8 // of this software and associated documentation files (the "Software"), to deal
\r
9 // in the Software without restriction, including without limitation the rights
\r
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
11 // copies of the Software, and to permit persons to whom the Software is
\r
12 // furnished to do so, subject to the following conditions:
\r
14 // The above copyright notice and this permission notice shall be included in
\r
15 // all copies or substantial portions of the Software.
\r
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
\r
28 using System.Collections.Generic;
\r
30 using DbLinq.Schema;
\r
31 using DbLinq.Schema.Dbml;
\r
33 namespace DbLinq.Vendor.Implementation
\r
35 partial class SchemaLoader
\r
38 /// Checks all names in DBML schema,
\r
39 /// and enquotes the ones where a casing problem could occur
\r
41 /// <param name="schema"></param>
\r
42 public virtual void CheckNamesSafety(Database schema)
\r
44 schema.Name = Vendor.SqlProvider.GetSafeName(schema.Name);
\r
45 foreach (var table in schema.Table)
\r
47 table.Name = Vendor.SqlProvider.GetSafeName(table.Name);
\r
48 foreach (var column in table.Type.Columns)
\r
50 column.Name = Vendor.SqlProvider.GetSafeName(column.Name);
\r
52 foreach (var association in table.Type.Associations)
\r
54 association.Name = Vendor.SqlProvider.GetSafeName(association.Name);
\r
57 foreach (var storedProcedure in schema.Functions)
\r
59 storedProcedure.Name = Vendor.SqlProvider.GetSafeName(storedProcedure.Name);
\r
64 /// Gets the primary keys.
\r
66 /// <param name="table">The table.</param>
\r
67 /// <returns></returns>
\r
68 protected static List<string> GetPrimaryKeys(Table table)
\r
70 return (from c in table.Type.Columns where c.IsPrimaryKey select c.Name).ToList();
\r
74 /// Checks for problematic names on columns
\r
75 /// We currently have 1 case, where column is equal to table name
\r
77 /// <param name="schema"></param>
\r
78 protected virtual void CheckColumnsName(Database schema)
\r
80 foreach (var table in schema.Tables)
\r
82 foreach (var column in table.Type.Columns)
\r
85 if (column.Member == table.Type.Name)
\r
87 // now, we try to append 1, then 2, etc.
\r
88 var appendValue = 0;
\r
91 var newColumnMember = column.Member + ++appendValue;
\r
92 if (!table.Type.Columns.Any(c => c.Member == newColumnMember))
\r
94 column.Member = newColumnMember;
\r
103 /// Checks for name conflicts, given lambdas to correct on conflicts
\r
105 /// <param name="schema"></param>
\r
106 /// <param name="tableNamedAssociationRenamer"></param>
\r
107 /// <param name="columnNamedAssociationRenamer"></param>
\r
108 protected virtual void CheckConstraintsName(Database schema,
\r
109 Func<Association, string> tableNamedAssociationRenamer,
\r
110 Func<Association, string> columnNamedAssociationRenamer)
\r
112 foreach (var table in schema.Tables)
\r
114 foreach (var association in table.Type.Associations)
\r
116 var localAssociation = association;
\r
117 if (association.Member == table.Type.Name)
\r
118 association.Member = tableNamedAssociationRenamer(association);
\r
119 else if ((from column in table.Type.Columns where column.Member == localAssociation.Member select column).FirstOrDefault() != null)
\r
121 association.Member = columnNamedAssociationRenamer(association);
\r
128 /// Checks for name conflicts
\r
130 /// <param name="schema"></param>
\r
131 protected virtual void CheckConstraintsName(Database schema)
\r
133 CheckConstraintsName(schema,
\r
134 association => association.ThisKey.Replace(',', '_') + association.Member,
\r
135 association => association.Member + association.Type);
\r
139 /// Generates storage fields, given a formatting method
\r
141 /// <param name="schema"></param>
\r
142 /// <param name="storageGenerator"></param>
\r
143 protected virtual void GenerateStorageAndMemberFields(Database schema, Func<string, string> storageGenerator)
\r
145 foreach (var table in schema.Tables)
\r
147 foreach (var column in table.Type.Columns)
\r
149 column.Storage = storageGenerator(column.Member);
\r
152 Dictionary<string, int> storageFields = new Dictionary<string,int>();
\r
153 Dictionary<string, int> memberFields = new Dictionary<string,int>();
\r
154 foreach (var association in table.Type.Associations)
\r
156 association.Storage = storageGenerator(association.Member);
\r
158 //Associations may contain the same foreign key more than once - add a number suffix to duplicates
\r
159 int storageSuffix = 0;
\r
160 if ( storageFields.TryGetValue(association.Storage, out storageSuffix) )
\r
161 association.Storage += storageSuffix;
\r
162 storageFields[association.Storage] = storageSuffix + 1;
\r
164 int memberSuffix = 0;
\r
165 if ( memberFields.TryGetValue(association.Member, out memberSuffix) )
\r
166 association.Member += memberSuffix;
\r
167 memberFields[association.Member] = memberSuffix + 1;
\r
173 /// Generates all storage fields
\r
175 /// <param name="schema"></param>
\r
176 protected virtual void GenerateStorageAndMemberFields(Database schema)
\r
178 GenerateStorageAndMemberFields(schema, delegate(string name)
\r
180 //jgm 2008June: pre-pended underscore to have same storage format as MS
\r
181 // TODO: add an option for this behavior
\r
182 var storage = "_" + NameFormatter.Format(name, Case.camelCase);
\r
183 if (storage == name)
\r
184 storage = "_" + storage;
\r