Пример #1
0
        /// <summary>
        /// 输出为文本。
        /// </summary>
        /// <param name="valueSpliter">key/value分割符。</param>
        /// <param name="nameSpliter">多个分割符。</param>
        /// <returns></returns>
        public string ToString(string valueSpliter, string nameSpliter)
        {
            CommonException.CheckArgumentNull(valueSpliter, "valueSpliter");
            CommonException.CheckArgumentNull(nameSpliter, "nameSpliter");

            System.Text.StringBuilder builder = new System.Text.StringBuilder();

            foreach (string name in AllKeys)
            {
                string[] values = GetValues(name);
                if (values == null)
                {
                    continue;
                }
                bool emptyName = string.IsNullOrEmpty(name);
                foreach (string value in values)
                {
                    if (builder.Length > 0)
                    {
                        builder.Append(nameSpliter);
                    }
                    if (!emptyName)
                    {
                        builder.Append(name);
                        builder.Append(valueSpliter);
                    }
                    builder.Append(HttpUtility.UrlEncode(value, Encoding));
                }
            }

            return(builder.ToString());
        }
Пример #2
0
        /// <summary>
        /// 创建TcpListener实例。
        /// </summary>
        /// <param name="setting">设置项。</param>
        public TcpListener(ServerSetting setting)
        {
            CommonException.CheckArgumentNull(setting, "setting");
            _serverSetting   = setting;
            setting.Readonly = true;

            _log = LogBase.Empty;
        }
 /// <summary>
 /// 生成求avg命令。
 /// </summary>
 /// <param name="field">字段名称。</param>
 /// <param name="clear">是否清空所有字段</param>
 /// <returns></returns>
 public virtual ISelectCommandBuilder Average(string field, bool clear)
 {
     CommonException.CheckArgumentNull(field, "field");
     if (clear)
     {
         _orderbys.Clear();
         _fields.Clear();
     }
     _fields.Add(string.Format("avg({0})", _dialect.PreName(field)));
     return(this);
 }
Пример #4
0
        /// <summary>
        /// 创建数据上下文。
        /// </summary>
        /// <param name="type">数据库提供者名称。</param>
        /// <param name="connectionOptions">连接参数。</param>
        /// <returns>返回数据上下文。</returns>
        public static IDataContext CreateDataContext(string type, object connectionOptions)
        {
            CommonException.CheckArgumentNull(type, "type");
            var provider = Get(type);

            if (provider == null)
            {
                CommonException.ThrowNotSupported($"未找到数据提供者“{type}”");
            }

            return(provider.CreateDataContext(connectionOptions));
        }
Пример #5
0
        /// <summary>
        /// 创建ReferEntry实例
        /// </summary>
        /// <param name="name">名称。</param>
        /// <param name="source">源。</param>
        /// <param name="sourceField">源字段。</param>
        /// <param name="target">目标。</param>
        /// <param name="targetField">目标字段。</param>
        public ReferEntry(string name, string source, string sourceField, string target, string targetField)
        {
            CommonException.CheckArgumentNull(name, "name");
            CommonException.CheckArgumentNull(source, "source");
            CommonException.CheckArgumentNull(sourceField, "sourceField");


            _name        = name;
            _source      = source;
            _sourceField = sourceField;

            if (!IsThis)
            {
                CommonException.CheckArgumentNull(target, "target");
                CommonException.CheckArgumentNull(targetField, "targetField");
                _target      = target;
                _targetField = targetField;
            }
        }
Пример #6
0
 /// <summary>
 /// 创建数据库连接。
 /// </summary>
 /// <param name="connectionString">连接字符串。</param>
 /// <returns>返回数据库连接。</returns>
 public override IConnection CreateConnection(string connectionString)
 {
     CommonException.CheckArgumentNull(connectionString, "connectionString");
     System.Type type = GetConnectionType(true);
     return(new PostgreSQLConnection(this, FastWrapper.CreateInstance <IDbConnection>(type, connectionString), connectionString));
 }
Пример #7
0
        /// <summary>
        /// 映射DataReader当前数据记录。
        /// </summary>
        /// <param name="reader">数据读取对象。</param>
        /// <param name="type">实体类型。</param>
        /// <returns>返回映射结果。</returns>
        public static object Current(
#if !net20
            this
#endif
            IDataReader reader, Type type)
        {
            CommonException.CheckArgumentNull(reader, "reader");
            if (reader.IsClosed)
            {
                return(null);
            }
            if (type != null && (type.IsValueType || reader.GetFieldType(0) == type || (reader.FieldCount == 1 && type == typeof(string))))
            {
                //只拿第一列
                object value = reader.GetValue(0);
                if (value == DBNull.Value)
                {
                    return(TypeExtensions.DefaultValue(type));
                }
                return(TypeExtensions.Convert(value, type));
            }
            if (type != null)
            {
                if ((reader.FieldCount == 1 && (type.IsEnum || (TypeExtensions.IsNullableType(type) && TypeExtensions.GetNullableType(type).IsEnum))) || //枚举
                    (type.IsValueType || reader.GetFieldType(0) == type))     //类型匹配
                {
                    object value = reader.GetValue(0);
                    return(TypeExtensions.Convert((value == DBNull.Value ? TypeExtensions.DefaultValue(type) : value), type));
                }
            }
            Symbol.Collections.Generic.NameValueCollection <object> values = null;
            object result = null;

            if (type == null)
            {
                values = new Symbol.Collections.Generic.NameValueCollection <object>();
                result = values;
            }
            else
            {
                result = FastWrapper.CreateInstance(type);
                //IExtensibleModel extensiableModel = result as IExtensibleModel;
                //if (extensiableModel != null) {
                //    values = extensiableModel.Extendeds = new Symbol.Collections.Generic.NameValueCollection<object>();
                if (result is Symbol.Collections.Generic.NameValueCollection <object> )
                {
                    values = (Symbol.Collections.Generic.NameValueCollection <object>)result;
                    type   = null;
                }
            }

            for (int i = 0; i < reader.FieldCount; i++)
            {
                string name = reader.GetName(i);
                //int extIndex = name.IndexOf("ext_");
                //if (extIndex != -1)
                //    name = name.Substring(extIndex + 4);
                bool isJson = false;
                if (reader.GetFieldType(i) == typeof(string))
                {
                    string dataTypeName = reader.GetDataTypeName(i);
                    isJson = string.Equals(dataTypeName, "jsonb", StringComparison.OrdinalIgnoreCase) ||
                             string.Equals(dataTypeName, "json", StringComparison.OrdinalIgnoreCase);
                }

                object value = reader[i];
                if (value == DBNull.Value)
                {
                    value = null;
                }
                //if (isJson) {
                //    string text = value as string;
                //    if (!string.IsNullOrEmpty(text)) {
                //        value = JSON.Parse(text);
                //    }
                //} else
                if (reader.GetFieldType(i) == typeof(byte[]) && string.Equals(reader.GetDataTypeName(i), "timestamp", StringComparison.OrdinalIgnoreCase))
                {
                    byte[] buffer = (byte[])value;
                    Array.Reverse(buffer);
                }
                if (string.IsNullOrEmpty(name))
                {
                    if (values != null)
                    {
                        values.Add("无列名" + i, value);
                    }
                    continue;
                }
                if (type != null)
                {
                    PropertyInfo property = type.GetProperty(name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.IgnoreCase);
                    if (property != null)
                    {
                        if (!isJson)
                        {
                            isJson = TypeExtensions.Convert <bool>(ConstAttributeExtensions.Const(property, "SaveAsJson"), false);
                        }
                        if (!isJson && reader.GetFieldType(i) == typeof(string) &&
                            (!property.PropertyType.IsValueType && property.PropertyType != typeof(string)))
                        {
                            isJson = true;
                        }
                        if (isJson && !property.PropertyType.IsValueType &&
                            property.PropertyType != typeof(string))
                        {
                            //value = Symbol.Serialization.ObjectConverter.ConvertObjectToType(value, property.PropertyType, new Serialization.JavaScriptSerializer());
                            value = JSON.ToObject(value as string, property.PropertyType);
                        }
                        else
                        {
                            value = TypeExtensions.Convert(value, property.PropertyType);
                        }
                        property.SetValue(result, value, null);
                        continue;
                    }

                    FieldInfo fieldInfo = type.GetField(name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.IgnoreCase);
                    if (fieldInfo != null)
                    {
                        if (isJson && !fieldInfo.FieldType.IsValueType && fieldInfo.FieldType != typeof(string))
                        {
                            //value = Symbol.Serialization.ObjectConverter.ConvertObjectToType(value, fieldInfo.FieldType, new Serialization.JavaScriptSerializer());
                            value = JSON.ToObject(value as string, fieldInfo.FieldType);
                        }
                        else
                        {
                            value = TypeExtensions.Convert(value, fieldInfo.FieldType);
                        }

                        fieldInfo.SetValue(result, value);
                        continue;
                    }
                }
                if (values == null)
                {
                    continue;
                }
                if (value != null)
                {
lb_Json_retry:
                    if (isJson)
                    {
                        value = JSON.Parse(value as string, true);
                    }
                    else if (string.Equals(reader.GetDataTypeName(i), "char(1)", StringComparison.OrdinalIgnoreCase) ||
                             string.Equals(reader.GetDataTypeName(i), "nchar(1)", StringComparison.OrdinalIgnoreCase))
                    {
                        value = reader.GetChar(i);
                    }
                    else if (value is string)
                    {
                        string text = ((string)value).Trim();
                        if (text != null && ((text.StartsWith("{") && text.EndsWith("}")) || (text.StartsWith("[") && text.EndsWith("]"))))
                        {
                            isJson = true;
                            goto lb_Json_retry;
                        }
                    }
                }
                values[name] = value;
                if (isJson && reader.FieldCount == 1 && (type == null || type == typeof(object)))
                {
                    return(value);
                }
                //values[name] = reader.GetValue(i);
                //if (values[name] == DBNull.Value)
                //    values[name] = null;
                //else if (values[name] != null) {
                //    if (string.Equals(reader.GetDataTypeName(i), "char(1)", StringComparison.OrdinalIgnoreCase)
                //        || string.Equals(reader.GetDataTypeName(i), "nchar(1)", StringComparison.OrdinalIgnoreCase)) {
                //        values[name] = reader.GetChar(i);
                //    }
                //}
            }

            return(result);
        }
Пример #8
0
 /// <summary>
 /// 创建数据库连接。
 /// </summary>
 /// <param name="connectionString">连接字符串。</param>
 /// <returns>返回数据库连接。</returns>
 public override IConnection CreateConnection(string connectionString)
 {
     CommonException.CheckArgumentNull(connectionString, "connectionString");
     return(new SqlServerConnection(this, FastWrapper.CreateInstance <System.Data.SqlClient.SqlConnection>(connectionString), connectionString));
 }
Пример #9
0
 /// <summary>
 /// 创建数据库连接。
 /// </summary>
 /// <param name="connectionString">连接字符串。</param>
 /// <returns>返回数据库连接。</returns>
 public override IConnection CreateConnection(string connectionString)
 {
     CommonException.CheckArgumentNull(connectionString, "connectionString");
     return(new AdoConnection(this, SQLite.SQLiteHelper.CreateConnection(connectionString), connectionString));
 }