Skip to content

Commit

Permalink
Unification of writing. Also, the "mapping" version doesn't copy rows…
Browse files Browse the repository at this point in the history
… out of the dataframe.
  • Loading branch information
King-Ozymandias authored and King-Ozymandias committed Sep 4, 2024
1 parent d1f88c1 commit 882775d
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 118 deletions.
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,7 @@ df := DataFrame readFromSqliteCursor: (conn execute: 'SELECT * FROM table').
```st
df writeToSqlite: conn tableName: 'table'.
```
#### Write to differently named colums (provide names for ALL DataFrame columns!)
```st
df writeToSqlite: conn tableName: 'table' columnNames: #('col1' 'col2' 'col3').
```
#### Mapping (selecting / renaming dataframe columns):
#### Selecting / renaming dataframe columns:
Let's assume:
- CREATE TABLE tbl (a,b,c)
- DataFrame with columns (a,x,c,d)
Expand Down
4 changes: 2 additions & 2 deletions src/DataFrame-IO-Sqlite/DataFrame.extension.st
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ DataFrame >> writeToSqlite: aSqlite3Connection tableName: aString [
DataFrame >> writeToSqlite: aSqlite3Connection tableName: aString columnMappings: aCollection [

| writer |
writer := DataFrameSqliteColumnMappingWriter
writer := DataFrameSqliteWriter
writeToTable: aString
columnMappings: aCollection.
self writeTo: aSqlite3Connection using: writer
Expand All @@ -30,6 +30,6 @@ DataFrame >> writeToSqlite: aSqlite3Connection tableName: aString columnNames: a
| writer |
writer := DataFrameSqliteWriter
writeToTable: aString
columnNames: aCollection.
columnMappings: aCollection.
self writeTo: aSqlite3Connection using: writer
]
35 changes: 0 additions & 35 deletions src/DataFrame-IO-Sqlite/DataFrameAbstractSqliteWriter.class.st

This file was deleted.

This file was deleted.

85 changes: 66 additions & 19 deletions src/DataFrame-IO-Sqlite/DataFrameSqliteWriter.class.st
Original file line number Diff line number Diff line change
@@ -1,58 +1,105 @@
Class {
#name : 'DataFrameSqliteWriter',
#superclass : 'DataFrameAbstractSqliteWriter',
#superclass : 'DataFrameWriter',
#instVars : [
'columnNames'
'tableName',
'columnMappings'
],
#category : 'DataFrame-IO-Sqlite',
#package : 'DataFrame-IO-Sqlite'
}

{ #category : 'instance creation' }
{ #category : 'writing' }
DataFrameSqliteWriter class >> writeToTable: aString [

^ self new
tableName: aString;
yourself
]

{ #category : 'instance creation' }
DataFrameSqliteWriter class >> writeToTable: aString columnNames: aCollection [
{ #category : 'writing' }
DataFrameSqliteWriter class >> writeToTable: aString columnMappings: aCollection [

^ self new
tableName: aString;
columnNames: aCollection;
columnMappings: aCollection;
yourself
]

{ #category : 'accessing' }
DataFrameSqliteWriter >> columnNames [
DataFrameSqliteWriter >> columnMappings [

^ columnNames
^ columnMappings
]

{ #category : 'accessing' }
DataFrameSqliteWriter >> columnNames: anObject [
DataFrameSqliteWriter >> columnMappings: anObject [

columnMappings := anObject
]

{ #category : 'helpers' }
DataFrameSqliteWriter >> fieldIndicesFor: aDataFrame [
"gather indices of columns in dataframe (to avoid lookup by field name later, in loop)"

^ (self getColumnMappings: aDataFrame) collect: [ :m |
| sourceName |
sourceName := m isAssociation
ifTrue: [ m key ]
ifFalse: [ m ].
aDataFrame columnNames indexOf: sourceName ]
]

{ #category : 'helpers' }
DataFrameSqliteWriter >> getColumnMappings: aDataFrame [

^ columnMappings ifNil: [ aDataFrame columnNames ]
]

{ #category : 'helpers' }
DataFrameSqliteWriter >> getColumnNames: aDataFrame [

columnNames := anObject
^ (self getColumnMappings: aDataFrame) collect: [ :m | m value ]
]

{ #category : 'helpers' }
DataFrameSqliteWriter >> getColumnNamesFor: aDataFrame [
DataFrameSqliteWriter >> insertQueryForColumns: aSequence [
""
^ String streamContents: [ :strm |
strm
nextPutAll: 'INSERT INTO ';
nextPutAll: tableName;
nextPut: $(;
nextPutAll: (',' join: aSequence);
nextPutAll: ')VALUES('.
aSequence do: [ :ignore | strm nextPut: $? ] separatedBy: [ strm nextPut: $, ].
strm nextPut: $) ]
]

{ #category : 'accessing' }
DataFrameSqliteWriter >> tableName [

columnNames ifNil: [ ^ aDataFrame columnNames ].
columnNames size ~= aDataFrame columns size ifTrue: [
self error:
'Column count mismatch (Writer columns <=> DataFrame columns)' ].
^ columnNames
^ tableName
]

{ #category : 'accessing' }
DataFrameSqliteWriter >> tableName: anObject [

tableName := anObject
]

{ #category : 'writing' }
DataFrameSqliteWriter >> write: aDataFrame to: aSqliteConnection [

| stmt |
| fieldIndices args stmt |
fieldIndices := self fieldIndicesFor: aDataFrame.
args := Array new: fieldIndices size.
stmt := aSqliteConnection prepare:
(self insertQueryForColumns:
(self getColumnNamesFor: aDataFrame)).
aDataFrame do: [ :row | stmt execute: row asArray ]
(self getColumnNames: aDataFrame)).

1 to: aDataFrame dimensions x do: [ :rowIndex |
fieldIndices withIndexDo: [ :srcCol :dstCol |
args at: dstCol put: (aDataFrame contents at: rowIndex at: srcCol) ].
stmt execute: args ]
]

0 comments on commit 882775d

Please sign in to comment.