Exemplo n.º 1
0
        /// <summary>
        /// 为给定的候选名称生成有效的参数名。
        /// </summary>
        /// <param name="name">参数的候选名称</param>
        /// <returns> 基于候选名称的有效名称</returns>
        public override string GenerateParameterName(string name)
        {
            try
            {
                if (Check.IsTraceEnabled(m_oracleLogger?.Logger))
                {
                    Trace <DbLoggerCategory.Database.Command> .Write(m_oracleLogger, LogLevel.Trace, OracleTraceTag.Entry, OracleTraceClassName.OracleSqlGenerationHelper, OracleTraceFuncName.GenerateParameterName);
                }

                while (name.StartsWith("_", StringComparison.Ordinal) || Regex.IsMatch(name, "^\\d"))
                {
                    name = name.Substring(1);
                }
            }
            catch (Exception ex)
            {
                if (Check.IsErrorEnabled(m_oracleLogger?.Logger))
                {
                    Trace <DbLoggerCategory.Database.Command> .Write(m_oracleLogger, LogLevel.Error, OracleTraceTag.Error, OracleTraceClassName.OracleSqlGenerationHelper, OracleTraceFuncName.GenerateParameterName, ex.ToString());
                }
                throw;
            }
            finally
            {
                if (Check.IsTraceEnabled(m_oracleLogger?.Logger))
                {
                    Trace <DbLoggerCategory.Database.Command> .Write(m_oracleLogger, LogLevel.Trace, OracleTraceTag.Exit, OracleTraceClassName.OracleSqlGenerationHelper, OracleTraceFuncName.GenerateParameterName);
                }
            }
            if (Encoding.UTF8.GetByteCount(name) > 30)
            {
                name = OracleMigrationsSqlGenerator.DeriveObjectName(null, name);
            }
            return(":" + name);
        }
Exemplo n.º 2
0
        private static string GetVariableName(ColumnModification columnModification)
        {
            string text = "v" + columnModification.ParameterName + "_" + columnModification.ColumnName;

            if (Encoding.UTF8.GetByteCount(text) > 30)
            {
                text = OracleMigrationsSqlGenerator.DeriveObjectName(null, text);
            }
            return(text);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 添加批量插入操作
        /// </summary>
        /// <param name="commandStringBuilder">字符串</param>
        /// <param name="variablesInsert">插入变量</param>
        /// <param name="modificationCommands">修改命令</param>
        /// <param name="commandPosition">命令位置</param>
        /// <param name="cursorPosition">游标位置</param>
        /// <param name="cursorPositionList">游标位置列表</param>
        /// <returns></returns>
        public virtual ResultSetMapping AppendBatchInsertOperation(
            StringBuilder commandStringBuilder,
            Dictionary <string, string> variablesInsert,
            IReadOnlyDictionary <ModificationCommand, int> modificationCommands,
            int commandPosition,
            ref int cursorPosition,
            List <int> cursorPositionList)
        {
            try
            {
                if (Check.IsTraceEnabled(m_oracleLogger?.Logger))
                {
                    Trace <DbLoggerCategory.Update> .Write(m_oracleLogger, LogLevel.Trace, OracleTraceTag.Entry, OracleTraceClassName.OracleUpdateSqlGenerator, OracleTraceFuncName.AppendBatchInsertOperation);
                }

                commandStringBuilder.Clear();
                var result       = ResultSetMapping.NoResultSet;
                var keyValuePair = modificationCommands.First();
                var tableName    = keyValuePair.Key.TableName;
                var schema       = keyValuePair.Key.Schema;
                var reads        = keyValuePair.Key.ColumnModifications.Where(o => o.IsRead).ToArray();
                var name         = tableName;

                int num = 28 - commandPosition.ToString().Length;
                if (Encoding.UTF8.GetByteCount(name) > num)
                {
                    name = OracleMigrationsSqlGenerator.DeriveObjectName(null, name, num);
                }

                string nameVariable = $"{name}_{commandPosition}";
                if (reads.Length != 0)
                {
                    if (!variablesInsert.Any(p => p.Key == nameVariable))
                    {
                        StringBuilder stringBuilder = new StringBuilder();

                        stringBuilder
                        .AppendLine("TYPE r" + nameVariable + " IS RECORD")
                        .AppendLine("(");

                        stringBuilder
                        .AppendJoin(reads, delegate(StringBuilder sb, ColumnModification cm)
                        {
                            //去掉列名的引号
                            //sb.Append("\"" + cm.ColumnName + "\"").Append(" ").AppendLine(GetVariableType(cm));
                            sb
                            .Append(cm.ColumnName)
                            .Append(" ")
                            .AppendLine(GetVariableType(cm));
                        }, ",")
                        .Append(")")
                        .AppendLine(SqlGenerationHelper.StatementTerminator);

                        stringBuilder
                        .Append("TYPE t" + nameVariable + " IS TABLE OF r" + nameVariable)
                        .AppendLine(SqlGenerationHelper.StatementTerminator)
                        .Append("l" + nameVariable + " t" + nameVariable)
                        .AppendLine(SqlGenerationHelper.StatementTerminator);

                        variablesInsert.Add(nameVariable, stringBuilder.ToString());
                    }

                    commandStringBuilder
                    .Append("l")
                    .Append(nameVariable)
                    .Append(" := ")
                    .Append("t" + nameVariable)
                    .Append("()")
                    .AppendLine(SqlGenerationHelper.StatementTerminator);

                    commandStringBuilder
                    .Append("l" + nameVariable + ".extend(")
                    .Append(modificationCommands.Count)
                    .Append(")")
                    .AppendLine(SqlGenerationHelper.StatementTerminator);
                }

                num = 0;
                foreach (var modificationCommand in modificationCommands)
                {
                    var operations      = modificationCommand.Key.ColumnModifications;
                    var readOperations  = operations.Where(o => o.IsRead).ToArray();
                    var writeOperations = operations.Where(o => o.IsWrite).ToArray();

                    AppendInsertCommand(commandStringBuilder, tableName, schema, writeOperations, (IReadOnlyCollection <ColumnModification>)(object) readOperations);
                    AppendReturnInsert(commandStringBuilder, nameVariable, readOperations, num);
                    num++;
                }

                num = 0;
                foreach (var modificationCommand in modificationCommands)
                {
                    var readOperations = modificationCommand.Key.ColumnModifications.Where(o => o.IsRead).ToArray();

                    if (readOperations.Length != 0)
                    {
                        int value = modificationCommand.Value;
                        AppendReturnCursor(commandStringBuilder, nameVariable, readOperations, num, value);
                        result = ResultSetMapping.LastInResultSet;
                    }
                    num++;
                }

                return(result);
            }
            catch (Exception ex)
            {
                if (Check.IsErrorEnabled(m_oracleLogger?.Logger))
                {
                    Trace <DbLoggerCategory.Update> .Write(m_oracleLogger, LogLevel.Error, OracleTraceTag.Error, OracleTraceClassName.OracleUpdateSqlGenerator, OracleTraceFuncName.AppendBatchInsertOperation, ex.ToString());
                }
                throw;
            }
            finally
            {
                if (Check.IsTraceEnabled(m_oracleLogger?.Logger))
                {
                    Trace <DbLoggerCategory.Update> .Write(m_oracleLogger, LogLevel.Trace, OracleTraceTag.Exit, OracleTraceClassName.OracleUpdateSqlGenerator, OracleTraceFuncName.AppendBatchInsertOperation);
                }
            }
        }