Skip to content

Commit bafcb64

Browse files
committed
feat(compression): enabled by default
1 parent e87c739 commit bafcb64

File tree

5 files changed

+154
-19
lines changed

5 files changed

+154
-19
lines changed

cli/sqlc.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ func parseParameters() (Parameter, error) {
317317
p["--host"] = getFirstNoneEmptyString([]string{dropError(p.String("--host")), "localhost"})
318318
p["--port"] = getFirstNoneEmptyString([]string{dropError(p.String("--port")), "8860"})
319319
p["--timeout"] = getFirstNoneEmptyString([]string{dropError(p.String("--timeout")), "10"})
320-
p["--compress"] = getFirstNoneEmptyString([]string{dropError(p.String("--compress")), "NO"})
320+
p["--compress"] = getFirstNoneEmptyString([]string{dropError(p.String("--compress")), sqlitecloud.CompressModeLZ4})
321321
p["--tls"] = getFirstNoneEmptyString([]string{dropError(p.String("--tls")), "YES"})
322322
p["--separator"] = getFirstNoneEmptyString([]string{dropError(p.String("--separator")), dropError(sqlitecloud.GetDefaultSeparatorForOutputFormat(outputformat)), "|"})
323323
p["--maxdata"] = getFirstNoneEmptyString([]string{dropError(p.String("--maxdata")), "0"})
@@ -420,6 +420,7 @@ func main() {
420420
Password: parameter.Password,
421421
Database: parameter.Database,
422422
Timeout: time.Duration(parameter.Timeout) * time.Second,
423+
Compression: parameter.Compress == sqlitecloud.CompressModeLZ4,
423424
CompressMode: parameter.Compress,
424425
Zerotext: parameter.Zerotext,
425426
Memory: parameter.Memory,

connection.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ func ParseConnectionString(ConnectionString string) (config *SQCloudConfig, err
113113
config.Password, _ = u.User.Password()
114114
config.Database = strings.TrimPrefix(u.Path, "/")
115115
config.Timeout = 0
116-
config.Compression = false
117-
config.CompressMode = CompressModeNo
116+
config.Compression = true // enabled by default
117+
config.CompressMode = CompressModeLZ4
118118
config.Zerotext = false
119119
config.Memory = false
120120
config.Create = false
@@ -146,14 +146,18 @@ func ParseConnectionString(ConnectionString string) (config *SQCloudConfig, err
146146
}
147147

148148
case "compress":
149-
config.CompressMode = strings.ToUpper(lastLiteral)
150-
if config.CompressMode == CompressModeLZ4 {
151-
config.Compression = true
149+
mode := strings.ToUpper(lastLiteral)
150+
if mode == CompressModeNo {
151+
config.CompressMode = CompressModeNo
152+
config.Compression = false
153+
} else if enable, err := parseBool(mode, config.Compression); err == nil && !enable {
154+
config.CompressMode = CompressModeNo
155+
config.Compression = false
152156
}
153157
case "compression":
154-
if b, err := parseBool(lastLiteral, config.Compression); err == nil && b {
155-
config.Compression = true
156-
config.CompressMode = CompressModeLZ4
158+
if enable, err := parseBool(lastLiteral, config.Compression); err == nil && !enable {
159+
config.Compression = false
160+
config.CompressMode = CompressModeNo
157161
}
158162
case "zerotext":
159163
if b, err := parseBool(lastLiteral, config.Zerotext); err == nil {
@@ -239,12 +243,12 @@ func (this *SQCloud) CheckConnectionParameter() error {
239243
return fmt.Errorf("Invalid hostname (%s)", this.Host)
240244
}
241245

242-
ip := net.ParseIP(this.Host)
243-
if ip == nil {
244-
if _, err := net.LookupHost(this.Host); err != nil {
245-
return errors.New(fmt.Sprintf("Can't resolve hostname (%s)", this.Host))
246-
}
247-
}
246+
// ip := net.ParseIP(this.Host)
247+
// if ip == nil {
248+
// if _, err := net.LookupHost(this.Host); err != nil {
249+
// return errors.New(fmt.Sprintf("Can't resolve hostname (%s)", this.Host))
250+
// }
251+
// }
248252

249253
if this.Port == 0 {
250254
this.Port = 8860
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"flag"
55
"log"
66
"os"
7+
"testing"
78

89
"github.com/joho/godotenv"
10+
sqlitecloud "github.com/sqlitecloud/go-sdk"
911
)
1012

1113
func init() {
@@ -25,3 +27,17 @@ func contains[T comparable](s []T, e T) bool {
2527
}
2628
return false
2729
}
30+
31+
func setupDatabase(t *testing.T) (*sqlitecloud.SQCloud, func()) {
32+
connectionString, _ := os.LookupEnv("SQLITE_CONNECTION_STRING")
33+
apikey, _ := os.LookupEnv("SQLITE_API_KEY")
34+
35+
db, err := sqlitecloud.Connect(connectionString + "?apikey=" + apikey)
36+
if err != nil {
37+
t.Fatal("Connection error: ", err.Error())
38+
}
39+
40+
return db, func() {
41+
db.Close()
42+
}
43+
}

test/server_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"testing"
2525

2626
sqlitecloud "github.com/sqlitecloud/go-sdk"
27+
"github.com/stretchr/testify/assert"
2728
)
2829

2930
const testDbnameServer = "test-gosdk-server-db.sqlite"
@@ -213,3 +214,16 @@ func TestServer(t *testing.T) {
213214
// }
214215
// fmt.Printf( "ok.\r\n" )
215216
}
217+
218+
func TestCompressionEnabledByDefault(t *testing.T) {
219+
db, cleaup := setupDatabase(t)
220+
defer cleaup()
221+
222+
result, _ := db.Select("GET CLIENT KEY COMPRESSION")
223+
value, err := result.GetString()
224+
if err != nil {
225+
t.Fatal(err.Error())
226+
}
227+
228+
assert.Equal(t, "1", value)
229+
}

test/unit/connection_test.go

Lines changed: 104 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ func TestParseConnectionString(t *testing.T) {
2020
Password: "",
2121
Database: "mydatabase",
2222
Timeout: time.Duration(11) * time.Second,
23-
CompressMode: "YES",
23+
Compression: true,
24+
CompressMode: sqlitecloud.CompressModeLZ4,
2425
Secure: true,
2526
TlsInsecureSkipVerify: false,
2627
Pem: "",
@@ -50,7 +51,8 @@ func TestParseConnectionStringWithAPIKey(t *testing.T) {
5051
Password: "pass",
5152
Database: "dbname",
5253
Timeout: time.Duration(11) * time.Second,
53-
CompressMode: "TRUE",
54+
Compression: true,
55+
CompressMode: sqlitecloud.CompressModeLZ4,
5456
Secure: true,
5557
TlsInsecureSkipVerify: false,
5658
Pem: "",
@@ -104,8 +106,8 @@ func TestParseConnectionStringWithParameters(t *testing.T) {
104106
{
105107
param: "compression",
106108
configParam: "Compression",
107-
value: "true",
108-
expectedValue: true,
109+
value: "false",
110+
expectedValue: false,
109111
},
110112
{
111113
param: "zerotext",
@@ -203,6 +205,104 @@ func TestParseConnectionStringWithParameters(t *testing.T) {
203205
}
204206
}
205207

208+
func TestParseConnectionStringCompressionCombinations(t *testing.T) {
209+
tests := []struct {
210+
param string
211+
configParam string
212+
value string
213+
expectedValue any
214+
}{
215+
{
216+
param: "compression",
217+
configParam: "Compression",
218+
value: "false",
219+
expectedValue: false,
220+
},
221+
{
222+
param: "compression",
223+
configParam: "Compression",
224+
value: "disabled",
225+
expectedValue: false,
226+
},
227+
{
228+
param: "compression",
229+
configParam: "Compression",
230+
// value not supported
231+
value: "no",
232+
expectedValue: true,
233+
},
234+
{
235+
param: "compression",
236+
configParam: "Compression",
237+
value: "0",
238+
expectedValue: false,
239+
},
240+
{
241+
param: "compression",
242+
configParam: "Compression",
243+
value: "true",
244+
expectedValue: true,
245+
},
246+
{
247+
param: "compression",
248+
configParam: "Compression",
249+
value: "enabled",
250+
expectedValue: true,
251+
},
252+
{
253+
param: "compression",
254+
configParam: "Compression",
255+
value: "yes",
256+
expectedValue: true,
257+
},
258+
{
259+
param: "compression",
260+
configParam: "Compression",
261+
value: "1",
262+
expectedValue: true,
263+
},
264+
{
265+
param: "compress",
266+
configParam: "CompressMode",
267+
value: "lz4",
268+
expectedValue: sqlitecloud.CompressModeLZ4,
269+
},
270+
{
271+
param: "compress",
272+
configParam: "Compression",
273+
value: "lz4",
274+
expectedValue: true,
275+
},
276+
{
277+
param: "compress",
278+
configParam: "CompressMode",
279+
value: "no",
280+
expectedValue: sqlitecloud.CompressModeNo,
281+
},
282+
{
283+
param: "compress",
284+
configParam: "Compression",
285+
value: "no",
286+
expectedValue: false,
287+
},
288+
}
289+
290+
for _, tt := range tests {
291+
t.Run(tt.param, func(t *testing.T) {
292+
config, err := sqlitecloud.ParseConnectionString("sqlitecloud://myhost.sqlite.cloud/mydatabase?" + tt.param + "=" + tt.value)
293+
294+
assert.NoError(t, err)
295+
296+
actualValue := reflect.ValueOf(*config).FieldByName(tt.configParam)
297+
if !actualValue.IsValid() {
298+
t.Fatalf("Field %s not found in config", tt.configParam)
299+
} else {
300+
assert.Equal(t, tt.expectedValue, actualValue.Interface())
301+
}
302+
})
303+
}
304+
}
305+
206306
func TestParseConnectionStringWithTLSParameter(t *testing.T) {
207307
tests := []struct {
208308
tlsValue string

0 commit comments

Comments
 (0)