예제 #1
0
        private static YukonDependency GetYukonDependency(SqlYukonCacheDependency yukonDep)
        {
            YukonDependency protoYukonDep = new YukonDependency();

            protoYukonDep.commandType      = (int)yukonDep.CommandType;
            protoYukonDep.connectionString = yukonDep.ConnectionString;
            protoYukonDep.query            = yukonDep.QueryString;

            foreach (DictionaryEntry entry in yukonDep.CommandParams)
            {
                SqlCmdParams yukonCommandParams = (SqlCmdParams)entry.Value;

                YukonParam param = new YukonParam();
                param.key = (string)entry.Key;

                param.cmdParam                   = new YukonCommandParam();
                param.cmdParam.cmpOptions        = (int)yukonCommandParams.CmpInfo;
                param.cmdParam.direction         = (int)yukonCommandParams.Direction;
                param.cmdParam.isNullable        = yukonCommandParams.IsNullable;
                param.cmdParam.localeId          = yukonCommandParams.LocaleID;
                param.cmdParam.offset            = yukonCommandParams.Offset;
                param.cmdParam.precision         = yukonCommandParams.Precision;
                param.cmdParam.scale             = yukonCommandParams.Scale;
                param.cmdParam.size              = yukonCommandParams.ParamSize;
                param.cmdParam.sourceColumn      = yukonCommandParams.SourceColumn;
                param.cmdParam.sourceColumnNull  = yukonCommandParams.SourceColumnNullMapping;
                param.cmdParam.sqlValue          = yukonCommandParams.SqlValue.ToString();
                param.cmdParam.version           = (int)yukonCommandParams.SrcVersion;
                param.cmdParam.typeId            = (int)yukonCommandParams.DbType;
                param.cmdParam.typeName          = yukonCommandParams.TypeName;
                param.cmdParam.udtTypeName       = yukonCommandParams.UdtName;
                param.cmdParam.nullValueProvided = yukonCommandParams.Value == null;

                if (!param.cmdParam.nullValueProvided)
                {
                    if (yukonCommandParams.DbType == SqlDbType.Binary || yukonCommandParams.DbType == SqlDbType.VarBinary || yukonCommandParams.DbType == SqlDbType.Image || yukonCommandParams.DbType == SqlDbType.Timestamp)
                    {
                        byte[] val = yukonCommandParams.Value as byte[];
                        System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
                        param.cmdParam.value = encoding.GetString(val);
                    }
                    else if (yukonCommandParams.DbType == SqlDbType.DateTime || yukonCommandParams.DbType == SqlDbType.DateTime2 || yukonCommandParams.DbType == SqlDbType.Date || yukonCommandParams.DbType == SqlDbType.SmallDateTime)
                    {
                        DateTime val = (DateTime)yukonCommandParams.Value;
                        param.cmdParam.value = val.Ticks.ToString();
                    }
                    else if (yukonCommandParams.DbType == SqlDbType.Time)
                    {
                        TimeSpan val = (TimeSpan)yukonCommandParams.Value;
                        param.cmdParam.value = val.Ticks.ToString();
                    }
                    else if (yukonCommandParams.DbType == SqlDbType.DateTimeOffset)
                    {
                        DateTimeOffset val = (DateTimeOffset)yukonCommandParams.Value;
                        param.cmdParam.value = String.Concat(val.Date.Ticks, ",", val.Offset.Minutes);
                    }
                    else
                    {
                        param.cmdParam.value = yukonCommandParams.Value.ToString();
                    }
                }

                protoYukonDep.param.Add(param);
            }

            return(protoYukonDep);
        }
예제 #2
0
        public static Dependency GetProtoBufDependency(CacheDependency cacheDependency, Dependency dependency)
        {
            if (cacheDependency is Runtime.Dependencies.FileDependency)
            {
                Runtime.Dependencies.FileDependency fileDependency  = cacheDependency as Runtime.Dependencies.FileDependency;
                Protobuf.FileDependency             protoDependency = new Protobuf.FileDependency();
                protoDependency.filePaths.AddRange(fileDependency.fileNames);
                protoDependency.startAfter = fileDependency.StartAfterTicks;

                dependency.fileDep.Add(protoDependency);

                return(dependency);
            }
            else if (cacheDependency is Runtime.Dependencies.KeyDependency)
            {
                Runtime.Dependencies.KeyDependency keyDependency = cacheDependency as Runtime.Dependencies.KeyDependency;

                Alachisoft.NCache.Common.Protobuf.KeyDependency protoDependency = new Alachisoft.NCache.Common.Protobuf.KeyDependency();
                protoDependency.keys.AddRange(keyDependency.CacheKeys);
                protoDependency.startAfter = keyDependency.StartAfterTicks;

                dependency.keyDep.Add(protoDependency);

                return(dependency);
            }
            else if (cacheDependency is Runtime.Dependencies.DBCacheDependency)
            {
                Runtime.Dependencies.DBCacheDependency dbDependency = cacheDependency as Runtime.Dependencies.DBCacheDependency;

                switch (dbDependency.Type)
                {
                case Runtime.Dependencies.DBDependencyType.OleDbCacheDependency:
                    OleDbDependency oleDbDependency = new OleDbDependency();
                    oleDbDependency.connectionString = dbDependency.ConnectionString;
                    oleDbDependency.dbCacheKey       = dbDependency.PrimaryKey;
                    dependency.oleDbDep.Add(oleDbDependency);

                    break;

                case Runtime.Dependencies.DBDependencyType.SqlCacheDependency:
                    Sql7Dependency sqlDependency = new Sql7Dependency();
                    sqlDependency.connectionString = dbDependency.ConnectionString;
                    sqlDependency.dbCacheKey       = dbDependency.PrimaryKey;
                    dependency.sql7Dep.Add(sqlDependency);

                    break;
                }

                return(dependency);
            }
            else if (cacheDependency is Runtime.Dependencies.SqlCacheDependency)
            {
                Runtime.Dependencies.SqlCacheDependency sqlDependency = cacheDependency as Runtime.Dependencies.SqlCacheDependency;

                YukonDependency yukonDependency = new YukonDependency();
                yukonDependency.commandType      = Convert.ToInt32(sqlDependency.CommandType);
                yukonDependency.connectionString = sqlDependency.ConnectionString;
                yukonDependency.query            = sqlDependency.CommandText;

                if (sqlDependency.CommandParams != null)
                {
                    foreach (KeyValuePair <string, Runtime.Dependencies.SqlCmdParams> pair in sqlDependency.CommandParams)
                    {
                        Runtime.Dependencies.SqlCmdParams param = pair.Value;


                        YukonParam yukonParam = new YukonParam();
                        yukonParam.key = pair.Key;

                        YukonCommandParam yukonCmdParam = new YukonCommandParam();
                        yukonCmdParam.dbType           = (int)param.SqlParamType;
                        yukonCmdParam.direction        = (int)param.SqlParamDir;
                        yukonCmdParam.isNullable       = param.IsNullable;
                        yukonCmdParam.localeId         = param.LocaleID;
                        yukonCmdParam.offset           = param.Offset;
                        yukonCmdParam.precision        = param.Precision;
                        yukonCmdParam.scale            = param.Scale;
                        yukonCmdParam.size             = param.Size;
                        yukonCmdParam.sourceColumn     = param.SourceColumn;
                        yukonCmdParam.sourceColumnNull = param.SourceColumnNullMapping;
                        yukonCmdParam.sqlValue         = param.SqlValue != null?param.SqlValue.ToString() : "";

                        yukonCmdParam.version           = (int)param.SrcVersion;
                        yukonCmdParam.typeName          = param.TypeName;
                        yukonCmdParam.typeId            = (int)param.Type;
                        yukonCmdParam.udtTypeName       = param.UdtTypeName;
                        yukonCmdParam.nullValueProvided = param.Value == null ? true : false;

                        if (!yukonCmdParam.nullValueProvided)
                        {
                            if (param.Type == CmdParamsType.Binary || param.Type == CmdParamsType.VarBinary /*|| param.Type == CmdParamsType.Image */ || param.Type == CmdParamsType.Timestamp)
                            {
                                byte[] val = param.Value as byte[];
                                if (val == null)
                                {
                                    throw new OperationFailedException("Expected 'System.Byte[]' value for parameter '" + param.SourceColumn + "'");
                                }
                                else
                                {
                                    System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
                                    yukonCmdParam.value = encoding.GetString(val);
                                }
                            }
                            else if (param.Type == CmdParamsType.DateTime || param.Type == CmdParamsType.DateTime2 || param.Type == CmdParamsType.Date || param.Type == CmdParamsType.SmallDateTime)
                            {
                                try
                                {
                                    DateTime val = (DateTime)param.Value;
                                    yukonCmdParam.value = val.Ticks.ToString();
                                }
                                catch (InvalidCastException ex)
                                {
                                    throw new OperationFailedException("Expected 'System.DateTime' value for parameter type '" + param.Type + "'");
                                }
                            }
                            else if (param.Type == CmdParamsType.Time)
                            {
                                try
                                {
                                    TimeSpan val = (TimeSpan)param.Value;
                                    yukonCmdParam.value = val.Ticks.ToString();
                                }
                                catch (InvalidCastException ex)
                                {
                                    throw new OperationFailedException("Expected 'System.TimeSpan' value for parameter '" + param.Type + "'");
                                }
                            }
                            else if (param.Type == CmdParamsType.DateTimeOffset)
                            {
                                try
                                {
                                    DateTimeOffset val = (DateTimeOffset)param.Value;
                                    yukonCmdParam.value = String.Concat(val.Date.Ticks, ",", val.Offset.Minutes);
                                }
                                catch (InvalidCastException ex)
                                {
                                    throw new OperationFailedException("Expected 'System.DateTimeOffset' value for parameter '" + param.Type + "'");
                                }
                            }
                            else
                            {
                                yukonCmdParam.value = param.Value.ToString();
                            }
                        }

                        yukonParam.cmdParam = yukonCmdParam;
                        yukonDependency.param.Add(yukonParam);
                    }
                }

                dependency.yukonDep.Add(yukonDependency);

                return(dependency);
            }

            else if (cacheDependency is Runtime.Dependencies.OracleCacheDependency)
            {
                Runtime.Dependencies.OracleCacheDependency oracleDependency = cacheDependency as Runtime.Dependencies.OracleCacheDependency;

                OracleDependency protoDependency = new OracleDependency();
                protoDependency.commandType      = (int)oracleDependency.CommandType;
                protoDependency.connectionString = oracleDependency.ConnectionString;
                protoDependency.query            = oracleDependency.CommandText;

                if (oracleDependency.CommandParams != null)
                {
                    foreach (KeyValuePair <string, Runtime.Dependencies.OracleCmdParams> pair in oracleDependency.CommandParams)
                    {
                        Runtime.Dependencies.OracleCmdParams oracleCommandParams = pair.Value;

                        OracleParam param = new OracleParam();
                        param.key = pair.Key;

                        OracleCommandParam oracleCmdParam = new OracleCommandParam();
                        oracleCmdParam.dbType    = (int)oracleCommandParams.Type;
                        oracleCmdParam.direction = (int)oracleCommandParams.Direction;
                        oracleCmdParam.value     = oracleCommandParams.Value != null?oracleCommandParams.Value.ToString() : "";

                        param.cmdParam = oracleCmdParam;

                        protoDependency.param.Add(param);
                    }
                }

                dependency.oracleDep.Add(protoDependency);

                return(dependency);
            }


            else if (cacheDependency is Runtime.Dependencies.ExtensibleDependency)
            {
                Runtime.Dependencies.ExtensibleDependency extDependency = cacheDependency as Runtime.Dependencies.ExtensibleDependency;

                IFormatter   formatter = new BinaryFormatter();
                MemoryStream stream    = new MemoryStream();
                formatter.Serialize(stream, extDependency);

                Alachisoft.NCache.Common.Protobuf.ExtensibleDependency extensibleDependency = new Alachisoft.NCache.Common.Protobuf.ExtensibleDependency();
                extensibleDependency.data = stream.ToArray();

                dependency.xtDep.Add(extensibleDependency);

                return(dependency);
            }
            else if (cacheDependency is NosDBDependency)
            {
                NosDBDependency sqlDependency = cacheDependency as NosDBDependency;

                NosDbDependency nosDependency = new NosDbDependency();
                nosDependency.timeout          = Convert.ToInt32(sqlDependency.Timeout);
                nosDependency.connectionString = sqlDependency.ConnectionString;
                nosDependency.query            = sqlDependency.CommandText;

                if (sqlDependency.Parameters != null)
                {
                    CommandHelper.PopulateValues(sqlDependency.Parameters, nosDependency.param);
                }
                dependency.NosDep.Add(nosDependency);

                return(dependency);
            }
            else
            {
                foreach (Runtime.Dependencies.CacheDependency dep in cacheDependency.Dependencies)
                {
                    dependency = GetProtoBufDependency(dep, dependency);
                }

                return(dependency);
            }
        }