public object GetValue(int i)
        {
            if (!this.isOpen)
            {
                throw new Exception("No current query in data reader");
            }
            if (i >= this.fields.Length)
            {
                throw new IndexOutOfRangeException();
            }
            MySqlValue fieldValue = this.GetFieldValue(i, false);

            if (fieldValue.IsNull)
            {
                return(DBNull.Value);
            }
            if (!(fieldValue is MySqlDateTime))
            {
                return(fieldValue.ValueAsObject);
            }
            MySqlDateTime mySqlDateTime = (MySqlDateTime)fieldValue;

            if (!mySqlDateTime.IsValidDateTime && this.connection.Settings.ConvertZeroDateTime)
            {
                return(DateTime.MinValue);
            }
            if (this.connection.Settings.AllowZeroDateTime)
            {
                return(fieldValue);
            }
            return(mySqlDateTime.GetDateTime());
        }
Exemple #2
0
        public void GetDateTimeForInvalidDate()
        {
            var msdt = new MySqlDateTime();

            Assert.False(msdt.IsValidDateTime);
            Assert.Throws <MySqlConversionException>(() => msdt.GetDateTime());
        }
Exemple #3
0
        public void GetDateTime()
        {
            var msdt = new MySqlDateTime(2018, 6, 9, 12, 34, 56, 123456);

            Assert.True(msdt.IsValidDateTime);
            var dt = msdt.GetDateTime();

            Assert.Equal(new DateTime(2018, 6, 9, 12, 34, 56, 123).AddTicks(4560), dt);
        }
Exemple #4
0
 public DateTime GetDateTime( )
 {
     try {
         return(datum.GetDateTime( ));
     }
     catch {
         return(DateTime.MinValue);
     }
 }
Exemple #5
0
        public static string conv_mysql_datetime_2_string(IDataReader reader, string feld)
        {
            MySqlDateTime ob = (MySqlDateTime)reader.GetValue(reader.GetOrdinal(feld));

            string ausgabe = "";

            if (ob.Year < 1)
            {
                ausgabe = "0000-00-00 00:00:00";
            }
            else
            {
                ausgabe = ob.GetDateTime().ToString("yyyy-MM-dd HH:mm:ss");
            }

            return(ausgabe);
        }
Exemple #6
0
        /// <summary>
        /// Gets the value of the specified column in its native format.
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public override object GetValue(int i)
        {
            if (!isOpen)
            {
                Throw(new Exception("No current query in data reader"));
            }
            if (i >= FieldCount)
            {
                Throw(new IndexOutOfRangeException());
            }

            IMySqlValue val = GetFieldValue(i, false);

            if (val.IsNull)
            {
                return(DBNull.Value);
            }

            // if the column is a date/time, then we return a MySqlDateTime
            // so .ToString() will print '0000-00-00' correctly
            if (val is MySqlDateTime)
            {
                MySqlDateTime dt = (MySqlDateTime)val;
                if (!dt.IsValidDateTime && connection.Settings.ConvertZeroDateTime)
                {
                    return(DateTime.MinValue);
                }
                else if (connection.Settings.AllowZeroDateTime)
                {
                    return(val);
                }
                else
                {
                    return(dt.GetDateTime());
                }
            }

            if (val is MySqlJson)
            {
                return(new JsonObject(val.Value.ToString()));
            }

            return(val.Value);
        }
Exemple #7
0
        public override object ConvertDbValue(object sqlValue, string dataType)
        {
            if (sqlValue != null && sqlValue.GetType() == typeof(MySqlDateTime))
            {
                MySqlDateTime sql_date_time = (MySqlDateTime)sqlValue;
                if (sql_date_time.IsValidDateTime)
                {
                    return(ConvertDateTime(dataType, sql_date_time.GetDateTime()));
                }

                if (dataType == "DATE" || dataType == "NEWDATE")
                {
                    return("0000-00-00");
                }
                else
                {
                    return("0000-00-00 00:00:00");
                }
            }

            return(base.ConvertDbValue(sqlValue, dataType));
        }
Exemple #8
0
        /// <summary>
        /// Gets the value of the specified column in its native format.
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public object GetValue(int i)
        {
            if (!isOpen)
            {
                throw new Exception("No current query in data reader");
            }
            if (i >= fields.Length)
            {
                throw new IndexOutOfRangeException();
            }

            MySqlValue val = GetFieldValue(i);

            if (val.IsNull)
            {
                return(DBNull.Value);
            }

            // if the column is a date/time, then we return a MySqlDateTime
            // so .ToString() will print '0000-00-00' correctly
            if (val is MySqlDateTime)
            {
                MySqlDateTime dt = (MySqlDateTime)val;
                if (!dt.IsValidDateTime && connection.Settings.ConvertZeroDateTime)
                {
                    return(DateTime.MinValue);
                }
                else if (connection.Settings.AllowZeroDateTime)
                {
                    return(val);
                }
                else
                {
                    return(dt.GetDateTime());
                }
            }

            return(val.ValueAsObject);
        }
        public DateTime GetDateTime(int index)
        {
            MySqlValue fieldValue = this.GetFieldValue(index, true);

            if (fieldValue is MySqlDateTime)
            {
                MySqlDateTime mySqlDateTime = (MySqlDateTime)fieldValue;
                if (this.connection.Settings.ConvertZeroDateTime && !mySqlDateTime.IsValidDateTime)
                {
                    return(DateTime.MinValue);
                }
                return(mySqlDateTime.GetDateTime());
            }
            else
            {
                if (!(fieldValue is MySqlString))
                {
                    throw new NotSupportedException("Unable to convert from type " + fieldValue.GetType().ToString() + " to DateTime");
                }
                MySqlDateTime mySqlDateTime2 = new MySqlDateTime(MySqlDbType.Datetime);
                mySqlDateTime2 = mySqlDateTime2.ParseMySql(this.GetString(index), true);
                return(mySqlDateTime2.GetDateTime());
            }
        }
Exemple #10
0
        /// <include file='docs/MySqlDataReader.xml' path='docs/GetDateTime/*'/>
        public DateTime GetDateTime(int index)
        {
            MySqlValue val = GetFieldValue(index);

            if (val is MySqlDateTime)
            {
                MySqlDateTime dt = (MySqlDateTime)val;
                if (connection.Settings.ConvertZeroDateTime && !dt.IsValidDateTime)
                {
                    return(DateTime.MinValue);
                }
                else
                {
                    return(dt.GetDateTime());
                }
            }
            else if (val is MySqlString)
            {
                MySqlDateTime d = new MySqlDateTime(MySqlDbType.Datetime);
                d = d.ParseMySql((val as MySqlString).Value, true);
                return(d.GetDateTime());
            }
            throw new NotSupportedException("Unable to convert from type " + val.GetType().ToString() + " to DateTime");
        }
Exemple #11
0
        /// <summary>
        /// Converts a value of a specified MySQL DB type to PHP value.
        /// </summary>
        /// <param name="dataType">MySQL DB data type.</param>
        /// <param name="sqlValue">The value.</param>
        /// <returns>PHP value.</returns>
        private static object ConvertDbValue(string dataType, object sqlValue)
        {
            if (sqlValue == null || sqlValue.GetType() == typeof(string))
            {
                return(sqlValue);
            }

            if (sqlValue.GetType() == typeof(double))
            {
                return(Core.Convert.DoubleToString((double)sqlValue));
            }

            if (sqlValue == System.DBNull.Value)
            {
                return(null);
            }

            if (sqlValue.GetType() == typeof(int))
            {
                return(((int)sqlValue).ToString());
            }

            if (sqlValue.GetType() == typeof(uint))
            {
                return(((uint)sqlValue).ToString());
            }

            if (sqlValue.GetType() == typeof(bool))
            {
                return((bool)sqlValue ? "1" : "0");
            }

            if (sqlValue.GetType() == typeof(byte))
            {
                return(((byte)sqlValue).ToString());
            }

            if (sqlValue.GetType() == typeof(sbyte))
            {
                return(((sbyte)sqlValue).ToString());
            }

            if (sqlValue.GetType() == typeof(short))
            {
                return(((short)sqlValue).ToString());
            }

            if (sqlValue.GetType() == typeof(ushort))
            {
                return(((ushort)sqlValue).ToString());
            }

            if (sqlValue.GetType() == typeof(float))
            {
                return(Core.Convert.DoubleToString((float)sqlValue));
            }

            if (sqlValue.GetType() == typeof(DateTime))
            {
                return(ConvertDateTime(dataType, (DateTime)sqlValue));
            }

            if (sqlValue.GetType() == typeof(long))
            {
                return(((long)sqlValue).ToString());
            }

            if (sqlValue.GetType() == typeof(ulong))
            {
                return(((ulong)sqlValue).ToString());
            }

            if (sqlValue.GetType() == typeof(TimeSpan))
            {
                return(((TimeSpan)sqlValue).ToString());
            }

            if (sqlValue.GetType() == typeof(decimal))
            {
                return(((decimal)sqlValue).ToString());
            }

            if (sqlValue.GetType() == typeof(byte[]))
            {
                return(new PhpBytes((byte[])sqlValue));
            }

            //MySqlDateTime sql_date_time = sqlValue as MySqlDateTime;
            if (sqlValue.GetType() == typeof(MySqlDateTime))
            {
                MySqlDateTime sql_date_time = (MySqlDateTime)sqlValue;
                if (sql_date_time.IsValidDateTime)
                {
                    return(ConvertDateTime(dataType, sql_date_time.GetDateTime()));
                }

                if (dataType == "DATE" || dataType == "NEWDATE")
                {
                    return("0000-00-00");
                }
                else
                {
                    return("0000-00-00 00:00:00");
                }
            }

            Debug.Fail("Unexpected DB field type " + sqlValue.GetType() + ".");
            return(sqlValue.ToString());
        }