示例#1
0
 /// <summary>
 /// 释放占用的资源。
 /// </summary>
 /// <param name="disposing"></param>
 protected virtual void Dispose(bool disposing)
 {
     if (!disposing)
     {
         return;
     }
     if (_disposed)
     {
         return;
     }
     if (_vars != null)
     {
         _vars.Clear();
         _vars = null;
     }
     if (_dataContext != null)
     {
         _dataContext.Dispose();
         _dataContext = null;
     }
     _databaseName = null;
     if (_log != null)
     {
         _log.Dispose();
         _log = null;
     }
     GC.SuppressFinalize(this);
     _disposed = true;
 }
示例#2
0
 /// <summary>
 /// 释放对象占用的所有资源。
 /// </summary>
 public virtual void Dispose()
 {
     _tableName = null;
     _removedFields?.Clear();
     _removedFields = null;
     _fields?.Clear();
     _fields      = null;
     _dataContext = null;
 }
示例#3
0
 /// <summary>
 /// 创建DatabaseSchemaContext实例。
 /// </summary>
 /// <param name="dataContext">DataContext对象</param>
 public DatabaseSchemaContext(Symbol.Data.IDataContext dataContext)
 {
     _dataContext  = dataContext;
     _databaseName = dataContext.Connection.Database;
     _log          = LogBase.Empty;
     _vars         = new Symbol.Collections.Generic.NameValueCollection <object>();
     _vars.Add("log", _log);
     _vars.Add("dataContext", _dataContext);
     _vars.Add("databaseName", _databaseName);
 }
示例#4
0
 /// <summary>
 /// 创建InsertCommandBuilder实例。
 /// </summary>
 /// <param name="dataContext">数据上下文接口。</param>
 /// <param name="tableName">表名。</param>
 public UpdateCommandBuilder(IDataContext dataContext, string tableName)
 {
     _dataContext = dataContext;
     dataContext.DisposableObjects?.Add(this);
     _tableName                 = tableName;
     _removedFields             = new Collections.Generic.HashSet <string>(System.StringComparer.OrdinalIgnoreCase);
     _fields                    = new Collections.Generic.NameValueCollection <object>();
     _fields.NullValue          = System.DBNull.Value;
     _fields.PropertyDescriptor = FieldValueWrapper;
 }
示例#5
0
        static SQLiteHelper()
        {
            _isDebug    = new bool[] { false };
            _inited     = new bool[] { false };
            _assemblies = new System.Reflection.Assembly[1];
            _types      = new Collections.Generic.NameValueCollection <FastWrapper>();
            _typeNames  = new Collections.Generic.HashSet <string>();

            LoadAssembly(typeof(System.Data.SQLite.SQLiteConnection).Assembly);
        }
示例#6
0
        static PostgreSQLProvider()
        {
            _types = new Collections.Generic.NameValueCollection <System.Type>();
#if netcore || net45
            _types.Add("Npgsql.NpgsqlConnection", typeof(Npgsql.NpgsqlConnection));
            _types.Add("Npgsql.NpgsqlConnectionStringBuilder", typeof(Npgsql.NpgsqlConnectionStringBuilder));
            _types.Add("NpgsqlTypes.NpgsqlDbType", typeof(NpgsqlTypes.NpgsqlDbType));
#else
            GetType("Npgsql.NpgsqlConnection");
            GetType("Npgsql.NpgsqlConnectionStringBuilder");
            GetType("NpgsqlTypes.NpgsqlDbType");
#endif
        }
示例#7
0
        static MySqlProvider()
        {
            _types = new Collections.Generic.NameValueCollection <System.Type>();
#if netcore || net452
            _types.Add("MySql.Data.MySqlClient.MySqlConnection", typeof(MySql.Data.MySqlClient.MySqlConnection));
            _types.Add("MySql.Data.MySqlClient.MySqlConnectionStringBuilder", typeof(MySql.Data.MySqlClient.MySqlConnectionStringBuilder));
            _types.Add("MySql.Data.MySqlClient.MySqlDbType", typeof(MySql.Data.MySqlClient.MySqlDbType));
#else
            GetType("MySql.Data.MySqlClient.MySqlConnection");
            GetType("MySql.Data.MySqlClient.MySqlConnectionStringBuilder");
            GetType("MySql.Data.MySqlClient.MySqlDbType");
#endif
        }
示例#8
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);
        }
示例#9
0
        /// <summary>
        /// 映射为实体对象:字典尝试。
        /// </summary>
        /// <param name="type">类型,为空则尝试失败。</param>
        /// <param name="result">输出结果。</param>
        /// <returns>返回是否尝试成功。</returns>
        protected virtual bool ToObject_Try_Dictionary(System.Type type, out object result)
        {
            result = null;

            DictionaryAddAction dictionaryAdd;

            if (type == null || type == typeof(object))
            {
                //默认类型
                var values = new Symbol.Collections.Generic.NameValueCollection <object>();
                result        = values;
                dictionaryAdd = values.Add;
            }
            else if (TypeExtensions.IsInheritFrom(type, typeof(Collections.Generic.NameValueCollection <object>)))
            {
                //继承关系
                var values = FastWrapper.CreateInstance <Collections.Generic.NameValueCollection <object> >(type, new object[0]);
                result        = values;
                dictionaryAdd = values.Add;
            }
            else if (TypeExtensions.IsInheritFrom(type, typeof(System.Collections.Generic.IDictionary <string, object>)))
            {
                //泛型
                var values = FastWrapper.CreateInstance <System.Collections.Generic.IDictionary <string, object> >(type, new object[0]);
                result        = values;
                dictionaryAdd = (key, value) => {
                    if (values.TryGetValue(key, out object p))
                    {
                        values[key] = value;
                    }
                    else
                    {
                        values.Add(key, value);
                    }
                };
            }
            else if (TypeExtensions.IsInheritFrom(type, typeof(System.Collections.IDictionary)))
            {
                //非泛型
                var values = FastWrapper.CreateInstance <System.Collections.IDictionary>(type, new object[0]);
                result        = values;
                dictionaryAdd = (key, value) => {
                    if (values.Contains(key))
                    {
                        values[key] = value;
                    }
                    else
                    {
                        values.Add(key, value);
                    }
                };
            }
            else
            {
                //剩下的没有可能性
                return(false);
            }

            for (int i = 0; i < FieldCount; i++)
            {
                string name  = GetName(i);
                object value = GetValue(i);
                if (TryParseJSON(value, out object jsonObject))
                {
                    value = jsonObject;
                }
                dictionaryAdd(string.IsNullOrEmpty(name) ? ("无列名" + i) : name, value);
            }
            return(true);
        }
示例#10
0
 /// <summary>
 /// 创建Sorter实例
 /// </summary>
 public Sorter()
 {
     _values = new Collections.Generic.NameValueCollection <string>();
 }
 /// <summary>
 /// 创建ConnectionOptions实例。
 /// </summary>
 public ConnectionOptions()
 {
     _vars = new Collections.Generic.NameValueCollection <object>();
 }