Datei: NDOEnhancer/NDOEnhancer/Enhancer/Generator/GenericSqlGenerator.cs
Last Commit (ab290a4)
| 1 | // |
| 2 | // Copyright (c) 2002-2024 Mirko Matytschak |
| 3 | // (www.netdataobjects.de) |
| 4 | // |
| 5 | // Author: Mirko Matytschak |
| 6 | // |
| 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 8 | // documentation files (the "Software"), to deal in the Software without restriction, including without limitation |
| 9 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
| 10 | // Software, and to permit persons to whom the Software is furnished to do so, subject to the following |
| 11 | // conditions: |
| 12 | |
| 13 | // The above copyright notice and this permission notice shall be included in all copies or substantial portions |
| 14 | // of the Software. |
| 15 | // |
| 16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
| 17 | // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 19 | // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 | // DEALINGS IN THE SOFTWARE. |
| 21 | |
| 22 | |
| 23 | using System; |
| 24 | using System.Data; |
| 25 | using NDO.Mapping; |
| 26 | using NDOInterfaces; |
| 27 | |
| 28 | namespace NDOEnhancer |
| 29 | { |
| 30 | ····/// <summary> |
| 31 | ····/// Zusammenfassung für GenericSqlGenerator. |
| 32 | ····/// </summary> |
| 33 | ····internal class GenericSqlGenerator : GenericSqlGeneratorBase |
| 34 | ····{ |
| 35 | ········private readonly IProvider provider; |
| 36 | |
| 37 | ········public GenericSqlGenerator(IProvider provider, ISqlGenerator concreteGenerator, MessageAdapter messages, NDOMapping mappings) |
| 38 | ············: base(provider, concreteGenerator, messages, mappings) |
| 39 | ········{ |
| 40 | ············this.provider = provider; |
| 41 | ········} |
| 42 | |
| 43 | ········public void Generate(DataSet dsSchema, DataSet dsOld, System.IO.StreamWriter stream, TypeManager typeManager, bool generateConstraints) |
| 44 | ········{············ |
| 45 | ············if ( dsOld != null ) |
| 46 | ············{ |
| 47 | ················foreach ( DataTable dt in dsOld.Tables ) |
| 48 | ················{ |
| 49 | ····················if ( generateConstraints ) |
| 50 | ····················{ |
| 51 | ························for ( int i = 0; i < dt.ParentRelations.Count; i++ ) |
| 52 | ························{ |
| 53 | ····························DataRelation dr = dt.ParentRelations[i]; |
| 54 | ····························stream.Write("ALTER TABLE "); |
| 55 | ····························stream.Write(QualifiedTableName.Get(dt.TableName, provider)); |
| 56 | ····························stream.Write(" DROP CONSTRAINT "); |
| 57 | ····························stream.Write(provider.GetQuotedName(dr.RelationName)); |
| 58 | ····························stream.WriteLine(";"); |
| 59 | ························} |
| 60 | ····················} |
| 61 | ················} |
| 62 | ················foreach ( DataTable dt in dsOld.Tables ) |
| 63 | ················{ |
| 64 | ····················stream.WriteLine( concreteGenerator.DropTable( QualifiedTableName.Get(dt.TableName, provider) ) ); |
| 65 | ················} |
| 66 | ················if (dsOld.Tables.Count > 0) |
| 67 | ····················stream.WriteLine(); |
| 68 | ············} |
| 69 | |
| 70 | |
| 71 | ············foreach (DataTable dt in dsSchema.Tables) |
| 72 | ················stream.Write(CreateTable(dt)); |
| 73 | |
| 74 | |
| 75 | ············if ( generateConstraints ) |
| 76 | ············{ |
| 77 | ················foreach ( DataTable dt in dsSchema.Tables ) |
| 78 | ················{ |
| 79 | ····················for ( int i = 0; i < dt.ParentRelations.Count; i++ ) |
| 80 | ····················{ |
| 81 | ························DataRelation dr = dt.ParentRelations[i]; |
| 82 | ························stream.Write("ALTER TABLE "); |
| 83 | ························stream.Write(QualifiedTableName.Get(dt.TableName, provider)); |
| 84 | ························stream.Write(" ADD "); |
| 85 | ························stream.WriteLine(concreteGenerator.CreateForeignKeyConstraint( dr.ChildColumns, dr.ParentColumns, |
| 86 | ····························provider.GetQuotedName( dr.RelationName ), QualifiedTableName.Get(dr.ParentTable.TableName, provider) ) + ";" ); |
| 87 | ····················} |
| 88 | ················} |
| 89 | ············} |
| 90 | |
| 91 | ············if (typeManager != null && typeManager.Entries.Length > 0) |
| 92 | ············{ |
| 93 | ················DataTable typeTable = new DataTable("NDOTypeCodes"); |
| 94 | ················DataColumn pkColumn = typeTable.Columns.Add("TypeCode", typeof (int)); |
| 95 | ················typeTable.Columns.Add("TypeName", typeof(string)); |
| 96 | ················typeTable.Columns.Add("AssemblyName", typeof(string)); |
| 97 | ················typeTable.Columns.Add("TableName", typeof(string)); |
| 98 | ················typeTable.PrimaryKey = new DataColumn[] { pkColumn }; |
| 99 | ················stream.Write(CreateTable(typeTable)); |
| 100 | |
| 101 | ················Class[] descriptors = typeManager.Entries; |
| 102 | ················int l = descriptors.Length; |
| 103 | ················for (int i = 0; i < l; i++) |
| 104 | ················{ |
| 105 | ····················Class cl = descriptors[i]; |
| 106 | ····················stream.Write("INSERT INTO NDOTypeCodes (TypeCode, TypeName, AssemblyName, TableName) VALUES ("); |
| 107 | ····················stream.Write(cl.TypeCode); |
| 108 | ····················stream.Write(","); |
| 109 | ····················stream.Write(provider.GetSqlLiteral(cl.FullName)); |
| 110 | ····················stream.Write(","); |
| 111 | ····················stream.Write(provider.GetSqlLiteral(cl.AssemblyName)); |
| 112 | ····················stream.Write(","); |
| 113 | ····················stream.Write(provider.GetSqlLiteral(cl.TableName)); |
| 114 | ····················stream.WriteLine(");"); |
| 115 | ················} |
| 116 | |
| 117 | ············} |
| 118 | #if noindex |
| 119 | ············foreach (DataTable dt in dsSchema.Tables) |
| 120 | for ( int i = 0; i < dt. ParentRelations. Count; i++) |
| 121 | ············{ |
| 122 | ················DataRelation dr = dt.ParentRelations[i]; |
| 123 | ················string index = concreteGenerator.CreateIndex(provider.GetQuotedName("Ix" + dr.RelationName), QualifiedTableName.Get(dt.TableName, provider), dr.ChildColumns); |
| 124 | ················if (index != string.Empty) |
| 125 | ················{ |
| 126 | ····················stream.Write(index); |
| 127 | ····················stream.WriteLine(";"); |
| 128 | ················} |
| 129 | ············} |
| 130 | ············foreach(DataTable dt in dsSchema.Tables) |
| 131 | ············{ |
| 132 | ················if (GetClassForTable(dt.TableName, mappings) == null) // mappingTable |
| 133 | ················{ |
| 134 | ····················int i = 1; |
| 135 | ····················string tname = dt.TableName; |
| 136 | ····················tname = tname.Substring(tname.IndexOf('.') + 1); |
| 137 | ····················if (tname.StartsWith("rel")) |
| 138 | ························tname = tname.Substring(3); |
| 139 | ····················tname = "Ix" + tname; |
| 140 | ····················foreach(DataColumn dc in dt.Columns) |
| 141 | ····················{ |
| 142 | ························string index = concreteGenerator.CreateIndex(provider.GetQuotedName(tname + i.ToString()), QualifiedTableName.Get(dt.TableName, provider), new DataColumn[] {dc}); |
| 143 | ························if (index != string.Empty) |
| 144 | ························{ |
| 145 | ····························stream.Write(index); |
| 146 | ····························stream.WriteLine(";"); |
| 147 | ····························i++; |
| 148 | ························} |
| 149 | ····················} |
| 150 | ················} |
| 151 | ············} |
| 152 | #endif |
| 153 | ········} |
| 154 | |
| 155 | ····} |
| 156 | } |
| 157 |
New Commit (f5f4fd8)
| 1 | // |
| 2 | // Copyright (c) 2002-2024 Mirko Matytschak |
| 3 | // (www.netdataobjects.de) |
| 4 | // |
| 5 | // Author: Mirko Matytschak |
| 6 | // |
| 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 8 | // documentation files (the "Software"), to deal in the Software without restriction, including without limitation |
| 9 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
| 10 | // Software, and to permit persons to whom the Software is furnished to do so, subject to the following |
| 11 | // conditions: |
| 12 | |
| 13 | // The above copyright notice and this permission notice shall be included in all copies or substantial portions |
| 14 | // of the Software. |
| 15 | // |
| 16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
| 17 | // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 19 | // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 | // DEALINGS IN THE SOFTWARE. |
| 21 | |
| 22 | |
| 23 | using System; |
| 24 | using System.Data; |
| 25 | using NDO.Mapping; |
| 26 | using NDOInterfaces; |
| 27 | |
| 28 | namespace NDOEnhancer |
| 29 | { |
| 30 | ····/// <summary> |
| 31 | ····/// Zusammenfassung für GenericSqlGenerator. |
| 32 | ····/// </summary> |
| 33 | ····internal class GenericSqlGenerator : GenericSqlGeneratorBase |
| 34 | ····{ |
| 35 | ········private readonly IProvider provider; |
| 36 | |
| 37 | ········public GenericSqlGenerator(IProvider provider, ISqlGenerator concreteGenerator, MessageAdapter messages, NDOMapping mappings) |
| 38 | ············: base(provider, concreteGenerator, messages, mappings) |
| 39 | ········{ |
| 40 | ············this.provider = provider; |
| 41 | ········} |
| 42 | |
| 43 | ········public void Generate(DataSet dsSchema, DataSet dsOld, System.IO.StreamWriter stream, TypeManager typeManager, bool generateConstraints) |
| 44 | ········{············ |
| 45 | ············if ( dsOld != null ) |
| 46 | ············{ |
| 47 | ················foreach ( DataTable dt in dsOld.Tables ) |
| 48 | ················{ |
| 49 | ····················if ( generateConstraints ) |
| 50 | ····················{ |
| 51 | ························for ( int i = 0; i < dt.ParentRelations.Count; i++ ) |
| 52 | ························{ |
| 53 | ····························DataRelation dr = dt.ParentRelations[i]; |
| 54 | ····························stream.Write("ALTER TABLE "); |
| 55 | ····························stream.Write(QualifiedTableName.Get(dt.TableName, provider)); |
| 56 | ····························stream.Write(" DROP CONSTRAINT "); |
| 57 | ····························stream.Write(provider.GetQuotedName(dr.RelationName)); |
| 58 | ····························stream.WriteLine(";"); |
| 59 | ························} |
| 60 | ····················} |
| 61 | ················} |
| 62 | ················foreach ( DataTable dt in dsOld.Tables ) |
| 63 | ················{ |
| 64 | ····················stream.WriteLine( concreteGenerator.DropTable( QualifiedTableName.Get(dt.TableName, provider) ) ); |
| 65 | ················} |
| 66 | ················if (dsOld.Tables.Count > 0) |
| 67 | ····················stream.WriteLine(); |
| 68 | ············} |
| 69 | |
| 70 | |
| 71 | ············foreach (DataTable dt in dsSchema.Tables) |
| 72 | ················stream.Write(CreateTable(dt)); |
| 73 | |
| 74 | |
| 75 | ············if ( generateConstraints ) |
| 76 | ············{ |
| 77 | ················foreach ( DataTable dt in dsSchema.Tables ) |
| 78 | ················{ |
| 79 | ····················for ( int i = 0; i < dt.ParentRelations.Count; i++ ) |
| 80 | ····················{ |
| 81 | ························DataRelation dr = dt.ParentRelations[i]; |
| 82 | ························stream.Write("ALTER TABLE "); |
| 83 | ························stream.Write(QualifiedTableName.Get(dt.TableName, provider)); |
| 84 | ························stream.Write(" ADD "); |
| 85 | ························stream.WriteLine(concreteGenerator.CreateForeignKeyConstraint( dr.ChildColumns, dr.ParentColumns, |
| 86 | ····························provider.GetQuotedName( dr.RelationName ), QualifiedTableName.Get(dr.ParentTable.TableName, provider) ) + ";" ); |
| 87 | ····················} |
| 88 | ················} |
| 89 | ············} |
| 90 | |
| 91 | ············if (typeManager != null && typeManager.Entries.Length > 0) |
| 92 | ············{ |
| 93 | ················DataTable typeTable = new DataTable("NDOTypeCodes"); |
| 94 | ················DataColumn pkColumn = typeTable.Columns.Add("TypeCode", typeof (int)); |
| 95 | ················typeTable.Columns.Add("TypeName", typeof(string)); |
| 96 | ················typeTable.Columns.Add("AssemblyName", typeof(string)); |
| 97 | ················typeTable.Columns.Add("TableName", typeof(string)); |
| 98 | ················typeTable.PrimaryKey = new DataColumn[] { pkColumn }; |
| 99 | ················stream.Write(CreateTable(typeTable)); |
| 100 | |
| 101 | ················Class[] descriptors = typeManager.Entries; |
| 102 | ················int l = descriptors.Length; |
| 103 | ················for (int i = 0; i < l; i++) |
| 104 | ················{ |
| 105 | ····················Class cl = descriptors[i]; |
| 106 | ····················stream.Write("INSERT INTO NDOTypeCodes (TypeCode, TypeName, AssemblyName, TableName) VALUES ("); |
| 107 | ····················stream.Write(cl.TypeCode); |
| 108 | ····················stream.Write(","); |
| 109 | ····················stream.Write(provider.GetSqlLiteral(cl.FullName)); |
| 110 | ····················stream.Write(","); |
| 111 | ····················stream.Write(provider.GetSqlLiteral(cl.AssemblyName)); |
| 112 | ····················stream.Write(","); |
| 113 | ····················stream.Write(provider.GetSqlLiteral(cl.TableName)); |
| 114 | ····················stream.WriteLine(");"); |
| 115 | ················} |
| 116 | |
| 117 | ············} |
| 118 | ············foreach (DataTable dt in dsSchema.Tables) |
| 119 | stream. Write( CreateIndexes( dt) ) ; |
| 120 | ········} |
| 121 | |
| 122 | ····} |
| 123 | } |
| 124 |