Skip to content

Commit 178bab5

Browse files
King-OzymandiasKing-Ozymandias
authored andcommitted
Support for selecting / renaming columns when writing to SQLite
1 parent c075a57 commit 178bab5

File tree

4 files changed

+102
-28
lines changed

4 files changed

+102
-28
lines changed

src/DataFrame-IO-Sqlite/DataFrame.extension.st

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ DataFrame >> writeToSqlite: aSqlite3Connection tableName: aString [
1414
self writeTo: aSqlite3Connection using: writer
1515
]
1616

17+
{ #category : '*DataFrame-IO-Sqlite' }
18+
DataFrame >> writeToSqlite: aSqlite3Connection tableName: aString columnMappings: aCollection [
19+
20+
| writer |
21+
writer := DataFrameSqliteColumnMappingWriter
22+
writeToTable: aString
23+
columnMappings: aCollection.
24+
self writeTo: aSqlite3Connection using: writer
25+
]
26+
1727
{ #category : '*DataFrame-IO-Sqlite' }
1828
DataFrame >> writeToSqlite: aSqlite3Connection tableName: aString columnNames: aCollection [
1929

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
Class {
2+
#name : 'DataFrameAbstractSqliteWriter',
3+
#superclass : 'DataFrameWriter',
4+
#instVars : [
5+
'tableName'
6+
],
7+
#category : 'DataFrame-IO-Sqlite',
8+
#package : 'DataFrame-IO-Sqlite'
9+
}
10+
11+
{ #category : 'helpers' }
12+
DataFrameAbstractSqliteWriter >> insertQueryForColumns: aSequence [
13+
""
14+
^ String streamContents: [ :strm |
15+
strm
16+
nextPutAll: 'INSERT INTO ';
17+
nextPutAll: tableName;
18+
nextPut: $(;
19+
nextPutAll: (',' join: aSequence);
20+
nextPutAll: ')VALUES('.
21+
aSequence do: [ :ignore | strm nextPut: $? ] separatedBy: [ strm nextPut: $, ].
22+
strm nextPut: $) ]
23+
]
24+
25+
{ #category : 'accessing' }
26+
DataFrameAbstractSqliteWriter >> tableName [
27+
28+
^ tableName
29+
]
30+
31+
{ #category : 'accessing' }
32+
DataFrameAbstractSqliteWriter >> tableName: anObject [
33+
34+
tableName := anObject
35+
]
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
Class {
2+
#name : 'DataFrameSqliteColumnMappingWriter',
3+
#superclass : 'DataFrameAbstractSqliteWriter',
4+
#instVars : [
5+
'columnMappings'
6+
],
7+
#category : 'DataFrame-IO-Sqlite',
8+
#package : 'DataFrame-IO-Sqlite'
9+
}
10+
11+
{ #category : 'writing' }
12+
DataFrameSqliteColumnMappingWriter class >> writeToTable: aString columnMappings: aCollection [
13+
14+
^ self new
15+
tableName: aString;
16+
columnMappings: aCollection;
17+
yourself
18+
]
19+
20+
{ #category : 'accessing' }
21+
DataFrameSqliteColumnMappingWriter >> columnMappings [
22+
23+
^ columnMappings
24+
]
25+
26+
{ #category : 'accessing' }
27+
DataFrameSqliteColumnMappingWriter >> columnMappings: anObject [
28+
29+
columnMappings := anObject
30+
]
31+
32+
{ #category : 'writing' }
33+
DataFrameSqliteColumnMappingWriter >> write: aDataFrame to: aSqliteConnection [
34+
35+
| fieldIndices args dfCols tblCols stmt nCols |
36+
nCols := columnMappings size.
37+
dfCols := aDataFrame columnNames.
38+
fieldIndices := columnMappings collect: [ :m |
39+
dfCols indexOf: (m isAssociation
40+
ifTrue: [ m key ]
41+
ifFalse: [ m ]) ].
42+
tblCols := columnMappings collect: [ :m | m value ].
43+
args := Array new: fieldIndices size.
44+
45+
stmt := aSqliteConnection prepare:
46+
(self insertQueryForColumns: tblCols).
47+
48+
aDataFrame do: [ :r |
49+
| row |
50+
row := r asArray.
51+
1 to: nCols do: [ :i |
52+
| rowVal |
53+
rowVal := row at: (fieldIndices at: i).
54+
args at: i put: rowVal ].
55+
stmt execute: args ]
56+
]

src/DataFrame-IO-Sqlite/DataFrameSqliteWriter.class.st

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
Class {
22
#name : 'DataFrameSqliteWriter',
3-
#superclass : 'DataFrameWriter',
3+
#superclass : 'DataFrameAbstractSqliteWriter',
44
#instVars : [
5-
'tableName',
65
'columnNames'
76
],
87
#category : 'DataFrame-IO-Sqlite',
@@ -48,32 +47,6 @@ DataFrameSqliteWriter >> getColumnNamesFor: aDataFrame [
4847
^ columnNames
4948
]
5049

51-
{ #category : 'helpers' }
52-
DataFrameSqliteWriter >> insertQueryForColumns: aSequence [
53-
""
54-
^ String streamContents: [ :strm |
55-
strm
56-
nextPutAll: 'INSERT INTO ';
57-
nextPutAll: tableName;
58-
nextPut: $(;
59-
nextPutAll: (',' join: aSequence);
60-
nextPutAll: ')VALUES('.
61-
aSequence do: [ :ignore | strm nextPut: $? ] separatedBy: [ strm nextPut: $, ].
62-
strm nextPut: $) ]
63-
]
64-
65-
{ #category : 'accessing' }
66-
DataFrameSqliteWriter >> tableName [
67-
68-
^ tableName
69-
]
70-
71-
{ #category : 'accessing' }
72-
DataFrameSqliteWriter >> tableName: anObject [
73-
74-
tableName := anObject
75-
]
76-
7750
{ #category : 'writing' }
7851
DataFrameSqliteWriter >> write: aDataFrame to: aSqliteConnection [
7952

0 commit comments

Comments
 (0)