コード例 #1
0
        private Boolean TryConvert(Object p_value, EDBPrimitive target_type, out Object p_converted)
        {
            if (p_value == null || p_value is DBNull)
            {
                p_converted = null;
                return(true);
            }

            try
            {
                p_converted = Convert.ChangeType(p_value, target_type.ToType());
                return(true);
            }
            catch (MySqlConversionException)
            {
                if (p_value is MySqlDateTime && !((MySqlDateTime)p_value).IsValidDateTime)
                {
                    Nullable <DateTime> value = null;
                    p_converted = value;
                    return(true);
                }
                p_converted = false;
                return(false);
            }
            catch
            {
                p_converted = null;
                return(false);
            }
        }
コード例 #2
0
        /// <summary>
        /// Constructs a new primitive store attribute with the given primitive type, column name and auto conversion option.
        /// </summary>
        /// <param name="primitive_type">The primitive type to store.</param>
        /// <param name="p_column_name">The name of the column to store the data in.</param>
        /// <param name="auto_convert">Whether the field this attribute is associated with should be automatically converted to the given primitive type.</param>
        /// <param name="store_options">Specifies how the data should be loaded.</param>
        public CPrimitiveAttribute(EDBPrimitive primitive_type, String p_column_name, Boolean auto_convert, EStoreOptions store_options)
        {
            switch (store_options)
            {
            case EStoreOptions.none:
                throw new ArgumentException("ELoadOptions.none is not a valid option.");

            case EStoreOptions.direct_assignment:
            case EStoreOptions.user_callback:
                break;

            default:
                throw new ArgumentException(store_options.ToString() + " is not a valid option.");
            }
            switch (primitive_type)
            {
            case EDBPrimitive.none:
                throw new ArgumentException("EDBPrimitive.none is not a valid primitive-type to store data with.");

            case EDBPrimitive.binary:
            case EDBPrimitive.bit:
            case EDBPrimitive.boolean:
            case EDBPrimitive.@char:
            case EDBPrimitive.@decimal:
            case EDBPrimitive.@double:
            case EDBPrimitive.@float:
            case EDBPrimitive.int16:
            case EDBPrimitive.int24:
            case EDBPrimitive.int32:
            case EDBPrimitive.int64:
            case EDBPrimitive.int8:
            case EDBPrimitive.uint16:
            case EDBPrimitive.uint24:
            case EDBPrimitive.uint32:
            case EDBPrimitive.uint64:
            case EDBPrimitive.uint8:
            case EDBPrimitive.varbinary:
            case EDBPrimitive.varchar:
            case EDBPrimitive.date:
            case EDBPrimitive.datetime:
            case EDBPrimitive.time:
            case EDBPrimitive.timestamp:
            case EDBPrimitive.year:
            case EDBPrimitive.text:
            case EDBPrimitive.infer:
                break;

            default:
                throw new ArgumentException(primitive_type.ToString() + " is not a valid primitive-type to store data with.");
            }

            m_primitive_type = primitive_type;
            m_p_column_name  = p_column_name;
            m_auto_convert   = auto_convert;
            m_store_options  = store_options;
        }
コード例 #3
0
 /// <summary>
 /// Constructs new primitive store options with the given settings.
 /// </summary>
 /// <param name="p_column_name">The name of the column in the table.</param>
 /// <param name="primitive_type">The type of the database primitive.</param>
 /// <param name="p_field">The field holding the value that maps to the primitive.</param>
 /// <param name="load_options">Specified the way the primitive data should be loaded.</param>
 internal SStorePrimitiveOptions(String p_column_name, EDBPrimitive primitive_type, FieldInfo p_field, MethodInfo p_method, Boolean auto_convert, EStoreOptions load_options)
     : this()
 {
     m_p_column_name  = p_column_name;
     m_auto_convert   = auto_convert;
     m_primitive_type = primitive_type;
     m_p_field        = p_field;
     m_p_method       = p_method;
     m_store_options  = load_options;
     m_is_identity    = false;
 }
コード例 #4
0
        public static Boolean RequiresPrecisionAndScale(this EDBPrimitive primtive_type)
        {
            switch (primtive_type)
            {
            case EDBPrimitive.@decimal:
                return(true);

            default:
                return(false);
            }
        }
コード例 #5
0
        public static Boolean RequiresLength(this EDBPrimitive primitive_type)
        {
            switch (primitive_type)
            {
            default:
                return(true);

                /*default:
                 *  return false;*/
            }
        }
コード例 #6
0
        internal static Object MakePrimitiveType(String p_value, EDBPrimitive db_primitive)
        {
            switch (db_primitive)
            {
            case EDBPrimitive.binary:
            case EDBPrimitive.varbinary:
                return(p_value.FromHexToBytes());

            case EDBPrimitive.@char:
            case EDBPrimitive.varchar:
            case EDBPrimitive.text:
                return(p_value);

            case EDBPrimitive.int8:
                return(SByte.Parse(p_value));

            case EDBPrimitive.int16:
                return(Int16.Parse(p_value));

            case EDBPrimitive.int24:
            case EDBPrimitive.int32:
            case EDBPrimitive.year:
                return(Int32.Parse(p_value));

            case EDBPrimitive.int64:
                return(Int64.Parse(p_value));

            case EDBPrimitive.uint8:
                return(Byte.Parse(p_value));

            case EDBPrimitive.uint16:
                return(UInt16.Parse(p_value));

            case EDBPrimitive.uint24:
            case EDBPrimitive.uint32:
                return(UInt32.Parse(p_value));

            case EDBPrimitive.uint64:
                return(UInt64.Parse(p_value));

            case EDBPrimitive.boolean:
                return(Boolean.Parse(p_value));

            case EDBPrimitive.date:
            case EDBPrimitive.datetime:
            case EDBPrimitive.time:
            case EDBPrimitive.timestamp:
                return(DateTime.Parse(p_value));

            default:
                throw new Exception("Cannot create a primitive type from " + db_primitive.ToString());
            }
        }
コード例 #7
0
 /// <summary>
 /// Constructs a new primitive store attribute with the given primitive type, column name and auto conversion option.
 /// </summary>
 /// <param name="primitive_type">The primitive type to store.</param>
 /// <param name="p_column_name">The name of the column to store the data in.</param>
 /// <param name="store_options">Specifies how the data should be loaded.</param>
 public CPrimitiveAttribute(EDBPrimitive primitive_type, String p_column_name, EStoreOptions store_options)
     : this(primitive_type, p_column_name, true, store_options)
 {
 }
コード例 #8
0
 /// <summary>
 /// Constructs a new primitive store attribute with the given primitive type and column name and auto conversion turned off.
 /// </summary>
 /// <param name="primitive_type">The primitive type to store.</param>
 /// <param name="p_column_name">The name of the column to store the data in.</param>
 public CPrimitiveAttribute(EDBPrimitive primitive_type, String p_column_name)
     : this(primitive_type, p_column_name, true, EStoreOptions.direct_assignment)
 {
 }
コード例 #9
0
        public static Type ToType(this EDBPrimitive primtive_type)
        {
            switch (primtive_type)
            {
            case EDBPrimitive.binary:     // Byte[]
            case EDBPrimitive.varbinary:  // Byte[]
                return(typeof(Byte[]));

            case EDBPrimitive.boolean:     // boolean
                return(typeof(Boolean));

            case EDBPrimitive.@decimal:     // decimal
                return(typeof(Decimal));

            case EDBPrimitive.@double:     // double
                return(typeof(Double));

            case EDBPrimitive.@float:     // single
                return(typeof(Single));

            case EDBPrimitive.int8:     // sbyte
                return(typeof(SByte));

            case EDBPrimitive.int16:     // int16
                return(typeof(Int16));

            case EDBPrimitive.int24:     // int32
            case EDBPrimitive.int32:     // int32
                return(typeof(Int32));

            case EDBPrimitive.int64:     // int64
                return(typeof(Int64));

            case EDBPrimitive.uint8:     // Byte
                return(typeof(Byte));

            case EDBPrimitive.uint16:     // uint16
                return(typeof(UInt16));

            case EDBPrimitive.uint24:     // uint32
            case EDBPrimitive.uint32:     // uint32
                return(typeof(UInt32));

            case EDBPrimitive.uint64:  // uint64
            case EDBPrimitive.bit:     // uint64
                return(typeof(UInt64));

            case EDBPrimitive.@char:     // String
            case EDBPrimitive.varchar:   // String
            case EDBPrimitive.text:
                return(typeof(String));

            case EDBPrimitive.time:
            case EDBPrimitive.timestamp:
            case EDBPrimitive.date:
            case EDBPrimitive.datetime:
                return(typeof(DateTime));

            default:
                return(null);
            }
        }
コード例 #10
0
        public static Boolean ToDBPrimitive(this Type p_type, out EDBPrimitive result)
        {
            Type p_nullable;

            if (p_type.GetNullableType(out p_nullable))
            {
                p_type = p_nullable;
            }

            if (p_type == typeof(Byte[]))
            {
                result = EDBPrimitive.varbinary;
                return(true);
            }
            else if (p_type == typeof(Boolean))
            {
                result = EDBPrimitive.boolean;
                return(true);
            }
            else if (p_type == typeof(Decimal))
            {
                result = EDBPrimitive.@decimal;
                return(true);
            }
            else if (p_type == typeof(Double))
            {
                result = EDBPrimitive.@double;
                return(true);
            }
            else if (p_type == typeof(Single))
            {
                result = EDBPrimitive.@float;
                return(true);
            }
            else if (p_type == typeof(SByte))
            {
                result = EDBPrimitive.int8;
                return(true);
            }
            else if (p_type == typeof(Int16))
            {
                result = EDBPrimitive.int16;
                return(true);
            }
            else if (p_type == typeof(Int32))
            {
                result = EDBPrimitive.int32;
                return(true);
            }
            else if (p_type == typeof(Int64))
            {
                result = EDBPrimitive.int64;
                return(true);
            }
            else if (p_type == typeof(Byte))
            {
                result = EDBPrimitive.uint8;
                return(true);
            }
            else if (p_type == typeof(UInt16))
            {
                result = EDBPrimitive.uint16;
                return(true);
            }
            else if (p_type == typeof(UInt32))
            {
                result = EDBPrimitive.uint32;
                return(true);
            }
            else if (p_type == typeof(UInt64))
            {
                result = EDBPrimitive.uint64;
                return(true);
            }
            else if (p_type == typeof(String))
            {
                result = EDBPrimitive.varchar;
                return(true);
            }
            else if (p_type == typeof(DateTime))
            {
                result = EDBPrimitive.datetime;
                return(true);
            }
            else if (p_type == typeof(TimeSpan))
            {
                result = EDBPrimitive.time;
                return(true);
            }
            result = EDBPrimitive.none;
            return(false);
        }
コード例 #11
0
        public static DbType ToDbType(this EDBPrimitive primitive_type)
        {
            switch (primitive_type)
            {
            case EDBPrimitive.binary:
            case EDBPrimitive.varbinary:
                return(DbType.Binary);

            case EDBPrimitive.bit:
                return(DbType.UInt64);

            case EDBPrimitive.boolean:
                return(DbType.Boolean);

            case EDBPrimitive.@char:
                return(DbType.StringFixedLength);

            case EDBPrimitive.date:
                return(DbType.Date);

            case EDBPrimitive.datetime:
                return(DbType.DateTime);

            case EDBPrimitive.@decimal:
                return(DbType.Decimal);

            case EDBPrimitive.@double:
                return(DbType.Double);

            case EDBPrimitive.@float:
                return(DbType.Single);

            case EDBPrimitive.int16:
                return(DbType.Int16);

            case EDBPrimitive.int32:
                return(DbType.Int32);

            case EDBPrimitive.int64:
                return(DbType.Int64);

            case EDBPrimitive.int8:
                return(DbType.SByte);

            case EDBPrimitive.time:
                return(DbType.Time);

            case EDBPrimitive.timestamp:
                return(DbType.DateTime);

            case EDBPrimitive.uint16:
                return(DbType.UInt16);

            case EDBPrimitive.uint24:
                return(DbType.UInt32);

            case EDBPrimitive.uint32:
                return(DbType.UInt32);

            case EDBPrimitive.uint64:
                return(DbType.UInt64);

            case EDBPrimitive.uint8:
                return(DbType.Byte);

            case EDBPrimitive.varchar:
            case EDBPrimitive.text:
                return(DbType.String);

            case EDBPrimitive.year:
                return(DbType.Int32);

            default:
                throw new InvalidCastException("Cannot convert DBWizard's \"" + primitive_type.ToString() + "\" to a " + typeof(DbType).FullName + ".");
            }
        }
コード例 #12
0
        protected override void PrepareCommand(DbCommand p_cmd)
        {
            StringBuilder p_cmd_text = new StringBuilder();

            if (m_on_duplicate_key_update && m_p_data_base.m_driver_type == EDriverType.mssql)
            {
                // ms sql specific
                p_cmd_text.Append("MERGE INTO ");
                p_cmd_text.Append(m_p_table_name);
                p_cmd_text.Append(" As __DB_WIZARD_MERGE_TARGET__ USING (SELECT ");
                // get all values that correspond to keys
                for (Int32 i = 0; i < m_p_values.Count; ++i)
                {
                    ReadOnlyCollection <Object> p_values = m_p_values[i];
                    for (Int32 j = 0; j < p_values.Count; ++j)
                    {
                        p_cmd_text.Append("@insertval_");
                        p_cmd_text.Append(i.ToString());
                        p_cmd_text.Append('_');
                        p_cmd_text.Append(j.ToString());

                        if ((j + 1) < p_values.Count)
                        {
                            p_cmd_text.Append(',');
                        }
                    }
                    if ((i + 1) < m_p_values.Count)
                    {
                        p_cmd_text.Append(" UNION ALL SELECT ");
                    }
                }
                p_cmd_text.Append(") As __DB_WIZARD_MERGE_SOURCE__ (");
                for (Int32 i = 0; i < m_p_column_names.Count; ++i)
                {
                    p_cmd_text.Append(m_p_column_names[i]);
                    if ((i + 1) < m_p_column_names.Count)
                    {
                        p_cmd_text.Append(',');
                    }
                }
                p_cmd_text.Append(")ON(");
                Boolean is_first = true;
                if (m_p_keys.Count > 0)
                {
                    for (Int32 i = 0; i < m_p_keys.Count; ++i)
                    {
                        if (m_p_column_names.Contains(m_p_keys[i]))
                        {
                            if (!is_first)
                            {
                                p_cmd_text.Append(" AND ");
                            }
                            is_first = false;
                            p_cmd_text.Append("__DB_WIZARD_MERGE_TARGET__.");
                            p_cmd_text.Append(m_p_keys[i]);
                            p_cmd_text.Append(" = __DB_WIZARD_MERGE_SOURCE__.");
                            p_cmd_text.Append(m_p_keys[i]);
                        }
                    }
                }
                else
                {
                    for (Int32 i = 0; i < m_p_column_names.Count; ++i)
                    {
                        if (!is_first)
                        {
                            p_cmd_text.Append(" AND ");
                        }
                        is_first = false;
                        p_cmd_text.Append("__DB_WIZARD_MERGE_TARGET__.");
                        p_cmd_text.Append(m_p_column_names[i]);
                        p_cmd_text.Append(" = __DB_WIZARD_MERGE_SOURCE__.");
                        p_cmd_text.Append(m_p_column_names[i]);
                    }
                }
                p_cmd_text.Append(")WHEN MATCHED THEN UPDATE SET ");
                for (Int32 i = 0; i < m_p_column_names.Count; ++i)
                {
                    if (m_p_keys.Contains(m_p_column_names[i]))
                    {
                        continue;
                    }

                    p_cmd_text.Append("__DB_WIZARD_MERGE_TARGET__.");
                    p_cmd_text.Append(m_p_column_names[i]);
                    p_cmd_text.Append('=');
                    p_cmd_text.Append("__DB_WIZARD_MERGE_SOURCE__.");
                    p_cmd_text.Append(m_p_column_names[i]);

                    if ((i + 1) < m_p_column_names.Count)
                    {
                        p_cmd_text.Append(',');
                    }
                }
                p_cmd_text.Append(" WHEN NOT MATCHED THEN INSERT (");
                for (Int32 i = 0; i < m_p_column_names.Count; ++i)
                {
                    if (m_p_keys.Contains(m_p_column_names[i]))
                    {
                        continue;
                    }

                    p_cmd_text.Append(m_p_column_names[i]);
                    if ((i + 1) < m_p_column_names.Count)
                    {
                        p_cmd_text.Append(',');
                    }
                }
                p_cmd_text.Append(")VALUES(");
                for (Int32 i = 0; i < m_p_column_names.Count; ++i)
                {
                    if (m_p_keys.Contains(m_p_column_names[i]))
                    {
                        continue;
                    }

                    p_cmd_text.Append("__DB_WIZARD_MERGE_SOURCE__.");
                    p_cmd_text.Append(m_p_column_names[i]);
                    if ((i + 1) < m_p_column_names.Count)
                    {
                        p_cmd_text.Append(',');
                    }
                }
                p_cmd_text.Append(");");
            }
            else
            {
                p_cmd_text.Append("INSERT INTO ");
                p_cmd_text.Append(m_p_table_name);
                p_cmd_text.Append('(');
                for (Int32 i = 0; i < m_p_column_names.Count; ++i)
                {
                    p_cmd_text.Append(m_p_column_names[i]);
                    if ((i + 1) < m_p_column_names.Count)
                    {
                        p_cmd_text.Append(',');
                    }
                }
                p_cmd_text.Append(") VALUES ");
                for (Int32 i = 0; i < m_p_values.Count; ++i)
                {
                    ReadOnlyCollection <Object> p_values = m_p_values[i];
                    p_cmd_text.Append('(');
                    for (Int32 j = 0; j < p_values.Count; ++j)
                    {
                        p_cmd_text.Append("@insertval_");
                        p_cmd_text.Append(i.ToString());
                        p_cmd_text.Append('_');
                        p_cmd_text.Append(j.ToString());
                        if ((j + 1) < p_values.Count)
                        {
                            p_cmd_text.Append(',');
                        }
                    }
                    p_cmd_text.Append(')');
                    if ((i + 1) < m_p_values.Count)
                    {
                        p_cmd_text.Append(',');
                    }
                }
                if (m_on_duplicate_key_update) // mysql specific
                {
                    p_cmd_text.Append(" ON DUPLICATE KEY UPDATE ");
                    for (Int32 i = 0; i < m_p_column_names.Count; ++i)
                    {
                        p_cmd_text.Append(m_p_column_names[i]);
                        p_cmd_text.Append("=VALUES(");
                        p_cmd_text.Append(m_p_column_names[i]);
                        p_cmd_text.Append(')');
                        if ((i + 1) < m_p_column_names.Count)
                        {
                            p_cmd_text.Append(',');
                        }
                    }
                }
            }

            p_cmd.CommandText = p_cmd_text.ToString();
            for (Int32 i = 0; i < m_p_values.Count; ++i)
            {
                ReadOnlyCollection <Object> p_values = m_p_values[i];
                for (Int32 j = 0; j < p_values.Count; ++j)
                {
                    EDBPrimitive primitive_type = _m_p_map.m_p_primitives_map[m_p_column_names[j]].m_primitive_type;
                    DbParameter  p_param        = p_cmd.CreateParameter();
                    p_param.ParameterName = "@insertval_" + i.ToString() + '_' + j.ToString();
                    p_param.Value         = p_values[j];
                    p_param.DbType        = primitive_type.ToDbType();
                    if (primitive_type.RequiresLength())
                    {
                        if (p_values[j] is Array)
                        {
                            p_param.Size = ((Array)p_values[j]).Length;
                            if (p_param.Size == 0 && m_p_data_base.m_driver_type == EDriverType.mssql)
                            {
                                p_param.Size = 1;
                            }
                        }
                        else if (p_values[j] == null)
                        {
                            p_param.Size = 1;
                        }
                        else
                        {
                            p_param.Size = p_values[j].ToString().Length;
                        }
                    }
                    if (primitive_type.RequiresPrecisionAndScale())
                    {
                        if (m_p_data_base.m_driver_type == EDriverType.mssql)
                        {
                            ((SqlParameter)p_param).Precision = 18;
                            ((SqlParameter)p_param).Scale     = 0;
                        }
                        else if (m_p_data_base.m_driver_type == EDriverType.mysql)
                        {
                            ((MySqlParameter)p_param).Precision = 18;
                            ((MySqlParameter)p_param).Scale     = 0;
                        }
                    }
                    p_cmd.Parameters.Add(p_param);
                }
            }
        }