Datei: NDODLL/SqlPersistenceHandling/SqlColumnListGenerator.cs
Last Commit (123a4dc)
| 1 | using NDO.Mapping; |
| 2 | using NDOInterfaces; |
| 3 | using System; |
| 4 | using System.Collections.Generic; |
| 5 | using System.Linq; |
| 6 | using System.Reflection; |
| 7 | using System.Text; |
| 8 | |
| 9 | namespace NDO.SqlPersistenceHandling |
| 10 | { |
| 11 | ····/// <summary> |
| 12 | ····/// Helper class to generate lists of columns which can be used in Sql queries |
| 13 | ····/// </summary> |
| 14 | ····public class SqlColumnListGenerator |
| 15 | ····{ |
| 16 | ········IProvider provider; |
| 17 | ········List<string> baseColumnList = new List<string>(); |
| 18 | ········List<string> selectColumnList = new List<string>(); |
| 19 | ········List<string> paramList = new List<string>(); |
| 20 | ········List<string> hollowList = new List<string>(); |
| 21 | |
| 22 | ········string tableName; |
| 23 | ········Type resultType; |
| 24 | |
| 25 | ········/// <summary> |
| 26 | ········/// Constructor |
| 27 | ········/// </summary> |
| 28 | ········/// <param name="cls"></param> |
| 29 | ········public SqlColumnListGenerator( Class cls ) |
| 30 | ········{ |
| 31 | ············Init( cls ); |
| 32 | ········} |
| 33 | |
| 34 | ········/// <summary> |
| 35 | ········/// Initializes the ColumnListGenerator |
| 36 | ········/// </summary> |
| 37 | ········/// <param name="cls"></param> |
| 38 | ········public void Init( Class cls ) |
| 39 | ········{ |
| 40 | ············this.tableName = cls.TableName; |
| 41 | ············this.resultType = cls.SystemType; |
| 42 | |
| 43 | ············provider = cls.Provider; |
| 44 | ············FieldMap fm = new FieldMap( cls ); |
| 45 | ············var persistentFields = fm.PersistentFields; |
| 46 | ············var relationInfos = new RelationCollector( cls ).CollectRelations().ToList(); |
| 47 | |
| 48 | ············foreach (OidColumn oidColumn in cls.Oid.OidColumns) |
| 49 | ············{ |
| 50 | ················hollowList.Add( oidColumn.Name ); |
| 51 | |
| 52 | ················Relation r = oidColumn.Relation; |
| 53 | ················if (r != null && r.ForeignKeyTypeColumnName != null) |
| 54 | ····················hollowList.Add( r.ForeignKeyTypeColumnName );··// Die Frage ist, ob das nicht auch in der baseColumnList auftauchen muss. |
| 55 | |
| 56 | ················if (!oidColumn.AutoIncremented && oidColumn.FieldName == null && oidColumn.RelationName == null) |
| 57 | ················{ |
| 58 | ····················baseColumnList.Add( oidColumn.Name ); |
| 59 | ····················paramList.Add( oidColumn.Name ); |
| 60 | ················} |
| 61 | ············} |
| 62 | |
| 63 | ············foreach (var e in persistentFields) |
| 64 | ············{ |
| 65 | ················Type memberType; |
| 66 | ················if (e.Value is FieldInfo) |
| 67 | ····················memberType = ((FieldInfo)e.Value).FieldType; |
| 68 | ················else |
| 69 | ····················memberType = ((PropertyInfo)e.Value).PropertyType; |
| 70 | |
| 71 | ················var fieldMapping = cls.FindField( (string)e.Key ); |
| 72 | |
| 73 | ················// Ignore fields without mapping. |
| 74 | ················if (null == fieldMapping) |
| 75 | ····················continue; |
| 76 | |
| 77 | ················if (null == fieldMapping.Column.DbType) |
| 78 | ················{ |
| 79 | ····················fieldMapping.ColumnDbType = (int)provider.GetDbType( memberType ); |
| 80 | ················} |
| 81 | ················else |
| 82 | ················{ |
| 83 | ····················fieldMapping.ColumnDbType = (int)provider.GetDbType( fieldMapping.Column.DbType ); |
| 84 | ················} |
| 85 | |
| 86 | ················baseColumnList.Add( fieldMapping.Column.Name ); |
| 87 | ················paramList.Add( fieldMapping.Column.Name ); |
| 88 | ············} |
| 89 | |
| 90 | ············foreach (RelationFieldInfo ri in relationInfos) |
| 91 | ············{ |
| 92 | ················Relation r = ri.Rel; |
| 93 | ················foreach (ForeignKeyColumn fkColumn in r.ForeignKeyColumns) |
| 94 | ················{ |
| 95 | ····················baseColumnList.Add( fkColumn.Name ); |
| 96 | ····················paramList.Add( fkColumn.Name ); |
| 97 | ················} |
| 98 | ················if (r.ForeignKeyTypeColumnName != null) |
| 99 | ················{ |
| 100 | ····················baseColumnList.Add( r.ForeignKeyTypeColumnName ); |
| 101 | ····················paramList.Add( r.ForeignKeyTypeColumnName ); |
| 102 | ················} |
| 103 | |
| 104 | ············} |
| 105 | |
| 106 | ············if (cls.TimeStampColumn != null) |
| 107 | ············{ |
| 108 | ················baseColumnList.Add( cls.TimeStampColumn ); |
| 109 | ················paramList.Add( cls.TimeStampColumn ); |
| 110 | ················hollowList.Add( cls.TimeStampColumn ); |
| 111 | ············} |
| 112 | |
| 113 | ············if (cls.TypeNameColumn != null) |
| 114 | ············{ |
| 115 | ················var typeColumnName = cls.TypeNameColumn.Name; |
| 116 | ················baseColumnList.Add( typeColumnName ); |
| 117 | ················paramList.Add( typeColumnName ); |
| 118 | ················hollowList.Add( typeColumnName ); |
| 119 | ············} |
| 120 | |
| 121 | ············foreach (OidColumn oidColumn in cls.Oid.OidColumns) |
| 122 | ············{ |
| 123 | ················if (oidColumn.FieldName == null && oidColumn.RelationName == null && oidColumn.AutoIncremented) |
| 124 | ····················selectColumnList.Add( oidColumn.Name ); |
| 125 | ············} |
| 126 | |
| 127 | ············selectColumnList.AddRange( baseColumnList ); |
| 128 | |
| 129 | ········} |
| 130 | |
| 131 | ········/// <summary/> |
| 132 | ········public string BaseList => Result( this.baseColumnList, false, false ); |
| 133 | ········/// <summary/> |
| 134 | ········public string SelectList => Result( this.selectColumnList, false, false ); |
| 135 | ········/// <summary/> |
| 136 | ········public string SelectListWithAlias => Result( this.selectColumnList, true, false ); |
| 137 | ········/// <summary/> |
| 138 | ········public string HollowFields => Result( this.hollowList, false, false ); |
| 139 | ········/// <summary/> |
| 140 | ········public string HollowFieldsWithAlias => Result( this.hollowList, true, false ); |
| 141 | |
| 142 | ········/// <summary/> |
| 143 | ········public string ParamList |
| 144 | ········{ |
| 145 | ············get |
| 146 | ············{ |
| 147 | ················StringBuilder result = new StringBuilder(); |
| 148 | ················int ende = this.paramList.Count - 1; |
| 149 | ················for (int i = 0; i < this.paramList.Count; i++) |
| 150 | ················{ |
| 151 | ····················if (this.provider.UseNamedParams) |
| 152 | ····················{ |
| 153 | ························result.Append( this.provider.GetNamedParameter( this.paramList[i] ) ); |
| 154 | ························if (i < ende) |
| 155 | ························{ |
| 156 | ····························result.Append( ", " ); |
| 157 | ························} |
| 158 | ····················} |
| 159 | ····················else |
| 160 | ····················{ |
| 161 | ························if (i < ende) |
| 162 | ························{ |
| 163 | ····························result.Append( "?, " ); |
| 164 | ························} |
| 165 | ························else |
| 166 | ····························result.Append( "?" ); |
| 167 | ····················} |
| 168 | ················} |
| 169 | |
| 170 | ················return result.ToString(); |
| 171 | ············} |
| 172 | ········} |
| 173 | |
| 174 | ········/// <summary/> |
| 175 | ········public string Result(bool hollow, bool generateAliasNames, bool useTableName) |
| 176 | ········{ |
| 177 | ············return Result( hollow ? this.hollowList : this.selectColumnList, generateAliasNames, useTableName ); |
| 178 | ········} |
| 179 | |
| 180 | ········string Result(List<string> columnList, bool generateAliasNames, bool useTableName ) |
| 181 | ········{ |
| 182 | ············StringBuilder sb = new StringBuilder(); |
| 183 | ············int ende = columnList.Count - 1; |
| 184 | ············for (int i = 0; i < columnList.Count; i++) |
| 185 | ············{ |
| 186 | ················string f = columnList[i]; |
| 187 | ················if (useTableName) |
| 188 | ················{ |
| 189 | ····················sb.Append( provider.GetQualifiedTableName( tableName ) ); |
| 190 | ····················sb.Append( '.' ); |
| 191 | ················} |
| 192 | ················sb.Append( provider.GetQuotedName( f ) ); |
| 193 | ················if (generateAliasNames) |
| 194 | ················{ |
| 195 | ····················sb.Append( " AS " ); |
| 196 | ····················sb.Append( provider.GetQuotedName( f ) ); |
| 197 | ················} |
| 198 | ················if (i < ende) |
| 199 | ················{ |
| 200 | ····················sb.Append( ", " ); |
| 201 | ················} |
| 202 | ············} |
| 203 | |
| 204 | ············return sb.ToString(); |
| 205 | ········} |
| 206 | ····} |
| 207 | } |
| 208 |
New Commit (16046d5)
| 1 | using NDO.Mapping; |
| 2 | using NDOInterfaces; |
| 3 | using System; |
| 4 | using System.Collections.Generic; |
| 5 | using System.Linq; |
| 6 | using System.Reflection; |
| 7 | using System.Text; |
| 8 | |
| 9 | namespace NDO.SqlPersistenceHandling |
| 10 | { |
| 11 | ····/// <summary> |
| 12 | ····/// Helper class to generate lists of columns which can be used in Sql queries |
| 13 | ····/// </summary> |
| 14 | ····public class SqlColumnListGenerator |
| 15 | ····{ |
| 16 | ········IProvider provider; |
| 17 | ········List<string> baseColumnList = new List<string>(); |
| 18 | ········List<string> selectColumnList = new List<string>(); |
| 19 | ········List<string> paramList = new List<string>(); |
| 20 | ········List<string> hollowList = new List<string>(); |
| 21 | |
| 22 | ········string tableName; |
| 23 | ········Type resultType; |
| 24 | |
| 25 | ········/// <summary> |
| 26 | ········/// Constructor |
| 27 | ········/// </summary> |
| 28 | ········/// <param name="cls"></param> |
| 29 | ········public SqlColumnListGenerator( Class cls ) |
| 30 | ········{ |
| 31 | ············Init( cls ); |
| 32 | ········} |
| 33 | |
| 34 | ········/// <summary> |
| 35 | ········/// Initializes the ColumnListGenerator |
| 36 | ········/// </summary> |
| 37 | ········/// <param name="cls"></param> |
| 38 | ········public void Init( Class cls ) |
| 39 | ········{ |
| 40 | ············this.tableName = cls.TableName; |
| 41 | ············this.resultType = cls.SystemType; |
| 42 | |
| 43 | ············provider = cls.Provider; |
| 44 | ············FieldMap fm = new FieldMap( cls ); |
| 45 | ············var persistentFields = fm.PersistentFields; |
| 46 | ············var relationInfos = new RelationCollector( cls ).CollectRelations().ToList(); |
| 47 | ············List<string> readOnlyColumns = new List<string>(); |
| 48 | |
| 49 | ············foreach (OidColumn oidColumn in cls.Oid.OidColumns) |
| 50 | ············{ |
| 51 | ················hollowList.Add( oidColumn.Name ); |
| 52 | |
| 53 | ················Relation r = oidColumn.Relation; |
| 54 | ················if (r != null && r.ForeignKeyTypeColumnName != null) |
| 55 | ····················hollowList.Add( r.ForeignKeyTypeColumnName );··// Die Frage ist, ob das nicht auch in der baseColumnList auftauchen muss. |
| 56 | |
| 57 | ················if (!oidColumn.AutoIncremented && oidColumn.FieldName == null && oidColumn.RelationName == null) |
| 58 | ················{ |
| 59 | ····················baseColumnList.Add( oidColumn.Name ); |
| 60 | ····················paramList.Add( oidColumn.Name ); |
| 61 | ················} |
| 62 | ············} |
| 63 | |
| 64 | ············foreach (var e in persistentFields) |
| 65 | ············{ |
| 66 | ················Type memberType; |
| 67 | ················if (e.Value is FieldInfo) |
| 68 | ····················memberType = ((FieldInfo)e.Value).FieldType; |
| 69 | ················else |
| 70 | ····················memberType = ((PropertyInfo)e.Value).PropertyType; |
| 71 | |
| 72 | ················var fieldMapping = cls.FindField( (string)e.Key ); |
| 73 | ················bool isReadOnly = false; |
| 74 | |
| 75 | ················if (e.Value.CustomAttributes.Any( c => c.AttributeType == typeof( NDOReadOnlyAttribute ) )) |
| 76 | ················{ |
| 77 | ····················readOnlyColumns.Add( fieldMapping.Column.Name ); |
| 78 | ····················isReadOnly = true; |
| 79 | ················} |
| 80 | |
| 81 | ················// Ignore fields without mapping. |
| 82 | ················if (null == fieldMapping) |
| 83 | ····················continue; |
| 84 | |
| 85 | ················if (null == fieldMapping.Column.DbType) |
| 86 | ················{ |
| 87 | ····················fieldMapping.ColumnDbType = (int)provider.GetDbType( memberType ); |
| 88 | ················} |
| 89 | ················else |
| 90 | ················{ |
| 91 | ····················fieldMapping.ColumnDbType = (int)provider.GetDbType( fieldMapping.Column.DbType ); |
| 92 | ················} |
| 93 | |
| 94 | ················if (!isReadOnly) |
| 95 | ················{ |
| 96 | ····················baseColumnList.Add( fieldMapping.Column.Name ); |
| 97 | ····················paramList.Add( fieldMapping.Column.Name ); |
| 98 | ················} |
| 99 | ············} |
| 100 | |
| 101 | ············foreach (RelationFieldInfo ri in relationInfos) |
| 102 | ············{ |
| 103 | ················Relation r = ri.Rel; |
| 104 | ················foreach (ForeignKeyColumn fkColumn in r.ForeignKeyColumns) |
| 105 | ················{ |
| 106 | ····················baseColumnList.Add( fkColumn.Name ); |
| 107 | ····················paramList.Add( fkColumn.Name ); |
| 108 | ················} |
| 109 | ················if (r.ForeignKeyTypeColumnName != null) |
| 110 | ················{ |
| 111 | ····················baseColumnList.Add( r.ForeignKeyTypeColumnName ); |
| 112 | ····················paramList.Add( r.ForeignKeyTypeColumnName ); |
| 113 | ················} |
| 114 | |
| 115 | ············} |
| 116 | |
| 117 | ············if (cls.TimeStampColumn != null) |
| 118 | ············{ |
| 119 | ················baseColumnList.Add( cls.TimeStampColumn ); |
| 120 | ················paramList.Add( cls.TimeStampColumn ); |
| 121 | ················hollowList.Add( cls.TimeStampColumn ); |
| 122 | ············} |
| 123 | |
| 124 | ············if (cls.TypeNameColumn != null) |
| 125 | ············{ |
| 126 | ················var typeColumnName = cls.TypeNameColumn.Name; |
| 127 | ················baseColumnList.Add( typeColumnName ); |
| 128 | ················paramList.Add( typeColumnName ); |
| 129 | ················hollowList.Add( typeColumnName ); |
| 130 | ············} |
| 131 | |
| 132 | ············foreach (OidColumn oidColumn in cls.Oid.OidColumns) |
| 133 | ············{ |
| 134 | ················if (oidColumn.FieldName == null && oidColumn.RelationName == null && oidColumn.AutoIncremented) |
| 135 | ····················selectColumnList.Add( oidColumn.Name ); |
| 136 | ············} |
| 137 | |
| 138 | ············selectColumnList.AddRange( baseColumnList ); |
| 139 | ············selectColumnList.AddRange( readOnlyColumns ); |
| 140 | |
| 141 | ········} |
| 142 | |
| 143 | ········/// <summary/> |
| 144 | ········public string BaseList => Result( this.baseColumnList, false, false ); |
| 145 | ········/// <summary/> |
| 146 | ········public string SelectList => Result( this.selectColumnList, false, false ); |
| 147 | ········/// <summary/> |
| 148 | ········public string SelectListWithAlias => Result( this.selectColumnList, true, false ); |
| 149 | ········/// <summary/> |
| 150 | ········public string HollowFields => Result( this.hollowList, false, false ); |
| 151 | ········/// <summary/> |
| 152 | ········public string HollowFieldsWithAlias => Result( this.hollowList, true, false ); |
| 153 | |
| 154 | ········/// <summary/> |
| 155 | ········public string ParamList |
| 156 | ········{ |
| 157 | ············get |
| 158 | ············{ |
| 159 | ················StringBuilder result = new StringBuilder(); |
| 160 | ················int ende = this.paramList.Count - 1; |
| 161 | ················for (int i = 0; i < this.paramList.Count; i++) |
| 162 | ················{ |
| 163 | ····················if (this.provider.UseNamedParams) |
| 164 | ····················{ |
| 165 | ························result.Append( this.provider.GetNamedParameter( this.paramList[i] ) ); |
| 166 | ························if (i < ende) |
| 167 | ························{ |
| 168 | ····························result.Append( ", " ); |
| 169 | ························} |
| 170 | ····················} |
| 171 | ····················else |
| 172 | ····················{ |
| 173 | ························if (i < ende) |
| 174 | ························{ |
| 175 | ····························result.Append( "?, " ); |
| 176 | ························} |
| 177 | ························else |
| 178 | ····························result.Append( "?" ); |
| 179 | ····················} |
| 180 | ················} |
| 181 | |
| 182 | ················return result.ToString(); |
| 183 | ············} |
| 184 | ········} |
| 185 | |
| 186 | ········/// <summary/> |
| 187 | ········public string Result(bool hollow, bool generateAliasNames, bool useTableName) |
| 188 | ········{ |
| 189 | ············return Result( hollow ? this.hollowList : this.selectColumnList, generateAliasNames, useTableName ); |
| 190 | ········} |
| 191 | |
| 192 | ········string Result(List<string> columnList, bool generateAliasNames, bool useTableName ) |
| 193 | ········{ |
| 194 | ············StringBuilder sb = new StringBuilder(); |
| 195 | ············int ende = columnList.Count - 1; |
| 196 | ············for (int i = 0; i < columnList.Count; i++) |
| 197 | ············{ |
| 198 | ················string f = columnList[i]; |
| 199 | ················if (useTableName) |
| 200 | ················{ |
| 201 | ····················sb.Append( provider.GetQualifiedTableName( tableName ) ); |
| 202 | ····················sb.Append( '.' ); |
| 203 | ················} |
| 204 | ················sb.Append( provider.GetQuotedName( f ) ); |
| 205 | ················if (generateAliasNames) |
| 206 | ················{ |
| 207 | ····················sb.Append( " AS " ); |
| 208 | ····················sb.Append( provider.GetQuotedName( f ) ); |
| 209 | ················} |
| 210 | ················if (i < ende) |
| 211 | ················{ |
| 212 | ····················sb.Append( ", " ); |
| 213 | ················} |
| 214 | ············} |
| 215 | |
| 216 | ············return sb.ToString(); |
| 217 | ········} |
| 218 | ····} |
| 219 | } |
| 220 |