-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathProgram.cs
182 lines (158 loc) · 6.93 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using System;
using System.IO;
using System.Linq;
using System.Text;
using CommandLine;
using DbfDataReader;
namespace Dbf
{
public class Program
{
private static readonly char[] newLineChars = { '\r', '\n' };
public static int Main(string[] args)
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
return Parser.Default.ParseArguments<Options>(args)
.MapResult(RunAndReturnExitCode, _ => 1);
}
public static int RunAndReturnExitCode(Options options)
{
if (options.Csv)
PrintCsv(options);
else if (options.Schema)
PrintSchema(options);
else
PrintSummaryInfo(options);
return 0;
}
private static void PrintSummaryInfo(Options options)
{
var encoding = GetEncoding();
using (var dbfTable = new DbfTable(options.Filename, encoding))
{
var header = dbfTable.Header;
Console.WriteLine($"Filename: {options.Filename}");
Console.WriteLine($"Type: {header.VersionDescription}");
Console.WriteLine($"Memo File: {dbfTable.Memo != null}");
Console.WriteLine($"Records: {header.RecordCount}");
Console.WriteLine();
Console.WriteLine("Fields:");
Console.WriteLine("Name Type Length Decimal");
Console.WriteLine("------------------------------------------------------------------------------");
foreach (var dbfColumn in dbfTable.Columns)
{
var name = dbfColumn.ColumnName;
var columnType = ((char) dbfColumn.ColumnType).ToString();
var length = dbfColumn.Length.ToString();
var decimalCount = dbfColumn.DecimalCount;
Console.WriteLine(
$"{name.PadRight(16)} {columnType.PadRight(10)} {length.PadRight(10)} {decimalCount}");
}
}
}
private static void PrintCsv(Options options)
{
var encoding = GetEncoding();
using (var dbfTable = new DbfTable(options.Filename, encoding))
{
var columnNames = string.Join(",", dbfTable.Columns.Select(c => c.ColumnName));
if (!options.SkipDeleted) columnNames += ",Deleted";
Console.WriteLine(columnNames);
var dbfRecord = new DbfRecord(dbfTable);
while (dbfTable.Read(dbfRecord))
{
if (options.SkipDeleted && dbfRecord.IsDeleted) continue;
var values = string.Join(",", dbfRecord.Values.Select(v => EscapeValue(v)));
if (!options.SkipDeleted) values += $",{dbfRecord.IsDeleted}";
Console.WriteLine(values);
}
}
}
private static void PrintSchema(Options options)
{
var encoding = GetEncoding();
using (var dbfTable = new DbfTable(options.Filename, encoding))
{
var tableName = Path.GetFileNameWithoutExtension(options.Filename);
Console.WriteLine($"CREATE TABLE [dbo].[{tableName}]");
Console.WriteLine("(");
foreach (var dbfColumn in dbfTable.Columns)
{
var columnSchema = ColumnSchema(dbfColumn);
Console.Write($" {columnSchema}");
if (dbfColumn.ColumnOrdinal < dbfTable.Columns.Count ||
!options.SkipDeleted)
Console.Write(",");
Console.WriteLine();
}
if (!options.SkipDeleted) Console.WriteLine(" [deleted] [bit] NULL DEFAULT ((0))");
Console.WriteLine(")");
}
}
private static Encoding GetEncoding()
{
return Encoding.GetEncoding(1252);
}
private static string ColumnSchema(DbfColumn dbfColumn)
{
var schema = string.Empty;
switch (dbfColumn.ColumnType)
{
case DbfColumnType.Boolean:
schema = $"[{dbfColumn.ColumnName}] [bit] NULL DEFAULT ((0))";
break;
case DbfColumnType.Character:
schema = $"[{dbfColumn.ColumnName}] [nvarchar]({dbfColumn.Length}) NULL";
break;
case DbfColumnType.Currency:
schema =
$"[{dbfColumn.ColumnName}] [decimal]({dbfColumn.Length + dbfColumn.DecimalCount},{dbfColumn.DecimalCount}) NULL DEFAULT (NULL)";
break;
case DbfColumnType.Date:
schema = $"[{dbfColumn.ColumnName}] [date] NULL DEFAULT (NULL)";
break;
case DbfColumnType.DateTime:
schema = $"[{dbfColumn.ColumnName}] [datetime] NULL DEFAULT (NULL)";
break;
case DbfColumnType.Double:
schema =
$"[{dbfColumn.ColumnName}] [decimal]({dbfColumn.Length + dbfColumn.DecimalCount},{dbfColumn.DecimalCount}) NULL DEFAULT (NULL)";
break;
case DbfColumnType.Float:
schema =
$"[{dbfColumn.ColumnName}] [decimal]({dbfColumn.Length + dbfColumn.DecimalCount},{dbfColumn.DecimalCount}) NULL DEFAULT (NULL)";
break;
case DbfColumnType.General:
schema = $"[{dbfColumn.ColumnName}] [nvarchar]({dbfColumn.Length}) NULL";
break;
case DbfColumnType.Memo:
schema = $"[{dbfColumn.ColumnName}] [ntext] NULL";
break;
case DbfColumnType.Number:
if (dbfColumn.DecimalCount > 0)
schema =
$"[{dbfColumn.ColumnName}] [decimal]({dbfColumn.Length + dbfColumn.DecimalCount},{dbfColumn.DecimalCount}) NULL DEFAULT (NULL)";
else
schema = $"[{dbfColumn.ColumnName}] [int] NULL DEFAULT (NULL)";
break;
case DbfColumnType.SignedLong:
schema = $"[{dbfColumn.ColumnName}] [int] NULL DEFAULT (NULL)";
break;
}
return schema;
}
private static string EscapeValue(IDbfValue dbfValue)
{
var value = dbfValue.ToString();
if (dbfValue is DbfValueString)
{
if (value.Contains(",") || value.IndexOfAny(newLineChars) > -1)
{
value = value.Replace("\"", "\"\"");
value = $"\"{value}\"";
}
}
return value;
}
}
}