/// <summary> /// 检测链接 /// </summary> /// <param name="assembly">程序集</param> internal unsafe static void CheckConnection(Assembly assembly) { bool isAssembly; string assemblyName = assembly.FullName; fixed(char *nameFixed = assemblyName) { char *splitIndex = unsafer.String.FindAscii(nameFixed, nameFixed + assemblyName.Length, checkConnectionAssemblyNameMap.Map); isAssembly = splitIndex == null || !checkConnectionAssemblyNames.Contains(assemblyName.Substring(0, (int)(splitIndex - nameFixed))); } if (isAssembly) { Type currentType = null; try { foreach (Type type in assembly.GetTypes()) { if (type.IsClass && !type.IsAbstract && !type.IsGenericType && type.IsVisible) { fastCSharp.emit.sqlTable sqlTable = fastCSharp.code.typeAttribute.GetAttribute <fastCSharp.emit.sqlTable>(type, false, true); if (sqlTable != null && Array.IndexOf(fastCSharp.config.sql.Default.CheckConnection, sqlTable.ConnectionType) != -1) { Type modelType; fastCSharp.code.cSharp.sqlModel sqlModel = fastCSharp.code.cSharp.dataModel.GetModelType <fastCSharp.code.cSharp.sqlModel>(type, out modelType); checkSqlTable(currentType = type, modelType ?? type, sqlModel, sqlTable); } } } } catch (Exception error) { fastCSharp.log.Error.Add(error, currentType.fullName(), true); } finally { EventWaitHandle wait = null; if (checkAssemblyNames.Set(assemblyName, null, out wait)) { wait.Set(); wait.Close(); } } } }
/// <summary> /// 检测SQL表格 /// </summary> /// <param name="type">表格绑定类型</param> /// <param name="modelType">表格模型类型</param> /// <param name="sqlModel">数据库表格模型配置</param> /// <param name="sqlTable">数据库表格配置</param> private static void checkSqlTable(Type type, Type modelType, fastCSharp.code.cSharp.sqlModel sqlModel, fastCSharp.emit.sqlTable sqlTable) { client sqlClient = GetConnection(sqlTable.ConnectionType).Client; table memberTable = (table)typeof(sqlModel <>).MakeGenericType(modelType).GetMethod("GetTable", BindingFlags.Static | BindingFlags.NonPublic, null, new Type[] { typeof(Type), typeof(fastCSharp.emit.sqlTable) }, null).Invoke(null, new object[] { type, sqlTable }); sqlClient.ToSqlColumn(memberTable); table table = sqlClient.GetTable(memberTable.Columns.Name); if (table == null) { if (!sqlClient.CreateTable(memberTable)) { fastCSharp.log.Error.Add("表格 " + memberTable.Columns.Name + " 创建失败", new System.Diagnostics.StackFrame(), false); } } else { bool ignoreCase = Enum <fastCSharp.sql.type, fastCSharp.sql.typeInfo> .Array((byte)sqlClient.Connection.Type).IgnoreCase; if (sqlModel.DeleteColumnNames != null) { HashSet <string> deleteNames = sqlModel.DeleteColumnNames.Split(',').getHash(value => ignoreCase ? value.toLower() : value); column[] deleteColumns = table.Columns.Columns.getFindArray(value => deleteNames.Contains(ignoreCase ? value.SqlName.ToLower() : value.SqlName)); if (deleteColumns.Length != 0) { table.Columns.Columns = table.Columns.Columns.getFindArray(value => !deleteNames.Contains(ignoreCase ? value.SqlName.ToLower() : value.SqlName)); if (!sqlClient.DeleteFields(new columnCollection { Name = memberTable.Columns.Name, Columns = deleteColumns })) { fastCSharp.log.Error.Add("表格 " + memberTable.Columns.Name + " 字段删除失败 : " + deleteColumns.joinString(',', value => value.SqlName), new System.Diagnostics.StackFrame(), false); } } } string[] names = ignoreCase ? table.Columns.Columns.getArray(value => value.SqlName.ToLower()) : table.Columns.Columns.getArray(value => value.SqlName); using (fastCSharp.stateSearcher.ascii <column> sqlColumnNames = new stateSearcher.ascii <column>(names, table.Columns.Columns, false)) { subArray <column> newColumns; if (ignoreCase) { newColumns = memberTable.Columns.Columns.getFind(value => !sqlColumnNames.ContainsKey(value.SqlName.ToLower())); } else { newColumns = memberTable.Columns.Columns.getFind(value => !sqlColumnNames.ContainsKey(value.SqlName)); } if (newColumns.length != 0) { if (sqlClient.IsAddField && sqlClient.AddFields(new columnCollection { Name = memberTable.Columns.Name, Columns = newColumns.ToArray() })) { table.Columns.Columns = newColumns.Add(table.Columns.Columns).ToArray(); } else { fastCSharp.log.Error.Add("表格 " + memberTable.Columns.Name + " 字段添加失败 : " + newColumns.joinString(',', value => value.SqlName), new System.Diagnostics.StackFrame(), false); } } if (ignoreCase) { newColumns = memberTable.Columns.Columns.getFind(value => !value.IsMatch(sqlColumnNames.Get(value.SqlName.ToLower()), ignoreCase)); } else { newColumns = memberTable.Columns.Columns.getFind(value => !value.IsMatch(sqlColumnNames.Get(value.SqlName), ignoreCase)); } if (newColumns.count() != 0) { fastCSharp.log.Default.Add("表格 " + memberTable.Columns.Name + " 字段类型不匹配 : " + newColumns.joinString(',', value => value.SqlName), new System.Diagnostics.StackFrame(), false); } } } }