public void Interval() { var expectedNpgsqlInterval = new NpgsqlTimeSpan(1, 2, 3, 4, 5); var expectedTimeSpan = new TimeSpan(1, 2, 3, 4, 5); using (var cmd = new NpgsqlCommand("SELECT @p1, @p2", Conn)) { var p1 = new NpgsqlParameter("p1", NpgsqlDbType.Interval); var p2 = new NpgsqlParameter("p2", expectedNpgsqlInterval); Assert.That(p2.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Interval)); Assert.That(p2.DbType, Is.EqualTo(DbType.Object)); cmd.Parameters.Add(p1); cmd.Parameters.Add(p2); p1.Value = expectedNpgsqlInterval; using (var reader = cmd.ExecuteReader()) { reader.Read(); // Regular type (TimeSpan) Assert.That(reader.GetFieldType(0), Is.EqualTo(typeof(TimeSpan))); Assert.That(reader.GetTimeSpan(0), Is.EqualTo(expectedTimeSpan)); Assert.That(reader.GetFieldValue <TimeSpan>(0), Is.EqualTo(expectedTimeSpan)); Assert.That(reader[0], Is.EqualTo(expectedTimeSpan)); Assert.That(reader.GetValue(0), Is.EqualTo(expectedTimeSpan)); // Provider-specific type (NpgsqlInterval) Assert.That(reader.GetInterval(0), Is.EqualTo(expectedNpgsqlInterval)); Assert.That(reader.GetProviderSpecificFieldType(0), Is.EqualTo(typeof(NpgsqlTimeSpan))); Assert.That(reader.GetProviderSpecificValue(0), Is.EqualTo(expectedNpgsqlInterval)); Assert.That(reader.GetFieldValue <NpgsqlTimeSpan>(0), Is.EqualTo(expectedNpgsqlInterval)); } } }
public async Task Interval() { using var conn = await OpenConnectionAsync(); var expectedNpgsqlInterval = new NpgsqlTimeSpan(1, 2, 3, 4, 5); var expectedTimeSpan = new TimeSpan(1, 2, 3, 4, 5); using var cmd = new NpgsqlCommand("SELECT @p1, @p2, @p3", conn); var p1 = new NpgsqlParameter("p1", NpgsqlDbType.Interval); var p2 = new NpgsqlParameter("p2", expectedTimeSpan); var p3 = new NpgsqlParameter("p3", expectedNpgsqlInterval); Assert.That(p2.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Interval)); Assert.That(p2.DbType, Is.EqualTo(DbType.Object)); Assert.That(p3.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Interval)); Assert.That(p3.DbType, Is.EqualTo(DbType.Object)); cmd.Parameters.Add(p1); cmd.Parameters.Add(p2); cmd.Parameters.Add(p3); p1.Value = expectedNpgsqlInterval; using var reader = await cmd.ExecuteReaderAsync(); reader.Read(); // Regular type (TimeSpan) Assert.That(reader.GetFieldType(0), Is.EqualTo(typeof(TimeSpan))); Assert.That(reader.GetTimeSpan(0), Is.EqualTo(expectedTimeSpan)); Assert.That(reader.GetFieldValue <TimeSpan>(0), Is.EqualTo(expectedTimeSpan)); Assert.That(reader[0], Is.EqualTo(expectedTimeSpan)); Assert.That(reader.GetValue(0), Is.EqualTo(expectedTimeSpan)); // Provider-specific type (NpgsqlInterval) Assert.That(reader.GetInterval(0), Is.EqualTo(expectedNpgsqlInterval)); Assert.That(reader.GetProviderSpecificFieldType(0), Is.EqualTo(typeof(NpgsqlTimeSpan))); Assert.That(reader.GetProviderSpecificValue(0), Is.EqualTo(expectedNpgsqlInterval)); Assert.That(reader.GetFieldValue <NpgsqlTimeSpan>(0), Is.EqualTo(expectedNpgsqlInterval)); }
public void Interval() { var expectedNpgsqlInterval = new NpgsqlTimeSpan(1, 2, 3, 4, 5); var expectedTimeSpan = new TimeSpan(1, 2, 3, 4, 5); using (var cmd = new NpgsqlCommand("SELECT '1 days 2 hours 3 minutes 4 seconds 5 milliseconds'::INTERVAL", Conn)) using (var reader = cmd.ExecuteReader()) { reader.Read(); // Regular type (TimeSpan) Assert.That(reader.GetFieldType(0), Is.EqualTo(typeof(TimeSpan))); Assert.That(reader.GetTimeSpan(0), Is.EqualTo(expectedTimeSpan)); Assert.That(reader.GetFieldValue <TimeSpan>(0), Is.EqualTo(expectedTimeSpan)); Assert.That(reader[0], Is.EqualTo(expectedTimeSpan)); Assert.That(reader.GetValue(0), Is.EqualTo(expectedTimeSpan)); // Provider-specific type (NpgsqlInterval) Assert.That(reader.GetInterval(0), Is.EqualTo(expectedNpgsqlInterval)); Assert.That(reader.GetProviderSpecificFieldType(0), Is.EqualTo(typeof(NpgsqlTimeSpan))); Assert.That(reader.GetProviderSpecificValue(0), Is.EqualTo(expectedNpgsqlInterval)); Assert.That(reader.GetFieldValue <NpgsqlTimeSpan>(0), Is.EqualTo(expectedNpgsqlInterval)); } }
void INpgsqlSimpleTypeHandler <NpgsqlTimeSpan> .Write(NpgsqlTimeSpan value, NpgsqlWriteBuffer buf, NpgsqlParameter?parameter) => _bclHandler.Write(value, buf, parameter);
int INpgsqlSimpleTypeHandler <NpgsqlTimeSpan> .ValidateAndGetLength(NpgsqlTimeSpan value, NpgsqlParameter?parameter) => _bclHandler.ValidateAndGetLength(value, parameter);
public void Interval() { var expectedNpgsqlInterval = new NpgsqlTimeSpan(1, 2, 3, 4, 5); var expectedTimeSpan = new TimeSpan(1, 2, 3, 4, 5); using (var cmd = new NpgsqlCommand("SELECT @p1, @p2", Conn)) { var p1 = new NpgsqlParameter("p1", NpgsqlDbType.Interval); var p2 = new NpgsqlParameter("p2", expectedNpgsqlInterval); Assert.That(p2.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Interval)); Assert.That(p2.DbType, Is.EqualTo(DbType.Object)); cmd.Parameters.Add(p1); cmd.Parameters.Add(p2); p1.Value = expectedNpgsqlInterval; using (var reader = cmd.ExecuteReader()) { reader.Read(); // Regular type (TimeSpan) Assert.That(reader.GetFieldType(0), Is.EqualTo(typeof (TimeSpan))); Assert.That(reader.GetTimeSpan(0), Is.EqualTo(expectedTimeSpan)); Assert.That(reader.GetFieldValue<TimeSpan>(0), Is.EqualTo(expectedTimeSpan)); Assert.That(reader[0], Is.EqualTo(expectedTimeSpan)); Assert.That(reader.GetValue(0), Is.EqualTo(expectedTimeSpan)); // Provider-specific type (NpgsqlInterval) Assert.That(reader.GetInterval(0), Is.EqualTo(expectedNpgsqlInterval)); Assert.That(reader.GetProviderSpecificFieldType(0), Is.EqualTo(typeof (NpgsqlTimeSpan))); Assert.That(reader.GetProviderSpecificValue(0), Is.EqualTo(expectedNpgsqlInterval)); Assert.That(reader.GetFieldValue<NpgsqlTimeSpan>(0), Is.EqualTo(expectedNpgsqlInterval)); } } }
public void NpgsqlIntervalParse() { string input; NpgsqlTimeSpan test; input = "1 day"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(TimeSpan.FromDays(1).Ticks, test.TotalTicks, input); input = "2 days"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(TimeSpan.FromDays(2).Ticks, test.TotalTicks, input); input = "2 days 3:04:05"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(new TimeSpan(2, 3, 4, 5).Ticks, test.TotalTicks, input); input = "-2 days"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(TimeSpan.FromDays(-2).Ticks, test.TotalTicks, input); input = "-2 days -3:04:05"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(new TimeSpan(-2, -3, -4, -5).Ticks, test.TotalTicks, input); input = "-2 days -0:01:02"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(new TimeSpan(-2, 0, -1, -2).Ticks, test.TotalTicks, input); input = "2 days -12:00"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(new TimeSpan(2, -12, 0, 0).Ticks, test.TotalTicks, input); input = "1 mon"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(TimeSpan.FromDays(30).Ticks, test.TotalTicks, input); input = "2 mons"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(TimeSpan.FromDays(60).Ticks, test.TotalTicks, input); input = "1 mon -1 day"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(TimeSpan.FromDays(29).Ticks, test.TotalTicks, input); input = "1 mon -2 days"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(TimeSpan.FromDays(28).Ticks, test.TotalTicks, input); input = "-1 mon -2 days -3:04:05"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(new TimeSpan(-32, -3, -4, -5).Ticks, test.TotalTicks, input); input = "1 year"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(TimeSpan.FromDays(30 * 12).Ticks, test.TotalTicks, input); input = "2 years"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(TimeSpan.FromDays(30 * 24).Ticks, test.TotalTicks, input); input = "1 year -1 mon"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(TimeSpan.FromDays(30 * 11).Ticks, test.TotalTicks, input); input = "1 year -2 mons"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(TimeSpan.FromDays(30 * 10).Ticks, test.TotalTicks, input); input = "1 year -1 day"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(TimeSpan.FromDays(30 * 12 - 1).Ticks, test.TotalTicks, input); input = "1 year -2 days"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(TimeSpan.FromDays(30 * 12 - 2).Ticks, test.TotalTicks, input); input = "1 year -1 mon -1 day"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(TimeSpan.FromDays(30 * 11 - 1).Ticks, test.TotalTicks, input); input = "1 year -2 mons -2 days"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(TimeSpan.FromDays(30 * 10 - 2).Ticks, test.TotalTicks, input); input = "1 day 2:3:4.005"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(new TimeSpan(1, 2, 3, 4, 5).Ticks, test.TotalTicks, input); var testCulture = new CultureInfo("fr-FR"); Assert.AreEqual(",", testCulture.NumberFormat.NumberDecimalSeparator, "decimal seperator"); using (TestUtil.SetCurrentCulture(testCulture)) { input = "1 day 2:3:4.005"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(new TimeSpan(1, 2, 3, 4, 5).Ticks, test.TotalTicks, input); } }
public void NpgsqlIntervalConstructors() { NpgsqlTimeSpan test; test = new NpgsqlTimeSpan(); Assert.AreEqual(0, test.Months, "Months"); Assert.AreEqual(0, test.Days, "Days"); Assert.AreEqual(0, test.Hours, "Hours"); Assert.AreEqual(0, test.Minutes, "Minutes"); Assert.AreEqual(0, test.Seconds, "Seconds"); Assert.AreEqual(0, test.Milliseconds, "Milliseconds"); Assert.AreEqual(0, test.Microseconds, "Microseconds"); test = new NpgsqlTimeSpan(1234567890); Assert.AreEqual(0, test.Months, "Months"); Assert.AreEqual(0, test.Days, "Days"); Assert.AreEqual(0, test.Hours, "Hours"); Assert.AreEqual(2, test.Minutes, "Minutes"); Assert.AreEqual(3, test.Seconds, "Seconds"); Assert.AreEqual(456, test.Milliseconds, "Milliseconds"); Assert.AreEqual(456789, test.Microseconds, "Microseconds"); test = new NpgsqlTimeSpan(new TimeSpan(1, 2, 3, 4, 5)).JustifyInterval(); Assert.AreEqual(0, test.Months, "Months"); Assert.AreEqual(1, test.Days, "Days"); Assert.AreEqual(2, test.Hours, "Hours"); Assert.AreEqual(3, test.Minutes, "Minutes"); Assert.AreEqual(4, test.Seconds, "Seconds"); Assert.AreEqual(5, test.Milliseconds, "Milliseconds"); Assert.AreEqual(5000, test.Microseconds, "Microseconds"); test = new NpgsqlTimeSpan(3, 2, 1234567890); Assert.AreEqual(3, test.Months, "Months"); Assert.AreEqual(2, test.Days, "Days"); Assert.AreEqual(0, test.Hours, "Hours"); Assert.AreEqual(2, test.Minutes, "Minutes"); Assert.AreEqual(3, test.Seconds, "Seconds"); Assert.AreEqual(456, test.Milliseconds, "Milliseconds"); Assert.AreEqual(456789, test.Microseconds, "Microseconds"); test = new NpgsqlTimeSpan(1, 2, 3, 4); Assert.AreEqual(0, test.Months, "Months"); Assert.AreEqual(1, test.Days, "Days"); Assert.AreEqual(2, test.Hours, "Hours"); Assert.AreEqual(3, test.Minutes, "Minutes"); Assert.AreEqual(4, test.Seconds, "Seconds"); Assert.AreEqual(0, test.Milliseconds, "Milliseconds"); Assert.AreEqual(0, test.Microseconds, "Microseconds"); test = new NpgsqlTimeSpan(1, 2, 3, 4, 5); Assert.AreEqual(0, test.Months, "Months"); Assert.AreEqual(1, test.Days, "Days"); Assert.AreEqual(2, test.Hours, "Hours"); Assert.AreEqual(3, test.Minutes, "Minutes"); Assert.AreEqual(4, test.Seconds, "Seconds"); Assert.AreEqual(5, test.Milliseconds, "Milliseconds"); Assert.AreEqual(5000, test.Microseconds, "Microseconds"); test = new NpgsqlTimeSpan(1, 2, 3, 4, 5, 6); Assert.AreEqual(1, test.Months, "Months"); Assert.AreEqual(2, test.Days, "Days"); Assert.AreEqual(3, test.Hours, "Hours"); Assert.AreEqual(4, test.Minutes, "Minutes"); Assert.AreEqual(5, test.Seconds, "Seconds"); Assert.AreEqual(6, test.Milliseconds, "Milliseconds"); Assert.AreEqual(6000, test.Microseconds, "Microseconds"); test = new NpgsqlTimeSpan(1, 2, 3, 4, 5, 6, 7); Assert.AreEqual(14, test.Months, "Months"); Assert.AreEqual(3, test.Days, "Days"); Assert.AreEqual(4, test.Hours, "Hours"); Assert.AreEqual(5, test.Minutes, "Minutes"); Assert.AreEqual(6, test.Seconds, "Seconds"); Assert.AreEqual(7, test.Milliseconds, "Milliseconds"); Assert.AreEqual(7000, test.Microseconds, "Microseconds"); }
public void NpgsqlIntervalParse() { string input; NpgsqlTimeSpan test; input = "1 day"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(TimeSpan.FromDays(1).Ticks, test.TotalTicks, input); input = "2 days"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(TimeSpan.FromDays(2).Ticks, test.TotalTicks, input); input = "2 days 3:04:05"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(new TimeSpan(2, 3, 4, 5).Ticks, test.TotalTicks, input); input = "-2 days"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(TimeSpan.FromDays(-2).Ticks, test.TotalTicks, input); input = "-2 days -3:04:05"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(new TimeSpan(-2, -3, -4, -5).Ticks, test.TotalTicks, input); input = "-2 days -0:01:02"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(new TimeSpan(-2, 0, -1, -2).Ticks, test.TotalTicks, input); input = "2 days -12:00"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(new TimeSpan(2, -12, 0, 0).Ticks, test.TotalTicks, input); input = "1 mon"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(TimeSpan.FromDays(30).Ticks, test.TotalTicks, input); input = "2 mons"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(TimeSpan.FromDays(60).Ticks, test.TotalTicks, input); input = "1 mon -1 day"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(TimeSpan.FromDays(29).Ticks, test.TotalTicks, input); input = "1 mon -2 days"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(TimeSpan.FromDays(28).Ticks, test.TotalTicks, input); input = "-1 mon -2 days -3:04:05"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(new TimeSpan(-32, -3, -4, -5).Ticks, test.TotalTicks, input); input = "1 year"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(TimeSpan.FromDays(30 * 12).Ticks, test.TotalTicks, input); input = "2 years"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(TimeSpan.FromDays(30 * 24).Ticks, test.TotalTicks, input); input = "1 year -1 mon"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(TimeSpan.FromDays(30 * 11).Ticks, test.TotalTicks, input); input = "1 year -2 mons"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(TimeSpan.FromDays(30 * 10).Ticks, test.TotalTicks, input); input = "1 year -1 day"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(TimeSpan.FromDays(30 * 12 - 1).Ticks, test.TotalTicks, input); input = "1 year -2 days"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(TimeSpan.FromDays(30 * 12 - 2).Ticks, test.TotalTicks, input); input = "1 year -1 mon -1 day"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(TimeSpan.FromDays(30 * 11 - 1).Ticks, test.TotalTicks, input); input = "1 year -2 mons -2 days"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(TimeSpan.FromDays(30 * 10 - 2).Ticks, test.TotalTicks, input); input = "1 day 2:3:4.005"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(new TimeSpan(1, 2, 3, 4, 5).Ticks, test.TotalTicks, input); var oldCulture = System.Threading.Thread.CurrentThread.CurrentCulture; var testCulture = new System.Globalization.CultureInfo("fr-FR"); Assert.AreEqual(",", testCulture.NumberFormat.NumberDecimalSeparator, "decimal seperator"); try { System.Threading.Thread.CurrentThread.CurrentCulture = testCulture; input = "1 day 2:3:4.005"; test = NpgsqlTimeSpan.Parse(input); Assert.AreEqual(new TimeSpan(1, 2, 3, 4, 5).Ticks, test.TotalTicks, input); } finally { System.Threading.Thread.CurrentThread.CurrentCulture = oldCulture; } }
public void NpgsqlIntervalConstructors() { NpgsqlTimeSpan test; test = new NpgsqlTimeSpan(); Assert.AreEqual(0, test.Months, "Months"); Assert.AreEqual(0, test.Days, "Days"); Assert.AreEqual(0, test.Hours, "Hours"); Assert.AreEqual(0, test.Minutes, "Minutes"); Assert.AreEqual(0, test.Seconds, "Seconds"); Assert.AreEqual(0, test.Milliseconds, "Milliseconds"); Assert.AreEqual(0, test.Microseconds, "Microseconds"); test = new NpgsqlTimeSpan(1234567890); Assert.AreEqual(0, test.Months, "Months"); Assert.AreEqual(0, test.Days, "Days"); Assert.AreEqual(0, test.Hours, "Hours"); Assert.AreEqual(2, test.Minutes, "Minutes"); Assert.AreEqual(3, test.Seconds, "Seconds"); Assert.AreEqual(456, test.Milliseconds, "Milliseconds"); Assert.AreEqual(456789, test.Microseconds, "Microseconds"); test = new NpgsqlTimeSpan(new TimeSpan(1, 2, 3, 4, 5)).JustifyInterval(); Assert.AreEqual(0, test.Months, "Months"); Assert.AreEqual(1, test.Days, "Days"); Assert.AreEqual(2, test.Hours, "Hours"); Assert.AreEqual(3, test.Minutes, "Minutes"); Assert.AreEqual(4, test.Seconds, "Seconds"); Assert.AreEqual(5, test.Milliseconds, "Milliseconds"); Assert.AreEqual(5000, test.Microseconds, "Microseconds"); test = new NpgsqlTimeSpan(3, 2, 1234567890); Assert.AreEqual(3, test.Months, "Months"); Assert.AreEqual(2, test.Days, "Days"); Assert.AreEqual(0, test.Hours, "Hours"); Assert.AreEqual(2, test.Minutes, "Minutes"); Assert.AreEqual(3, test.Seconds, "Seconds"); Assert.AreEqual(456, test.Milliseconds, "Milliseconds"); Assert.AreEqual(456789, test.Microseconds, "Microseconds"); test = new NpgsqlTimeSpan(1, 2, 3, 4); Assert.AreEqual(0, test.Months, "Months"); Assert.AreEqual(1, test.Days, "Days"); Assert.AreEqual(2, test.Hours, "Hours"); Assert.AreEqual(3, test.Minutes, "Minutes"); Assert.AreEqual(4, test.Seconds, "Seconds"); Assert.AreEqual(0, test.Milliseconds, "Milliseconds"); Assert.AreEqual(0, test.Microseconds, "Microseconds"); test = new NpgsqlTimeSpan(1, 2, 3, 4, 5); Assert.AreEqual(0, test.Months, "Months"); Assert.AreEqual(1, test.Days, "Days"); Assert.AreEqual(2, test.Hours, "Hours"); Assert.AreEqual(3, test.Minutes, "Minutes"); Assert.AreEqual(4, test.Seconds, "Seconds"); Assert.AreEqual(5, test.Milliseconds, "Milliseconds"); Assert.AreEqual(5000, test.Microseconds, "Microseconds"); test = new NpgsqlTimeSpan(1, 2, 3, 4, 5, 6); Assert.AreEqual(1, test.Months, "Months"); Assert.AreEqual(2, test.Days, "Days"); Assert.AreEqual(3, test.Hours, "Hours"); Assert.AreEqual(4, test.Minutes, "Minutes"); Assert.AreEqual(5, test.Seconds, "Seconds"); Assert.AreEqual(6, test.Milliseconds, "Milliseconds"); Assert.AreEqual(6000, test.Microseconds, "Microseconds"); test = new NpgsqlTimeSpan(1, 2, 3, 4, 5, 6, 7); Assert.AreEqual(14, test.Months, "Months"); Assert.AreEqual(3, test.Days, "Days"); Assert.AreEqual(4, test.Hours, "Hours"); Assert.AreEqual(5, test.Minutes, "Minutes"); Assert.AreEqual(6, test.Seconds, "Seconds"); Assert.AreEqual(7, test.Milliseconds, "Milliseconds"); Assert.AreEqual(7000, test.Microseconds, "Microseconds"); }
public Dictionary <DateTime, int> GetSynchronizedPhysicalContacts(int userId, IEnumerable <DateTime> dateTimes) { Check.CallerLog <CommonCitizenRepository>(Logger, LoggerExecutionPositions.Entrance, $"userId: {userId}, dateTimes: {dateTimes}"); Check.NotNull(userId, nameof(userId)); Check.NotNull(dateTimes, nameof(dateTimes)); var synchedDateTimes = new Dictionary <DateTime, int>(); if (dateTimes.Count() < 1) { Check.CallerLog <CommonCitizenRepository>(Logger, LoggerExecutionPositions.Exit, $"dateTimes does not have any item."); return(synchedDateTimes); } using (var connection_ = new NpgsqlConnection(RepoOptions.PgsqlPassword)) { try { var query = $"SELECT id, date, time FROM (SELECT id, date, time FROM physical_contacts WHERE user_id = {userId}) WHERE "; foreach (var dateTime in dateTimes) { var date = NpgsqlDate.ToNpgsqlDate(dateTime); var time = NpgsqlTimeSpan.ToNpgsqlTimeSpan(dateTime.TimeOfDay); query += $" (date = {date} AND time = {time}) OR"; } query = query.TrimEnd('R').TrimEnd('O'); Check.CallerLog <CommonCitizenRepository>(Logger, LoggerExecutionPositions.Body, $"constructed query: {query}"); var command = new NpgsqlCommand(query); command.Connection = connection_; command.CommandType = System.Data.CommandType.Text; command.Prepare(); using (var reader = command.ExecuteReader()) { Check.CallerLog <CommonCitizenRepository>(Logger, LoggerExecutionPositions.Body, $"reading data from reader"); while (reader.Read()) { var id = reader.GetInt32(0); var date = reader.GetDate(1); var time = reader.GetTimeSpan(2); var dateTime = new DateTime(date.Year, date.Month, date.Day, time.Hours, time.Minutes, time.Seconds, time.Milliseconds); synchedDateTimes.Add(dateTime, id); } } } catch (Exception exception) { Check.CallerLog <CommonCitizenRepository>(Logger, LoggerExecutionPositions.Body, $"Exception {exception}", LogLevel.Warning); if (exception is BackendException) { throw exception; } throw new BackendException(Convert.ToInt32(HttpStatusCode.InternalServerError), exception.Message); } } Check.CallerLog <CommonCitizenRepository>(Logger, LoggerExecutionPositions.Exit, $"synchedDateTimes: {synchedDateTimes}"); return(synchedDateTimes); }
private async void AddTask_Button_Click(object sender, RoutedEventArgs e) { if (AddTaskDescription_TextBox.Text.Length == 0 || AddTaskSerial_TextBox.Text.Length == 0 || AddTaskDaysToComplete_TextBox.Text.Length == 0 || AddTaskClient_ComboBox.SelectedItem == null || AddTaskPriority_ComboBox.SelectedItem == null || AddTaskSlave_ComboBox.SelectedItem == null) { return; } int serial; int days; if (!Int32.TryParse(AddTaskSerial_TextBox.Text, out serial)) { MessageBox.Show("Cерийный номер должен содержать только цифры", "Ошибка формата данных", MessageBoxButton.OK, MessageBoxImage.Error); return; } if (!Int32.TryParse(AddTaskDaysToComplete_TextBox.Text, out days)) { MessageBox.Show("Укажите колличество дней цифрами", "Ошибка формата данных", MessageBoxButton.OK, MessageBoxImage.Error); return; } if (Convert.ToInt32(AddTaskDaysToComplete_TextBox.Text) < 1) { MessageBox.Show("Укажите колличество дней больше 0", "Ошибка формата данных", MessageBoxButton.OK, MessageBoxImage.Error); return; } LoadGif.Visibility = Visibility.Visible; WorkSheet.IsEnabled = false; ComboBoxItem boxItem = AddTaskPriority_ComboBox.SelectedItem as ComboBoxItem; string priority = boxItem.Tag as string; string description = AddTaskDescription_TextBox.Text; ClientModel chosenClient = AddTaskClient_ComboBox.SelectedItem as ClientModel; WorkerModel chosenSlave = AddTaskSlave_ComboBox.SelectedItem as WorkerModel; if (currentUser.Role == "Работник") { if (currentUser.Id != chosenSlave.Id) { WorkSheet.IsEnabled = true; LoadGif.Visibility = Visibility.Collapsed; MessageBox.Show("Вы не можете назначать исполнителем не себя", "Ошибка доступа", MessageBoxButton.OK, MessageBoxImage.Error); return; } } else if (currentUser.Role == "Менеджер") { if (currentUser.Id != chosenSlave.Id && chosenSlave.Role == "manager") { WorkSheet.IsEnabled = true; LoadGif.Visibility = Visibility.Collapsed; MessageBox.Show("Вы не можете назначать исполнителем начальников отделов", "Ошибка доступа", MessageBoxButton.OK, MessageBoxImage.Error); return; } } await Task.Run(() => { NpgsqlCommand npgsqlCommand = new NpgsqlCommand($"INSERT INTO \"tasks\" (description, client_id, serial_number, creation_date, days_to_complete, master_id, priority, slave_id) VALUES('{description}', {chosenClient.Id}, {serial}, :date, :days, {currentUser.Id}, '{priority}', {chosenSlave.Id});", npgSqlConnection); NpgsqlParameter param1 = new NpgsqlParameter("date", NpgsqlDbType.Date); param1.Value = NpgsqlDate.Now; NpgsqlTimeSpan timeSpan = NpgsqlTimeSpan.FromDays(days); NpgsqlParameter param3 = new NpgsqlParameter("days", NpgsqlDbType.Interval); param3.Value = timeSpan; npgsqlCommand.Parameters.Add(param1); npgsqlCommand.Parameters.Add(param3); npgsqlCommand.ExecuteNonQuery(); }); tasksList = LoadTasksFromDB(); tasksList = currentUser.Role == "Работник" ? new BindingList <TaskModel>(tasksList.Where(t => t.Slave.Id == currentUser.Id).ToList()) : tasksList; Tasks_DataGrid.ItemsSource = tasksList; WorkSheet.IsEnabled = true; LoadGif.Visibility = Visibility.Collapsed; ClearForm_Button_Click(1, new RoutedEventArgs()); }