Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fixing #71

Merged
merged 1 commit into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 41 additions & 22 deletions connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ PG_FUNCTION_INFO_V1(sqlite_fdw_get_connections);
PG_FUNCTION_INFO_V1(sqlite_fdw_disconnect);
PG_FUNCTION_INFO_V1(sqlite_fdw_disconnect_all);

static sqlite3 *sqlite_open_db(const char *dbpath);
static void sqlite_make_new_connection(ConnCacheEntry *entry, ForeignServer *server);
void sqlite_do_sql_command(sqlite3 * conn, const char *sql, int level, List **busy_connection);
static void sqlite_begin_remote_xact(ConnCacheEntry *entry);
Expand Down Expand Up @@ -159,6 +160,11 @@ sqlite_get_connection(ForeignServer *server, bool truncatable)
entry->conn = NULL;
}

/*
* If cache entry doesn't have a connection, we have to establish a new
* connection. (If sqlite_open_db has an error, the cache entry will
* remain in a valid empty state, ie conn == NULL.)
*/
if (entry->conn == NULL)
sqlite_make_new_connection(entry, server);

Expand All @@ -182,6 +188,39 @@ sqlite_get_connection(ForeignServer *server, bool truncatable)
return entry->conn;
}

/*
* Open remote sqlite database using specified database path.
*/
static sqlite3 *
sqlite_open_db(const char *dbpath)
{
sqlite3 *conn = NULL;
int rc;
char *err;

rc = sqlite3_open(dbpath, &conn);
if (rc != SQLITE_OK)
ereport(ERROR,
(errcode(ERRCODE_FDW_UNABLE_TO_ESTABLISH_CONNECTION),
errmsg("failed to open SQLite DB. rc=%d path=%s", rc, dbpath)));

/* make 'LIKE' of SQLite case sensitive like PostgreSQL */
rc = sqlite3_exec(conn, "pragma case_sensitive_like=1",
NULL, NULL, &err);
if (rc != SQLITE_OK)
{
char *perr = pstrdup(err);

sqlite3_free(err);
sqlite3_close(conn);
ereport(ERROR,
(errcode(ERRCODE_FDW_UNABLE_TO_ESTABLISH_CONNECTION),
errmsg("failed to open SQLite DB. rc=%d err=%s", rc, perr)));
}

return conn;
}

/*
* Reset all transient state fields in the cached connection entry and
* establish new connection to the remote server.
Expand All @@ -190,8 +229,6 @@ static void
sqlite_make_new_connection(ConnCacheEntry *entry, ForeignServer *server)
{
const char *dbpath = NULL;
int rc;
char *err;
ListCell *lc;

Assert(entry->conn == NULL);
Expand All @@ -214,26 +251,8 @@ sqlite_make_new_connection(ConnCacheEntry *entry, ForeignServer *server)
entry->keep_connections = defGetBoolean(def);
}

rc = sqlite3_open(dbpath, &entry->conn);
if (rc != SQLITE_OK)
ereport(ERROR,
(errcode(ERRCODE_FDW_UNABLE_TO_ESTABLISH_CONNECTION),
errmsg("failed to open SQLite DB. rc=%d path=%s", rc, dbpath)));
/* make 'LIKE' of SQLite case sensitive like PostgreSQL */
rc = sqlite3_exec(entry->conn, "pragma case_sensitive_like=1",
NULL, NULL, &err);
if (rc != SQLITE_OK)
{
char *perr = pstrdup(err);

sqlite3_free(err);
sqlite3_close(entry->conn);
entry->conn = NULL;
ereport(ERROR,
(errcode(ERRCODE_FDW_UNABLE_TO_ESTABLISH_CONNECTION),
errmsg("failed to open SQLite DB. err=%s rc=%d", perr, rc)));
}

/* Try to make the connection */
entry->conn = sqlite_open_db(dbpath);
}

/*
Expand Down
152 changes: 76 additions & 76 deletions expected/11.17/extra/numeric.out
Original file line number Diff line number Diff line change
Expand Up @@ -978,17 +978,17 @@ SELECT t1.id1, t1.id2, t1.result, t2.expected
FROM num_result t1, num_exp_add t2
WHERE t1.id1 = t2.id1 AND t1.id2 = t2.id2
AND t1.result != t2.expected;
id1 | id2 | result | expected
-----+-----+-------------------+-------------------
2 | 2 | -68676984.430794 | -68676984.4307941
2 | 3 | -34338487.905397 | -34338487.9053971
2 | 6 | -34244590.6377667 | -34244590.6377668
2 | 9 | -59265296.2604444 | -59265296.2604445
3 | 2 | -34338487.905397 | -34338487.9053971
6 | 2 | -34244590.6377667 | -34244590.6377668
6 | 9 | -24832902.4674171 | -24832902.4674172
9 | 2 | -59265296.2604444 | -59265296.2604445
9 | 6 | -24832902.4674171 | -24832902.4674172
id1 | id2 | result | expected
-----+-----+----------------------+----------------------
2 | 2 | -68676984.4307940000 | -68676984.4307941000
2 | 3 | -34338487.9053970000 | -34338487.9053971000
2 | 6 | -34244590.6377667000 | -34244590.6377668000
2 | 9 | -59265296.2604444000 | -59265296.2604445000
3 | 2 | -34338487.9053970000 | -34338487.9053971000
6 | 2 | -34244590.6377667000 | -34244590.6377668000
6 | 9 | -24832902.4674171000 | -24832902.4674172000
9 | 2 | -59265296.2604444000 | -59265296.2604445000
9 | 6 | -24832902.4674171000 | -24832902.4674172000
(9 rows)

--Testcase 454:
Expand All @@ -1001,17 +1001,17 @@ SELECT t1.id1, t1.id2, t1.result, round(t2.expected, 10) as expected
FROM num_result t1, num_exp_add t2
WHERE t1.id1 = t2.id1 AND t1.id2 = t2.id2
AND t1.result != round(t2.expected, 10);
id1 | id2 | result | expected
-----+-----+-------------------+----------------------
2 | 2 | -68676984.430794 | -68676984.4307941000
2 | 3 | -34338487.905397 | -34338487.9053971000
2 | 6 | -34244590.6377667 | -34244590.6377668000
2 | 9 | -59265296.2604444 | -59265296.2604445000
3 | 2 | -34338487.905397 | -34338487.9053971000
6 | 2 | -34244590.6377667 | -34244590.6377668000
6 | 9 | -24832902.4674171 | -24832902.4674172000
9 | 2 | -59265296.2604444 | -59265296.2604445000
9 | 6 | -24832902.4674171 | -24832902.4674172000
id1 | id2 | result | expected
-----+-----+----------------------+----------------------
2 | 2 | -68676984.4307940000 | -68676984.4307941000
2 | 3 | -34338487.9053970000 | -34338487.9053971000
2 | 6 | -34244590.6377667000 | -34244590.6377668000
2 | 9 | -59265296.2604444000 | -59265296.2604445000
3 | 2 | -34338487.9053970000 | -34338487.9053971000
6 | 2 | -34244590.6377667000 | -34244590.6377668000
6 | 9 | -24832902.4674171000 | -24832902.4674172000
9 | 2 | -59265296.2604444000 | -59265296.2604445000
9 | 6 | -24832902.4674171000 | -24832902.4674172000
(9 rows)

-- ******************************
Expand All @@ -1027,10 +1027,10 @@ SELECT t1.id1, t1.id2, t1.result, t2.expected
FROM num_result t1, num_exp_sub t2
WHERE t1.id1 = t2.id1 AND t1.id2 = t2.id2
AND t1.result != t2.expected;
id1 | id2 | result | expected
-----+-----+------------------+-------------------
2 | 9 | -9411688.1703496 | -9411688.17034963
9 | 2 | 9411688.1703496 | 9411688.17034963
id1 | id2 | result | expected
-----+-----+---------------------+---------------------
2 | 9 | -9411688.1703496000 | -9411688.1703496300
9 | 2 | 9411688.1703496000 | 9411688.1703496300
(2 rows)

--Testcase 460:
Expand All @@ -1043,10 +1043,10 @@ SELECT t1.id1, t1.id2, t1.result, round(t2.expected, 40)
FROM num_result t1, num_exp_sub t2
WHERE t1.id1 = t2.id1 AND t1.id2 = t2.id2
AND t1.result != round(t2.expected, 40);
id1 | id2 | result | round
-----+-----+------------------+---------------------------------------------------
2 | 9 | -9411688.1703496 | -9411688.1703496300000000000000000000000000000000
9 | 2 | 9411688.1703496 | 9411688.1703496300000000000000000000000000000000
id1 | id2 | result | round
-----+-----+---------------------+---------------------------------------------------
2 | 9 | -9411688.1703496000 | -9411688.1703496300000000000000000000000000000000
9 | 2 | 9411688.1703496000 | 9411688.1703496300000000000000000000000000000000
(2 rows)

-- ******************************
Expand All @@ -1062,19 +1062,19 @@ SELECT t1.id1, t1.id2, t1.result, t2.expected
FROM num_result t1, num_exp_mul t2
WHERE t1.id1 = t2.id1 AND t1.id2 = t2.id2
AND t1.result != t2.expected;
id1 | id2 | result | expected
-----+-----+-------------------+-------------------
2 | 4 | -267821744976817 | -267821744976818
2 | 5 | -563049578578.768 | -563049578578.769
2 | 8 | -2571300635581.14 | -2571300635581.15
2 | 9 | 855948866655587 | 855948866655588
4 | 2 | -267821744976817 | -267821744976818
5 | 2 | -563049578578.768 | -563049578578.769
8 | 2 | -2571300635581.14 | -2571300635581.15
8 | 9 | -1866544013697.19 | -1866544013697.2
9 | 2 | 855948866655587 | 855948866655588
9 | 8 | -1866544013697.19 | -1866544013697.2
9 | 9 | 621345559900191 | 621345559900192
id1 | id2 | result | expected
-----+-----+-----------------------------+-----------------------------
2 | 4 | -267821744976817.0000000000 | -267821744976818.0000000000
2 | 5 | -563049578578.7680000000 | -563049578578.7690000000
2 | 8 | -2571300635581.1400000000 | -2571300635581.1500000000
2 | 9 | 855948866655587.0000000000 | 855948866655588.0000000000
4 | 2 | -267821744976817.0000000000 | -267821744976818.0000000000
5 | 2 | -563049578578.7680000000 | -563049578578.7690000000
8 | 2 | -2571300635581.1400000000 | -2571300635581.1500000000
8 | 9 | -1866544013697.1900000000 | -1866544013697.2000000000
9 | 2 | 855948866655587.0000000000 | 855948866655588.0000000000
9 | 8 | -1866544013697.1900000000 | -1866544013697.2000000000
9 | 9 | 621345559900191.0000000000 | 621345559900192.0000000000
(11 rows)

--Testcase 466:
Expand All @@ -1087,19 +1087,19 @@ SELECT t1.id1, t1.id2, t1.result, round(t2.expected, 30) as expected
FROM num_result t1, num_exp_mul t2
WHERE t1.id1 = t2.id1 AND t1.id2 = t2.id2
AND t1.result != round(t2.expected, 30);
id1 | id2 | result | expected
-----+-----+-------------------+-------------------------------------------------
2 | 4 | -267821744976817 | -267821744976818.000000000000000000000000000000
2 | 5 | -563049578578.768 | -563049578578.769000000000000000000000000000
2 | 8 | -2571300635581.14 | -2571300635581.150000000000000000000000000000
2 | 9 | 855948866655587 | 855948866655588.000000000000000000000000000000
4 | 2 | -267821744976817 | -267821744976818.000000000000000000000000000000
5 | 2 | -563049578578.768 | -563049578578.769000000000000000000000000000
8 | 2 | -2571300635581.14 | -2571300635581.150000000000000000000000000000
8 | 9 | -1866544013697.19 | -1866544013697.200000000000000000000000000000
9 | 2 | 855948866655587 | 855948866655588.000000000000000000000000000000
9 | 8 | -1866544013697.19 | -1866544013697.200000000000000000000000000000
9 | 9 | 621345559900191 | 621345559900192.000000000000000000000000000000
id1 | id2 | result | expected
-----+-----+-----------------------------+-------------------------------------------------
2 | 4 | -267821744976817.0000000000 | -267821744976818.000000000000000000000000000000
2 | 5 | -563049578578.7680000000 | -563049578578.769000000000000000000000000000
2 | 8 | -2571300635581.1400000000 | -2571300635581.150000000000000000000000000000
2 | 9 | 855948866655587.0000000000 | 855948866655588.000000000000000000000000000000
4 | 2 | -267821744976817.0000000000 | -267821744976818.000000000000000000000000000000
5 | 2 | -563049578578.7680000000 | -563049578578.769000000000000000000000000000
8 | 2 | -2571300635581.1400000000 | -2571300635581.150000000000000000000000000000
8 | 9 | -1866544013697.1900000000 | -1866544013697.200000000000000000000000000000
9 | 2 | 855948866655587.0000000000 | 855948866655588.000000000000000000000000000000
9 | 8 | -1866544013697.1900000000 | -1866544013697.200000000000000000000000000000
9 | 9 | 621345559900191.0000000000 | 621345559900192.000000000000000000000000000000
(11 rows)

-- ******************************
Expand All @@ -1116,10 +1116,10 @@ SELECT t1.id1, t1.id2, t1.result, t2.expected
FROM num_result t1, num_exp_div t2
WHERE t1.id1 = t2.id1 AND t1.id2 = t2.id2
AND t1.result != t2.expected;
id1 | id2 | result | expected
-----+-----+-------------------+-------------------
2 | 3 | -7967167.56737749 | -7967167.5673775
9 | 3 | -5783481.21694835 | -5783481.21694836
id1 | id2 | result | expected
-----+-----+---------------------+---------------------
2 | 3 | -7967167.5673774900 | -7967167.5673775000
9 | 3 | -5783481.2169483500 | -5783481.2169483600
(2 rows)

--Testcase 472:
Expand All @@ -1133,10 +1133,10 @@ SELECT t1.id1, t1.id2, t1.result, round(t2.expected, 80) as expected
FROM num_result t1, num_exp_div t2
WHERE t1.id1 = t2.id1 AND t1.id2 = t2.id2
AND t1.result != round(t2.expected, 80);
id1 | id2 | result | expected
-----+-----+-------------------+-------------------------------------------------------------------------------------------
2 | 3 | -7967167.56737749 | -7967167.56737750000000000000000000000000000000000000000000000000000000000000000000000000
9 | 3 | -5783481.21694835 | -5783481.21694836000000000000000000000000000000000000000000000000000000000000000000000000
id1 | id2 | result | expected
-----+-----+---------------------+-------------------------------------------------------------------------------------------
2 | 3 | -7967167.5673774900 | -7967167.56737750000000000000000000000000000000000000000000000000000000000000000000000000
9 | 3 | -5783481.2169483500 | -5783481.21694836000000000000000000000000000000000000000000000000000000000000000000000000
(2 rows)

-- ******************************
Expand Down Expand Up @@ -1206,9 +1206,9 @@ SELECT t1.id1, t1.result, t2.expected
FROM num_result t1, num_exp_power_10_ln t2
WHERE t1.id1 = t2.id
AND t1.result != t2.expected;
id1 | result | expected
-----+--------------------+--------------------
2 | 224790267919917000 | 224790267919918000
id1 | result | expected
-----+-------------------------------+-------------------------------
2 | 224790267919917000.0000000000 | 224790267919918000.0000000000
(1 row)

-- ******************************
Expand All @@ -1224,16 +1224,16 @@ SELECT AVG(val) FROM num_data;

--Testcase 488:
SELECT STDDEV(val) FROM num_data;
stddev
---------------------------
27791203.2875883484501033
stddev
-------------------------------
27791203.28758834845010326110
(1 row)

--Testcase 489:
SELECT VARIANCE(val) FROM num_data;
variance
----------------------------------
772350980172061.4271301683648535
variance
--------------------------------------
772350980172061.42713016836485351556
(1 row)

-- Check for appropriate rounding and overflow
Expand Down Expand Up @@ -1263,11 +1263,11 @@ INSERT INTO fract_only VALUES (8, '0.00017');
SELECT * FROM fract_only;
id | val
----+---------
1 | 0
2 | 0.1
1 | 0.0000
2 | 0.1000
4 | -0.9999
5 | 0.9999
7 | 0
7 | 0.0000
8 | 0.0002
(6 rows)

Expand Down
Loading