File: NameFormatter.cs

package info (click to toggle)
mono 4.6.2.7+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (447 lines) | stat: -rw-r--r-- 18,532 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#region MIT license
// 
// MIT license
//
// Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// 
#endregion

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using DbLinq.Factory;
using DbLinq.Language;

namespace DbLinq.Schema.Implementation
{
    /// <summary>
    /// INameFormatter default implementation
    /// </summary>
    internal class NameFormatter : INameFormatter
    {
        /// <summary>
        /// Singularization type
        /// </summary>
        internal enum Singularization
        {
            /// <summary>
            /// The word plural doesn't change
            /// </summary>
            DontChange,
            /// <summary>
            /// Singularize the word
            /// </summary>
            Singular,
            /// <summary>
            /// Pluralize the word
            /// </summary>
            Plural,
        }

        /// <summary>
        /// Indicates the word position. Internally used for capitalization
        /// </summary>
        [Flags]
        protected enum Position
        {
            /// <summary>
            /// Word is first in sentence
            /// </summary>
            First = 0x01,
            /// <summary>
            /// Word is last in sentence
            /// </summary>
            Last = 0x02,
        }

        /// <summary>
        /// ILanguageWords by culture info name
        /// </summary>
        private readonly IDictionary<string, ILanguageWords> languageWords = new Dictionary<string, ILanguageWords>();

        /// <summary>
        /// Substitution char for invalid characters
        /// </summary>
        private const char SubstitutionChar = '_';

        /// <summary>
        /// Gets the ILanguageWords by CultureInfo.
        /// </summary>
        /// <param name="cultureInfo">The culture info.</param>
        /// <returns></returns>
        protected virtual ILanguageWords GetLanguageWords(CultureInfo cultureInfo)
        {
            lock (languageWords)
            {
                ILanguageWords words;
                if (!languageWords.TryGetValue(cultureInfo.Name, out words))
                {
                    var languages = ObjectFactory.Get<ILanguages>();
                    words = languages.Load(cultureInfo);
                    languageWords[cultureInfo.Name] = words;
                }
                return words;
            }
        }

        /// <summary>
        /// Formats the specified words.
        /// </summary>
        /// <param name="words">The words.</param>
        /// <param name="oldName">The old name.</param>
        /// <param name="newCase">The new case.</param>
        /// <param name="singularization">The singularization.</param>
        /// <returns></returns>
        public virtual string Format(ILanguageWords words, string oldName, Case newCase, Singularization singularization)
        {
            var parts = ExtractWordsFromCaseAndLanguage(words, oldName);
            return Format(words, parts, newCase, singularization);
        }

        /// <summary>
        /// Formats the specified words.
        /// </summary>
        /// <param name="words">The words.</param>
        /// <param name="parts">The parts.</param>
        /// <param name="newCase">The new case.</param>
        /// <param name="singularization">The singularization.</param>
        /// <returns></returns>
        private string Format(ILanguageWords words, IList<string> parts, Case newCase, Singularization singularization)
        {
            var result = new StringBuilder();
            for (int partIndex = 0; partIndex < parts.Count; partIndex++)
            {
                Position position = 0;
                if (partIndex == 0)
                    position |= Position.First;
                if (partIndex == parts.Count - 1)
                    position |= Position.Last;
                result.Append(AdjustPart(words, parts[partIndex], position, newCase, singularization));
            }
            return result.ToString();
        }

        /// <summary>
        /// Toes the camel case.
        /// </summary>
        /// <param name="part">The part.</param>
        /// <returns></returns>
        public string ToCamelCase(string part)
        {
            return part.ToLower();
        }

        /// <summary>
        /// Toes the pascal case.
        /// </summary>
        /// <param name="part">The part.</param>
        /// <returns></returns>
        public string ToPascalCase(string part)
        {
            // we have a very special case here, for "ID" that goes to full uppercase even in PascalCase mode
            if (string.Compare(part, "id", true) == 0)
                return "ID";
            part = part.Substring(0, 1).ToUpper() + part.Substring(1).ToLower();
            return part;
        }

        /// <summary>
        /// Toes the net case.
        /// </summary>
        /// <param name="part">The part.</param>
        /// <returns></returns>
        public string ToNetCase(string part)
        {
            return ToPascalCase(part);
        }

        /// <summary>
        /// Adjusts the part.
        /// </summary>
        /// <param name="words">The words.</param>
        /// <param name="part">The part.</param>
        /// <param name="position">The position.</param>
        /// <param name="newCase">The new case.</param>
        /// <param name="singularization">The singularization.</param>
        /// <returns></returns>
        protected virtual string AdjustPart(ILanguageWords words, string part, Position position, Case newCase, Singularization singularization)
        {
            if (singularization != Singularization.DontChange && (position & Position.Last) != 0)
            {
                if (singularization == Singularization.Singular)
                    part = words.Singularize(part);
                else
                    part = words.Pluralize(part);
            }
            Case applyCase = newCase;
            if (applyCase == Case.camelCase && (position & Position.First) == 0)
                applyCase = Case.PascalCase;
            switch (applyCase)
            {
            case Case.Leave:
                break;
            case Case.camelCase:
                part = ToCamelCase(part);
                break;
            case Case.PascalCase:
                part = ToPascalCase(part);
                break;
            case Case.NetCase:
                part = ToNetCase(part);
                break;
            default:
                throw new ArgumentOutOfRangeException();
            }
            return part;
        }

        /// <summary>
        /// Pushes the word on a collection
        /// </summary>
        /// <param name="words">The words.</param>
        /// <param name="currentWord">The current word.</param>
        private static void PushWord(ICollection<string> words, StringBuilder currentWord)
        {
            if (currentWord.Length > 0)
            {
                words.Add(currentWord.ToString());
                currentWord.Remove(0, currentWord.Length);
            }
        }

        /// <summary>
        /// Extracts words from uppercase and _
        /// A word can also be composed of several uppercase letters
        /// </summary>
        /// <param name="name"></param>
        /// <param name="substitutionChar"></param>
        /// <returns></returns>
        protected virtual IList<string> ExtractWordsFromCase(string name, char substitutionChar)
        {
            var words = new List<string>();
            bool currentLowerCase = true;
            var currentWord = new StringBuilder();
            for (int charIndex = 0; charIndex < name.Length; charIndex++)
            {
                char currentChar = name[charIndex];
                bool isLower = char.IsLower(currentChar);
                // we switched to uppercase
                if (!isLower && currentLowerCase)
                {
                    PushWord(words, currentWord);
                }
                else if (isLower && !currentLowerCase)
                {
                    // if the current word has several uppercase letters, it is one unique word
                    if (currentWord.Length > 1)
                        PushWord(words, currentWord);
                }
                // only letters or digits are allowed
                if (char.IsLetterOrDigit(currentChar))
                    currentWord.Append(currentChar);
                // _ is the separator character, but all other characters will be kept
                else if (currentChar != '_' && !char.IsSeparator(currentChar))
                    currentWord.Append(substitutionChar);
                currentLowerCase = isLower;
            }
            PushWord(words, currentWord);

            return words;
        }

        /// <summary>
        /// Extracts the words from case and language.
        /// </summary>
        /// <param name="words">The words.</param>
        /// <param name="dbName">Name of the db.</param>
        /// <returns></returns>
        protected virtual IList<string> ExtractWordsFromCaseAndLanguage(ILanguageWords words, string dbName)
        {
            var extractedWords = new List<string>();
            foreach (var wordsMagma in ExtractWordsFromCase(dbName, SubstitutionChar))
            {
                extractedWords.AddRange(words.GetWords(wordsMagma));
            }
            return extractedWords;
        }

        /// <summary>
        /// Extracts the words from given text.
        /// </summary>
        /// <param name="words">The words.</param>
        /// <param name="dbName">Name of the db.</param>
        /// <param name="extraction">The extraction type (case or language identification).</param>
        /// <returns></returns>
        protected virtual IList<string> ExtractWords(ILanguageWords words, string dbName, WordsExtraction extraction)
        {
            switch (extraction)
            {
            case WordsExtraction.None:
                return new[] { dbName };
            case WordsExtraction.FromCase:
                return ExtractWordsFromCase(dbName, SubstitutionChar);
            case WordsExtraction.FromDictionary:
                return ExtractWordsFromCaseAndLanguage(words, dbName);
            default:
                throw new ArgumentOutOfRangeException("extraction");
            }
        }

        /// <summary>
        /// Gets the singularization.
        /// </summary>
        /// <param name="singularization">The singularization.</param>
        /// <param name="nameFormat">The name format.</param>
        /// <returns></returns>
        protected virtual Singularization GetSingularization(Singularization singularization, NameFormat nameFormat)
        {
            if (!nameFormat.Pluralize)
                return Singularization.DontChange;
            return singularization;
        }

        /// <summary>
        /// Reformats a name by adjusting its case.
        /// </summary>
        /// <param name="words">The words.</param>
        /// <param name="newCase">The new case.</param>
        /// <returns></returns>
        public string Format(string words, Case newCase)
        {
            return Format(null, ExtractWordsFromCase(words, SubstitutionChar), newCase, Singularization.DontChange);
        }

        /// <summary>
        /// Gets the name of the schema.
        /// </summary>
        /// <param name="dbName">Name of the db.</param>
        /// <param name="extraction">The extraction.</param>
        /// <param name="nameFormat">The name format.</param>
        /// <returns></returns>
        public SchemaName GetSchemaName(string dbName, WordsExtraction extraction, NameFormat nameFormat)
        {
            var words = GetLanguageWords(nameFormat.Culture);
            var schemaName = new SchemaName { DbName = dbName };
            schemaName.NameWords = ExtractWords(words, dbName, extraction);
            schemaName.ClassName = Format(words, schemaName.NameWords, nameFormat.Case, Singularization.DontChange);
            return schemaName;
        }

        /// <summary>
        /// Gets the name of the procedure.
        /// </summary>
        /// <param name="dbName">Name of the db.</param>
        /// <param name="extraction">The extraction.</param>
        /// <param name="nameFormat">The name format.</param>
        /// <returns></returns>
        public ProcedureName GetProcedureName(string dbName, WordsExtraction extraction, NameFormat nameFormat)
        {
            var words = GetLanguageWords(nameFormat.Culture);
            var procedureName = new ProcedureName { DbName = dbName };
            procedureName.NameWords = ExtractWords(words, dbName, extraction);
            procedureName.MethodName = Format(words, procedureName.NameWords, nameFormat.Case, Singularization.DontChange);
            return procedureName;
        }

        /// <summary>
        /// Gets the name of the parameter.
        /// </summary>
        /// <param name="dbName">Name of the db.</param>
        /// <param name="extraction">The extraction.</param>
        /// <param name="nameFormat">The name format.</param>
        /// <returns></returns>
        public ParameterName GetParameterName(string dbName, WordsExtraction extraction, NameFormat nameFormat)
        {
            var words = GetLanguageWords(nameFormat.Culture);
            var parameterName = new ParameterName { DbName = dbName };
            parameterName.NameWords = ExtractWords(words, dbName, extraction);
            parameterName.CallName = Format(words, parameterName.NameWords, Case.camelCase, Singularization.DontChange);
            return parameterName;
        }

        /// <summary>
        /// Gets the name of the table.
        /// </summary>
        /// <param name="dbName">Name of the db.</param>
        /// <param name="extraction">The extraction.</param>
        /// <param name="nameFormat">The name format.</param>
        /// <returns></returns>
        public TableName GetTableName(string dbName, WordsExtraction extraction, NameFormat nameFormat)
        {
            var words = GetLanguageWords(nameFormat.Culture);
            var tableName = new TableName { DbName = dbName };
            tableName.NameWords = ExtractWords(words, dbName, extraction);
            // if no extraction (preset name, just copy it)
            if (extraction == WordsExtraction.None)
                tableName.ClassName = tableName.DbName;
            else
                tableName.ClassName = Format(words, tableName.NameWords, nameFormat.Case, GetSingularization(Singularization.Singular, nameFormat));
            tableName.MemberName = Format(words, tableName.NameWords, nameFormat.Case, GetSingularization(Singularization.Plural, nameFormat));
            return tableName;
        }

        /// <summary>
        /// Gets the name of the column.
        /// </summary>
        /// <param name="dbName">Name of the db.</param>
        /// <param name="extraction">The extraction.</param>
        /// <param name="nameFormat">The name format.</param>
        /// <returns></returns>
        public ColumnName GetColumnName(string dbName, WordsExtraction extraction, NameFormat nameFormat)
        {
            var words = GetLanguageWords(nameFormat.Culture);
            var columnName = new ColumnName { DbName = dbName };
            columnName.NameWords = ExtractWords(words, dbName, extraction);
            // if no extraction (preset name, just copy it)
            if (extraction == WordsExtraction.None)
                columnName.PropertyName = dbName;
            else
                columnName.PropertyName = Format(words, columnName.NameWords, nameFormat.Case, Singularization.DontChange);
            return columnName;
        }

        /// <summary>
        /// Gets the name of the association.
        /// </summary>
        /// <param name="dbManyName">Name of the db many.</param>
        /// <param name="dbOneName">Name of the db one.</param>
        /// <param name="dbConstraintName">Name of the db constraint.</param>
        /// <param name="foreignKeyName">Name of the foreign key.</param>
        /// <param name="extraction">The extraction.</param>
        /// <param name="nameFormat">The name format.</param>
        /// <returns></returns>
        public AssociationName GetAssociationName(string dbManyName, string dbOneName, string dbConstraintName,
            string foreignKeyName, WordsExtraction extraction, NameFormat nameFormat)
        {
            var words = GetLanguageWords(nameFormat.Culture);
            var associationName = new AssociationName { DbName = dbManyName };
            associationName.NameWords = ExtractWords(words, dbManyName, extraction);
            associationName.ManyToOneMemberName = Format(words, dbOneName, nameFormat.Case, GetSingularization(Singularization.Singular, nameFormat));
            // TODO: this works only for PascalCase
            if (dbManyName == dbOneName)
                associationName.ManyToOneMemberName = foreignKeyName.Replace(',', '_') + associationName.ManyToOneMemberName;
            // TODO: support new extraction
            associationName.OneToManyMemberName = Format(words, dbManyName, nameFormat.Case, GetSingularization(Singularization.Plural, nameFormat));
            return associationName;
        }
    }
}