Beispiel #1
0
        /// <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);
                    }
                }
            }
        }