osquery-1/osquery/core/sqlite_util.cpp

67 lines
1.5 KiB
C++
Raw Normal View History

2014-07-31 00:35:19 +00:00
// Copyright 2004-present Facebook. All Rights Reserved.
#include "osquery/core.h"
#include "osquery/core/sqlite_util.h"
2014-07-31 00:35:19 +00:00
#include <iostream>
#include <map>
#include <string>
#include <glog/logging.h>
2014-08-12 00:37:49 +00:00
#include <sqlite3.h>
2014-07-31 00:35:19 +00:00
2014-08-06 00:37:04 +00:00
#include "osquery/database.h"
#include "osquery/tables/base.h"
#include "osquery/tables/registry.h"
2014-07-31 00:35:19 +00:00
using namespace osquery::db;
using namespace osquery::tables;
2014-08-15 07:25:30 +00:00
namespace osquery {
namespace core {
2014-07-31 00:35:19 +00:00
sqlite3* createDB() {
sqlite3* db = nullptr;
sqlite3_open(":memory:", &db);
2014-08-05 08:21:28 +00:00
osquery::tables::attachVirtualTables(db);
2014-07-31 00:35:19 +00:00
return db;
}
QueryData aggregateQuery(const std::string& q, int& error_return) {
return aggregateQuery(q, error_return, createDB());
}
2014-08-15 07:25:30 +00:00
QueryData aggregateQuery(const std::string& q, int& error_return, sqlite3* db) {
2014-07-31 00:35:19 +00:00
QueryData d;
2014-08-15 07:25:30 +00:00
char* err = nullptr;
sqlite3_exec(db, q.c_str(), query_data_callback, &d, &err);
2014-07-31 00:35:19 +00:00
if (err != nullptr) {
LOG(ERROR) << "Error launching query: " << err;
error_return = 1;
sqlite3_free(err);
} else {
error_return = 0;
}
return d;
}
2014-08-15 07:25:30 +00:00
int query_data_callback(void* argument,
int argc,
char* argv[],
char* column[]) {
2014-07-31 00:35:19 +00:00
if (argument == nullptr) {
LOG(ERROR) << "query_data_callback received nullptr as data argument";
2014-07-31 00:35:19 +00:00
return SQLITE_MISUSE;
}
2014-08-15 07:25:30 +00:00
QueryData* qData = (QueryData*)argument;
2014-07-31 00:35:19 +00:00
Row r;
for (int i = 0; i < argc; i++) {
r[column[i]] = argv[i];
}
(*qData).push_back(r);
return 0;
}
2014-08-15 07:25:30 +00:00
}
}