Impl get tables with sql

This commit is contained in:
Changhua
2022-08-07 23:49:37 +08:00
parent 1f5eae96de
commit a49339a2a0
12 changed files with 150 additions and 3 deletions
+41
View File
@@ -3,6 +3,7 @@
#include "exec_sql.h"
#include "load_calls.h"
#include "util.h"
using namespace std;
@@ -12,6 +13,32 @@ extern DWORD g_WeChatWinDllAddr;
typedef map<wstring, DWORD> dbMap_t;
static dbMap_t dbMap;
// 回调函数指针
typedef int (*sqlite3_callback)(void *, int, char **, char **);
// sqlite3_exec函数指针
typedef int(__cdecl *Sqlite3_exec)(DWORD, /* The database on which the SQL executes */
const char *, /* The SQL to be executed */
sqlite3_callback, /* Invoke this callback routine */
void *, /* First argument to xCallback() */
char ** /* Write error messages here */
);
static int cbGetTables(void *ret, int argc, char **argv, char **azColName)
{
vector<RpcTables_t> *p = (vector<RpcTables_t> *)ret;
RpcTables_t tbl = { 0 };
for (int i = 0; i < argc; i++) {
if (strcmp(azColName[i], "name") == 0) {
tbl.table = argv[i] ? GetBstrFromString(argv[i]) : NULL;
} else if (strcmp(azColName[i], "sql") == 0) {
tbl.sql = argv[i] ? GetBstrFromString(argv[i]) : NULL;
}
}
p->push_back(tbl);
return 0;
}
vector<wstring> GetDbNames()
{
vector<wstring> vDbs;
@@ -31,3 +58,17 @@ vector<wstring> GetDbNames()
}
return vDbs;
}
vector<RpcTables_t> GetDbTables(wstring db)
{
vector<RpcTables_t> vTables;
const char *sql = "select * from sqlite_master where type=\"table\";";
Sqlite3_exec p_Sqlite3_exec = (Sqlite3_exec)(g_WeChatWinDllAddr + g_WxCalls.sql.exec);
auto it = dbMap.find(db);
if (it != dbMap.end()) {
p_Sqlite3_exec(it->second, sql, (sqlite3_callback)cbGetTables, &vTables, 0);
}
return vTables;
}
+1
View File
@@ -6,3 +6,4 @@
#include "rpc_h.h"
std::vector<std::wstring> GetDbNames();
std::vector<RpcTables_t> GetDbTables(std::wstring db);
+23
View File
@@ -139,6 +139,29 @@ int server_GetDbNames(int *pNum, BSTR **dbs)
return 0;
}
int server_GetDbTables(const wchar_t *db, int *pNum, PPRpcTables *tbls)
{
vector<RpcTables_t> tables = GetDbTables(db);
*pNum = tables.size();
PPRpcTables pp = (PPRpcTables)midl_user_allocate(*pNum * sizeof(RpcTables_t));
if (pp == NULL) {
printf("server_GetMsgTypes midl_user_allocate Failed for pp\n");
return -2;
}
int index = 0;
for (auto it = tables.begin(); it != tables.end(); it++) {
PRpcTables p = (PRpcTables)midl_user_allocate(sizeof(RpcTables_t));
p->table = it->table;
p->sql = it->sql;
pp[index++] = p;
}
*tbls = pp;
return 0;
}
RPC_STATUS CALLBACK SecurityCallback(RPC_IF_HANDLE /*hInterface*/, void * /*pBindingHandle*/)
{
return RPC_S_OK; // Always allow anyone.