private void AddValueToSerializer(NpgsqlCopySerializer serializer, object value) { if (value.GetType() == typeof(Int32)) { serializer.AddInt32((int)value); } if (value.GetType() == typeof(Int64)) { serializer.AddInt64((Int64)value); } if (value.GetType() == typeof(string)) { serializer.AddString(value.ToString()); } if (value.GetType() == typeof(float)) { serializer.AddNumber((float)value); } if (value.GetType() == typeof(double)) { serializer.AddNumber((double)value); } if (value.GetType() == typeof(bool)) { serializer.AddBool((bool)value); } if (value.GetType() == typeof(DateTime)) { serializer.AddDateTime((DateTime)value); } if (value == null || value == DBNull.Value) { serializer.AddNull(); } if (value.GetType() == typeof(Guid)) { serializer.AddString(value.ToString()); } }
// Serializer success test static public void CopyInWithSerializer() { NpgsqlCopySerializer sink = new NpgsqlCopySerializer(conn); String q = "COPY copy2(field_int4, field_int8, field_text, field_timestamp, field_bool) FROM STDIN"; cin = new NpgsqlCopyIn(q, conn); cin.Start(); if (!cin.IsActive) { throw new Exception("Copy started inactive"); } sink.AddInt32(-13); sink.AddNull(); sink.AddString("First row"); sink.AddDateTime(new DateTime(2020, 12, 22, 23, 33, 45, 765)); sink.AddBool(true); sink.EndRow(); sink.AddNull(); sink.AddNull(); sink.AddString("Second row"); sink.Close(); Console.Out.WriteLine("Copy through serializer ok"); }
public bool export() { NpgsqlCommand cmd = new NpgsqlCommand(CreateInsertCmd(), connection); NpgsqlCopySerializer serializer = new NpgsqlCopySerializer(connection); NpgsqlCopyIn copyIn = new NpgsqlCopyIn(cmd, connection, serializer.ToStream); const int FLUSH_ROWS = 200000; copyIn.Start(); var linecounter = 0; foreach (var queryList in _queryLists) { bool[] array = new bool[200]; foreach (int i in queryList.Value) { array[i] = true; } serializer.AddInt32(queryList.Key); for (int i = 0; i < 200; i++) { serializer.AddBool(array[i]); } serializer.EndRow(); if (linecounter++ % FLUSH_ROWS == 0) { serializer.Flush(); } } serializer.Flush(); serializer.Close(); copyIn.End(); return(true); }
/// <summary> /// Wrzuca dane do bazy danych. /// </summary> private bool importData() { tic("Importing data:", true, true); const int FLUSH_ROWS = 200000; String[] toImport = new String[] { sessionTableName, queryTableName, queryTermTableName, queryUrlTableName, clickTableName, urlTableName }; foreach (String tableName in toImport) { tic(tableName, false, true); int[] types = getTableTypes(tableName); NpgsqlCommand cmd = new NpgsqlCommand(buildInsertCommand(tableName), connection); NpgsqlCopySerializer serializer = new NpgsqlCopySerializer(connection); NpgsqlCopyIn copyIn = new NpgsqlCopyIn(cmd, connection, serializer.ToStream); copyIn.Start(); using (BufferedBinaryReader reader = new BufferedBinaryReader(workDir + tableName)) { int lineCounter = 0; while (reader.PeekChar() > -1) { lineCounter++; for (int i = 0; i < types.Length; i++) { if (types[i] == 0) { int value = reader.ReadInt32(); serializer.AddInt32(value); } if (types[i] == 1) { bool value = reader.ReadBool(); serializer.AddBool(value); } } serializer.EndRow(); if ((lineCounter + 1) % FLUSH_ROWS == 0) { serializer.Flush(); } } Console.Write(String.Format("{0,-15}", String.Format("({0})", lineCounter))); } serializer.Flush(); serializer.Close(); copyIn.End(); toc(); } toc(true); return(true); }