Connection.LastInsertId is a new property that returns the value of the serial field used in the last inserted row.

[GB.DB]
* NEW: Connection.LastInsertId is a new property that returns the value of the serial field used in the last inserted row. It is supported on MySQL, PostgreSQL and SQLite3 only.
* BUG: Connection.Handle is correctly declared now.

[GB.DB.MYSQL]
* NEW: Support for Connection.LastInsertId.

[GB.DB.ODBC]
* NEW: Raise an error if Connection.LastInsertId is used.

[GB.DB.POSTGRESQL]
* NEW: Support for Connection.LastInsertId.

[GB.DB.SQLITE2]
* NEW: Raise an error if Connection.LastInsertId is used.

[GB.DB.SQLITE3]
* NEW: Support for Connection.LastInsertId.
This commit is contained in:
gambas 2018-03-20 16:05:33 +01:00
parent 76ac4ee2dd
commit e07ac2e4f2
9 changed files with 113 additions and 5 deletions

View File

@ -946,6 +946,29 @@ static int exec_query(DB_DATABASE *db, const char *query, DB_RESULT *result, con
}
/*****************************************************************************
get_last_insert_id()
Return the value of the last serial field used in an INSERT statement
<db> is the database handle, as returned by open_database()
*****************************************************************************/
static int64_t get_last_insert_id(DB_DATABASE *db)
{
MYSQL_RES *res;
MYSQL_ROW row;
if (do_query(db, "Unable to retrieve last insert id", &res, "select last_insert_id();", 0))
return -1;
row = mysql_fetch_row(res);
return atoll(row[0]);
}
/*****************************************************************************
query_init()

View File

@ -1211,6 +1211,7 @@ fflush(stderr);
<result> can be NULL, when we don't care getting the result.
*****************************************************************************/
static int exec_query(DB_DATABASE *db, const char *query, DB_RESULT * result, const char *err)
{
#ifdef ODBC_DEBUG_HEADER
@ -1329,6 +1330,22 @@ fflush(stderr);
}
/*****************************************************************************
get_last_insert_id()
Return the value of the last serial field used in an INSERT statement
<db> is the database handle, as returned by open_database()
*****************************************************************************/
static int64_t get_last_insert_id(DB_DATABASE *db)
{
GB.Error("Unsupported feature");
return -1;
}
/*****************************************************************************
query_init()

View File

@ -515,8 +515,7 @@ static void query_get_param(int index, char **str, int *len, char quote)
/* Internal function to run a query */
static int do_query(DB_DATABASE *db, const char *error, PGresult **pres,
const char *qtemp, int nsubst, ...)
static int do_query(DB_DATABASE *db, const char *error, PGresult **pres, const char *qtemp, int nsubst, ...)
{
PGconn *conn = (PGconn *)db->handle;
va_list args;
@ -951,6 +950,25 @@ static int exec_query(DB_DATABASE *db, const char *query, DB_RESULT *result, con
}
/*****************************************************************************
get_last_insert_id()
Return the value of the last serial field used in an INSERT statement
<db> is the database handle, as returned by open_database()
*****************************************************************************/
static int64_t get_last_insert_id(DB_DATABASE *db)
{
PGresult *res;
if (do_query(db, "Unable to retrieve last insert id: &1", &res, "select lastval()", 0))
return -1;
return atoll(PQgetvalue(res, 0, 0));
}
/*****************************************************************************

View File

@ -752,6 +752,23 @@ static int exec_query(DB_DATABASE *db, const char *query, DB_RESULT *result, con
}
/*****************************************************************************
get_last_insert_id()
Return the value of the last serial field used in an INSERT statement
<db> is the database handle, as returned by open_database()
*****************************************************************************/
static int64_t get_last_insert_id(DB_DATABASE *db)
{
GB.Error("Unsupported feature");
return -1;
}
/*****************************************************************************
query_init()

View File

@ -791,6 +791,27 @@ static int exec_query(DB_DATABASE *db, const char *query, DB_RESULT *result, con
}
/*****************************************************************************
get_last_insert_id()
Return the value of the last serial field used in an INSERT statement
<db> is the database handle, as returned by open_database()
*****************************************************************************/
static int64_t get_last_insert_id(DB_DATABASE *db)
{
SQLITE_RESULT *res;
if (do_query(db, "Unable to retrieve last insert id", &res, "select last_insert_rowid();", 0))
return -1;
return atoll(sqlite_query_get_string(res, 0, 0));
}
/*****************************************************************************
query_init()

View File

@ -763,6 +763,13 @@ BEGIN_PROPERTY(Connection_Handle)
END_PROPERTY
BEGIN_PROPERTY(Connection_LastInsertId)
CHECK_DB();
GB.ReturnLong((*THIS->driver->GetLastInsertId)(&THIS->db));
END_PROPERTY
GB_DESC CConnectionDesc[] =
{
GB_DECLARE("_Connection", sizeof(CCONNECTION)),
@ -786,7 +793,9 @@ GB_DESC CConnectionDesc[] =
//GB_PROPERTY_READ("Transaction", "i", Connection_Transaction),
GB_PROPERTY("IgnoreCharset", "b", CCONNECTION_ignore_charset),
GB_PROPERTY_READ("Collations", "String[]", Connection_Collations),
GB_STATIC_PROPERTY_READ("Handle", "p", Connection_Handle),
GB_PROPERTY_READ("Handle", "p", Connection_Handle),
GB_PROPERTY_READ("LastInsertId", "l", Connection_LastInsertId),
GB_METHOD("Open", NULL, CCONNECTION_open, NULL),
GB_METHOD("Close", NULL, CCONNECTION_close, NULL),

View File

@ -855,7 +855,7 @@ GB_DESC CResultDesc[] =
GB_METHOD("Update", NULL, Result_Update, NULL),
GB_METHOD("Delete", NULL, Result_Delete, "[(Keep)b]"),
GB_METHOD("GetAll", "Array", Result_GetAll, "(Field)s"),
GB_METHOD("All", "Array", Result_GetAll, "(Field)s"),
GB_PROPERTY_READ("Fields", ".Result.Fields", Result_Fields),
GB_PROPERTY_READ("Connection", "Connection", Result_Connection),

View File

@ -151,6 +151,7 @@ typedef
int (*Rollback)(DB_DATABASE *db);
GB_ARRAY (*GetCollations)(DB_DATABASE *db);
const char *(*GetQuote)(void);
int64_t (*GetLastInsertId)(DB_DATABASE *db);
struct {
void (*Init)(DB_RESULT result, DB_INFO *info, int *count);

View File

@ -30,6 +30,7 @@ static const char *get_quote(void);
static int open_database(DB_DESC *desc, DB_DATABASE *db);
static void close_database(DB_DATABASE *db);
static GB_ARRAY get_collations(DB_DATABASE *db);
static int64_t get_last_insert_id(DB_DATABASE *db);
static int format_value(GB_VALUE *arg, DB_FORMAT_CALLBACK add);
static void format_blob(DB_BLOB *blob, DB_FORMAT_CALLBACK add);
static int exec_query(DB_DATABASE *db, const char *query, DB_RESULT *result, const char *err);
@ -89,6 +90,7 @@ static DB_DRIVER _driver = \
rollback_transaction, \
get_collations, \
get_quote, \
get_last_insert_id, \
{ \
query_init, \
query_fill, \