From 87f546bb8166d699c62db289a237e693b7ff207f Mon Sep 17 00:00:00 2001 From: gambas Date: Mon, 16 Apr 2018 20:57:56 +0200 Subject: [PATCH] Correctly quote table names when using schemas. [GB.DB] * BUG: Correctly quote table names when using schemas. --- main/lib/db/CConnection.c | 6 ++---- main/lib/db/CResult.c | 6 +++--- main/lib/db/gb.db/.settings | 2 +- main/lib/db/main.c | 23 +++++++++++++---------- main/lib/db/main.h | 2 +- 5 files changed, 20 insertions(+), 19 deletions(-) diff --git a/main/lib/db/CConnection.c b/main/lib/db/CConnection.c index ac9bbad28..8927a16f8 100644 --- a/main/lib/db/CConnection.c +++ b/main/lib/db/CConnection.c @@ -531,9 +531,7 @@ static char *get_query(char *prefix, CCONNECTION *_object, char *table, int len_ q_add(prefix); q_add(" "); - q_add(THIS->driver->GetQuote()); - q_add_length(table, len_table); - q_add(THIS->driver->GetQuote()); + q_add(DB_GetQuotedTable(THIS->driver, &THIS->db, table, len_table)); if (query && len_query > 0) { @@ -630,7 +628,7 @@ BEGIN_METHOD(CCONNECTION_quote, GB_STRING name; GB_BOOLEAN is_table) CHECK_OPEN(); if (VARGOPT(is_table, FALSE)) // && THIS->db.flags.schema) - GB.ReturnNewZeroString(DB_GetQuotedTable(THIS->driver, &THIS->db, GB.ToZeroString(ARG(name)))); + GB.ReturnNewZeroString(DB_GetQuotedTable(THIS->driver, &THIS->db, STRING(name), LENGTH(name))); else { q_init(); diff --git a/main/lib/db/CResult.c b/main/lib/db/CResult.c index 8454dc76f..f027a541c 100644 --- a/main/lib/db/CResult.c +++ b/main/lib/db/CResult.c @@ -668,7 +668,7 @@ BEGIN_METHOD_VOID(Result_Update) break; q_add("INSERT INTO "); - q_add(DB_GetQuotedTable(THIS->driver, DB_CurrentDatabase, info->table)); + q_add(DB_GetQuotedTable(THIS->driver, DB_CurrentDatabase, info->table, -1)); q_add(" ( "); comma = FALSE; @@ -724,7 +724,7 @@ BEGIN_METHOD_VOID(Result_Update) break; q_add("UPDATE "); - q_add(DB_GetQuotedTable(THIS->driver, DB_CurrentDatabase, info->table)); + q_add(DB_GetQuotedTable(THIS->driver, DB_CurrentDatabase, info->table, -1)); q_add(" SET "); comma = FALSE; @@ -780,7 +780,7 @@ BEGIN_METHOD(Result_Delete, GB_BOOLEAN keep) case RESULT_EDIT: q_add("DELETE FROM "); - q_add(DB_GetQuotedTable(THIS->driver, DB_CurrentDatabase, info->table)); + q_add(DB_GetQuotedTable(THIS->driver, DB_CurrentDatabase, info->table, -1)); q_add(" WHERE "); q_add(THIS->edit); diff --git a/main/lib/db/gb.db/.settings b/main/lib/db/gb.db/.settings index 276f3fc76..6d2826803 100644 --- a/main/lib/db/gb.db/.settings +++ b/main/lib/db/gb.db/.settings @@ -23,7 +23,7 @@ Active=4 File[2]=".src/Connections.class:40.71" Count=4 File[3]=".src/Connection.class:21.25" -File[4]=".src/SQLRequest.class:11.73" +File[4]=".src/SQLRequest.class:26.156" [Watches] Count=0 diff --git a/main/lib/db/main.c b/main/lib/db/main.c index 38e334904..84287b351 100644 --- a/main/lib/db/main.c +++ b/main/lib/db/main.c @@ -593,31 +593,34 @@ DB_DATABASE *DB_GetCurrent() return DB_CurrentDatabase; } -char *DB_GetQuotedTable(DB_DRIVER *driver, DB_DATABASE *db, const char *table) +char *DB_GetQuotedTable(DB_DRIVER *driver, DB_DATABASE *db, const char *table, int len) { - int len; char *point = NULL; char *res; const char *quote; - if (!table || !*table) + if (!table) + return ""; + + if (len < 0) + len = strlen(table); + + if (len == 0) return ""; - len = strlen(table); if (db->flags.schema) point = index(table, '.'); quote = (*driver->GetQuote)(); + res = GB.TempString(NULL, len + 2); + if (!point) - { - res = GB.TempString(NULL, len + 2); - sprintf(res, "%s%s%s", quote, table, quote); - } + sprintf(res, "%s%.*s%s", quote, len, table, quote); else { - res = GB.TempString(NULL, len + 2); - sprintf(res, "%.*s.%s%s%s", (int)(point - table), table, quote, point + 1, quote); + int len_schema = (int)(point - table); + sprintf(res, "%.*s.%s%.*s%s", len_schema, table, quote, len - len_schema - 1, point + 1, quote); } return res; diff --git a/main/lib/db/main.h b/main/lib/db/main.h index ba5c632b0..3acbdefac 100644 --- a/main/lib/db/main.h +++ b/main/lib/db/main.h @@ -41,7 +41,7 @@ bool DB_Open(DB_DESC *desc, DB_DRIVER **driver, DB_DATABASE *db); char *DB_MakeQuery(DB_DRIVER *driver, const char *pattern, int len, int narg, GB_VALUE *arg); void DB_Format(DB_DRIVER *driver, GB_VALUE *arg, DB_FORMAT_CALLBACK func); void DB_FormatVariant(DB_DRIVER *driver, GB_VARIANT_VALUE *arg, DB_FORMAT_CALLBACK func); -char *DB_GetQuotedTable(DB_DRIVER *driver, DB_DATABASE *db, const char *table); +char *DB_GetQuotedTable(DB_DRIVER *driver, DB_DATABASE *db, const char *table, int len_table); void DB_LowerString(char *s); int DB_CheckNameWith(const char *name, const char *msg, const char *more);