Commit Graph

93 Commits

Author SHA1 Message Date
Harald Eilertsen
1e92aeb7f9 Improve dba_pdo::insert function 2025-11-13 16:14:05 +00:00
Mario
980fedadc0 dba_pdo: return results when using common table expressions (CTE) 2025-05-23 09:47:25 +00:00
Harald Eilertsen
e39b2eb7b9 Add an dba_pdo::update method
This is a convenience funcition to make it easier to update an existing
row in a database table.
2025-01-29 12:42:44 +01:00
Harald Eilertsen
9f8248cc9c Use returning clause on dba_pdo::insert where supported
By using the returning clause when inserting a new record, we get back
the inserted record right away; thus saving us an extra roundtrip to the
database.

Both PostgreSQL and MariaDB supports this clause, but MySQL don't. In
that case we fall back to manually fetching the last inserted row.
2025-01-29 12:42:44 +01:00
Harald Eilertsen
71e4326606 Fetch the server version on dba_pdo::connect.
The server version string is useful to among other things check whether
we are running on a real MySQL system, or MariaDB. Instead of fetching
it every time we need it, cache it in the dba_pdo object when we connect
to the db.
2025-01-29 12:42:44 +01:00
Harald Eilertsen
4625ffa6b4 Fix return value from dba_pdo::q()
According to the documentation, this function should never return null.
Still that's what it did if a non-select query raised a PDOException.
This patch fixes this, so that it so that the function conforms to the
docs.
2025-01-29 12:42:44 +01:00
Harald Eilertsen
3e6a646603 Add an insert method to dba_pdo
A common use case is to insert a record into a database table, but also
instantiate an object from the inserted data. This requires that we know
the value of any default or calculated columns that is filled in by the
database when the row is inserter.

This patch adds a `insert` method to pda_dbo that will insert a row, and
immediately fetch the row back from the database – including the default
and calculated values not specified by the insert itself.
2025-01-29 12:42:36 +01:00
Harald Eilertsen
25dbc8a9f6 include/dba: Make Dba driver transaction aware.
This patch introduced database transaction support to the Dba driver via
the DbaTransaction class.

The goal of this is to allow the driver control over the creation and
finalization of database transactions.

Until now code that has needed transaction support has done so directly
by issuing "BEGIN", "ROLLBACK" and "COMMIT" commands to the underlying
database directly.

This has several disadvantages:

  - We do have no control or knowledge of whether any transactions being
	active.

  - Since transactions can not be nested, we run the risk of unrelated
	code trying to create a transaction when one is already active.

  - Code using transactions are not testable, as the test runner wraps
	all tests within a transaction to begin with.

This patch should eliminate all these problems.

A transaction is started by instantiating the DbaTransaction class:

    $my_transaction = new \DbaTransaction();

The transaction will automatically be _rolled back_ if it has not been
committed before the instance is destroyed. (When the variable holding
it goes out of scope, i.e when the containing function returns.)

A transaction is committed like this:

    $my_transaction->commit();

This will immediately commit the changes in the transaction, and the
transaction will be marked as committed, so it will not be attempted to
be rolled back on destruction.

I have chosen to "ignore" the problem of nested transactions by having
the DbaTransaction class _not_ initiate a new transaction if one is
already active. This also makes the rollback and commit actions of the
DbaTransaction class into no-ops.

An alternative would be to simulate nested transactions by using save
points if a transaction is already active. However, I'm unsure about
wether there's any safe way to avoid all potential pitfalls when doing
that.

In any case, nested transactions should preferably be avoided, and
afaict we don't rely on that in any of the existing code. The reason we
need to support it in some way is that it's needed for testing where the
code under test is creating a transaction on it's own. (Since each test
is run within a db transaction to begin with.)

Also, I have taken the liberty to assume a PDO based db driver for this
stuff. I don't think that's going to be a problem, as that's the only
thing supported by the rest of the code in any case.
2024-02-26 15:11:39 +01:00
Mario Vavti
2bab7e2693 prevent duplicate definitions 2023-04-13 18:51:13 +02:00
Mario
35877b1382 allow to override the DB charset via the $db_charset variable in .htconfig.php 2022-02-10 18:57:44 +00:00
Mario
c185685f2d pdo: add the charset to the connection string 2022-02-09 08:57:27 +00:00
Hilmar R
c26dede97f get dev 2021-03-01 18:48:11 +01:00
Mario
43aea3ce38 we need 24h format 2020-01-18 20:05:05 +00:00
Mario
cbaf4b7536 introduce db_str_to_date() 2020-01-18 20:00:37 +00:00
Mario Vavti
fbe8d6144a port db_indexes() from zap 2019-05-13 10:46:12 +02:00
Daniel Lowe
9a6531e2a2 Fix infinite loop using postgres as backend
unescapebin is handed a string in some cases, and it causes an infinite
loop when it does.  This ensures that the argument is a resource before
loading its contents.
2019-04-21 11:14:17 +00:00
Max Kostikov
42f96dc7a6 Fix use UNIX socket file to connect DB 2019-04-10 14:13:16 +02:00
Andrew Manning
108a892a11 Remove DNS check for database connection during installation. Causes friction with Docker deployment. 2018-11-09 05:31:55 -05:00
Mario Vavti
5c4fbbebe1 refine stream filter and forum notification behaviour (mostly for performance). personal posts will be displayed in unthreaded mode and clicking a forum notification will show only the unseen items of the forum in unthreaded mode. group the filters in the widget - threaded at the top, unthreaded beneath. 2018-10-24 20:58:25 +02:00
zotlabs
cd200ee706 The channel import page seems to have missed the big theme cleanup of the last couple of years. 2018-05-01 19:38:32 -07:00
zotlabs
518db97962 never mind. dbg(1) now gives you the basic debug result and dbg(2) gives you the verbose debug option with all the results. 2018-04-14 16:56:30 -07:00
zotlabs
fdf5799857 allow dbg(2) to only return the canonical SQL used in the query and the number of results, but not log every single result. Maybe we should use 1 for this and 2 for the full results, but I didn't really want to change the way people do things today. If anybody has a strong opinion about doing this, go ahead and change it and send a note to the developer forum. 2018-04-14 16:47:51 -07:00
Klaus Weidenbach
4bf0c9e36a Add tests for non existent tables.
Prevent PHP warnings "Undefined variable" in dba_pdo::q();
2018-01-28 23:02:46 +01:00
Klaus Weidenbach
509844fd7e 🔥 Cleanup old database related files.
Remove non used database drivers, remove unused methods.
Improve documentation.
2018-01-28 23:02:15 +01:00
Mario Vavti
a86b260f73 query optimisations for notifications - use a specific index only 2018-01-28 12:09:47 +01:00
Klaus Weidenbach
8a60bb4619 Change dsn port handling in dba_pdo. 2018-01-18 23:46:49 +01:00
zotlabs
2d119c81a4 turn platform name and std_version into config variables 2017-07-13 20:18:19 -07:00
zotlabs
6c771f84ce remove unused page_widgets.php include and provide a general function for loading sql from file to use in the cdav migration 2017-06-22 22:43:08 -07:00
zotlabs
3a00140797 remove php version checks for older ( < 5.4) code incompatibilities 2017-05-23 21:20:40 -07:00
zotlabs
1c8c7479b8 Merge branch 'dev' of https://github.com/redmatrix/hubzilla into xdev_merge 2017-05-22 03:51:58 -07:00
Mario Vavti
a44c908147 silence php warning during install 2017-05-17 11:24:27 +02:00
zotlabs
33ff7bf968 provide platform specific install script 2017-03-31 10:27:28 +02:00
zotlabs
5c63f7dd58 provide platform specific install script 2017-03-25 14:19:32 -07:00
zotlabs
6f3493bb3c db issues 2017-03-12 15:50:47 -07:00
zotlabs
6cf17c7bcc use array_shift - it's cleaner 2016-12-15 16:41:01 -08:00
zotlabs
83a4999dbe issue #606, postgres binary data handling under PDO and HHVM when passed null 2016-11-30 19:45:46 -08:00
zotlabs
793047919d missed this from the earlier checkin 2016-11-29 02:57:29 -08:00
zotlabs
3b6248cb64 dba_pdo: return false on q() DB error like the old driver did 2016-11-17 15:15:34 -08:00
zotlabs
b5c72611b4 change log string on pdo log messages 2016-10-27 15:21:40 -07:00
zotlabs
88a68b941f put all dns checking into one function, allow it to be ignored 2016-10-25 18:27:32 -07:00
zotlabs
084b41fc2c first cut at edit activities 2016-10-25 16:21:56 -07:00
zotlabs
7b713e2576 pdo - fetch assoc only 2016-10-25 16:15:39 -07:00
zotlabs
25982f0475 set port if non-zero (instead of non-null) 2016-10-24 18:55:11 -07:00
zotlabs
20194bed42 this seems to work, but there are unanswered questions and is still undergoing investigation. It appears that the data stored with os_content = 1 is not being escaped in all circumstances or the scaled image data is being escaped twice. 2016-10-23 21:27:10 -07:00
zotlabs
39f0707201 fetch bytea as stream 2016-10-23 17:53:34 -07:00
zotlabs
d30892ea60 pdo hacks 2016-10-23 17:05:08 -07:00
zotlabs
04ac04e0ad allow a dsn override to the database via the server argument. This could be used to allow unix domain sockets and other unusual configurations. 2016-10-20 17:04:43 -07:00
zotlabs
29340152b6 pdo - cleanup 2016-10-20 16:21:15 -07:00
zotlabs
e2e3b81f32 more pdo tweaks 2016-10-20 16:13:06 -07:00
zotlabs
b4b5eb5bab pdo testing 2016-10-20 15:45:48 -07:00