Skip to content

Commit

Permalink
docs & test: add sqlite3 unit test , fix the issue of readme document
Browse files Browse the repository at this point in the history
  • Loading branch information
Breeze0806 committed Oct 5, 2024
1 parent a285334 commit de47f30
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 10 deletions.
13 changes: 12 additions & 1 deletion README_USER.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,18 @@ datax -c examples/postgrescsv/config.json
datax -c examples/postgresxlsx/config.json
```

##### 2.1.2.10 Other Synchronization Examples
##### 2.1.2.10 Synchronizing with sqlite3

* Before use, download the corresponding [SQLite Download Page](https://www.sqlite.org/download.html).
* Note: On Windows, set `path=%path%;/opt/sqlite/sqlite3.dll`.
* Initialize the database using `cmd/datax/examples/sqlite3/init.sql` **for testing purposes**
* Start the sqlite3 synchronization command:

```bash
datax -c examples/sqlite3/config.json
```

##### 2.1.2.11 Other Synchronization Examples

In addition to the above examples, all data sources listed in the go-etl features can be used interchangeably. Configurations can be set up for data sources such as MySQL to PostgreSQL, MySQL to Oracle, Oracle to DB2, etc.

Expand Down
9 changes: 8 additions & 1 deletion README_USER_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,14 @@ datax -c examples/postgrescsv/config.json
datax -c examples/postgresxlsx/config.json
```

##### 2.1.2.10 其他同步例子
##### 2.1.2.10 与 sqlite3 同步

* 在使用前,请下载相应的[SQLite驱动](https://www.sqlite.org/download.html).
* 注意:在 Windows 系统上,设置 `path=%path%;/opt/sqlite/sqlite3.dll`
* 使用 `cmd/datax/examples/sqlite3/init.sql` **用于测试目的** 初始化数据库
* 启动 sqlite3 同步命令:

##### 2.1.2.11 其他同步例子

除了上述例子外,在go-etl特性中所列出的数据源都可以交叉使用,还配置例如mysql到postgresql数据源,mysql到oracle,oracle到db2等等,

Expand Down
8 changes: 7 additions & 1 deletion datax/plugin/reader/sqlite3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,10 @@ Below is a list of type conversions that Sqlite3Reader performs for sqlite3 type

| go-etl的类型 | sqlite3数据类型 |
| ------------ |--------------------|
| string | INTEGER、TEXT、REAL、BLOB |
| bigInt | INTEGER |
| decimal | REAL, NUMERIC |
| string | TEXT |
| bytes | BLOB |

## Performance Report

Expand All @@ -142,4 +145,7 @@ To be tested.
### Database Encoding Issues
Currently, only the utf8 character set is supported.

### Data type limitation
The issue of the NUMERIC data type not supporting high-precision real numbers

## FAQ
8 changes: 7 additions & 1 deletion datax/plugin/reader/sqlite3/README_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ Sqlite3Reader通过使用dbmsreader中定义的查询流程调用go-etl自定义

| go-etl的类型 | sqlite3数据类型 |
| ------------ |--------------------|
| string | INTEGER、TEXT、REAL、BLOB |
| bigInt | INTEGER |
| decimal | REAL, NUMERIC |
| string | TEXT |
| bytes | BLOB |

## 性能报告

Expand All @@ -144,4 +147,7 @@ Sqlite3Reader通过使用dbmsreader中定义的查询流程调用go-etl自定义
### 数据库编码问题
目前仅支持utf8字符集

### 数据类型限制
数据类型NUMERIC不支持高精度实数的问题

## FAQ
8 changes: 5 additions & 3 deletions storage/database/sqlite3/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,16 @@ func NewFieldType(typ database.ColumnType) *FieldType {
f := &FieldType{
BaseFieldType: database.NewBaseFieldType(typ),
}
f.goType = database.GoTypeString
switch f.DatabaseTypeName() {
case "INTEGER", "BLOB", "NUMERIC", "REAL", "TEXT":
f.goType = database.GoTypeString
}
return f
}

// IsSupported - Indicates whether parsing is supported for a specific type.
func (f *FieldType) IsSupported() bool {
return true
//return f.GoType() != database.GoTypeUnknown
return f.GoType() != database.GoTypeUnknown
}

// GoType - Returns the Golang type used when processing numerical values.
Expand Down
14 changes: 14 additions & 0 deletions storage/database/sqlite3/field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,11 @@ func TestFieldType_IsSupportted(t *testing.T) {
f: NewFieldType(newMockColumnType("TEXT")),
want: true,
},
{
name: "6",
f: NewFieldType(newMockColumnType("TEXT1")),
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -453,6 +458,15 @@ func TestScanner_Scan(t *testing.T) {
},
wantErr: true,
},
{
name: "18",
s: NewScanner(NewField(database.NewBaseField(0,
"f1", NewFieldType(newMockColumnType("TEXT1"))))),
args: args{
src: 123,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions storage/database/sqlite3/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ func (t *Table) ExecParam(mode string, txOpts *sql.TxOptions) (database.Paramete
// ShouldRetry determines whether a retry is necessary.
func (t *Table) ShouldRetry(err error) bool {
switch cause := errors.Cause(err).(type) {
case sqlite3.Error:
return true
case *sqlite3.Error:
return false
default:
return cause == driver.ErrBadConn
}
Expand Down
83 changes: 82 additions & 1 deletion storage/database/sqlite3/table_test.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,40 @@
// Copyright 2020 the go-etl Authors.

//

// Licensed under the Apache License, Version 2.0 (the "License");

// you may not use this file except in compliance with the License.

// You may obtain a copy of the License at

//

// http://www.apache.org/licenses/LICENSE-2.0

//

// Unless required by applicable law or agreed to in writing, software

// distributed under the License is distributed on an "AS IS" BASIS,

// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

// See the License for the specific language governing permissions and

// limitations under the License.

package sqlite3

import (
"database/sql"
"database/sql/driver"
"net"
"reflect"
"testing"

"github.com/Breeze0806/go-etl/storage/database"
"github.com/mattn/go-sqlite3"
sqlite3 "github.com/mattn/go-sqlite3"
)

func TestTable_Quoted(t *testing.T) {
Expand Down Expand Up @@ -164,3 +178,70 @@ func TestTable_ShouldOneByOne(t *testing.T) {
})
}
}

func TestTable_ExecParam(t *testing.T) {
type args struct {
mode string
txOpts *sql.TxOptions
}
tests := []struct {
name string
tr *Table
args args
want database.Parameter
want1 bool
}{
{
name: "1",
tr: NewTable(database.NewBaseTable("", "", "table")),
args: args{
mode: "",
txOpts: nil,
},
want: nil,
want1: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1 := tt.tr.ExecParam(tt.args.mode, tt.args.txOpts)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Table.ExecParam() got = %v, want %v", got, tt.want)
}
if got1 != tt.want1 {
t.Errorf("Table.ExecParam() got1 = %v, want %v", got1, tt.want1)
}
})
}
}

func TestTable_AddField(t *testing.T) {
type args struct {
baseField *database.BaseField
}
tests := []struct {
name string
tr *Table
args args
want []database.Field
}{
{
name: "1",
tr: NewTable(database.NewBaseTable("", "schema", "table")),
args: args{
baseField: database.NewBaseField(0, "f1", database.NewBaseFieldType(&sql.ColumnType{})),
},
want: []database.Field{
NewField(database.NewBaseField(0, "f1", database.NewBaseFieldType(&sql.ColumnType{}))),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.tr.AddField(tt.args.baseField)
if !reflect.DeepEqual(tt.tr.Fields(), tt.want) {
t.Errorf("run %v Table.Fields() = %v want: %v", tt.name, tt.tr.Fields(), tt.want)
}
})
}
}

0 comments on commit de47f30

Please sign in to comment.