-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdna-query-tools.ps1
More file actions
152 lines (130 loc) · 4.32 KB
/
dna-query-tools.ps1
File metadata and controls
152 lines (130 loc) · 4.32 KB
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
function Get-SQLite{
param(
[string]$Query,
[string]$File
)
Add-Type -Path ".\System.Data.SQLite.dll"
$con = New-Object -TypeName System.Data.SQLite.SQLiteConnection
$con.ConnectionString = "Data Source=$File"
$con.Open()
$sql = $con.CreateCommand()
$sql.CommandText = "$Query"
$adapter = New-Object -TypeName System.Data.SQLite.SQLiteDataAdapter $sql
$data = New-Object System.Data.DataSet
[void]$adapter.Fill($data)
return $data
}
function Combine-CSVs{
param(
[string]$InputFolder,
[string]$OutputFile,
[int]$SkipLines
)
$ScanCSVs = Get-ChildItem -Path $InputFolder
$y=0
$outfile = New-Object System.IO.StreamWriter ("$OutputFile")
foreach($s in $ScanCSVs){
write-host "Reading from "$s.FullName
# Strip first 10 lines
if($y -eq 0)
{
$skip = $SkipLines
}
else{
$skip = $SkipLines + 1
}
$infile = New-Object System.IO.StreamReader ($s.FullName)
for( $i = 1; $i -le $skip -and !$infile.EndOfStream; $i++ ) {
$infile.ReadLine() | Out-Null
}
while( !$infile.EndOfStream ) {
$outfile.WriteLine( $infile.ReadLine() )
}
$infile.close()
$infile.dispose()
write-host "Finished reading "$s.FullName
$y++
}
$outfile.close()
$outfile.dispose()
}
function Import-CSVData{
param(
[string]$CSVFile,
[string]$database
)
# SQLite database path
$sqldb = ".\dnaquery.db"
Add-Type -Path ".\System.Data.SQLite.dll"
$con = New-Object -TypeName System.Data.SQLite.SQLiteConnection
$con.ConnectionString = "Data Source=$sqldb"
$con.Open()
$sql = $con.CreateCommand()
$file = New-Object System.IO.StreamReader ("$CSVFile")
$RecsPerTransaction = 50
$insertioncount = 0
$totalCount = 0
$insertion = $null
$header = $file.ReadLine() #| Out-Null
$headertoken = $header -split ","
$columns = $headertoken.Count
#write-host $columns
$newheader = $null
foreach($h in $headertoken)
{
$newheader += '"' + $h + '",'
}
$newheader = $newheader.TrimEnd(",")
while( !$file.EndOfStream ) {
$line = $file.ReadLine()
$token = $line -split ',(?=(?:[^"]|"[^"]*")*$)'
if((!$token.Count -eq $columns) -or ($token[0] -eq "")){
# write-host "Ignoring the following line, suspect broken CSV data:"
# write-host $line
# write-host $token.Count
}
else{
#write-host $token.Count
$newline = $null
$t = $null
$line = $line.Replace("`r`n",' ')
foreach($t in $token){
#$t = $t.Replace("`r`n",' ')
$t = $t.Replace('"',"'")
# $t = $t.Trim('"')
$t = $t.Replace(",","")
$t = '"'+$t+'"'
$newline += $t + ","
}
$newline = $newline.TrimEnd(",")
$testtoken = $newline.Split(",")
if($testtoken.Count -eq $columns){
$insertion += 'INSERT INTO '+ $database + ' ('+$newheader+') VALUES ('+$newline+');'
$insertioncount++
}
else{
write-host "Bad line: $newline"
}
# If Batch Size has been reach, begin bulk insert
if($insertioncount -eq $RecsPerTransaction){
$sql.CommandText = 'BEGIN TRANSACTION;' + $insertion + 'COMMIT;'
#write-host $sql.CommandText
$adapter = New-Object -TypeName System.Data.SQLite.SQLiteDataAdapter $sql
$data = New-Object System.Data.DataSet
[void]$adapter.Fill($data)
$totalCount += $insertioncount
$insertioncount = 0
$insertion = $null
write-progress -Activity "Inserted $totalCount records"
}
}
}
# Add last batch if not enough for 50 unit INSERT
$sql.CommandText = 'BEGIN TRANSACTION;' + $insertion + 'COMMIT;'
$adapter = New-Object -TypeName System.Data.SQLite.SQLiteDataAdapter $sql
$data = New-Object System.Data.DataSet
[void]$adapter.Fill($data)
#Close Database file
$file.close()
$file.dispose()
}