public void PqsqlCopyFromTest1() { PqsqlTransaction tran = mConnection.BeginTransaction(); mCmd.Transaction = tran; mCmd.CommandText = "create temporary table foo ( a int2, b int4, c int8 )"; mCmd.CommandType = CommandType.Text; int affected = mCmd.ExecuteNonQuery(); Assert.AreEqual(0, affected); PqsqlCopyFrom copy = new PqsqlCopyFrom(mConnection) { Table = "foo", ColumnList = "c,a,b", CopyTimeout = 10 }; copy.Start(); for (short i = 9; i >= 0; i--) { copy.WriteInt8(i); copy.WriteInt2(i); copy.WriteInt4(i); } copy.End(); copy.Close(); mCmd.CommandText = "foo"; mCmd.CommandType = CommandType.TableDirect; int value = 9; foreach (IDataRecord rec in mCmd.ExecuteReader()) { object[] o = new object[3]; rec.GetValues(o); Assert.IsInstanceOfType(o[0], typeof(short)); Assert.AreEqual((short)value, o[0]); Assert.IsInstanceOfType(o[1], typeof(int)); Assert.AreEqual(value, o[1]); Assert.IsInstanceOfType(o[2], typeof(long)); Assert.AreEqual((long)value, o[2]); value--; } Assert.AreEqual(-1, value); tran.Rollback(); }
public void PqsqlCopyFromTest3() { PqsqlTransaction t = mConnection.BeginTransaction(); PqsqlCommand cmd = mConnection.CreateCommand(); cmd.Transaction = t; cmd.CommandText = "CREATE TEMP TABLE testcopy (c0 int2, c1 int4, c2 int8, c3 bool, c4 text, c5 float4, c6 float8, c7 timestamp, c8 interval, c9 numeric, c10 date, c11 time, c12 timetz);"; cmd.CommandTimeout = 100; cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); PqsqlCopyFrom copy = new PqsqlCopyFrom(mConnection) { Table = "testcopy", ColumnList = "c0,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12", CopyTimeout = 5 }; copy.Start(); DateTime now = new DateTime(2001, 1, 1, 1, 2, 3, DateTimeKind.Utc); DateTime date = DateTime.UtcNow.Date; TimeSpan time = DateTime.UtcNow.TimeOfDay; long timeTicks = time.Ticks; timeTicks = timeTicks - timeTicks % TimeSpan.TicksPerMillisecond; TimeSpan timetz = DateTime.Now.TimeOfDay; long timetzTicks = timetz.Ticks; timetzTicks = timetzTicks - timetzTicks % TimeSpan.TicksPerMillisecond; for (int i = 0; i < 4; i++) { copy.WriteInt2((short)i); copy.WriteInt4(i); copy.WriteInt8(i); copy.WriteBool(i > 0); copy.WriteText(Convert.ToString(i)); copy.WriteFloat4((float)(i + 0.123)); copy.WriteFloat8(i + 0.123); copy.WriteTimestamp(now.AddSeconds(i)); copy.WriteInterval(TimeSpan.FromHours(24) + TimeSpan.FromDays(7) + TimeSpan.FromMinutes(i)); copy.WriteNumeric((decimal)i / 10); copy.WriteDate(date); copy.WriteTime(time); copy.WriteTimeTZ(timetz); } copy.End(); copy.Close(); cmd.Transaction = t; cmd.CommandText = "testcopy"; cmd.CommandType = CommandType.TableDirect; PqsqlDataReader r = cmd.ExecuteReader(); Assert.AreEqual(-1, r.RecordsAffected); int j = 0; foreach (IDataRecord row in r) { Assert.AreEqual((short)j, row.GetInt16(0)); Assert.AreEqual(j, row.GetInt32(1)); Assert.AreEqual(j, row.GetInt64(2)); Assert.AreEqual(j > 0, row.GetBoolean(3)); Assert.AreEqual(Convert.ToString(j), row.GetString(4)); Assert.AreEqual((float)(j + 0.123), row.GetFloat(5)); Assert.AreEqual(j + 0.123, row.GetDouble(6)); Assert.AreEqual(now.AddSeconds(j), row.GetDateTime(7)); Assert.AreEqual(TimeSpan.FromHours(24) + TimeSpan.FromDays(7) + TimeSpan.FromMinutes(j), row.GetValue(8)); Assert.AreEqual((double)j / 10, row.GetValue(9)); Assert.AreEqual(date, row.GetValue(10)); TimeSpan c11 = (TimeSpan)row.GetValue(11); long c11Ticks = c11.Ticks; c11Ticks = c11Ticks - c11Ticks % TimeSpan.TicksPerMillisecond; Assert.AreEqual(timeTicks, c11Ticks); TimeSpan c12 = (TimeSpan)row.GetValue(12); long c12Ticks = c12.Ticks; c12Ticks = c12Ticks - c12Ticks % TimeSpan.TicksPerMillisecond; Assert.AreEqual(timetzTicks, c12Ticks); j++; } t.Rollback(); }