• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Giorgi / DuckDB.NET / 10460156994

19 Aug 2024 08:05PM UTC coverage: 89.935% (-0.3%) from 90.251%
10460156994

push

github

Giorgi
Check for null

878 of 1006 branches covered (87.28%)

Branch coverage included in aggregate %.

1 of 1 new or added line in 1 file covered. (100.0%)

18 existing lines in 4 files now uncovered.

1767 of 1935 relevant lines covered (91.32%)

819430.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.59
/DuckDB.NET.Data/DuckDBAppender.cs
1
using DuckDB.NET.Data.Internal;
2
using DuckDB.NET.Data.Internal.Writer;
3
using DuckDB.NET.Native;
4
using System;
5
using System.Diagnostics;
6
using System.Diagnostics.CodeAnalysis;
7

8
namespace DuckDB.NET.Data;
9

10
public class DuckDBAppender : IDisposable
11
{
12
    private static readonly ulong DuckDBVectorSize = DuckDBGlobalData.VectorSize;
3✔
13

14
    private bool closed;
15
    private readonly Native.DuckDBAppender nativeAppender;
16
    private readonly string qualifiedTableName;
17

18
    private ulong rowCount;
19

20
    private readonly DuckDBLogicalType[] logicalTypes;
21
    private readonly DuckDBDataChunk dataChunk;
22
    private readonly VectorDataWriterBase[] vectorWriters;
23

24
    internal DuckDBAppender(Native.DuckDBAppender appender, string qualifiedTableName)
162✔
25
    {
26
        nativeAppender = appender;
162✔
27
        this.qualifiedTableName = qualifiedTableName;
162✔
28

29
        var columnCount = NativeMethods.Appender.DuckDBAppenderColumnCount(nativeAppender);
162✔
30

31
        vectorWriters = new VectorDataWriterBase[columnCount];
162✔
32
        logicalTypes = new DuckDBLogicalType[columnCount];
162✔
33
        var logicalTypeHandles = new IntPtr[columnCount];
162✔
34

35
        for (ulong index = 0; index < columnCount; index++)
1,422✔
36
        {
37
            logicalTypes[index] = NativeMethods.Appender.DuckDBAppenderColumnType(nativeAppender, index);
549✔
38
            logicalTypeHandles[index] = logicalTypes[index].DangerousGetHandle();
549✔
39
        }
40

41
        dataChunk = NativeMethods.DataChunks.DuckDBCreateDataChunk(logicalTypeHandles, columnCount);
162✔
42
    }
162✔
43

44
    public DuckDBAppenderRow CreateRow()
45
    {
46
        if (closed)
264,315✔
47
        {
48
            throw new InvalidOperationException("Appender is already closed");
3✔
49
        }
50

51
        if (rowCount % DuckDBVectorSize==0)
264,312✔
52
        {
53
            AppendDataChunk();
183✔
54

55
            InitVectorWriters();
183✔
56

57
            rowCount = 0;
183✔
58
        }
59

60
        rowCount++;
264,312✔
61
        return new DuckDBAppenderRow(qualifiedTableName, vectorWriters, rowCount - 1);
264,312✔
62
    }
63

64
    public void Close()
65
    {
66
        closed = true;
159✔
67

68
        try
69
        {
70
            AppendDataChunk();
159✔
71

72
            foreach (var logicalType in logicalTypes)
1,398✔
73
            {
74
                logicalType.Dispose();
540✔
75
            }
76

77
            foreach (var writer in vectorWriters)
1,398✔
78
            {
79
                writer?.Dispose();
540✔
80
            }
81

82
            var state = NativeMethods.Appender.DuckDBAppenderClose(nativeAppender);
159✔
83
            if (!state.IsSuccess())
159!
84
            {
UNCOV
85
                ThrowLastError(nativeAppender);
×
86
            }
87

88
            dataChunk.Dispose();
159✔
89
        }
159✔
90
        finally
91
        {
92
            nativeAppender.Close();
159✔
93
        }
159✔
94
    }
159✔
95

96
    public void Dispose()
97
    {
98
        if (!closed)
159✔
99
        {
100
            Close();
153✔
101
        }
102
    }
159✔
103

104
    private void InitVectorWriters()
105
    {
106
        for (long index = 0; index < vectorWriters.LongLength; index++)
1,542✔
107
        {
108
            var vector = NativeMethods.DataChunks.DuckDBDataChunkGetVector(dataChunk, index);
588✔
109

110
            if (vectorWriters[index] == null)
588✔
111
            {
112
                vectorWriters[index] = VectorDataWriterFactory.CreateWriter(vector, logicalTypes[index]); 
540✔
113
            }
114
            else
115
            {
116
                vectorWriters[index].InitializerWriter();
48✔
117
            }
118
        }
119
    }
183✔
120

121
    private void AppendDataChunk()
122
    {
123
        NativeMethods.DataChunks.DuckDBDataChunkSetSize(dataChunk, rowCount);
342✔
124
        var state = NativeMethods.Appender.DuckDBAppendDataChunk(nativeAppender, dataChunk);
342✔
125

126
        if (!state.IsSuccess())
342!
127
        {
UNCOV
128
            ThrowLastError(nativeAppender);
×
129
        }
130

131
        NativeMethods.DataChunks.DuckDBDataChunkReset(dataChunk);
342✔
132
    }
342✔
133

134
    [DoesNotReturn]
135
    [StackTraceHidden]
136
    internal static void ThrowLastError(Native.DuckDBAppender appender)
137
    {
138
        var errorMessage = NativeMethods.Appender.DuckDBAppenderError(appender).ToManagedString(false);
3✔
139

140
        throw new DuckDBException(errorMessage);
3✔
141
    }
142
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc