Exemple #1
0
    private static bool _CreateTable(string tableName, TableInfo tableInfo, out string errorString)
    {
        // 生成在创建数据表时所有字段的声明
        StringBuilder fieldDefineStringBuilder = new StringBuilder();

        foreach (FieldInfo fieldInfo in GetAllDatabaseFieldInfo(tableInfo))
        {
            // 在这里并不对每种本工具的数据类型是否能导出为指定的MySQL数据类型进行检查(比如本工具中的string型应该导出为MySQL中的文本类型如varchar,而不应该导出为数值类型)
            if (fieldInfo.DataType == DataType.Date)
            {
                string         toDatabaseFormatDefine = fieldInfo.ExtraParam[AppValues.TABLE_INFO_EXTRA_PARAM_KEY_DATE_TO_DATABASE_FORMAT].ToString();
                DateFormatType toDatabaseFormatType   = TableAnalyzeHelper.GetDateFormatType(toDatabaseFormatDefine);
                if (fieldInfo.DatabaseFieldType.StartsWith("time", StringComparison.CurrentCultureIgnoreCase))
                {
                    errorString = string.Format("date型字段\"{0}\"(列号:{1})声明导出到MySQL中的数据类型错误,不允许为time型,如果仅需要时分秒部分,请在Excel中将该字段在本工具中的数据类型改为time型", fieldInfo.FieldName, Utils.GetExcelColumnName(fieldInfo.ColumnSeq + 1));
                    return(false);
                }
                if (toDatabaseFormatType == DateFormatType.ReferenceDateSec || toDatabaseFormatType == DateFormatType.ReferenceDateMsec)
                {
                    if (fieldInfo.DatabaseFieldType.StartsWith("datetime", StringComparison.CurrentCultureIgnoreCase) || fieldInfo.DatabaseFieldType.Equals("date", StringComparison.CurrentCultureIgnoreCase))
                    {
                        errorString = string.Format("date型字段\"{0}\"(列号:{1})声明导出到MySQL中的形式为距1970年的时间({2}),但所填写的导出到MySQL中的格式为时间型的{3},请声明为MySQL中的数值型", fieldInfo.FieldName, Utils.GetExcelColumnName(fieldInfo.ColumnSeq + 1), toDatabaseFormatDefine, fieldInfo.DatabaseFieldType);
                        return(false);
                    }
                }
            }
            else if (fieldInfo.DataType == DataType.Time)
            {
                string         toDatabaseFormatDefine = fieldInfo.ExtraParam[AppValues.TABLE_INFO_EXTRA_PARAM_KEY_TIME_TO_DATABASE_FORMAT].ToString();
                TimeFormatType toDatabaseFormatType   = TableAnalyzeHelper.GetTimeFormatType(toDatabaseFormatDefine);
                if (fieldInfo.DatabaseFieldType.StartsWith("datetime", StringComparison.CurrentCultureIgnoreCase) || fieldInfo.DatabaseFieldType.Equals("date", StringComparison.CurrentCultureIgnoreCase))
                {
                    errorString = string.Format("time型字段\"{0}\"(列号:{1})声明导出到MySQL中的数据类型错误,不允许为datetime或date型,如果需要年月日部分,请在Excel中将该字段在本工具中的数据类型改为date型", fieldInfo.FieldName, Utils.GetExcelColumnName(fieldInfo.ColumnSeq + 1));
                    return(false);
                }
                if (toDatabaseFormatType == TimeFormatType.ReferenceTimeSec && fieldInfo.DatabaseFieldType.StartsWith("time", StringComparison.CurrentCultureIgnoreCase))
                {
                    errorString = string.Format("time型字段\"{0}\"(列号:{1})声明导出到MySQL中的形式为距0点的秒数(#sec),但所填写的导出到MySQL中的格式为时间型的time,请声明为MySQL中的数值型", fieldInfo.FieldName, Utils.GetExcelColumnName(fieldInfo.ColumnSeq + 1));
                    return(false);
                }
            }

            fieldDefineStringBuilder.AppendFormat("`{0}` {1} COMMENT '{2}',", fieldInfo.DatabaseFieldName, fieldInfo.DatabaseFieldType, fieldInfo.Desc);
        }

        string createTableSql = string.Format(_CREATE_TABLE_SQL, _CombineDatabaseTableFullName(tableName), fieldDefineStringBuilder.ToString(), tableInfo.GetKeyColumnFieldInfo().DatabaseFieldName);

        try
        {
            MySqlCommand cmd = new MySqlCommand(createTableSql, _conn);
            cmd.ExecuteNonQuery();
            errorString = null;
            return(true);
        }
        catch (MySqlException exception)
        {
            errorString = exception.Message;
            return(false);
        }
    }
Exemple #2
0
    private static string _GetTimeValue(FieldInfo fieldInfo, int row, int level)
    {
        StringBuilder content = new StringBuilder();

        TimeFormatType timeFormatType = TableAnalyzeHelper.GetTimeFormatType(fieldInfo.ExtraParam[LuaStruct.TimeToExportFormatKey].ToString());

        switch (timeFormatType)
        {
        case TimeFormatType.FormatString:
        {
            if (fieldInfo.Data[row] == null)
            {
                content.Append("nil");
            }
            else
            {
                content.Append("\"").Append(((DateTime)(fieldInfo.Data[row])).ToString(fieldInfo.ExtraParam[LuaStruct.TimeToExportFormatKey].ToString())).Append("\"");
            }

            break;
        }

        case TimeFormatType.ReferenceTimeSec:
        {
            if (fieldInfo.Data[row] == null)
            {
                content.Append("nil");
            }
            else
            {
                content.Append(((DateTime)(fieldInfo.Data[row]) - DateTimeValue.REFERENCE_DATE).TotalSeconds);
            }

            break;
        }

        default:
        {
            AppLog.LogErrorAndExit("错误:用_GetTimeValue函数导出lua文件的time型的TimeFormatType非法");
            break;
        }
        }

        return(content.ToString());
    }
Exemple #3
0
    private static string _GetTimeValue(FieldInfo fieldInfo, int row, int level)
    {
        StringBuilder content = new StringBuilder();

        TimeFormatType timeFormatType = TableAnalyzeHelper.GetTimeFormatType(fieldInfo.ExtraParam[AppValues.TABLE_INFO_EXTRA_PARAM_KEY_TIME_TO_LUA_FORMAT].ToString());

        switch (timeFormatType)
        {
        case TimeFormatType.FormatString:
        {
            if (fieldInfo.Data[row] == null)
            {
                content.Append("nil");
            }
            else
            {
                content.Append("\"").Append(((DateTime)(fieldInfo.Data[row])).ToString(fieldInfo.ExtraParam[AppValues.TABLE_INFO_EXTRA_PARAM_KEY_TIME_TO_LUA_FORMAT].ToString())).Append("\"");
            }

            break;
        }

        case TimeFormatType.ReferenceTimeSec:
        {
            if (fieldInfo.Data[row] == null)
            {
                content.Append("nil");
            }
            else
            {
                content.Append(((DateTime)(fieldInfo.Data[row]) - AppValues.REFERENCE_DATE).TotalSeconds);
            }

            break;
        }

        default:
        {
            Utils.LogErrorAndExit("错误:用_GetTimeValue函数导出lua文件的time型的TimeFormatType非法");
            break;
        }
        }

        return(content.ToString());
    }
    /// <summary>
    /// 检查time型的格式定义
    /// </summary>
    public static bool CheckTimeDefine(string defineString, out string errorString)
    {
        defineString = defineString.Trim();
        if (string.IsNullOrEmpty(defineString))
        {
            errorString = "未进行格式声明";
            return(false);
        }
        TimeFormatType formatType = TableAnalyzeHelper.GetTimeFormatType(defineString);

        if (formatType == TimeFormatType.FormatString)
        {
            // 检查time型的格式字符串声明,不允许出现代表年月日的y、M、d
            List <string> errorInfo = new List <string>();
            if (defineString.IndexOf('y') != -1)
            {
                errorInfo.Add("代表年的y");
            }
            if (defineString.IndexOf('M') != -1)
            {
                errorInfo.Add("代表月的M");
            }
            if (defineString.IndexOf('d') != -1)
            {
                errorInfo.Add("代表日的d");
            }

            if (errorInfo.Count > 0)
            {
                errorString = string.Format("time类型的格式定义中不允许出现以下与年月日相关的日期型格式定义字符:{0}", Utils.CombineString(errorInfo, ","));
                return(false);
            }
        }

        errorString = null;
        return(true);
    }
    private static bool _InsertData(string tableName, TableInfo tableInfo, out string errorString)
    {
        List <FieldInfo> allDatabaseFieldInfo = GetAllDatabaseFieldInfo(tableInfo);

        // 生成所有字段名对应的定义字符串
        List <string> fileNames = new List <string>();

        foreach (FieldInfo fieldInfo in allDatabaseFieldInfo)
        {
            fileNames.Add(string.Format("`{0}`", fieldInfo.DatabaseFieldName));
        }

        string fieldNameDefineString = Utils.CombineString(fileNames, ", ");

        // 用户是否配置该表中string型字段中的空单元格导出至MySQL中为NULL,默认为空字符串
        bool isWriteNullForEmptyString = tableInfo.TableConfig != null && tableInfo.TableConfig.ContainsKey(AppValues.CONFIG_NAME_EXPORT_DATABASE_WRITE_NULL_FOR_EMPTY_STRING) && tableInfo.TableConfig[AppValues.CONFIG_NAME_EXPORT_DATABASE_WRITE_NULL_FOR_EMPTY_STRING].Count > 0 && "true".Equals(tableInfo.TableConfig[AppValues.CONFIG_NAME_EXPORT_DATABASE_WRITE_NULL_FOR_EMPTY_STRING][0], StringComparison.CurrentCultureIgnoreCase);

        // 逐行生成插入数据的SQL语句中的value定义部分
        StringBuilder valueDefineStringBuilder = new StringBuilder();
        int           count = tableInfo.GetKeyColumnFieldInfo().Data.Count;

        if (count > 0)
        {
            for (int i = 0; i < count; ++i)
            {
                List <string> values = new List <string>();
                foreach (FieldInfo fieldInfo in allDatabaseFieldInfo)
                {
                    if (fieldInfo.Data[i] == null)
                    {
                        values.Add("NULL");
                    }
                    else if (fieldInfo.DataType == DataType.Date)
                    {
                        string         toDatabaseFormatDefine = fieldInfo.ExtraParam[AppValues.TABLE_INFO_EXTRA_PARAM_KEY_DATE_TO_DATABASE_FORMAT].ToString();
                        DateFormatType toDatabaseFormatType   = TableAnalyzeHelper.GetDateFormatType(toDatabaseFormatDefine);
                        if (toDatabaseFormatType == DateFormatType.FormatString)
                        {
                            // 注意MySQL中的时间型,datetime和time型后面可用括号进行具体设置,date型没有
                            // MySQL中的date型插入数据时不允许含有时分秒,否则会报错,故这里强制采用MySQL默认的yyyy-MM-dd格式插入
                            if (fieldInfo.DatabaseFieldType.Equals("date", StringComparison.CurrentCultureIgnoreCase))
                            {
                                values.Add(string.Format("'{0}'", ((DateTime)(fieldInfo.Data[i])).ToString(AppValues.APP_DEFAULT_ONLY_DATE_FORMAT)));
                            }
                            else if (fieldInfo.DatabaseFieldType.StartsWith("datetime", StringComparison.CurrentCultureIgnoreCase))
                            {
                                values.Add(string.Format("'{0}'", ((DateTime)(fieldInfo.Data[i])).ToString(AppValues.APP_DEFAULT_DATE_FORMAT)));
                            }
                            // date型导出到MySQL中的其他数据类型字段如varchar,采用声明的指定格式
                            else
                            {
                                values.Add(string.Format("'{0}'", ((DateTime)(fieldInfo.Data[i])).ToString(fieldInfo.ExtraParam[AppValues.TABLE_INFO_EXTRA_PARAM_KEY_DATE_TO_DATABASE_FORMAT].ToString())));
                            }
                        }
                        else if (toDatabaseFormatType == DateFormatType.ReferenceDateSec)
                        {
                            values.Add(string.Format("'{0}'", ((DateTime)fieldInfo.Data[i] - AppValues.REFERENCE_DATE_LOCAL).TotalSeconds));
                        }
                        else if (toDatabaseFormatType == DateFormatType.ReferenceDateMsec)
                        {
                            values.Add(string.Format("'{0}'", ((DateTime)fieldInfo.Data[i] - AppValues.REFERENCE_DATE_LOCAL).TotalMilliseconds));
                        }
                        else
                        {
                            errorString = "date型导出至MySQL的格式定义非法";
                            Utils.LogErrorAndExit(errorString);
                            return(false);
                        }
                    }
                    else if (fieldInfo.DataType == DataType.Time)
                    {
                        string         toDatabaseFormatDefine = fieldInfo.ExtraParam[AppValues.TABLE_INFO_EXTRA_PARAM_KEY_TIME_TO_DATABASE_FORMAT].ToString();
                        TimeFormatType toDatabaseFormatType   = TableAnalyzeHelper.GetTimeFormatType(toDatabaseFormatDefine);
                        if (toDatabaseFormatType == TimeFormatType.FormatString)
                        {
                            if (fieldInfo.DatabaseFieldType.StartsWith("time", StringComparison.CurrentCultureIgnoreCase))
                            {
                                values.Add(string.Format("'{0}'", ((DateTime)(fieldInfo.Data[i])).ToString(AppValues.APP_DEFAULT_TIME_FORMAT)));
                            }
                            else
                            {
                                values.Add(string.Format("'{0}'", ((DateTime)(fieldInfo.Data[i])).ToString(fieldInfo.ExtraParam[AppValues.TABLE_INFO_EXTRA_PARAM_KEY_TIME_TO_DATABASE_FORMAT].ToString())));
                            }
                        }
                        else if (toDatabaseFormatType == TimeFormatType.ReferenceTimeSec)
                        {
                            values.Add(string.Format("'{0}'", ((DateTime)fieldInfo.Data[i] - AppValues.REFERENCE_DATE).TotalSeconds));
                        }
                        else
                        {
                            errorString = "time型导出至MySQL的格式定义非法";
                            Utils.LogErrorAndExit(errorString);
                            return(false);
                        }
                    }
                    else if (fieldInfo.DataType == DataType.Bool)
                    {
                        bool inputData = (bool)fieldInfo.Data[i];
                        // 如果数据库用bit数据类型表示bool型,比如要写入true,SQL语句中的1不能加单引号
                        if (fieldInfo.DatabaseFieldType.Equals("bit", StringComparison.CurrentCultureIgnoreCase) || fieldInfo.DatabaseFieldType.StartsWith("bit(", StringComparison.CurrentCultureIgnoreCase))
                        {
                            if (inputData == true)
                            {
                                values.Add("1");
                            }
                            else
                            {
                                values.Add("0");
                            }
                        }
                        else
                        {
                            // 如果数据库用tinyint(1)数据类型表示bool型,比如要写入true,SQL语句中可以写为'1'或者不带单引号的true
                            if (inputData == true)
                            {
                                values.Add("'1'");
                            }
                            else
                            {
                                values.Add("'0'");
                            }
                        }
                    }
                    else if (fieldInfo.DataType == DataType.Json)
                    {
                        // json型直接向数据库写入原始json字符串,但需要对\进行转义
                        values.Add(string.Format("'{0}'", fieldInfo.JsonString[i]).Replace("\\", "\\\\"));
                    }
                    else if (fieldInfo.DataType == DataType.MapString)
                    {
                        // mapString型也直接写入原始mapString数据字符串,并对\进行转义
                        values.Add(string.Format("'{0}'", fieldInfo.JsonString[i]).Replace("\\", "\\\\"));
                    }
                    // 这里需要自行处理数据库中某些数据类型(如datetime)中不允许插入空字符串的情况,以及用户设置的string型中空单元格导出至数据库的形式
                    else if (string.IsNullOrEmpty(fieldInfo.Data[i].ToString()))
                    {
                        if (fieldInfo.DatabaseFieldType.StartsWith("datetime", StringComparison.CurrentCultureIgnoreCase))
                        {
                            values.Add("NULL");
                        }
                        else if (fieldInfo.DataType == DataType.String && isWriteNullForEmptyString == true)
                        {
                            values.Add("NULL");
                        }
                        else
                        {
                            values.Add(string.Format("'{0}'", fieldInfo.Data[i].ToString()));
                        }
                    }
                    else
                    {
                        // 注意对\进行转义
                        values.Add(string.Format("'{0}'", fieldInfo.Data[i].ToString()).Replace("\\", "\\\\"));
                    }
                }

                valueDefineStringBuilder.AppendFormat("({0}),", Utils.CombineString(values, ","));
            }
            // 去掉末尾多余的逗号
            string valueDefineString = valueDefineStringBuilder.ToString();
            valueDefineString = valueDefineString.Substring(0, valueDefineString.Length - 1);

            string insertSqlString = string.Format(_INSERT_DATA_SQL, _CombineDatabaseTableFullName(tableName), fieldNameDefineString, valueDefineString);

            // 执行插入操作
            try
            {
                MySqlCommand cmd         = new MySqlCommand(insertSqlString, _conn);
                int          insertCount = cmd.ExecuteNonQuery();
                if (insertCount < count)
                {
                    errorString = string.Format("需要插入{0}条数据但仅插入了{1}条", count, insertCount);
                    return(false);
                }
                else
                {
                    errorString = null;
                    return(true);
                }
            }
            catch (MySqlException exception)
            {
                errorString = exception.Message;
                return(false);
            }
        }
        else
        {
            errorString = null;
            return(true);
        }
    }
Exemple #6
0
    private static bool _InsertData(string tableName, TableInfo tableInfo, out string errorString)
    {
        List <FieldInfo> allDatabaseFieldInfo = GetAllDatabaseFieldInfo(tableInfo);

        // 生成所有字段名对应的定义字符串
        List <string> fileNames = new List <string>();

        foreach (FieldInfo fieldInfo in allDatabaseFieldInfo)
        {
            fileNames.Add(string.Format("`{0}`", fieldInfo.DatabaseFieldName));
        }

        string fieldNameDefineString = Utils.CombineString(fileNames, ", ");

        // 逐行生成插入数据的SQL语句中的value定义部分
        StringBuilder valueDefineStringBuilder = new StringBuilder();
        int           count = tableInfo.GetKeyColumnFieldInfo().Data.Count;

        for (int i = 0; i < count; ++i)
        {
            List <string> values = new List <string>();
            foreach (FieldInfo fieldInfo in allDatabaseFieldInfo)
            {
                if (fieldInfo.Data[i] == null)
                {
                    values.Add("NULL");
                }
                else if (fieldInfo.DataType == DataType.Date)
                {
                    string         toDatabaseFormatDefine = fieldInfo.ExtraParam[AppValues.TABLE_INFO_EXTRA_PARAM_KEY_DATE_TO_DATABASE_FORMAT].ToString();
                    DateFormatType toDatabaseFormatType   = TableAnalyzeHelper.GetDateFormatType(toDatabaseFormatDefine);
                    if (toDatabaseFormatType == DateFormatType.FormatString)
                    {
                        // 注意MySQL中的时间型,datetime和time型后面可用括号进行具体设置,date型没有
                        // MySQL中的date型插入数据时不允许含有时分秒,否则会报错,故这里强制采用MySQL默认的yyyy-MM-dd格式插入
                        if (fieldInfo.DatabaseFieldType.Equals("date", StringComparison.CurrentCultureIgnoreCase))
                        {
                            values.Add(string.Format("'{0}'", ((DateTime)(fieldInfo.Data[i])).ToString(AppValues.APP_DEFAULT_ONLY_DATE_FORMAT)));
                        }
                        else if (fieldInfo.DatabaseFieldType.StartsWith("datetime", StringComparison.CurrentCultureIgnoreCase))
                        {
                            values.Add(string.Format("'{0}'", ((DateTime)(fieldInfo.Data[i])).ToString(AppValues.APP_DEFAULT_DATE_FORMAT)));
                        }
                        // date型导出到MySQL中的其他数据类型字段如varchar,采用声明的指定格式
                        else
                        {
                            values.Add(string.Format("'{0}'", ((DateTime)(fieldInfo.Data[i])).ToString(fieldInfo.ExtraParam[AppValues.TABLE_INFO_EXTRA_PARAM_KEY_DATE_TO_DATABASE_FORMAT].ToString())));
                        }
                    }
                    else if (toDatabaseFormatType == DateFormatType.ReferenceDateSec)
                    {
                        values.Add(string.Format("'{0}'", ((DateTime)fieldInfo.Data[i] - AppValues.REFERENCE_DATE).TotalSeconds));
                    }
                    else if (toDatabaseFormatType == DateFormatType.ReferenceDateMsec)
                    {
                        values.Add(string.Format("'{0}'", ((DateTime)fieldInfo.Data[i] - AppValues.REFERENCE_DATE).TotalMilliseconds));
                    }
                    else
                    {
                        errorString = "date型导出至MySQL的格式定义非法";
                        Utils.LogErrorAndExit(errorString);
                        return(false);
                    }
                }
                else if (fieldInfo.DataType == DataType.Time)
                {
                    string         toDatabaseFormatDefine = fieldInfo.ExtraParam[AppValues.TABLE_INFO_EXTRA_PARAM_KEY_TIME_TO_DATABASE_FORMAT].ToString();
                    TimeFormatType toDatabaseFormatType   = TableAnalyzeHelper.GetTimeFormatType(toDatabaseFormatDefine);
                    if (toDatabaseFormatType == TimeFormatType.FormatString)
                    {
                        if (fieldInfo.DatabaseFieldType.StartsWith("time", StringComparison.CurrentCultureIgnoreCase))
                        {
                            values.Add(string.Format("'{0}'", ((DateTime)(fieldInfo.Data[i])).ToString(AppValues.APP_DEFAULT_TIME_FORMAT)));
                        }
                        else
                        {
                            values.Add(string.Format("'{0}'", ((DateTime)(fieldInfo.Data[i])).ToString(fieldInfo.ExtraParam[AppValues.TABLE_INFO_EXTRA_PARAM_KEY_TIME_TO_DATABASE_FORMAT].ToString())));
                        }
                    }
                    else if (toDatabaseFormatType == TimeFormatType.ReferenceTimeSec)
                    {
                        values.Add(string.Format("'{0}'", ((DateTime)fieldInfo.Data[i] - AppValues.REFERENCE_DATE).TotalSeconds));
                    }
                    else
                    {
                        errorString = "time型导出至MySQL的格式定义非法";
                        Utils.LogErrorAndExit(errorString);
                        return(false);
                    }
                }
                // 这里需要自行处理向数据库中某些数据类型如datetime的列不允许插入空字符串的情况
                else if (string.IsNullOrEmpty(fieldInfo.Data[i].ToString()))
                {
                    if (fieldInfo.DatabaseFieldType.StartsWith("datetime", StringComparison.CurrentCultureIgnoreCase))
                    {
                        values.Add("NULL");
                    }
                    else
                    {
                        values.Add(string.Format("'{0}'", fieldInfo.Data[i].ToString()));
                    }
                }
                else if (fieldInfo.DataType == DataType.Bool)
                {
                    string inputData = fieldInfo.Data[i].ToString();
                    // 如果数据库用tinyint(1)数据类型表示bool型,比如要写入true,SQL语句中可以写为'1'或者不带单引号的true
                    if ("true".Equals(inputData, StringComparison.CurrentCultureIgnoreCase))
                    {
                        values.Add("'1'");
                    }
                    else if ("false".Equals(inputData, StringComparison.CurrentCultureIgnoreCase))
                    {
                        values.Add("'0'");
                    }
                    else
                    {
                        values.Add(string.Format("'{0}'", fieldInfo.Data[i].ToString()));
                    }
                }
                else
                {
                    values.Add(string.Format("'{0}'", fieldInfo.Data[i].ToString()));
                }
            }
            valueDefineStringBuilder.AppendFormat("({0}),", Utils.CombineString(values, ","));
        }
        // 去掉末尾多余的逗号
        string valueDefineString = valueDefineStringBuilder.ToString();

        valueDefineString = valueDefineString.Substring(0, valueDefineString.Length - 1);

        string insertSqlString = string.Format(_INSERT_DATA_SQL, _CombineDatabaseTableFullName(tableName), fieldNameDefineString, valueDefineString);

        // 执行插入操作
        try
        {
            MySqlCommand cmd         = new MySqlCommand(insertSqlString, _conn);
            int          insertCount = cmd.ExecuteNonQuery();
            if (insertCount < count)
            {
                errorString = string.Format("需要插入{0}条数据但仅插入了{1}条");
                return(false);
            }
            else
            {
                errorString = null;
                return(true);
            }
        }
        catch (MySqlException exception)
        {
            errorString = exception.Message;
            return(false);
        }
    }
Exemple #7
0
    private static void _GetOneFieldCsvContent(FieldInfo fieldInfo, List <StringBuilder> rowContentList)
    {
        int rowCount = fieldInfo.Data.Count;

        switch (fieldInfo.DataType)
        {
        case DataType.Int:
        case DataType.Long:
        case DataType.Float:
        case DataType.String:
        case DataType.Lang:
        case DataType.TableString:
        {
            for (int row = 0; row < rowCount; ++row)
            {
                StringBuilder stringBuilder = rowContentList[row];
                // 先增加与上一字段间的分隔符
                stringBuilder.Append(AppValues.ExportCsvSplitString);
                // 再生成本行对应的内容
                if (fieldInfo.Data[row] != null)
                {
                    stringBuilder.Append(fieldInfo.Data[row].ToString());
                }
            }
            break;
        }

        case DataType.Bool:
        {
            for (int row = 0; row < rowCount; ++row)
            {
                StringBuilder stringBuilder = rowContentList[row];
                stringBuilder.Append(AppValues.ExportCsvSplitString);
                if (fieldInfo.Data[row] != null)
                {
                    if ((bool)fieldInfo.Data[row] == true)
                    {
                        stringBuilder.Append("true");
                    }
                    else
                    {
                        stringBuilder.Append("false");
                    }
                }
            }
            break;
        }

        case DataType.Json:
        {
            for (int row = 0; row < rowCount; ++row)
            {
                StringBuilder stringBuilder = rowContentList[row];
                stringBuilder.Append(AppValues.ExportCsvSplitString);
                if (fieldInfo.Data[row] != null)
                {
                    stringBuilder.Append(fieldInfo.JsonString[row]);
                }
            }
            break;
        }

        case DataType.MapString:
        {
            for (int row = 0; row < rowCount; ++row)
            {
                StringBuilder stringBuilder = rowContentList[row];
                stringBuilder.Append(AppValues.ExportCsvSplitString);
                if (fieldInfo.Data[row] != null)
                {
                    stringBuilder.Append(fieldInfo.JsonString[row]);
                }
            }
            break;
        }

        case DataType.Date:
        {
            DateFormatType dateFormatType     = TableAnalyzeHelper.GetDateFormatType(fieldInfo.ExtraParam[AppValues.TABLE_INFO_EXTRA_PARAM_KEY_DATE_TO_LUA_FORMAT].ToString());
            string         exportFormatString = null;
            // 若date型声明toLua的格式为dateTable,则按input格式进行导出
            if (dateFormatType == DateFormatType.DataTable)
            {
                dateFormatType     = TableAnalyzeHelper.GetDateFormatType(fieldInfo.ExtraParam[AppValues.TABLE_INFO_EXTRA_PARAM_KEY_DATE_INPUT_FORMAT].ToString());
                exportFormatString = fieldInfo.ExtraParam[AppValues.TABLE_INFO_EXTRA_PARAM_KEY_DATE_INPUT_FORMAT].ToString();
            }
            else
            {
                exportFormatString = fieldInfo.ExtraParam[AppValues.TABLE_INFO_EXTRA_PARAM_KEY_DATE_TO_LUA_FORMAT].ToString();
            }

            switch (dateFormatType)
            {
            case DateFormatType.FormatString:
            {
                for (int row = 0; row < rowCount; ++row)
                {
                    StringBuilder stringBuilder = rowContentList[row];
                    stringBuilder.Append(AppValues.ExportCsvSplitString);
                    if (fieldInfo.Data[row] != null)
                    {
                        stringBuilder.Append(((DateTime)(fieldInfo.Data[row])).ToString(exportFormatString));
                    }
                }
                break;
            }

            case DateFormatType.ReferenceDateSec:
            {
                for (int row = 0; row < rowCount; ++row)
                {
                    StringBuilder stringBuilder = rowContentList[row];
                    stringBuilder.Append(AppValues.ExportCsvSplitString);
                    if (fieldInfo.Data[row] != null)
                    {
                        stringBuilder.Append(((DateTime)(fieldInfo.Data[row]) - AppValues.REFERENCE_DATE_LOCAL).TotalSeconds);
                    }
                }
                break;
            }

            case DateFormatType.ReferenceDateMsec:
            {
                for (int row = 0; row < rowCount; ++row)
                {
                    StringBuilder stringBuilder = rowContentList[row];
                    stringBuilder.Append(AppValues.ExportCsvSplitString);
                    if (fieldInfo.Data[row] != null)
                    {
                        stringBuilder.Append(((DateTime)(fieldInfo.Data[row]) - AppValues.REFERENCE_DATE_LOCAL).TotalMilliseconds);
                    }
                }
                break;
            }

            default:
            {
                Utils.LogErrorAndExit("用_GetOneFieldCsvContent函数导出csv文件的date型的DateFormatType非法");
                break;
            }
            }
            break;
        }

        case DataType.Time:
        {
            TimeFormatType timeFormatType = TableAnalyzeHelper.GetTimeFormatType(fieldInfo.ExtraParam[AppValues.TABLE_INFO_EXTRA_PARAM_KEY_TIME_TO_LUA_FORMAT].ToString());
            switch (timeFormatType)
            {
            case TimeFormatType.FormatString:
            {
                for (int row = 0; row < rowCount; ++row)
                {
                    StringBuilder stringBuilder = rowContentList[row];
                    stringBuilder.Append(AppValues.ExportCsvSplitString);
                    if (fieldInfo.Data[row] != null)
                    {
                        stringBuilder.Append(((DateTime)(fieldInfo.Data[row])).ToString(fieldInfo.ExtraParam[AppValues.TABLE_INFO_EXTRA_PARAM_KEY_TIME_TO_LUA_FORMAT].ToString()));
                    }
                }
                break;
            }

            case TimeFormatType.ReferenceTimeSec:
            {
                for (int row = 0; row < rowCount; ++row)
                {
                    StringBuilder stringBuilder = rowContentList[row];
                    stringBuilder.Append(AppValues.ExportCsvSplitString);
                    if (fieldInfo.Data[row] != null)
                    {
                        stringBuilder.Append(((DateTime)(fieldInfo.Data[row]) - AppValues.REFERENCE_DATE).TotalSeconds);
                    }
                }
                break;
            }

            default:
            {
                Utils.LogErrorAndExit("错误:用_GetOneFieldCsvContent函数导出csv文件的time型的TimeFormatType非法");
                break;
            }
            }
            break;
        }

        case DataType.Array:
        case DataType.Dict:
        {
            for (int row = 0; row < rowCount; ++row)
            {
                StringBuilder stringBuilder = rowContentList[row];
                stringBuilder.Append(AppValues.ExportCsvSplitString);
                if ((bool)fieldInfo.Data[row] == false)
                {
                    stringBuilder.Append("-1");
                }
            }
            break;
        }

        default:
        {
            Utils.LogErrorAndExit(string.Format("_GetOneFieldCsvContent函数中未定义{0}类型数据导出至csv文件的形式", fieldInfo.DataType));
            break;
        }
        }
    }
Exemple #8
0
    private static string _GetCsClassFieldDefine(FieldInfo fieldInfo)
    {
        StringBuilder stringBuilder = new StringBuilder();

        switch (fieldInfo.DataType)
        {
        case DataType.Int:
        case DataType.Long:
        case DataType.Float:
        case DataType.Bool:
        case DataType.String:
        {
            stringBuilder.Append(fieldInfo.DataType.ToString().ToLower());
            break;
        }

        case DataType.Lang:
        {
            stringBuilder.Append("string");
            break;
        }

        case DataType.Json:
        {
            stringBuilder.Append("LitJson.JsonData");
            break;
        }

        case DataType.Array:
        {
            stringBuilder.Append("List<");
            stringBuilder.Append(_GetCsClassFieldDefine(fieldInfo.ChildField[0]));
            stringBuilder.Append(">");
            break;
        }

        case DataType.Dict:
        {
            // 如果dict型下属各字段都是同一种类型,则Dictionary的值类型可以具体指定,否则只能设为通用的Object
            bool     isSameChildDataType = true;
            DataType firstDataType       = fieldInfo.ChildField[0].DataType;
            foreach (FieldInfo childFieldInfo in fieldInfo.ChildField)
            {
                if (childFieldInfo.DataType != firstDataType)
                {
                    isSameChildDataType = false;
                    break;
                }
            }
            if (isSameChildDataType == true)
            {
                stringBuilder.Append("Dictionary<string, ");
                stringBuilder.Append(_GetCsClassFieldDefine(fieldInfo.ChildField[0]));
                stringBuilder.Append(">");
            }
            else
            {
                stringBuilder.Append("Dictionary<string, object>");
            }

            break;
        }

        case DataType.Date:
        {
            DateFormatType dateFormatType = TableAnalyzeHelper.GetDateFormatType(fieldInfo.ExtraParam[AppValues.TABLE_INFO_EXTRA_PARAM_KEY_DATE_TO_LUA_FORMAT].ToString());
            if (dateFormatType == DateFormatType.ReferenceDateSec)
            {
                stringBuilder.Append("int");
            }
            else if (dateFormatType == DateFormatType.ReferenceDateMsec)
            {
                stringBuilder.Append("long");
            }
            else
            {
                stringBuilder.Append("DateTime");
            }

            break;
        }

        case DataType.Time:
        {
            TimeFormatType timeFormatType = TableAnalyzeHelper.GetTimeFormatType(fieldInfo.ExtraParam[AppValues.TABLE_INFO_EXTRA_PARAM_KEY_TIME_TO_LUA_FORMAT].ToString());
            if (timeFormatType == TimeFormatType.ReferenceTimeSec)
            {
                stringBuilder.Append("int");
            }
            else
            {
                stringBuilder.Append("DateTime");
            }

            break;
        }

        case DataType.TableString:
        {
            if (fieldInfo.TableStringFormatDefine.KeyDefine.KeyType == TableStringKeyType.Seq)
            {
                stringBuilder.Append("List<");
                switch (fieldInfo.TableStringFormatDefine.ValueDefine.ValueType)
                {
                case TableStringValueType.True:
                {
                    stringBuilder.Append("bool");
                    break;
                }

                case TableStringValueType.Table:
                {
                    stringBuilder.Append("Dictionary<string, object>");
                    break;
                }

                case TableStringValueType.DataInIndex:
                {
                    string valueDataTypeString = _GetCsClassTableStringDataType(fieldInfo.TableStringFormatDefine.ValueDefine.DataInIndexDefine.DataType);
                    if (valueDataTypeString == null)
                    {
                        Utils.LogErrorAndExit("用_GetCsClassFieldDefine函数导出csv对应C#类文件中tableString型字段的seq型key的dataInIndex型value的数据类型非法");
                    }
                    else
                    {
                        stringBuilder.Append(valueDataTypeString);
                    }

                    break;
                }

                default:
                {
                    Utils.LogErrorAndExit("用_GetCsClassFieldDefine函数导出csv对应C#类文件中tableString型字段的seq型key的value类型非法");
                    break;
                }
                }

                stringBuilder.Append(">");
            }
            else if (fieldInfo.TableStringFormatDefine.KeyDefine.KeyType == TableStringKeyType.DataInIndex)
            {
                stringBuilder.Append("Dictionary<");
                // key
                string keyDataTypeString = _GetCsClassTableStringDataType(fieldInfo.TableStringFormatDefine.KeyDefine.DataInIndexDefine.DataType);
                if (keyDataTypeString == null)
                {
                    Utils.LogErrorAndExit("用_GetCsClassFieldDefine函数导出csv对应C#类文件中tableString型字段的dataInIndex型key的数据类型非法");
                }
                else
                {
                    stringBuilder.Append(keyDataTypeString);
                }

                stringBuilder.Append(" ,");
                // value
                switch (fieldInfo.TableStringFormatDefine.ValueDefine.ValueType)
                {
                case TableStringValueType.True:
                {
                    stringBuilder.Append("bool");
                    break;
                }

                case TableStringValueType.Table:
                {
                    stringBuilder.Append("Dictionary<string, object>");
                    break;
                }

                case TableStringValueType.DataInIndex:
                {
                    string valueDataTypeString = _GetCsClassTableStringDataType(fieldInfo.TableStringFormatDefine.KeyDefine.DataInIndexDefine.DataType);
                    if (valueDataTypeString == null)
                    {
                        Utils.LogErrorAndExit("用_GetCsClassFieldDefine函数导出csv对应C#类文件中tableString型字段的dataInIndex型key的dataInIndex型value的数据类型非法");
                    }
                    else
                    {
                        stringBuilder.Append(valueDataTypeString);
                    }

                    break;
                }

                default:
                {
                    Utils.LogErrorAndExit("用_GetCsClassFieldDefine函数导出csv对应C#类文件中tableString型字段的dataInIndex型key的value类型非法");
                    break;
                }
                }

                stringBuilder.Append(">");
            }
            else
            {
                Utils.LogErrorAndExit("用_GetCsClassFieldDefine函数导出csv对应C#类文件中tableString型字段的key非法");
            }

            break;
        }

        case DataType.MapString:
        {
            stringBuilder.Append("LitJson.JsonData");
            break;
        }

        default:
        {
            Utils.LogErrorAndExit("用_GetCsClassFieldDefine函数导出csv对应C#类文件的字段DataType非法");
            break;
        }
        }

        return(stringBuilder.ToString());
    }
    private static void _GetOneFieldTxtContent(FieldInfo fieldInfo, List <StringBuilder> rowContentList)
    {
        int rowCount = fieldInfo.Data.Count;

        switch (fieldInfo.DataType)
        {
        case DataType.Int:
        case DataType.Long:
        case DataType.Float:
        case DataType.String:
        {
            for (int row = 0; row < rowCount; ++row)
            {
                StringBuilder stringBuilder = rowContentList[row];
                // 先增加与上一字段间的分隔符
                stringBuilder.Append(TxtStruct.ExportSpaceString);
                // 再生成本行对应的内容
                if (fieldInfo.Data[row] != null)
                {
                    stringBuilder.Append(fieldInfo.Data[row].ToString().Replace("\n", "\\n"));
                }
            }
            break;
        }

        case DataType.Lang:
        case DataType.TableString:
        {
            for (int row = 0; row < rowCount; ++row)
            {
                StringBuilder stringBuilder = rowContentList[row];
                // 先增加与上一字段间的分隔符
                stringBuilder.Append(TxtStruct.ExportSpaceString);
                // 再生成本行对应的内容
                if (fieldInfo.Data[row] != null)
                {
                    stringBuilder.Append(fieldInfo.Data[row].ToString().Replace("\n", "\\n"));
                }
            }
            break;
        }

        case DataType.Bool:
        {
            for (int row = 0; row < rowCount; ++row)
            {
                StringBuilder stringBuilder = rowContentList[row];
                stringBuilder.Append(TxtStruct.ExportSpaceString);
                if (fieldInfo.Data[row] != null)
                {
                    if ((bool)fieldInfo.Data[row] == true)
                    {
                        stringBuilder.Append("true");
                    }
                    else
                    {
                        stringBuilder.Append("false");
                    }
                }
            }
            break;
        }

        case DataType.Json:
        {
            for (int row = 0; row < rowCount; ++row)
            {
                StringBuilder stringBuilder = rowContentList[row];
                stringBuilder.Append(TxtStruct.ExportSpaceString);
                if (fieldInfo.Data[row] != null)
                {
                    stringBuilder.Append(fieldInfo.JsonString[row]);
                }
            }
            break;
        }

        case DataType.Date:
        {
            DateFormatType dateFormatType     = TableAnalyzeHelper.GetDateFormatType(fieldInfo.ExtraParam[DateTimeValue.DateInputFormat].ToString());
            string         exportFormatString = null;
            // 若date型声明toLua的格式为dateTable,则按input格式进行导出
            if (dateFormatType == DateFormatType.DataTable)
            {
                dateFormatType     = TableAnalyzeHelper.GetDateFormatType(fieldInfo.ExtraParam[DateTimeValue.DateInputFormat].ToString());
                exportFormatString = fieldInfo.ExtraParam[DateTimeValue.DateInputFormat].ToString();
            }
            else
            {
                exportFormatString = fieldInfo.ExtraParam[DateTimeValue.DateInputFormat].ToString();
            }

            switch (dateFormatType)
            {
            case DateFormatType.FormatString:
            {
                for (int row = 0; row < rowCount; ++row)
                {
                    StringBuilder stringBuilder = rowContentList[row];
                    stringBuilder.Append(TxtStruct.ExportSpaceString);
                    if (fieldInfo.Data[row] != null)
                    {
                        stringBuilder.Append(((DateTime)(fieldInfo.Data[row])).ToString(exportFormatString));
                    }
                }
                break;
            }

            case DateFormatType.ReferenceDateSec:
            {
                for (int row = 0; row < rowCount; ++row)
                {
                    StringBuilder stringBuilder = rowContentList[row];
                    stringBuilder.Append(TxtStruct.ExportSpaceString);
                    if (fieldInfo.Data[row] != null)
                    {
                        stringBuilder.Append(((DateTime)(fieldInfo.Data[row]) - DateTimeValue.REFERENCE_DATE).TotalSeconds);
                    }
                }
                break;
            }

            case DateFormatType.ReferenceDateMsec:
            {
                for (int row = 0; row < rowCount; ++row)
                {
                    StringBuilder stringBuilder = rowContentList[row];
                    stringBuilder.Append(TxtStruct.ExportSpaceString);
                    if (fieldInfo.Data[row] != null)
                    {
                        stringBuilder.Append(((DateTime)(fieldInfo.Data[row]) - DateTimeValue.REFERENCE_DATE).TotalMilliseconds);
                    }
                }
                break;
            }

            default:
            {
                AppLog.LogErrorAndExit("用_GetOneFieldTxtContent函数导出txt文件的date型的DateFormatType非法");
                break;
            }
            }
            break;
        }

        case DataType.Time:
        {
            TimeFormatType timeFormatType = TableAnalyzeHelper.GetTimeFormatType(fieldInfo.ExtraParam[DateTimeValue.TimeInputFormat].ToString());
            switch (timeFormatType)
            {
            case TimeFormatType.FormatString:
            {
                for (int row = 0; row < rowCount; ++row)
                {
                    StringBuilder stringBuilder = rowContentList[row];
                    stringBuilder.Append(TxtStruct.ExportSpaceString);
                    if (fieldInfo.Data[row] != null)
                    {
                        stringBuilder.Append(((DateTime)(fieldInfo.Data[row])).ToString(fieldInfo.ExtraParam[DateTimeValue.TimeInputFormat].ToString()));
                    }
                }
                break;
            }

            case TimeFormatType.ReferenceTimeSec:
            {
                for (int row = 0; row < rowCount; ++row)
                {
                    StringBuilder stringBuilder = rowContentList[row];
                    stringBuilder.Append(TxtStruct.ExportSpaceString);
                    if (fieldInfo.Data[row] != null)
                    {
                        stringBuilder.Append(((DateTime)(fieldInfo.Data[row]) - DateTimeValue.REFERENCE_DATE).TotalSeconds);
                    }
                }
                break;
            }

            default:
            {
                AppLog.LogErrorAndExit("错误:用_GetOneFieldTxtContent函数导出txt文件的time型的TimeFormatType非法");
                break;
            }
            }
            break;
        }

        case DataType.Array:
        case DataType.Dict:
        {
            for (int row = 0; row < rowCount; ++row)
            {
                StringBuilder stringBuilder = rowContentList[row];
                stringBuilder.Append(TxtStruct.ExportSpaceString);
                if ((bool)fieldInfo.Data[row] == false)
                {
                    stringBuilder.Append("-1");
                }
            }
            break;
        }

        default:
        {
            AppLog.LogErrorAndExit(string.Format("_GetOneFieldTxtContent函数中未定义{0}类型数据导出至txt文件的形式", fieldInfo.DataType));
            break;
        }
        }
    }