Ejemplo n.º 1
0
        public static object ExecuteScalar(string sql, TkDbContext context,
                                           DbParameterList parameterlist)
        {
            TkDebug.AssertArgumentNull(parameterlist, "parameterlist", null);

            return(ExecuteScalar(sql, context, parameterlist.CreateParameters(context)));
        }
Ejemplo n.º 2
0
        public static string ExecuteCache(string name, TkDbContext context)
        {
            int    step = context.ContextConfig.IdStep;
            IdPair pair;

            if (fIdTable.TryGetValue(name, out pair))
            {
                if (++pair.Current >= pair.Max)
                {
                    pair.Current = int.Parse(ExecuteProcWithStep(name, context),
                                             ObjectUtil.SysCulture);
                    pair.Max = pair.Current + step - 1;
                }
                return(pair.Current.ToString(ObjectUtil.SysCulture));
            }
            else
            {
                int value = int.Parse(ExecuteProcWithStep(name, context),
                                      ObjectUtil.SysCulture);
                fIdTable.Add(name, new IdPair {
                    Current = value, Max = value + step - 1
                });
                return(value.ToString(ObjectUtil.SysCulture));
            }
        }
Ejemplo n.º 3
0
 public static void Select(TkDbContext context, DataSet dataSet, string tableName, string sql)
 {
     using (SqlSelector selector = new SqlSelector(context, dataSet))
     {
         selector.Select(tableName, sql);
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Initializes a new instance of the StoredProc class.
        /// </summary>
        protected AbstractStoredProc(string procName, TkDbContext context)
            : this(procName)
        {
            TkDebug.AssertArgumentNull(context, "context", null);

            Context = context;
        }
Ejemplo n.º 5
0
 private static string GetField(TkDbContext context, IFieldInfo item)
 {
     if (item.FieldName == item.NickName)
     {
         if (context != null)
         {
             return(context.EscapeName(item.NickName));
         }
         else
         {
             return(item.NickName);
         }
     }
     else
     {
         string fieldName = item.FieldName;
         string nickName  = item.NickName;
         if (context != null)
         {
             fieldName = context.EscapeName(fieldName);
             nickName  = context.EscapeName(nickName);
         }
         return(string.Format(ObjectUtil.SysCulture, "{0} {1}", fieldName, nickName));
     }
 }
Ejemplo n.º 6
0
        public static IParamBuilder CreateSingleSql(TkDbContext context, IFieldInfo field,
                                                    string oper, string paramName, object fieldValue)
        {
            TkDebug.AssertArgumentNull(field, "field", null);

            return(CreateSingleSql(context, field.FieldName, field.DataType, oper, paramName, fieldValue));
        }
Ejemplo n.º 7
0
        private IParamBuilder InternalCreateLikeSql(TkDbContext context, IFieldInfo fieldInfo, string paramName, string fieldValue)
        {
            fIsEscape = fieldValue.IndexOfAny(WIDE_CHARS) != -1;
            string fieldName  = context.EscapeName(fieldInfo.FieldName);
            string fieldName1 = paramName + "1";
            string fieldName2 = paramName + "2";

            if (fIsEscape)
            {
                fSql = string.Format(ObjectUtil.SysCulture,
                                     "(({0} LIKE {1} ESCAPE '\\') OR ({0} LIKE {2} ESCAPE '\\'))", fieldName,
                                     context.GetSqlParamName(fieldName1), context.GetSqlParamName(fieldName2));
                fieldValue = StringUtil.EscapeSqlString(fieldValue);
            }
            else
            {
                fSql = string.Format(ObjectUtil.SysCulture,
                                     "(({0} LIKE {1}) OR ({0} LIKE {2}))", fieldName,
                                     context.GetSqlParamName(fieldName1), context.GetSqlParamName(fieldName2));
            }

            fParams.Add(fieldName1, fieldInfo.DataType, fieldValue + "%");
            fParams.Add(fieldName2, fieldInfo.DataType, "%" + fieldValue + "%");
            return(this);
        }
Ejemplo n.º 8
0
        public static IParamBuilder CreateLikeSql(TkDbContext context,
                                                  IFieldInfo fieldName, string paramName, string fieldValue)
        {
            LikeParamBuilder builder = new LikeParamBuilder();

            return(builder.InternalCreateLikeSql(context, fieldName, paramName, fieldValue));
        }
Ejemplo n.º 9
0
        string ISqlProvider.GetUniId(string name, TkDbContext context)
        {
            TkDebug.AssertArgumentNullOrEmpty(name, "name", this);
            TkDebug.AssertArgumentNull(context, "context", this);

            return(UniIdProc.Execute(name, context));
        }
Ejemplo n.º 10
0
        private static string CreateWhereSql(TableResolver resolver, IDbCommand command,
                                             List <FieldInfoEventArgs> list, bool isOrigin)
        {
            StringBuilder whereSql = new StringBuilder(BUFFER_SIZE);
            int           i        = 0;
            TkDbContext   context  = resolver.Context;

            foreach (FieldInfoEventArgs item in list)
            {
                if ((item.Position & SqlPosition.Where) == SqlPosition.Where)
                {
                    string fieldName = item.FieldInfo.FieldName;
                    JoinStringItem(whereSql, i++, string.Format(ObjectUtil.SysCulture, "{0} = {1}",
                                                                context.EscapeName(fieldName), context.GetSqlParamName(fieldName, isOrigin)),
                                   " AND ");
                    command.Parameters.Add(CreateDataParameter(context, item.FieldInfo, isOrigin));
                }
            }
            string sql = whereSql.ToString();

            TkDebug.Assert(!string.IsNullOrEmpty(sql), string.Format(ObjectUtil.SysCulture,
                                                                     "表{0}在设置提交SQL的Where部分时,没有找到对应的字段,请确认主键是否设置,或者SetFieldInfo事件是否正确",
                                                                     resolver.TableName), resolver);
            return("WHERE " + sql);
        }
Ejemplo n.º 11
0
        public static void UpdateTableResolvers(TkDbContext context, Action <Transaction> applyData,
                                                bool sort, params TableResolver[] resolvers)
        {
            TkDebug.AssertArgumentNull(resolvers, "resolvers", null);

            UpdateTableResolvers(context, applyData, sort, resolvers as IEnumerable <TableResolver>);
        }
Ejemplo n.º 12
0
        internal static string GetLayer(TableResolver resolver, DbTreeDefinition tree, string parentId)
        {
            TkDbContext   context    = resolver.Context;
            IFieldInfo    layerField = resolver.GetFieldInfo(tree.LayerField);
            IParamBuilder fixBuilder = resolver.CreateFixCondition();
            string        execRootId = tree.ExecuteRootId;

            if (execRootId == parentId)
            {
                string subStringSql = context.ContextConfig.GetFunction("SubString",
                                                                        layerField.FieldName, 1, 3);
                string sql = string.Format(ObjectUtil.SysCulture,
                                           "SELECT MAX({0}) FROM {1}", subStringSql, resolver.TableName);
                string value = (fixBuilder == null ? DbUtil.ExecuteScalar(sql, context)
                    : DbUtil.ExecuteScalar(sql, context, fixBuilder)).ToString();
                if (string.IsNullOrEmpty(value))
                {
                    return("000");
                }
                else
                {
                    return(AddLayer(value));
                }
            }
            else
            {
                try
                {
                    string sql = string.Format(ObjectUtil.SysCulture, "SELECT {0} FROM {1}",
                                               layerField.FieldName, resolver.TableName);
                    IParamBuilder builder = ParamBuilder.CreateParamBuilder(fixBuilder,
                                                                            SqlParamBuilder.CreateEqualSql(context, resolver.GetFieldInfo(tree.IdField), parentId));
                    string topLayer     = DbUtil.ExecuteScalar(sql, context, builder).ToString();
                    string subStringSql = context.ContextConfig.GetFunction("SubString",
                                                                            layerField.FieldName, topLayer.Length + 1, 3);

                    builder = CreateLayerParamBuilder(context, layerField, topLayer);
                    builder = SqlParamBuilder.CreateParamBuilder(fixBuilder, builder);
                    sql     = string.Format(ObjectUtil.SysCulture, "SELECT MAX({0}) FROM {1}",
                                            subStringSql, resolver.TableName);
                    string value = DbUtil.ExecuteScalar(sql, context, builder).ToString().Trim();
                    if (string.IsNullOrEmpty(value))
                    {
                        DataRow parentRow = resolver.SelectRowWithParam(tree.IdField, parentId);
                        parentRow[tree.LeafField] = 0;
                        resolver.SetCommands(AdapterCommand.Update);
                        return(topLayer + "000");
                    }
                    else
                    {
                        return(topLayer + AddLayer(value.Substring(value.Length - 3)));
                    }
                }
                catch
                {
                    return(string.Empty);
                }
            }
        }
Ejemplo n.º 13
0
        public static string GetSelectFields(this ITableScheme scheme, TkDbContext context)
        {
            TkDebug.AssertArgumentNull(scheme, "scheme", null);

            TableSchemeData data = TableSchemeData.Create(context, scheme);

            return(data.SelectFields);
        }
Ejemplo n.º 14
0
 public static void Select(TkDbContext context, DataSet dataSet, string tableName,
                           string sql, params IDbDataParameter[] dbParams)
 {
     using (SqlSelector selector = new SqlSelector(context, dataSet))
     {
         selector.Select(tableName, sql, dbParams);
     }
 }
Ejemplo n.º 15
0
        public static void UpdateTableResolvers(TkDbContext context, Action <Transaction> applyData,
                                                bool sort, TableResolver resolver, IEnumerable <TableResolver> resolvers)
        {
            TkDebug.AssertArgumentNull(resolver, "resolver", null);
            TkDebug.AssertArgumentNull(resolvers, "resolvers", null);

            UpdateTableResolvers(context, applyData, sort, EnumUtil.Convert(resolver, resolvers));
        }
Ejemplo n.º 16
0
        protected internal static string CreateSingleSql(TkDbContext context, string fieldName,
                                                         string oper, string paramName)
        {
            string sql = string.Format(ObjectUtil.SysCulture, "{0} {1} {2}",
                                       context.EscapeName(fieldName), oper, context.GetSqlParamName(paramName));

            return(sql);
        }
Ejemplo n.º 17
0
 private void SetContext(TkDbContext context)
 {
     if (fContext != null)
     {
         fContext.Dispose();
     }
     fContext = context;
 }
Ejemplo n.º 18
0
 public static void Select(TkDbContext context, DataSet dataSet, string tableName,
                           string selectSql, IParamBuilder builder, string orderBy)
 {
     using (SqlSelector selector = new SqlSelector(context, dataSet))
     {
         selector.Select(tableName, selectSql, builder, orderBy);
     }
 }
Ejemplo n.º 19
0
        public IParamBuilder CreateParamBuilder(TkDbContext context, IFieldInfoIndexer indexer)
        {
            if (FakeDeleteInfo == null)
            {
                return(null);
            }

            return(FakeDeleteInfo.CreateParamBuilder(context, indexer));
        }
Ejemplo n.º 20
0
        public SqlSelector(TkDbContext context, DataSet hostDataSet)
        {
            TkDebug.AssertArgumentNull(context, "context", null);
            TkDebug.AssertArgumentNull(hostDataSet, "hostDataSet", null);

            Context     = context;
            HostDataSet = hostDataSet;
            //fDataAdapter = context.CreateDataAdapter();
        }
Ejemplo n.º 21
0
        private static IParamBuilder CreateLayerParamBuilder(TkDbContext context,
                                                             IFieldInfo layer, string topLayer)
        {
            string        fieldName = layer.FieldName;
            IParamBuilder builder   = SqlParamBuilder.CreateParamBuilder(
                SqlParamBuilder.CreateSingleSql(context, layer, "!=", fieldName + "0", topLayer),
                SqlParamBuilder.CreateSingleSql(context, layer, "LIKE", fieldName + "1", topLayer + "%"));

            return(builder);
        }
Ejemplo n.º 22
0
 public static string Execute(string name, TkDbContext context)
 {
     if (context.ContextConfig.IdStep == 1)
     {
         return(ExecuteProc(name, context));
     }
     else
     {
         return(ExecuteCache(name, context));
     }
 }
Ejemplo n.º 23
0
 public static string ExecuteProc(string name, TkDbContext context)
 {
     using (UniIdProc proc = new UniIdProc(context)
     {
         Name = name
     })
     {
         proc.Execute();
         return(proc.Value);
     }
 }
Ejemplo n.º 24
0
        private static IDbDataParameter CreateDataParameter(TkDbContext context, IFieldInfo fieldInfo, bool isOrigin)
        {
            IDbDataParameter result = context.CreateParameter(fieldInfo, isOrigin);

            result.SourceColumn = fieldInfo.NickName;
            if (isOrigin)
            {
                result.SourceVersion = DataRowVersion.Original;
            }
            return(result);
        }
Ejemplo n.º 25
0
        public static IParamBuilder CreateInSql(TkDbContext context, IFieldInfo fieldName,
                                                IEnumerable <string> values)
        {
            TkDebug.AssertArgumentNull(fieldName, "fieldName", null);
            TkDebug.AssertArgumentNull(values, "values", null);

            string[] valueArray = values.ToArray();
            if (valueArray.Length == 0)
            {
                return(ParamBuilder.NoResult);
            }

            string seperator = string.Empty;

            switch (fieldName.DataType)
            {
            case TkDataType.String:
                seperator = "\'";
                break;

            case TkDataType.Int:
            case TkDataType.Byte:
            case TkDataType.Short:
            case TkDataType.Double:
            case TkDataType.Long:
                break;

            default:
                TkDebug.ThrowToolkitException(string.Format(ObjectUtil.SysCulture,
                                                            "生成IN SQL语句的字段只支持字符串、整形、浮点三种类型,现在字段{0}类型是{1},不在支持范围",
                                                            fieldName.FieldName, fieldName.DataType), null);
                break;
            }
            var items = from value in valueArray
                        select seperator + StringUtil.EscapeAposString(value) + seperator;
            string insql = string.Join(",", items);
            string field;

            if (context != null)
            {
                field = context.EscapeName(fieldName.FieldName);
            }
            else
            {
                field = fieldName.FieldName;
            }
            string sql = string.Format(ObjectUtil.SysCulture, "{0} IN ({1})",
                                       field, insql);
            InternalParamBuilder builder = new InternalParamBuilder {
                Sql = sql
            };

            return(builder);
        }
Ejemplo n.º 26
0
        public static object ExecuteScalar(string sql, TkDbContext context)
        {
            TkDebug.AssertArgumentNullOrEmpty(sql, "sql", null);
            TkDebug.AssertArgumentNull(context, "context", null);

            using (IDbCommand command = context.CreateCommand())
            {
                command.CommandText = sql;
                return(ExecuteScalar(context.DbConnection, command));
            }
        }
Ejemplo n.º 27
0
        private static string GetUploadItemField(TkDbContext context, IFieldInfoIndexer indexer, string nickName)
        {
            var field = indexer[nickName];

            if (field == null)
            {
                return(null);
            }
            return(string.Format(ObjectUtil.SysCulture, "{0} {1}",
                                 context.EscapeName(field.FieldName), context.EscapeName(field.NickName)));
        }
Ejemplo n.º 28
0
 public static string ExecuteProcWithStep(string name, TkDbContext context)
 {
     using (UniIdProc proc = new UniIdProc(context)
     {
         Name = name,
         Step = context.ContextConfig.IdStep
     })
     {
         proc.Execute();
         return(proc.Value);
     }
 }
Ejemplo n.º 29
0
        public static int ExecuteNonQuery(string sql, TkDbContext context,
                                          params IDbDataParameter[] parameters)
        {
            TkDebug.AssertArgumentNullOrEmpty(sql, "sql", null);
            TkDebug.AssertArgumentNull(context, "context", null);
            TkDebug.AssertEnumerableArgumentNull <IDbDataParameter>(parameters, "parameters", null);

            using (IDbCommand command = context.CreateCommand())
            {
                command.CommandText = sql;
                return(ExecuteNonQuery(context.DbConnection, command, parameters));
            }
        }
Ejemplo n.º 30
0
        public TableResolver CreateObject(params object[] args)
        {
            IDbDataSource source  = ObjectUtil.ConfirmQueryObject <IDbDataSource>(this, args);
            TkDbContext   context = string.IsNullOrEmpty(Context) ? DbContextUtil.CreateDefault()
                : DbContextUtil.CreateDbContext(Context);

            using (context)
            {
                var scheme   = DbUtil.CreateSqlTableScheme(Sql, TableName, KeyFields, context);
                var schemeEx = MetaDataUtil.ConvertToTableSchemeEx(scheme);
                return(new SqlTableResolver(Sql, schemeEx, source));
            }
        }