summaryrefslogtreecommitdiffstats
path: root/src/sql_util.hh
blob: 361942c2213f80ef70208e32da866c7218bf2318 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
 * Copyright (c) 2013, Timothy Stack
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * * Neither the name of Timothy Stack nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * @file sql_util.hh
 */

#ifndef lnav_sql_util_hh
#define lnav_sql_util_hh

#include <map>
#include <string>
#include <unordered_map>
#include <vector>

#include <sqlite3.h>
#include <sys/time.h>
#include <time.h>

#include "base/intern_string.hh"
#include "base/lnav.console.hh"
#include "base/time_util.hh"
#include "sqlitepp.hh"

extern const char* sql_keywords[145];
extern const char* sql_function_names[];
extern const std::unordered_map<unsigned char, const char*>
    sql_constraint_names;

inline const char*
sql_constraint_op_name(unsigned char op)
{
    auto iter = sql_constraint_names.find(op);
    if (iter == sql_constraint_names.end()) {
        return "??";
    }

    return iter->second;
}

using sqlite_exec_callback = int (*)(void*, int, char**, char**);
typedef std::vector<std::string> db_table_list_t;
using db_table_map_t = std::map<std::string, db_table_list_t>;

struct sqlite_metadata_callbacks {
    sqlite_exec_callback smc_collation_list;
    sqlite_exec_callback smc_database_list;
    sqlite_exec_callback smc_table_list;
    sqlite_exec_callback smc_table_info;
    sqlite_exec_callback smc_foreign_key_list;
    void* smc_userdata{nullptr};
    db_table_map_t smc_db_list{};
};

int walk_sqlite_metadata(sqlite3* db, struct sqlite_metadata_callbacks& smc);

void dump_sqlite_schema(sqlite3* db, std::string& schema_out);

void attach_sqlite_db(sqlite3* db, const std::string& filename);

inline ssize_t
sql_strftime(char* buffer,
             size_t buffer_size,
             lnav::time64_t tim,
             int millis,
             char sep = ' ')
{
    return lnav::strftime_rfc3339(buffer, buffer_size, tim, millis, sep);
}

inline ssize_t
sql_strftime(char* buffer,
             size_t buffer_size,
             const struct timeval& tv,
             char sep = ' ')
{
    return sql_strftime(buffer, buffer_size, tv.tv_sec, tv.tv_usec / 1000, sep);
}

void sql_install_logger();

bool sql_ident_needs_quote(const char* ident);

char* sql_quote_ident(const char* ident);

std::string sql_safe_ident(const string_fragment& ident);

void sql_execute_script(
    sqlite3* db,
    const std::map<std::string, scoped_value_t>& global_vars,
    const char* src_name,
    const char* script,
    std::vector<lnav::console::user_message>& errors);

int guess_type_from_pcre(const std::string& pattern, std::string& collator);

const char* sqlite3_type_to_string(int type);

attr_line_t sqlite3_errmsg_to_attr_line(sqlite3* db);

attr_line_t annotate_sql_with_error(sqlite3* db,
                                    const char* sql,
                                    const char* tail);

int sqlite_authorizer(void* pUserData,
                      int action_code,
                      const char* detail1,
                      const char* detail2,
                      const char* detail3,
                      const char* detail4);

#endif