From 9a3db621a04f7c916a369a79c9d8c52b885a10f0 Mon Sep 17 00:00:00 2001 From: Dominik Kraus Date: Fri, 29 Apr 2022 18:06:10 +0200 Subject: [PATCH 1/6] Add build tag sqlite_math_functions to enable built-in scalar math functions --- README.md | 1 + sqlite3_opt_math_functions.go | 15 +++++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 sqlite3_opt_math_functions.go diff --git a/README.md b/README.md index 25a24965..98156c4a 100644 --- a/README.md +++ b/README.md @@ -173,6 +173,7 @@ go build --tags "icu json1 fts5 secure_delete" | Introspect PRAGMAS | sqlite_introspect | This option adds some extra PRAGMA statements. | | JSON SQL Functions | sqlite_json | When this option is defined in the amalgamation, the JSON SQL functions are added to the build automatically | | OS Trace | sqlite_os_trace | This option enables OSTRACE() debug logging. This can be verbose and should not be used in production. | +| Math Functions | sqlite_math_functions | This compile-time option enables built-in scalar math functions | | Pre Update Hook | sqlite_preupdate_hook | Registers a callback function that is invoked prior to each INSERT, UPDATE, and DELETE operation on a database table. | | Secure Delete | sqlite_secure_delete | This compile-time option changes the default setting of the secure_delete pragma.

When this option is not used, secure_delete defaults to off. When this option is present, secure_delete defaults to on.

The secure_delete setting causes deleted content to be overwritten with zeros. There is a small performance penalty since additional I/O must occur.

On the other hand, secure_delete can prevent fragments of sensitive information from lingering in unused parts of the database file after it has been deleted. See the documentation on the secure_delete pragma for additional information | | Secure Delete (FAST) | sqlite_secure_delete_fast | For more information see [PRAGMA secure_delete](https://www.sqlite.org/pragma.html#pragma_secure_delete) | diff --git a/sqlite3_opt_math_functions.go b/sqlite3_opt_math_functions.go new file mode 100644 index 00000000..37a044ae --- /dev/null +++ b/sqlite3_opt_math_functions.go @@ -0,0 +1,15 @@ +// Copyright (C) 2019 Yasuhiro Matsumoto . +// Copyright (C) 2018 G.J.R. Timmer . +// +// Use of this source code is governed by an MIT-style +// license that can be found in the LICENSE file. + +// +build sqlite_math_functions + +package sqlite3 + +/* +#cgo CFLAGS: -DSQLITE_ENABLE_MATH_FUNCTIONS +#cgo LDFLAGS: -lm +*/ +import "C" From a2ece2e449debe29ed104b31c9748e4cb147f3d9 Mon Sep 17 00:00:00 2001 From: Levi Gruspe Date: Sun, 19 Jun 2022 16:37:19 +0300 Subject: [PATCH 2/6] Test if math functions are compiled in --- .github/workflows/go.yaml | 4 ++-- sqlite3_opt_math_functions_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 sqlite3_opt_math_functions_test.go diff --git a/.github/workflows/go.yaml b/.github/workflows/go.yaml index ec8b30ab..2030c57b 100644 --- a/.github/workflows/go.yaml +++ b/.github/workflows/go.yaml @@ -44,7 +44,7 @@ jobs: run: go-acc . -- -race -v -tags "libsqlite3" - name: 'Tags: full' - run: go-acc . -- -race -v -tags "sqlite_allow_uri_authority sqlite_app_armor sqlite_foreign_keys sqlite_fts5 sqlite_icu sqlite_introspect sqlite_json sqlite_os_trace sqlite_preupdate_hook sqlite_secure_delete sqlite_see sqlite_stat4 sqlite_trace sqlite_userauth sqlite_vacuum_incr sqlite_vtable sqlite_unlock_notify sqlite_column_metadata" + run: go-acc . -- -race -v -tags "sqlite_allow_uri_authority sqlite_app_armor sqlite_column_metadata sqlite_foreign_keys sqlite_fts5 sqlite_icu sqlite_introspect sqlite_json sqlite_math_functions sqlite_os_trace sqlite_preupdate_hook sqlite_secure_delete sqlite_see sqlite_stat4 sqlite_trace sqlite_unlock_notify sqlite_userauth sqlite_vacuum_incr sqlite_vtable" - name: 'Tags: vacuum' run: go-acc . -- -race -v -tags "sqlite_vacuum_full" @@ -99,7 +99,7 @@ jobs: - name: 'Tags: full' run: | echo 'skip this test' - echo go build -race -v -tags "sqlite_allow_uri_authority sqlite_app_armor sqlite_foreign_keys sqlite_fts5 sqlite_icu sqlite_introspect sqlite_json sqlite_preupdate_hook sqlite_secure_delete sqlite_see sqlite_stat4 sqlite_trace sqlite_userauth sqlite_vacuum_incr sqlite_vtable sqlite_unlock_notify" + echo go build -race -v -tags "sqlite_allow_uri_authority sqlite_app_armor sqlite_column_metadata sqlite_foreign_keys sqlite_fts5 sqlite_icu sqlite_introspect sqlite_json sqlite_math_functions sqlite_preupdate_hook sqlite_secure_delete sqlite_see sqlite_stat4 sqlite_trace sqlite_unlock_notify sqlite_userauth sqlite_vacuum_incr sqlite_vtable" shell: msys2 {0} - name: 'Tags: vacuum' diff --git a/sqlite3_opt_math_functions_test.go b/sqlite3_opt_math_functions_test.go new file mode 100644 index 00000000..ef5d30ff --- /dev/null +++ b/sqlite3_opt_math_functions_test.go @@ -0,0 +1,28 @@ +// +build sqlite_math_functions + +package sqlite3 + +import ( + "database/sql" + "testing" +) + +func TestMathFunctions(t *testing.T) { + db, err := sql.Open("sqlite3", ":memory:") + if err != nil { + t.Fatal("Failed to open database:", err) + } + defer db.Close() + + queries := []string{ + `SELECT acos(1)`, + `SELECT log(10, 100)`, + `SELECT power(2, 2)`, + } + + for _, query := range queries { + if _, err := db.Exec(query); err != nil { + t.Fatal("Failed to call math function:", err) + } + } +} From 68833c4576e8dfb7a8cc984274a28a3f33cde16a Mon Sep 17 00:00:00 2001 From: Levi Gruspe Date: Mon, 20 Jun 2022 09:06:36 +0300 Subject: [PATCH 3/6] Add link to SQLite docs on built-in math functions --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 98156c4a..913bf6a7 100644 --- a/README.md +++ b/README.md @@ -172,8 +172,8 @@ go build --tags "icu json1 fts5 secure_delete" | International Components for Unicode | sqlite_icu | This option causes the International Components for Unicode or "ICU" extension to SQLite to be added to the build | | Introspect PRAGMAS | sqlite_introspect | This option adds some extra PRAGMA statements.
  • PRAGMA function_list
  • PRAGMA module_list
  • PRAGMA pragma_list
| | JSON SQL Functions | sqlite_json | When this option is defined in the amalgamation, the JSON SQL functions are added to the build automatically | +| Math Functions | sqlite_math_functions | This compile-time option enables built-in scalar math functions. For more information see [Built-In Mathematical SQL Functions](https://www.sqlite.org/lang_mathfunc.html) | | OS Trace | sqlite_os_trace | This option enables OSTRACE() debug logging. This can be verbose and should not be used in production. | -| Math Functions | sqlite_math_functions | This compile-time option enables built-in scalar math functions | | Pre Update Hook | sqlite_preupdate_hook | Registers a callback function that is invoked prior to each INSERT, UPDATE, and DELETE operation on a database table. | | Secure Delete | sqlite_secure_delete | This compile-time option changes the default setting of the secure_delete pragma.

When this option is not used, secure_delete defaults to off. When this option is present, secure_delete defaults to on.

The secure_delete setting causes deleted content to be overwritten with zeros. There is a small performance penalty since additional I/O must occur.

On the other hand, secure_delete can prevent fragments of sensitive information from lingering in unused parts of the database file after it has been deleted. See the documentation on the secure_delete pragma for additional information | | Secure Delete (FAST) | sqlite_secure_delete_fast | For more information see [PRAGMA secure_delete](https://www.sqlite.org/pragma.html#pragma_secure_delete) | From ebe26e19eae088a6f67a994371c40246cef50621 Mon Sep 17 00:00:00 2001 From: Levi Gruspe Date: Tue, 16 Aug 2022 22:48:51 +0300 Subject: [PATCH 4/6] Fix copyright notice --- sqlite3_opt_math_functions.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sqlite3_opt_math_functions.go b/sqlite3_opt_math_functions.go index 37a044ae..7cd68d3f 100644 --- a/sqlite3_opt_math_functions.go +++ b/sqlite3_opt_math_functions.go @@ -1,5 +1,4 @@ -// Copyright (C) 2019 Yasuhiro Matsumoto . -// Copyright (C) 2018 G.J.R. Timmer . +// Copyright (C) 2022 Yasuhiro Matsumoto . // // Use of this source code is governed by an MIT-style // license that can be found in the LICENSE file. From fe6ab896587c0d36d84f6f8feeb698be950eaa5b Mon Sep 17 00:00:00 2001 From: Levi Gruspe Date: Tue, 16 Aug 2022 23:03:01 +0300 Subject: [PATCH 5/6] Use db.Query instead of db.Exec for select statement in test --- sqlite3_opt_math_functions_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sqlite3_opt_math_functions_test.go b/sqlite3_opt_math_functions_test.go index ef5d30ff..6d9e8ef1 100644 --- a/sqlite3_opt_math_functions_test.go +++ b/sqlite3_opt_math_functions_test.go @@ -21,7 +21,7 @@ func TestMathFunctions(t *testing.T) { } for _, query := range queries { - if _, err := db.Exec(query); err != nil { + if _, err := db.Query(query); err != nil { t.Fatal("Failed to call math function:", err) } } From d37b86e1aabd02e5f7901199791e89693abff31c Mon Sep 17 00:00:00 2001 From: Levi Gruspe Date: Sat, 17 Sep 2022 21:49:48 +0800 Subject: [PATCH 6/6] Use QueryRow in test --- sqlite3_opt_math_functions_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sqlite3_opt_math_functions_test.go b/sqlite3_opt_math_functions_test.go index 6d9e8ef1..6ff076b4 100644 --- a/sqlite3_opt_math_functions_test.go +++ b/sqlite3_opt_math_functions_test.go @@ -21,8 +21,9 @@ func TestMathFunctions(t *testing.T) { } for _, query := range queries { - if _, err := db.Query(query); err != nil { - t.Fatal("Failed to call math function:", err) + var result float64 + if err := db.QueryRow(query).Scan(&result); err != nil { + t.Errorf("invoking math function query %q: %v", query, err) } } }