Skip to content

Commit 9f31046

Browse files
King-OzymandiasKing-Ozymandias
authored andcommitted
SQLite write support
1 parent 6889bd8 commit 9f31046

File tree

3 files changed

+115
-3
lines changed

3 files changed

+115
-3
lines changed

README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ EpMonitor disableDuring: [
2828
load ].
2929
```
3030

31-
If you'd be interested in (basic, read-only for now) SQLite support, use `load: 'sqlite'` at the end:
31+
If you'd be interested in SQLite support, use `load: 'sqlite'` at the end:
3232

3333
```st
3434
EpMonitor disableDuring: [
@@ -130,11 +130,20 @@ weather transposed.
130130
| **2** | true | true | false | true | true |
131131
| **3** | snow | rain | - | rain | snow |
132132

133-
### Load data from SQLite query:
133+
### SQLite examples
134+
*Following examples expect valid/connected SQLite connection in a variable `conn`*
135+
#### Load data from SQLite query:
134136
```st
135-
"If you have a connection ready in conn"
136137
df := DataFrame readFromSqliteCursor: (conn execute: 'SELECT * FROM table').
137138
```
139+
#### Write data to SQLite table (DataFrame column names <=> table column names):
140+
```st
141+
df writeToSqlite: conn tableName: 'table'.
142+
```
143+
#### Write to differently named colums (provide names for ALL DataFrame columns!)
144+
```st
145+
df writeToSqlite: conn tableName: 'table' columnNames: #('col1' 'col2' 'col3').
146+
```
138147

139148
## Documentation and Literature
140149

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,21 @@ DataFrame class >> readFromSqliteCursor: aSqliteCursor [
55
"Convenience shortcut for SQLite3Cursor => DataFrame"
66
^ self readFrom: aSqliteCursor using: DataFrameSqliteReader new
77
]
8+
9+
{ #category : '*DataFrame-IO-Sqlite' }
10+
DataFrame >> writeToSqlite: aSqlite3Connection tableName: aString [
11+
12+
| writer |
13+
writer := DataFrameSqliteWriter writeToTable: aString.
14+
self writeTo: aSqlite3Connection using: writer
15+
]
16+
17+
{ #category : '*DataFrame-IO-Sqlite' }
18+
DataFrame >> writeToSqlite: aSqlite3Connection tableName: aString columnNames: aCollection [
19+
20+
| writer |
21+
writer := DataFrameSqliteWriter
22+
writeToTable: aString
23+
columnNames: aCollection.
24+
self writeTo: aSqlite3Connection using: writer
25+
]
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
Class {
2+
#name : 'DataFrameSqliteWriter',
3+
#superclass : 'DataFrameWriter',
4+
#instVars : [
5+
'tableName',
6+
'columnNames'
7+
],
8+
#category : 'DataFrame-IO-Sqlite',
9+
#package : 'DataFrame-IO-Sqlite'
10+
}
11+
12+
{ #category : 'instance creation' }
13+
DataFrameSqliteWriter class >> writeToTable: aString [
14+
15+
^ self new
16+
tableName: aString;
17+
yourself
18+
]
19+
20+
{ #category : 'instance creation' }
21+
DataFrameSqliteWriter class >> writeToTable: aString columnNames: aCollection [
22+
23+
^ self new
24+
tableName: aString;
25+
columnNames: aCollection;
26+
yourself
27+
]
28+
29+
{ #category : 'accessing' }
30+
DataFrameSqliteWriter >> columnNames [
31+
32+
^ columnNames
33+
]
34+
35+
{ #category : 'accessing' }
36+
DataFrameSqliteWriter >> columnNames: anObject [
37+
38+
columnNames := anObject
39+
]
40+
41+
{ #category : 'helpers' }
42+
DataFrameSqliteWriter >> getColumnNamesFor: aDataFrame [
43+
44+
columnNames ifNil: [ ^ aDataFrame columnNames ].
45+
columnNames size ~= aDataFrame columns size ifTrue: [
46+
self error:
47+
'Column count mismatch (Writer columns <=> DataFrame columns)' ].
48+
^ columnNames
49+
]
50+
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+
77+
{ #category : 'writing' }
78+
DataFrameSqliteWriter >> write: aDataFrame to: aSqliteConnection [
79+
80+
| stmt |
81+
stmt := aSqliteConnection prepare:
82+
(self insertQueryForColumns:
83+
(self getColumnNamesFor: aDataFrame)).
84+
aDataFrame do: [ :row | stmt execute: row asArray ]
85+
]

0 commit comments

Comments
 (0)