-
Notifications
You must be signed in to change notification settings - Fork 7
/
sample_model.cc
93 lines (70 loc) · 1.98 KB
/
sample_model.cc
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
/* Copyright (c) 2011 Akamai Technologies, Inc. */
#include <errno.h>
#include <stdio.h>
#include <re2/stringpiece.h>
using re2::StringPiece;
#include "cgen.h"
#include "sqlite3_compat.h"
#include "binder.h"
#include "sample_model.h"
static const char* sql_t_samples = \
"CREATE TABLE IF NOT EXISTS samples (sample TEXT PRIMARY KEY);";
static const char* sql_i_samples = "INSERT INTO samples VALUES (?);";
static const char* sql_s_samples = "SELECT ROWID from samples WHERE sample = ?;";
SampleModel::SampleModel() : sample_() {}
int SampleModel::InitTable(sqlite3* db)
{
db_ = db;
int ret = 0;
TST(db == NULL, errno = EINVAL,
"db must not be NULL", err);
LET(ret = sqlite3_exec(db_, sql_t_samples, NULL, NULL, NULL), ret != SQLITE_OK,
"unable to create samples table", err, DBERR);
return 0;
err:
return 1;
}
const StringPiece& SampleModel::sample() const
{
return sample_;
}
void SampleModel::set_sample(const StringPiece& sample)
{
sample_ = sample;
}
int SampleModel::InitStatements()
{
sqlite3* const & db = db_;
int ret = 0;
TST(db == NULL, errno = EINVAL,
"db must not be NULL", err);
LET(ret = sqlite3_prepare(db, sql_i_samples, -1, &insert_, NULL), ret != SQLITE_OK,
"unable to prepare SampleModel insert_ ", err, DBERR);
LET(ret = sqlite3_prepare(db, sql_s_samples, -1, &select_, NULL), ret != SQLITE_OK,
"unable to prepare SampleModel select_", err, DBERR);
return 0;
err:
return 1;
}
int SampleModel::Bind(sqlite3_stmt* stmt)
{
TST(sample_.data() == NULL, errno = EINVAL,
"SampleModel::sample_ must not be NULL", err);
TST(sample_.size() == 0, errno = EINVAL,
"SampleModel::sample_ must have positive length", err);
TST(stmt == NULL, errno = EINVAL,
"stmt must not be NULL", err);
CHK(sqlite3_bind_text(stmt, 1, sample_.data(), sample_.size(), SQLITE_STATIC) != SQLITE_OK,
"unable to bind stmt[1] to sample_", err);
return 0;
err:
return 1;
}
int SampleModel::BindInsert()
{
return Bind(insert_);
}
int SampleModel::BindSelect()
{
return Bind(select_);
}