Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
D
dlib
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
钟尚武
dlib
Commits
94ff8e83
Commit
94ff8e83
authored
Sep 24, 2011
by
Davis King
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Just removed the sqlite namespace and unindended everything.
parent
8d1cf196
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
1100 additions
and
1112 deletions
+1100
-1112
sqlite.h
dlib/sqlite/sqlite.h
+430
-433
sqlite_abstract.h
dlib/sqlite/sqlite_abstract.h
+416
-419
sqlite_tools.h
dlib/sqlite/sqlite_tools.h
+131
-134
sqlite_tools_abstract.h
dlib/sqlite/sqlite_tools_abstract.h
+123
-126
No files found.
dlib/sqlite/sqlite.h
View file @
94ff8e83
...
@@ -11,551 +11,548 @@
...
@@ -11,551 +11,548 @@
#include <sqlite3.h>
#include <sqlite3.h>
#include "../smart_pointers.h"
#include "../smart_pointers.h"
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
----
namespace
dlib
namespace
dlib
{
{
namespace
sqlite
// --------------------------------------------------------------------------------------------
struct
sqlite_error
:
public
error
{
{
sqlite_error
(
const
std
::
string
&
message
)
:
error
(
message
)
{}
};
//
------------------------------------------------------------------------------------
// --------
------------------------------------------------------------------------------------
struct
sqlite_error
:
public
error
namespace
impl
{
struct
db_deleter
{
{
sqlite_error
(
const
std
::
string
&
message
)
:
error
(
message
)
{}
void
operator
()(
sqlite3
*
db
)
const
{
sqlite3_close
(
db
);
}
};
};
}
//
------------------------------------------------------------------------------------
// --------
------------------------------------------------------------------------------------
namespace
impl
class
database
:
noncopyable
{
public
:
database
(
)
{
{
struct
db_deleter
{
void
operator
()(
sqlite3
*
db
)
const
{
sqlite3_close
(
db
);
}
};
}
}
// ------------------------------------------------------------------------------------
database
(
const
std
::
string
&
file
)
{
open
(
file
);
}
class
database
:
noncopyable
bool
is_open
(
)
const
{
{
public
:
return
db
.
get
()
!=
0
;
database
(
}
)
{
}
database
(
void
open
(
const
std
::
string
&
file
const
std
::
string
&
file
)
)
{
filename
=
file
;
sqlite3
*
ptr
=
0
;
int
status
=
sqlite3_open
(
file
.
c_str
(),
&
ptr
);
db
.
reset
(
ptr
,
impl
::
db_deleter
());
if
(
status
!=
SQLITE_OK
)
{
{
open
(
file
);
throw
sqlite_error
(
sqlite3_errmsg
(
db
.
get
())
);
}
}
}
bool
is_open
(
const
std
::
string
&
get_database_filename
(
)
const
)
const
{
{
return
db
.
get
()
!=
0
;
// make sure requires clause is not broken
}
DLIB_ASSERT
(
is_open
()
==
true
,
"
\t
std::string database::get_database_filename()"
<<
"
\n\t
The database must be opened before calling this routine."
<<
"
\n\t
this: "
<<
this
);
void
open
(
return
filename
;
const
std
::
string
&
file
}
)
{
filename
=
file
;
sqlite3
*
ptr
=
0
;
int
status
=
sqlite3_open
(
file
.
c_str
(),
&
ptr
);
db
.
reset
(
ptr
,
impl
::
db_deleter
());
if
(
status
!=
SQLITE_OK
)
{
throw
sqlite_error
(
sqlite3_errmsg
(
db
.
get
()));
}
}
const
std
::
string
&
get_database_filename
(
inline
void
exec
(
)
const
const
std
::
string
&
sql_statement
{
);
// make sure requires clause is not broken
DLIB_ASSERT
(
is_open
()
==
true
,
"
\t
std::string sqlite::database::get_database_filename()"
<<
"
\n\t
The database must be opened before calling this routine."
<<
"
\n\t
this: "
<<
this
);
return
filename
;
private
:
}
inline
void
exec
(
friend
class
statement
;
const
std
::
string
&
sql_statement
);
private
:
std
::
string
filename
;
shared_ptr
<
sqlite3
>
db
;
};
friend
class
statement
;
// --------------------------------------------------------------------------------------------
std
::
string
filename
;
class
statement
:
noncopyable
shared_ptr
<
sqlite3
>
db
;
{
};
public
:
statement
(
database
&
db_
,
const
std
::
string
sql_statement
)
:
needs_reset
(
false
),
step_status
(
SQLITE_DONE
),
at_first_step
(
true
),
db
(
db_
.
db
),
stmt
(
0
),
sql_string
(
sql_statement
)
{
// make sure requires clause is not broken
DLIB_ASSERT
(
db_
.
is_open
()
==
true
,
"
\t
statement::statement()"
<<
"
\n\t
The database must be opened before calling this routine."
<<
"
\n\t
this: "
<<
this
);
// ------------------------------------------------------------------------------------
int
status
=
sqlite3_prepare_v2
(
db
.
get
(),
sql_string
.
c_str
(),
sql_string
.
size
()
+
1
,
&
stmt
,
NULL
);
class
statement
:
noncopyable
if
(
status
!=
SQLITE_OK
)
{
public
:
statement
(
database
&
db_
,
const
std
::
string
sql_statement
)
:
needs_reset
(
false
),
step_status
(
SQLITE_DONE
),
at_first_step
(
true
),
db
(
db_
.
db
),
stmt
(
0
),
sql_string
(
sql_statement
)
{
{
// make sure requires clause is not broken
sqlite3_finalize
(
stmt
);
DLIB_ASSERT
(
db_
.
is_open
()
==
true
,
throw
sqlite_error
(
sqlite3_errmsg
(
db
.
get
()));
"
\t
sqlite::statement::statement()"
}
<<
"
\n\t
The database must be opened before calling this routine."
if
(
stmt
==
0
)
<<
"
\n\t
this: "
<<
this
{
);
throw
sqlite_error
(
"Invalid SQL statement"
);
}
}
int
status
=
sqlite3_prepare_v2
(
db
.
get
(),
~
statement
(
sql_string
.
c_str
(),
)
sql_string
.
size
()
+
1
,
{
&
stmt
,
sqlite3_finalize
(
stmt
);
NULL
);
}
if
(
status
!=
SQLITE_OK
)
void
exec
(
{
)
sqlite3_finalize
(
stmt
);
{
throw
sqlite_error
(
sqlite3_errmsg
(
db
.
get
()));
reset
();
}
if
(
stmt
==
0
)
{
throw
sqlite_error
(
"Invalid SQL statement"
);
}
}
~
statement
(
step_status
=
sqlite3_step
(
stmt
);
)
needs_reset
=
true
;
if
(
step_status
!=
SQLITE_DONE
&&
step_status
!=
SQLITE_ROW
)
{
{
sqlite3_finalize
(
stmt
);
if
(
step_status
==
SQLITE_ERROR
)
throw
sqlite_error
(
sqlite3_errmsg
(
db
.
get
()));
else
if
(
step_status
==
SQLITE_BUSY
)
throw
sqlite_error
(
"statement::exec() failed. SQLITE_BUSY returned"
);
else
throw
sqlite_error
(
"statement::exec() failed."
);
}
}
}
void
exec
(
bool
move_next
(
)
)
{
if
(
step_status
==
SQLITE_ROW
)
{
{
reset
();
if
(
at_first_step
)
step_status
=
sqlite3_step
(
stmt
);
needs_reset
=
true
;
if
(
step_status
!=
SQLITE_DONE
&&
step_status
!=
SQLITE_ROW
)
{
{
if
(
step_status
==
SQLITE_ERROR
)
at_first_step
=
false
;
throw
sqlite_error
(
sqlite3_errmsg
(
db
.
get
()));
return
true
;
else
if
(
step_status
==
SQLITE_BUSY
)
throw
sqlite_error
(
"sqlite::statement::exec() failed. SQLITE_BUSY returned"
);
else
throw
sqlite_error
(
"sqlite::statement::exec() failed."
);
}
}
}
else
bool
move_next
(
)
{
if
(
step_status
==
SQLITE_ROW
)
{
{
if
(
at_first_step
)
step_status
=
sqlite3_step
(
stmt
);
if
(
step_status
==
SQLITE_DONE
)
{
return
false
;
}
else
if
(
step_status
==
SQLITE_ROW
)
{
{
at_first_step
=
false
;
return
true
;
return
true
;
}
}
else
else
{
{
step_status
=
sqlite3_step
(
stmt
);
throw
sqlite_error
(
sqlite3_errmsg
(
db
.
get
()));
if
(
step_status
==
SQLITE_DONE
)
{
return
false
;
}
else
if
(
step_status
==
SQLITE_ROW
)
{
return
true
;
}
else
{
throw
sqlite_error
(
sqlite3_errmsg
(
db
.
get
()));
}
}
}
}
}
else
{
return
false
;
}
}
}
else
unsigned
long
get_num_columns
(
)
const
{
{
if
(
(
at_first_step
==
false
)
&&
(
step_status
==
SQLITE_ROW
))
return
false
;
{
return
sqlite3_column_count
(
stmt
);
}
else
{
return
0
;
}
}
}
}
const
std
::
string
&
get_sql_string
(
unsigned
long
get_num_columns
(
)
const
)
const
{
if
(
(
at_first_step
==
false
)
&&
(
step_status
==
SQLITE_ROW
))
{
return
sqlite3_column_count
(
stmt
);
}
else
{
{
return
sql_string
;
return
0
;
}
}
}
const
std
::
string
&
get_sql_string
(
)
const
{
return
sql_string
;
}
const
std
::
vector
<
char
>
get_column_as_blob
(
unsigned
long
idx
)
const
{
// make sure requires clause is not broken
DLIB_ASSERT
(
idx
<
get_num_columns
(),
"
\t
std::vector<char> sqlite::statement::get_column_as_blob()"
<<
"
\n\t
Invalid column index."
<<
"
\n\t
idx: "
<<
idx
<<
"
\n\t
this: "
<<
this
);
const
char
*
data
=
static_cast
<
const
char
*>
(
sqlite3_column_blob
(
stmt
,
idx
));
const
std
::
vector
<
char
>
get_column_as_blob
(
const
int
size
=
sqlite3_column_bytes
(
stmt
,
idx
);
unsigned
long
idx
)
const
{
// make sure requires clause is not broken
DLIB_ASSERT
(
idx
<
get_num_columns
(),
"
\t
std::vector<char> statement::get_column_as_blob()"
<<
"
\n\t
Invalid column index."
<<
"
\n\t
idx: "
<<
idx
<<
"
\n\t
this: "
<<
this
);
return
std
::
vector
<
char
>
(
data
,
data
+
size
);
const
char
*
data
=
static_cast
<
const
char
*>
(
sqlite3_column_blob
(
stmt
,
idx
)
);
}
const
int
size
=
sqlite3_column_bytes
(
stmt
,
idx
);
template
<
typename
T
>
return
std
::
vector
<
char
>
(
data
,
data
+
size
);
void
get_column_as_object
(
}
unsigned
long
idx
,
T
&
item
)
const
{
// make sure requires clause is not broken
DLIB_ASSERT
(
idx
<
get_num_columns
(),
"
\t
void sqlite::statement::get_column_as_object()"
<<
"
\n\t
Invalid column index."
<<
"
\n\t
idx: "
<<
idx
<<
"
\n\t
this: "
<<
this
);
const
char
*
data
=
static_cast
<
const
char
*>
(
sqlite3_column_blob
(
stmt
,
idx
));
template
<
typename
T
>
const
int
size
=
sqlite3_column_bytes
(
stmt
,
idx
);
void
get_column_as_object
(
std
::
istringstream
sin
(
std
::
string
(
data
,
size
));
unsigned
long
idx
,
deserialize
(
item
,
sin
);
T
&
item
}
)
const
{
// make sure requires clause is not broken
DLIB_ASSERT
(
idx
<
get_num_columns
(),
"
\t
void statement::get_column_as_object()"
<<
"
\n\t
Invalid column index."
<<
"
\n\t
idx: "
<<
idx
<<
"
\n\t
this: "
<<
this
);
const
std
::
string
get_column_as_text
(
const
char
*
data
=
static_cast
<
const
char
*>
(
sqlite3_column_blob
(
stmt
,
idx
));
unsigned
long
idx
const
int
size
=
sqlite3_column_bytes
(
stmt
,
idx
);
)
const
std
::
istringstream
sin
(
std
::
string
(
data
,
size
));
{
deserialize
(
item
,
sin
);
// make sure requires clause is not broken
}
DLIB_ASSERT
(
idx
<
get_num_columns
(),
"
\t
std::string sqlite::statement::get_column_as_text()"
<<
"
\n\t
Invalid column index."
<<
"
\n\t
idx: "
<<
idx
<<
"
\n\t
this: "
<<
this
);
const
char
*
data
=
reinterpret_cast
<
const
char
*>
(
sqlite3_column_text
(
stmt
,
idx
));
const
std
::
string
get_column_as_text
(
return
std
::
string
(
data
);
unsigned
long
idx
}
)
const
{
// make sure requires clause is not broken
DLIB_ASSERT
(
idx
<
get_num_columns
(),
"
\t
std::string statement::get_column_as_text()"
<<
"
\n\t
Invalid column index."
<<
"
\n\t
idx: "
<<
idx
<<
"
\n\t
this: "
<<
this
);
double
get_column_as_double
(
const
char
*
data
=
reinterpret_cast
<
const
char
*>
(
sqlite3_column_text
(
stmt
,
idx
));
unsigned
long
idx
return
std
::
string
(
data
);
)
const
}
{
// make sure requires clause is not broken
DLIB_ASSERT
(
idx
<
get_num_columns
(),
"
\t
double sqlite::statement::get_column_as_double()"
<<
"
\n\t
Invalid column index."
<<
"
\n\t
idx: "
<<
idx
<<
"
\n\t
this: "
<<
this
);
return
sqlite3_column_double
(
stmt
,
idx
);
double
get_column_as_double
(
}
unsigned
long
idx
)
const
{
// make sure requires clause is not broken
DLIB_ASSERT
(
idx
<
get_num_columns
(),
"
\t
double statement::get_column_as_double()"
<<
"
\n\t
Invalid column index."
<<
"
\n\t
idx: "
<<
idx
<<
"
\n\t
this: "
<<
this
);
int
get_column_as_int
(
return
sqlite3_column_double
(
stmt
,
idx
);
unsigned
long
idx
}
)
const
{
// make sure requires clause is not broken
DLIB_ASSERT
(
idx
<
get_num_columns
(),
"
\t
int sqlite::statement::get_column_as_int()"
<<
"
\n\t
Invalid column index."
<<
"
\n\t
idx: "
<<
idx
<<
"
\n\t
this: "
<<
this
);
return
sqlite3_column_int
(
stmt
,
idx
);
int
get_column_as_int
(
}
unsigned
long
idx
)
const
{
// make sure requires clause is not broken
DLIB_ASSERT
(
idx
<
get_num_columns
(),
"
\t
int statement::get_column_as_int()"
<<
"
\n\t
Invalid column index."
<<
"
\n\t
idx: "
<<
idx
<<
"
\n\t
this: "
<<
this
);
int64
get_column_as_int64
(
return
sqlite3_column_int
(
stmt
,
idx
);
unsigned
long
idx
}
)
const
{
// make sure requires clause is not broken
DLIB_ASSERT
(
idx
<
get_num_columns
(),
"
\t
int64 sqlite::statement::get_column_as_int64()"
<<
"
\n\t
Invalid column index."
<<
"
\n\t
idx: "
<<
idx
<<
"
\n\t
this: "
<<
this
);
return
sqlite3_column_int64
(
stmt
,
idx
);
int64
get_column_as_int64
(
}
unsigned
long
idx
)
const
{
// make sure requires clause is not broken
DLIB_ASSERT
(
idx
<
get_num_columns
(),
"
\t
int64 statement::get_column_as_int64()"
<<
"
\n\t
Invalid column index."
<<
"
\n\t
idx: "
<<
idx
<<
"
\n\t
this: "
<<
this
);
const
std
::
string
get_column_name
(
return
sqlite3_column_int64
(
stmt
,
idx
);
unsigned
long
idx
}
)
const
{
// make sure requires clause is not broken
DLIB_ASSERT
(
idx
<
get_num_columns
(),
"
\t
std::string sqlite::statement::get_column_name()"
<<
"
\n\t
Invalid column index."
<<
"
\n\t
idx: "
<<
idx
<<
"
\n\t
this: "
<<
this
);
return
std
::
string
(
sqlite3_column_name
(
stmt
,
idx
));
const
std
::
string
get_column_name
(
}
unsigned
long
idx
)
const
{
// make sure requires clause is not broken
DLIB_ASSERT
(
idx
<
get_num_columns
(),
"
\t
std::string statement::get_column_name()"
<<
"
\n\t
Invalid column index."
<<
"
\n\t
idx: "
<<
idx
<<
"
\n\t
this: "
<<
this
);
unsigned
long
get_max_parameter_id
(
return
std
::
string
(
sqlite3_column_name
(
stmt
,
idx
));
)
const
}
{
return
SQLITE_LIMIT_VARIABLE_NUMBER
;
}
unsigned
long
get_parameter_id
(
unsigned
long
get_max_parameter_id
(
const
std
::
string
&
name
)
const
)
const
{
{
return
SQLITE_LIMIT_VARIABLE_NUMBER
;
return
sqlite3_bind_parameter_index
(
stmt
,
name
.
c_str
());
}
}
void
bind_blob
(
unsigned
long
get_parameter_id
(
unsigned
long
parameter_id
,
const
std
::
string
&
name
const
std
::
vector
<
char
>&
item
)
const
)
{
{
return
sqlite3_bind_parameter_index
(
stmt
,
name
.
c_str
());
// make sure requires clause is not broken
}
DLIB_ASSERT
(
1
<=
parameter_id
&&
parameter_id
<=
get_max_parameter_id
(),
"
\t
void sqlite::statement::bind_blob()"
<<
"
\n\t
Invalid parameter id."
<<
"
\n\t
parameter_id: "
<<
parameter_id
<<
"
\n\t
get_max_parameter_id(): "
<<
get_max_parameter_id
()
<<
"
\n\t
this: "
<<
this
);
reset
();
void
bind_blob
(
int
status
=
sqlite3_bind_blob
(
stmt
,
parameter_id
,
&
item
[
0
],
item
.
size
(),
SQLITE_TRANSIENT
);
unsigned
long
parameter_id
,
const
std
::
vector
<
char
>&
item
)
{
// make sure requires clause is not broken
DLIB_ASSERT
(
1
<=
parameter_id
&&
parameter_id
<=
get_max_parameter_id
(),
"
\t
void statement::bind_blob()"
<<
"
\n\t
Invalid parameter id."
<<
"
\n\t
parameter_id: "
<<
parameter_id
<<
"
\n\t
get_max_parameter_id(): "
<<
get_max_parameter_id
()
<<
"
\n\t
this: "
<<
this
);
if
(
status
!=
SQLITE_OK
)
reset
();
{
int
status
=
sqlite3_bind_blob
(
stmt
,
parameter_id
,
&
item
[
0
],
item
.
size
(),
SQLITE_TRANSIENT
);
throw
sqlite_error
(
sqlite3_errmsg
(
db
.
get
()));
}
}
template
<
typename
T
>
if
(
status
!=
SQLITE_OK
)
void
bind_object
(
unsigned
long
parameter_id
,
const
T
&
item
)
{
{
// make sure requires clause is not broken
throw
sqlite_error
(
sqlite3_errmsg
(
db
.
get
()));
DLIB_ASSERT
(
1
<=
parameter_id
&&
parameter_id
<=
get_max_parameter_id
(),
}
"
\t
void sqlite::statement::bind_object()"
}
<<
"
\n\t
Invalid parameter id."
<<
"
\n\t
parameter_id: "
<<
parameter_id
<<
"
\n\t
get_max_parameter_id(): "
<<
get_max_parameter_id
()
<<
"
\n\t
this: "
<<
this
);
reset
();
template
<
typename
T
>
std
::
ostringstream
sout
;
void
bind_object
(
serialize
(
item
,
sout
);
unsigned
long
parameter_id
,
const
std
::
string
&
str
=
sout
.
str
();
const
T
&
item
int
status
=
sqlite3_bind_blob
(
stmt
,
parameter_id
,
str
.
data
(),
str
.
size
(),
SQLITE_TRANSIENT
);
)
{
// make sure requires clause is not broken
DLIB_ASSERT
(
1
<=
parameter_id
&&
parameter_id
<=
get_max_parameter_id
(),
"
\t
void statement::bind_object()"
<<
"
\n\t
Invalid parameter id."
<<
"
\n\t
parameter_id: "
<<
parameter_id
<<
"
\n\t
get_max_parameter_id(): "
<<
get_max_parameter_id
()
<<
"
\n\t
this: "
<<
this
);
if
(
status
!=
SQLITE_OK
)
reset
();
{
std
::
ostringstream
sout
;
throw
sqlite_error
(
sqlite3_errmsg
(
db
.
get
())
);
serialize
(
item
,
sout
);
}
const
std
::
string
&
str
=
sout
.
str
();
}
int
status
=
sqlite3_bind_blob
(
stmt
,
parameter_id
,
str
.
data
(),
str
.
size
(),
SQLITE_TRANSIENT
);
void
bind_double
(
if
(
status
!=
SQLITE_OK
)
unsigned
long
parameter_id
,
const
double
&
item
)
{
{
// make sure requires clause is not broken
throw
sqlite_error
(
sqlite3_errmsg
(
db
.
get
()));
DLIB_ASSERT
(
1
<=
parameter_id
&&
parameter_id
<=
get_max_parameter_id
(),
}
"
\t
void sqlite::statement::bind_double()"
}
<<
"
\n\t
Invalid parameter id."
<<
"
\n\t
parameter_id: "
<<
parameter_id
<<
"
\n\t
get_max_parameter_id(): "
<<
get_max_parameter_id
()
<<
"
\n\t
this: "
<<
this
);
reset
();
void
bind_double
(
int
status
=
sqlite3_bind_double
(
stmt
,
parameter_id
,
item
);
unsigned
long
parameter_id
,
const
double
&
item
)
{
// make sure requires clause is not broken
DLIB_ASSERT
(
1
<=
parameter_id
&&
parameter_id
<=
get_max_parameter_id
(),
"
\t
void statement::bind_double()"
<<
"
\n\t
Invalid parameter id."
<<
"
\n\t
parameter_id: "
<<
parameter_id
<<
"
\n\t
get_max_parameter_id(): "
<<
get_max_parameter_id
()
<<
"
\n\t
this: "
<<
this
);
if
(
status
!=
SQLITE_OK
)
reset
();
{
int
status
=
sqlite3_bind_double
(
stmt
,
parameter_id
,
item
);
throw
sqlite_error
(
sqlite3_errmsg
(
db
.
get
()));
}
}
void
bind_int
(
if
(
status
!=
SQLITE_OK
)
unsigned
long
parameter_id
,
const
int
&
item
)
{
{
// make sure requires clause is not broken
throw
sqlite_error
(
sqlite3_errmsg
(
db
.
get
()));
DLIB_ASSERT
(
1
<=
parameter_id
&&
parameter_id
<=
get_max_parameter_id
(),
}
"
\t
void sqlite::statement::bind_int()"
}
<<
"
\n\t
Invalid parameter id."
<<
"
\n\t
parameter_id: "
<<
parameter_id
<<
"
\n\t
get_max_parameter_id(): "
<<
get_max_parameter_id
()
<<
"
\n\t
this: "
<<
this
);
reset
();
void
bind_int
(
int
status
=
sqlite3_bind_int
(
stmt
,
parameter_id
,
item
);
unsigned
long
parameter_id
,
const
int
&
item
)
{
// make sure requires clause is not broken
DLIB_ASSERT
(
1
<=
parameter_id
&&
parameter_id
<=
get_max_parameter_id
(),
"
\t
void statement::bind_int()"
<<
"
\n\t
Invalid parameter id."
<<
"
\n\t
parameter_id: "
<<
parameter_id
<<
"
\n\t
get_max_parameter_id(): "
<<
get_max_parameter_id
()
<<
"
\n\t
this: "
<<
this
);
if
(
status
!=
SQLITE_OK
)
reset
();
{
int
status
=
sqlite3_bind_int
(
stmt
,
parameter_id
,
item
);
throw
sqlite_error
(
sqlite3_errmsg
(
db
.
get
()));
}
}
void
bind_int64
(
if
(
status
!=
SQLITE_OK
)
unsigned
long
parameter_id
,
const
int64
&
item
)
{
{
// make sure requires clause is not broken
throw
sqlite_error
(
sqlite3_errmsg
(
db
.
get
()));
DLIB_ASSERT
(
1
<=
parameter_id
&&
parameter_id
<=
get_max_parameter_id
(),
}
"
\t
void sqlite::statement::bind_int64()"
}
<<
"
\n\t
Invalid parameter id."
<<
"
\n\t
parameter_id: "
<<
parameter_id
<<
"
\n\t
get_max_parameter_id(): "
<<
get_max_parameter_id
()
<<
"
\n\t
this: "
<<
this
);
reset
();
void
bind_int64
(
int
status
=
sqlite3_bind_int64
(
stmt
,
parameter_id
,
item
);
unsigned
long
parameter_id
,
const
int64
&
item
)
{
// make sure requires clause is not broken
DLIB_ASSERT
(
1
<=
parameter_id
&&
parameter_id
<=
get_max_parameter_id
(),
"
\t
void statement::bind_int64()"
<<
"
\n\t
Invalid parameter id."
<<
"
\n\t
parameter_id: "
<<
parameter_id
<<
"
\n\t
get_max_parameter_id(): "
<<
get_max_parameter_id
()
<<
"
\n\t
this: "
<<
this
);
if
(
status
!=
SQLITE_OK
)
reset
();
{
int
status
=
sqlite3_bind_int64
(
stmt
,
parameter_id
,
item
);
throw
sqlite_error
(
sqlite3_errmsg
(
db
.
get
()));
}
}
void
bind_null
(
if
(
status
!=
SQLITE_OK
)
unsigned
long
parameter_id
)
{
{
// make sure requires clause is not broken
throw
sqlite_error
(
sqlite3_errmsg
(
db
.
get
()));
DLIB_ASSERT
(
1
<=
parameter_id
&&
parameter_id
<=
get_max_parameter_id
(),
}
"
\t
void sqlite::statement::bind_null()"
}
<<
"
\n\t
Invalid parameter id."
<<
"
\n\t
parameter_id: "
<<
parameter_id
<<
"
\n\t
get_max_parameter_id(): "
<<
get_max_parameter_id
()
<<
"
\n\t
this: "
<<
this
);
reset
();
void
bind_null
(
int
status
=
sqlite3_bind_null
(
stmt
,
parameter_id
);
unsigned
long
parameter_id
)
{
// make sure requires clause is not broken
DLIB_ASSERT
(
1
<=
parameter_id
&&
parameter_id
<=
get_max_parameter_id
(),
"
\t
void statement::bind_null()"
<<
"
\n\t
Invalid parameter id."
<<
"
\n\t
parameter_id: "
<<
parameter_id
<<
"
\n\t
get_max_parameter_id(): "
<<
get_max_parameter_id
()
<<
"
\n\t
this: "
<<
this
);
if
(
status
!=
SQLITE_OK
)
reset
();
{
int
status
=
sqlite3_bind_null
(
stmt
,
parameter_id
);
throw
sqlite_error
(
sqlite3_errmsg
(
db
.
get
()));
}
}
void
bind_text
(
if
(
status
!=
SQLITE_OK
)
unsigned
long
parameter_id
,
const
std
::
string
&
item
)
{
{
// make sure requires clause is not broken
throw
sqlite_error
(
sqlite3_errmsg
(
db
.
get
()));
DLIB_ASSERT
(
1
<=
parameter_id
&&
parameter_id
<=
get_max_parameter_id
(),
}
"
\t
void sqlite::statement::bind_text()"
}
<<
"
\n\t
Invalid parameter id."
<<
"
\n\t
parameter_id: "
<<
parameter_id
<<
"
\n\t
get_max_parameter_id(): "
<<
get_max_parameter_id
()
<<
"
\n\t
this: "
<<
this
);
reset
();
void
bind_text
(
int
status
=
sqlite3_bind_text
(
stmt
,
parameter_id
,
item
.
c_str
(),
-
1
,
SQLITE_TRANSIENT
);
unsigned
long
parameter_id
,
const
std
::
string
&
item
)
{
// make sure requires clause is not broken
DLIB_ASSERT
(
1
<=
parameter_id
&&
parameter_id
<=
get_max_parameter_id
(),
"
\t
void statement::bind_text()"
<<
"
\n\t
Invalid parameter id."
<<
"
\n\t
parameter_id: "
<<
parameter_id
<<
"
\n\t
get_max_parameter_id(): "
<<
get_max_parameter_id
()
<<
"
\n\t
this: "
<<
this
);
if
(
status
!=
SQLITE_OK
)
reset
();
{
int
status
=
sqlite3_bind_text
(
stmt
,
parameter_id
,
item
.
c_str
(),
-
1
,
SQLITE_TRANSIENT
);
throw
sqlite_error
(
sqlite3_errmsg
(
db
.
get
()));
}
if
(
status
!=
SQLITE_OK
)
{
throw
sqlite_error
(
sqlite3_errmsg
(
db
.
get
()));
}
}
}
private
:
private
:
void
reset
()
void
reset
()
{
if
(
needs_reset
)
{
{
if
(
needs_reset
)
if
(
sqlite3_reset
(
stmt
)
!=
SQLITE_OK
)
{
{
if
(
sqlite3_reset
(
stmt
)
!=
SQLITE_OK
)
{
step_status
=
SQLITE_DONE
;
throw
sqlite_error
(
sqlite3_errmsg
(
db
.
get
()));
}
needs_reset
=
false
;
step_status
=
SQLITE_DONE
;
step_status
=
SQLITE_DONE
;
at_first_step
=
true
;
throw
sqlite_error
(
sqlite3_errmsg
(
db
.
get
()))
;
}
}
needs_reset
=
false
;
step_status
=
SQLITE_DONE
;
at_first_step
=
true
;
}
}
}
bool
needs_reset
;
// true if sqlite3_step() has been called more recently than sqlite3_reset()
bool
needs_reset
;
// true if sqlite3_step() has been called more recently than sqlite3_reset()
int
step_status
;
int
step_status
;
bool
at_first_step
;
bool
at_first_step
;
shared_ptr
<
sqlite3
>
db
;
shared_ptr
<
sqlite3
>
db
;
sqlite3_stmt
*
stmt
;
sqlite3_stmt
*
stmt
;
std
::
string
sql_string
;
std
::
string
sql_string
;
};
};
//
------------------------------------------------------------------------------------
// --------
------------------------------------------------------------------------------------
void
database
::
void
database
::
exec
(
exec
(
const
std
::
string
&
sql_statement
const
std
::
string
&
sql_statement
)
)
{
{
// make sure requires clause is not broken
// make sure requires clause is not broken
DLIB_ASSERT
(
is_open
()
==
true
,
DLIB_ASSERT
(
is_open
()
==
true
,
"
\t
void sqlite::
database::exec()"
"
\t
void
database::exec()"
<<
"
\n\t
The database must be opened before calling this routine."
<<
"
\n\t
The database must be opened before calling this routine."
<<
"
\n\t
this: "
<<
this
<<
"
\n\t
this: "
<<
this
);
);
statement
(
*
this
,
sql_statement
).
exec
();
statement
(
*
this
,
sql_statement
).
exec
();
}
}
//
------------------------------------------------------------------------------------
// --------
------------------------------------------------------------------------------------
}
}
}
#endif // DLIB_SQLiTE_H_
#endif // DLIB_SQLiTE_H_
...
...
dlib/sqlite/sqlite_abstract.h
View file @
94ff8e83
...
@@ -14,430 +14,427 @@
...
@@ -14,430 +14,427 @@
namespace
dlib
namespace
dlib
{
{
namespace
sqlite
// ----------------------------------------------------------------------------------------
struct
sqlite_error
:
public
error
{
{
/*!
WHAT THIS OBJECT REPRESENTS
This is the exception object used by the SQLite tools to indicate
that an error has occurred. An of the functions defined in this
file might throw this exception.
!*/
};
// ------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
struct
sqlite_error
:
public
error
{
/*!
WHAT THIS OBJECT REPRESENTS
This is the exception object used by the SQLite tools to indicate
that an error has occurred. An of the functions defined in this
file might throw this exception.
!*/
};
// ------------------------------------------------------------------------------------
class
database
:
noncopyable
{
/*!
WHAT THIS OBJECT REPRESENTS
This object is a C++ wrapper around a SQLite database connection
handle and therefore represents a SQLite database file.
Note that this wrapper is targeted at SQLite Version 3.
Note also that whenever SQLite indicates an error has occurred
this object will throw the sqlite_error exception.
!*/
public
:
database
(
);
/*!
ensures
- #is_open() == false
!*/
database
(
const
std
::
string
&
file
);
/*!
ensures
- opens the indicated database file or creates a new
database with the given name if one doesn't already exist.
- #get_database_filename() == file
- #is_open() == true
!*/
~
database
(
);
/*!
ensures
- safely disposes of any SQLite database connection. If
any statement objects still exist which reference this database
then the SQLite database connection won't be fully closed
until those statement objects are also destroyed. This allows
for any destruction order between database and statement objects.
!*/
void
open
(
const
std
::
string
&
file
);
/*!
ensures
- opens the indicated database file or creates a new
database with the given name if one doesn't already exist.
- #get_database_filename() == file
- #is_open() == true
- safely disposes of any previous SQLite database connection. If
any statement objects still exist which reference this database
then the SQLite database connection won't be fully closed
until those statement objects are also destroyed.
!*/
bool
is_open
(
)
const
;
/*!
ensures
- if (this object has an open connection to a SQLite database) then
- returns true
- else
- returns false
!*/
const
std
::
string
&
get_database_filename
(
)
const
;
/*!
requires
- is_open() == true
ensures
- returns the name of the SQLite database file this object
currently has open.
!*/
void
exec
(
const
std
::
string
&
sql_statement
);
/*!
requires
- is_open() == true
ensures
- executes the supplied SQL statement against this database
!*/
};
// ------------------------------------------------------------------------------------
class
statement
:
noncopyable
{
/*!
WHAT THIS OBJECT REPRESENTS
This object represents a SQL statement which can be executed
against a database object. In particular, this object is a
C++ wrapper around a SQLite prepared statement.
Note that whenever SQLite indicates an error has occurred this
object will throw the sqlite_error exception.
BINDABLE SQL PARAMETERS
Sometimes you want to execute a bunch of very similar SQL statements.
For example, you might need to execute many insert statements where each
statement changes only the value of a field. Since it is somewhat
costly to construct a statement object for each SQL operation, SQLite
supports defining bindable parameters for a statement object. This allows
you to reuse the same statement object.
Therefore, in SQL statements used with SQLite, wherever it is valid to
include a string literal, one can use a parameter in one of the following
forms:
?
?NNN
:AAA
$AAA
@AAA
In the examples above, NNN is an integer value and AAA is an identifier. A
parameter initially has a value of NULL. You can use the bind_*() routines
to attach values to the parameters. Each call to a bind_*() routine overrides
prior bindings on the same parameter.
Each SQL parameter has a numeric ID which is used to reference it when invoking
a bind_*() routine. The leftmost SQL parameter in a statement has an index of 1,
the next parameter has an index of 2, and so on, except when the following rules
apply. When the same named SQL parameter is used more than once, second and
subsequent occurrences have the same index as the first occurrence. The index
for named parameters can be looked up using the get_parameter_id() method if desired.
The index for "?NNN" parameters is the value of NNN. The NNN value must be between
1 and get_max_parameter_id().
!*/
public
:
statement
(
database
&
db
,
const
std
::
string
sql_statement
);
/*!
requires
- db.is_open() == true
ensures
- The given SQL statement can be executed against the given
database by calling exec().
- #get_sql_string() == sql_statement
!*/
~
statement
(
);
/*!
ensures
- any resources associated with this object have been freed.
!*/
const
std
::
string
&
get_sql_string
(
)
const
;
/*!
ensures
- returns a copy of the SQL statement used to create this statement object.
!*/
void
exec
(
);
/*!
ensures
- #get_num_columns() == 0
- executes the SQL statement get_sql_string() against the database
given to this object's constructor.
- If this was a select statement then you can obtain the resulting
rows by calling move_next() and using the get_column_by_*() member
functions.
!*/
// ----------------------------
bool
move_next
(
);
/*!
ensures
- if (there is a result row for this query) then
- #get_num_columns() == the number of columns in the result row.
- The get_column_by_*() routines can be used to access the elements
of the row data.
- returns true
- else
- returns false
- #get_num_columns() == 0
!*/
unsigned
long
get_num_columns
(
)
const
;
/*!
ensures
- returns the number of columns of data available via the get_column_as_*()
routines.
!*/
const
std
::
vector
<
char
>
get_column_as_blob
(
unsigned
long
idx
)
const
;
/*!
requires
- idx < get_num_columns()
ensures
- returns the contents of the idx-th column as a binary BLOB.
!*/
template
<
typename
T
>
void
get_column_as_object
(
unsigned
long
idx
,
T
&
item
)
const
;
/*!
requires
- idx < get_num_columns()
- item is deserializable
(i.e. Calling deserialize(item, some_input_stream) reads an item
of type T from the some_input_stream stream)
ensures
- gets the contents of the idx-th column as a binary BLOB and then
deserializes it into item.
!*/
const
std
::
string
get_column_as_text
(
unsigned
long
idx
)
const
;
/*!
requires
- idx < get_num_columns()
ensures
- returns the contents of the idx-th column as a text string.
!*/
double
get_column_as_double
(
unsigned
long
idx
)
const
;
/*!
requires
- idx < get_num_columns()
ensures
- returns the contents of the idx-th column as a double.
!*/
int
get_column_as_int
(
unsigned
long
idx
)
const
;
/*!
requires
- idx < get_num_columns()
ensures
- returns the contents of the idx-th column as an int.
!*/
int64
get_column_as_int64
(
unsigned
long
idx
)
const
;
/*!
requires
- idx < get_num_columns()
ensures
- returns the contents of the idx-th column as a 64bit int.
!*/
const
std
::
string
get_column_name
(
unsigned
long
idx
)
const
;
/*!
requires
- idx < get_num_columns()
ensures
- returns the name of the idx-th column. In particular:
The name of a result column is the value of the "AS" clause for
that column, if there is an AS clause. If there is no AS clause
then the name of the column is unspecified and may change from
one release of SQLite to the next.
!*/
// ----------------------------
unsigned
long
get_max_parameter_id
(
)
const
;
/*!
ensures
- returns the max parameter ID value which can be used with the
bind_() member functions defined below.
- In SQLite, the default value of this limit is usually 999.
!*/
unsigned
long
get_parameter_id
(
const
std
::
string
&
name
)
const
;
/*!
ensures
- if (This SQL statement contains a SQL parameter with the given name) then
- returns the parameter_id number which can be used in the bind_*()
member functions defined below.
- else
- returns 0
!*/
void
bind_blob
(
unsigned
long
parameter_id
,
const
std
::
vector
<
char
>&
item
);
/*!
requires
- 1 <= parameter_id <= get_max_parameter_id()
ensures
- #get_num_columns() == 0
- binds the value of item into the SQL parameter indicated by
parameter_id.
!*/
template
<
typename
T
>
void
bind_object
(
unsigned
long
parameter_id
,
const
T
&
item
);
/*!
requires
- 1 <= parameter_id <= get_max_parameter_id()
- item is serializable
(i.e. Calling serialize(item, some_output_stream) writes an item
of type T to the some_output_stream stream)
ensures
- #get_num_columns() == 0
- binds the value of item into the SQL parameter indicated by
parameter_id. This is performed by serializing item and then
binding it as a binary BLOB.
!*/
void
bind_double
(
unsigned
long
parameter_id
,
const
double
&
item
);
/*!
requires
- 1 <= parameter_id <= get_max_parameter_id()
ensures
- #get_num_columns() == 0
- binds the value of item into the SQL parameter indicated by
parameter_id.
!*/
void
bind_int
(
unsigned
long
parameter_id
,
const
int
&
item
);
/*!
requires
- 1 <= parameter_id <= get_max_parameter_id()
ensures
- #get_num_columns() == 0
- binds the value of item into the SQL parameter indicated by
parameter_id.
!*/
void
bind_int64
(
unsigned
long
parameter_id
,
const
int64
&
item
);
/*!
requires
- 1 <= parameter_id <= get_max_parameter_id()
ensures
- #get_num_columns() == 0
- binds the value of item into the SQL parameter indicated by
parameter_id.
!*/
void
bind_null
(
unsigned
long
parameter_id
);
/*!
requires
- 1 <= parameter_id <= get_max_parameter_id()
ensures
- #get_num_columns() == 0
- binds a NULL to the SQL parameter indicated by parameter_id.
!*/
void
bind_text
(
unsigned
long
parameter_id
,
const
std
::
string
&
item
);
/*!
requires
- 1 <= parameter_id <= get_max_parameter_id()
ensures
- #get_num_columns() == 0
- binds the value of item into the SQL parameter indicated by
parameter_id.
!*/
};
class
database
:
noncopyable
{
/*!
WHAT THIS OBJECT REPRESENTS
This object is a C++ wrapper around a SQLite database connection
handle and therefore represents a SQLite database file.
Note that this wrapper is targeted at SQLite Version 3.
Note also that whenever SQLite indicates an error has occurred
this object will throw the sqlite_error exception.
!*/
public
:
database
(
);
/*!
ensures
- #is_open() == false
!*/
database
(
const
std
::
string
&
file
);
/*!
ensures
- opens the indicated database file or creates a new
database with the given name if one doesn't already exist.
- #get_database_filename() == file
- #is_open() == true
!*/
~
database
(
);
/*!
ensures
- safely disposes of any SQLite database connection. If
any statement objects still exist which reference this database
then the SQLite database connection won't be fully closed
until those statement objects are also destroyed. This allows
for any destruction order between database and statement objects.
!*/
void
open
(
const
std
::
string
&
file
);
/*!
ensures
- opens the indicated database file or creates a new
database with the given name if one doesn't already exist.
- #get_database_filename() == file
- #is_open() == true
- safely disposes of any previous SQLite database connection. If
any statement objects still exist which reference this database
then the SQLite database connection won't be fully closed
until those statement objects are also destroyed.
!*/
bool
is_open
(
)
const
;
/*!
ensures
- if (this object has an open connection to a SQLite database) then
- returns true
- else
- returns false
!*/
const
std
::
string
&
get_database_filename
(
)
const
;
/*!
requires
- is_open() == true
ensures
- returns the name of the SQLite database file this object
currently has open.
!*/
void
exec
(
const
std
::
string
&
sql_statement
);
/*!
requires
- is_open() == true
ensures
- executes the supplied SQL statement against this database
!*/
};
// ----------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------
class
statement
:
noncopyable
{
/*!
WHAT THIS OBJECT REPRESENTS
This object represents a SQL statement which can be executed
against a database object. In particular, this object is a
C++ wrapper around a SQLite prepared statement.
Note that whenever SQLite indicates an error has occurred this
object will throw the sqlite_error exception.
BINDABLE SQL PARAMETERS
Sometimes you want to execute a bunch of very similar SQL statements.
For example, you might need to execute many insert statements where each
statement changes only the value of a field. Since it is somewhat
costly to construct a statement object for each SQL operation, SQLite
supports defining bindable parameters for a statement object. This allows
you to reuse the same statement object.
Therefore, in SQL statements used with SQLite, wherever it is valid to
include a string literal, one can use a parameter in one of the following
forms:
?
?NNN
:AAA
$AAA
@AAA
In the examples above, NNN is an integer value and AAA is an identifier. A
parameter initially has a value of NULL. You can use the bind_*() routines
to attach values to the parameters. Each call to a bind_*() routine overrides
prior bindings on the same parameter.
Each SQL parameter has a numeric ID which is used to reference it when invoking
a bind_*() routine. The leftmost SQL parameter in a statement has an index of 1,
the next parameter has an index of 2, and so on, except when the following rules
apply. When the same named SQL parameter is used more than once, second and
subsequent occurrences have the same index as the first occurrence. The index
for named parameters can be looked up using the get_parameter_id() method if desired.
The index for "?NNN" parameters is the value of NNN. The NNN value must be between
1 and get_max_parameter_id().
!*/
public
:
statement
(
database
&
db
,
const
std
::
string
sql_statement
);
/*!
requires
- db.is_open() == true
ensures
- The given SQL statement can be executed against the given
database by calling exec().
- #get_sql_string() == sql_statement
!*/
~
statement
(
);
/*!
ensures
- any resources associated with this object have been freed.
!*/
const
std
::
string
&
get_sql_string
(
)
const
;
/*!
ensures
- returns a copy of the SQL statement used to create this statement object.
!*/
void
exec
(
);
/*!
ensures
- #get_num_columns() == 0
- executes the SQL statement get_sql_string() against the database
given to this object's constructor.
- If this was a select statement then you can obtain the resulting
rows by calling move_next() and using the get_column_by_*() member
functions.
!*/
// ----------------------------
bool
move_next
(
);
/*!
ensures
- if (there is a result row for this query) then
- #get_num_columns() == the number of columns in the result row.
- The get_column_by_*() routines can be used to access the elements
of the row data.
- returns true
- else
- returns false
- #get_num_columns() == 0
!*/
unsigned
long
get_num_columns
(
)
const
;
/*!
ensures
- returns the number of columns of data available via the get_column_as_*()
routines.
!*/
const
std
::
vector
<
char
>
get_column_as_blob
(
unsigned
long
idx
)
const
;
/*!
requires
- idx < get_num_columns()
ensures
- returns the contents of the idx-th column as a binary BLOB.
!*/
template
<
typename
T
>
void
get_column_as_object
(
unsigned
long
idx
,
T
&
item
)
const
;
/*!
requires
- idx < get_num_columns()
- item is deserializable
(i.e. Calling deserialize(item, some_input_stream) reads an item
of type T from the some_input_stream stream)
ensures
- gets the contents of the idx-th column as a binary BLOB and then
deserializes it into item.
!*/
const
std
::
string
get_column_as_text
(
unsigned
long
idx
)
const
;
/*!
requires
- idx < get_num_columns()
ensures
- returns the contents of the idx-th column as a text string.
!*/
double
get_column_as_double
(
unsigned
long
idx
)
const
;
/*!
requires
- idx < get_num_columns()
ensures
- returns the contents of the idx-th column as a double.
!*/
int
get_column_as_int
(
unsigned
long
idx
)
const
;
/*!
requires
- idx < get_num_columns()
ensures
- returns the contents of the idx-th column as an int.
!*/
int64
get_column_as_int64
(
unsigned
long
idx
)
const
;
/*!
requires
- idx < get_num_columns()
ensures
- returns the contents of the idx-th column as a 64bit int.
!*/
const
std
::
string
get_column_name
(
unsigned
long
idx
)
const
;
/*!
requires
- idx < get_num_columns()
ensures
- returns the name of the idx-th column. In particular:
The name of a result column is the value of the "AS" clause for
that column, if there is an AS clause. If there is no AS clause
then the name of the column is unspecified and may change from
one release of SQLite to the next.
!*/
// ----------------------------
unsigned
long
get_max_parameter_id
(
)
const
;
/*!
ensures
- returns the max parameter ID value which can be used with the
bind_() member functions defined below.
- In SQLite, the default value of this limit is usually 999.
!*/
unsigned
long
get_parameter_id
(
const
std
::
string
&
name
)
const
;
/*!
ensures
- if (This SQL statement contains a SQL parameter with the given name) then
- returns the parameter_id number which can be used in the bind_*()
member functions defined below.
- else
- returns 0
!*/
void
bind_blob
(
unsigned
long
parameter_id
,
const
std
::
vector
<
char
>&
item
);
/*!
requires
- 1 <= parameter_id <= get_max_parameter_id()
ensures
- #get_num_columns() == 0
- binds the value of item into the SQL parameter indicated by
parameter_id.
!*/
template
<
typename
T
>
void
bind_object
(
unsigned
long
parameter_id
,
const
T
&
item
);
/*!
requires
- 1 <= parameter_id <= get_max_parameter_id()
- item is serializable
(i.e. Calling serialize(item, some_output_stream) writes an item
of type T to the some_output_stream stream)
ensures
- #get_num_columns() == 0
- binds the value of item into the SQL parameter indicated by
parameter_id. This is performed by serializing item and then
binding it as a binary BLOB.
!*/
void
bind_double
(
unsigned
long
parameter_id
,
const
double
&
item
);
/*!
requires
- 1 <= parameter_id <= get_max_parameter_id()
ensures
- #get_num_columns() == 0
- binds the value of item into the SQL parameter indicated by
parameter_id.
!*/
void
bind_int
(
unsigned
long
parameter_id
,
const
int
&
item
);
/*!
requires
- 1 <= parameter_id <= get_max_parameter_id()
ensures
- #get_num_columns() == 0
- binds the value of item into the SQL parameter indicated by
parameter_id.
!*/
void
bind_int64
(
unsigned
long
parameter_id
,
const
int64
&
item
);
/*!
requires
- 1 <= parameter_id <= get_max_parameter_id()
ensures
- #get_num_columns() == 0
- binds the value of item into the SQL parameter indicated by
parameter_id.
!*/
void
bind_null
(
unsigned
long
parameter_id
);
/*!
requires
- 1 <= parameter_id <= get_max_parameter_id()
ensures
- #get_num_columns() == 0
- binds a NULL to the SQL parameter indicated by parameter_id.
!*/
void
bind_text
(
unsigned
long
parameter_id
,
const
std
::
string
&
item
);
/*!
requires
- 1 <= parameter_id <= get_max_parameter_id()
ensures
- #get_num_columns() == 0
- binds the value of item into the SQL parameter indicated by
parameter_id.
!*/
};
// ----------------------------------------------------------------------------------------
}
}
}
#endif // DLIB_SQLiTE_ABSTRACT_H_
#endif // DLIB_SQLiTE_ABSTRACT_H_
...
...
dlib/sqlite/sqlite_tools.h
View file @
94ff8e83
...
@@ -11,181 +11,178 @@
...
@@ -11,181 +11,178 @@
namespace
dlib
namespace
dlib
{
{
namespace
sqlite
{
class
transaction
:
noncopyable
class
transaction
:
noncopyable
{
public
:
transaction
(
database
&
db_
)
:
db
(
db_
),
committed
(
false
)
{
{
public
:
db
.
exec
(
"begin transaction"
);
transaction
(
}
database
&
db_
)
:
db
(
db_
),
committed
(
false
)
{
db
.
exec
(
"begin transaction"
);
}
void
commit
()
void
commit
()
{
if
(
!
committed
)
{
{
if
(
!
committed
)
committed
=
true
;
{
db
.
exec
(
"commit"
);
committed
=
true
;
db
.
exec
(
"commit"
);
}
}
}
}
~
transaction
()
~
transaction
()
{
{
if
(
!
committed
)
if
(
!
committed
)
db
.
exec
(
"rollback"
);
db
.
exec
(
"rollback"
);
}
}
private
:
private
:
database
&
db
;
database
&
db
;
bool
committed
;
bool
committed
;
};
};
//
------------------------------------------------------------------------------------
// ----
------------------------------------------------------------------------------------
template
<
template
<
typename
T
typename
T
>
>
void
query_object
(
void
query_object
(
database
&
db
,
database
&
db
,
const
std
::
string
&
query
,
const
std
::
string
&
query
,
T
&
item
T
&
item
)
)
{
statement
st
(
db
,
query
);
st
.
exec
();
if
(
st
.
move_next
()
&&
st
.
get_num_columns
()
==
1
)
{
{
statement
st
(
db
,
query
);
st
.
get_column_as_object
(
0
,
item
);
st
.
exec
();
if
(
st
.
move_next
())
if
(
st
.
move_next
()
&&
st
.
get_num_columns
()
==
1
)
{
st
.
get_column_as_object
(
0
,
item
);
if
(
st
.
move_next
())
throw
sqlite_error
(
"query doesn't result in exactly 1 element"
);
}
else
{
throw
sqlite_error
(
"query doesn't result in exactly 1 element"
);
throw
sqlite_error
(
"query doesn't result in exactly 1 element"
);
}
}
}
else
{
throw
sqlite_error
(
"query doesn't result in exactly 1 element"
);
}
}
//
------------------------------------------------------------------------------------
// ----
------------------------------------------------------------------------------------
inline
std
::
string
query_text
(
inline
std
::
string
query_text
(
database
&
db
,
database
&
db
,
const
std
::
string
&
query
const
std
::
string
&
query
)
)
{
statement
st
(
db
,
query
);
st
.
exec
();
if
(
st
.
move_next
()
&&
st
.
get_num_columns
()
==
1
)
{
{
statement
st
(
db
,
query
);
const
std
::
string
&
temp
=
st
.
get_column_as_text
(
0
);
st
.
exec
();
if
(
st
.
move_next
())
if
(
st
.
move_next
()
&&
st
.
get_num_columns
()
==
1
)
{
const
std
::
string
&
temp
=
st
.
get_column_as_text
(
0
);
if
(
st
.
move_next
())
throw
sqlite_error
(
"query doesn't result in exactly 1 element"
);
return
temp
;
}
else
{
throw
sqlite_error
(
"query doesn't result in exactly 1 element"
);
throw
sqlite_error
(
"query doesn't result in exactly 1 element"
);
}
return
temp
;
}
}
else
{
throw
sqlite_error
(
"query doesn't result in exactly 1 element"
);
}
}
//
------------------------------------------------------------------------------------
// ----
------------------------------------------------------------------------------------
inline
double
query_double
(
inline
double
query_double
(
database
&
db
,
database
&
db
,
const
std
::
string
&
query
const
std
::
string
&
query
)
)
{
statement
st
(
db
,
query
);
st
.
exec
();
if
(
st
.
move_next
()
&&
st
.
get_num_columns
()
==
1
)
{
{
statement
st
(
db
,
query
);
double
temp
=
st
.
get_column_as_double
(
0
);
st
.
exec
();
if
(
st
.
move_next
())
if
(
st
.
move_next
()
&&
st
.
get_num_columns
()
==
1
)
{
double
temp
=
st
.
get_column_as_double
(
0
);
if
(
st
.
move_next
())
throw
sqlite_error
(
"query doesn't result in exactly 1 element"
);
return
temp
;
}
else
{
throw
sqlite_error
(
"query doesn't result in exactly 1 element"
);
throw
sqlite_error
(
"query doesn't result in exactly 1 element"
);
}
return
temp
;
}
else
{
throw
sqlite_error
(
"query doesn't result in exactly 1 element"
);
}
}
}
//
------------------------------------------------------------------------------------
// ----
------------------------------------------------------------------------------------
inline
int
query_int
(
inline
int
query_int
(
database
&
db
,
database
&
db
,
const
std
::
string
&
query
const
std
::
string
&
query
)
)
{
statement
st
(
db
,
query
);
st
.
exec
();
if
(
st
.
move_next
()
&&
st
.
get_num_columns
()
==
1
)
{
{
statement
st
(
db
,
query
);
int
temp
=
st
.
get_column_as_int
(
0
);
st
.
exec
();
if
(
st
.
move_next
())
if
(
st
.
move_next
()
&&
st
.
get_num_columns
()
==
1
)
{
int
temp
=
st
.
get_column_as_int
(
0
);
if
(
st
.
move_next
())
throw
sqlite_error
(
"query doesn't result in exactly 1 element"
);
return
temp
;
}
else
{
throw
sqlite_error
(
"query doesn't result in exactly 1 element"
);
throw
sqlite_error
(
"query doesn't result in exactly 1 element"
);
}
return
temp
;
}
else
{
throw
sqlite_error
(
"query doesn't result in exactly 1 element"
);
}
}
}
//
------------------------------------------------------------------------------------
// ----
------------------------------------------------------------------------------------
inline
int64
query_int64
(
inline
int64
query_int64
(
database
&
db
,
database
&
db
,
const
std
::
string
&
query
const
std
::
string
&
query
)
)
{
statement
st
(
db
,
query
);
st
.
exec
();
if
(
st
.
move_next
()
&&
st
.
get_num_columns
()
==
1
)
{
{
statement
st
(
db
,
query
);
int64
temp
=
st
.
get_column_as_int64
(
0
);
st
.
exec
();
if
(
st
.
move_next
())
if
(
st
.
move_next
()
&&
st
.
get_num_columns
()
==
1
)
{
int64
temp
=
st
.
get_column_as_int64
(
0
);
if
(
st
.
move_next
())
throw
sqlite_error
(
"query doesn't result in exactly 1 element"
);
return
temp
;
}
else
{
throw
sqlite_error
(
"query doesn't result in exactly 1 element"
);
throw
sqlite_error
(
"query doesn't result in exactly 1 element"
);
}
return
temp
;
}
else
{
throw
sqlite_error
(
"query doesn't result in exactly 1 element"
);
}
}
}
//
------------------------------------------------------------------------------------
// ----
------------------------------------------------------------------------------------
inline
const
std
::
vector
<
char
>
query_blob
(
inline
const
std
::
vector
<
char
>
query_blob
(
database
&
db
,
database
&
db
,
const
std
::
string
&
query
const
std
::
string
&
query
)
)
{
statement
st
(
db
,
query
);
st
.
exec
();
if
(
st
.
move_next
()
&&
st
.
get_num_columns
()
==
1
)
{
{
statement
st
(
db
,
query
);
const
std
::
vector
<
char
>&
temp
=
st
.
get_column_as_blob
(
0
);
st
.
exec
();
if
(
st
.
move_next
())
if
(
st
.
move_next
()
&&
st
.
get_num_columns
()
==
1
)
{
const
std
::
vector
<
char
>&
temp
=
st
.
get_column_as_blob
(
0
);
if
(
st
.
move_next
())
throw
sqlite_error
(
"query doesn't result in exactly 1 element"
);
return
temp
;
}
else
{
throw
sqlite_error
(
"query doesn't result in exactly 1 element"
);
throw
sqlite_error
(
"query doesn't result in exactly 1 element"
);
}
return
temp
;
}
else
{
throw
sqlite_error
(
"query doesn't result in exactly 1 element"
);
}
}
}
//
------------------------------------------------------------------------------------
// ----
------------------------------------------------------------------------------------
}
}
}
#endif // DLIB_SQLiTE_TOOLS_H_
#endif // DLIB_SQLiTE_TOOLS_H_
...
...
dlib/sqlite/sqlite_tools_abstract.h
View file @
94ff8e83
...
@@ -10,156 +10,153 @@
...
@@ -10,156 +10,153 @@
namespace
dlib
namespace
dlib
{
{
namespace
sqlite
{
class
transaction
:
noncopyable
class
transaction
:
noncopyable
{
{
/*!
WHAT THIS OBJECT REPRESENTS
This object is a tool for creating exception safe
database transactions.
!*/
public
:
transaction
(
database
&
db
);
/*!
ensures
- Begins a database transaction which will be rolled back
if commit() isn't called eventually.
- In particular, performs: db.exec("begin transaction");
!*/
void
commit
(
);
/*!
ensures
- if (commit() hasn't already been called) then
- Commits all changes made during this database transaction.
- In particular, performs: db.exec("commit");
- else
- does nothing
!*/
~
transaction
(
);
/*!
ensures
- if (commit() was never called) then
- rolls back any changes made to the database during this transaction.
- In particular, performs: db.exec("rollback");
- else
- does nothing
!*/
};
// ------------------------------------------------------------------------------------
template
<
typename
T
>
void
query_object
(
database
&
db
,
const
std
::
string
&
query
,
T
&
item
);
/*!
/*!
ensures
WHAT THIS OBJECT REPRESENTS
- executes the given SQL query against db. If the query results in a
This object is a tool for creating exception safe
single row and column being returned then the data in the column is
database transactions.
interpreted as a binary BLOB and deserialized into item.
throws
- sqlite_error or serialization_error if an error occurs which prevents
this operation from succeeding.
!*/
!*/
// ------------------------------------------------------------------------------------
public
:
transaction
(
std
::
string
query_text
(
database
&
db
database
&
db
,
const
std
::
string
&
query
);
);
/*!
/*!
ensures
ensures
- executes the given SQL query against db. If the query results in a
- Begins a database transaction which will be rolled back
single row and column being returned then the data in the column is
if commit() isn't called eventually.
converted to text and returned.
- In particular, performs: db.exec("begin transaction");
throws
- sqlite_error if an error occurs which prevents this operation from
succeeding.
!*/
!*/
// ------------------------------------------------------------------------------------
void
commit
(
double
query_double
(
database
&
db
,
const
std
::
string
&
query
);
);
/*!
/*!
ensures
ensures
- executes the given SQL query against db. If the query results in a
- if (commit() hasn't already been called) then
single row and column being returned then the data in the column is
- Commits all changes made during this database transaction.
converted to a double and returned.
- In particular, performs: db.exec("commit");
throws
- else
- sqlite_error if an error occurs which prevents this operation from
- does nothing
succeeding.
!*/
!*/
// ------------------------------------------------------------------------------------
~
transaction
(
int
query_int
(
database
&
db
,
const
std
::
string
&
query
);
);
/*!
/*!
ensures
ensures
- executes the given SQL query against db. If the query results in a
- if (commit() was never called) then
single row and column being returned then the data in the column is
- rolls back any changes made to the database during this transaction.
converted to an int and returned.
- In particular, performs: db.exec("rollback");
throws
- else
- sqlite_error if an error occurs which prevents this operation from
- does nothing
succeeding.
!*/
!*/
// ------------------------------------------------------------------------------------
};
int64
query_int64
(
// ----------------------------------------------------------------------------------------
database
&
db
,
const
std
::
string
&
query
);
/*!
ensures
- executes the given SQL query against db. If the query results in a
single row and column being returned then the data in the column is
converted to an int64 and returned.
throws
- sqlite_error if an error occurs which prevents this operation from
succeeding.
!*/
// ------------------------------------------------------------------------------------
template
<
typename
T
>
void
query_object
(
database
&
db
,
const
std
::
string
&
query
,
T
&
item
);
/*!
ensures
- executes the given SQL query against db. If the query results in a
single row and column being returned then the data in the column is
interpreted as a binary BLOB and deserialized into item.
throws
- sqlite_error or serialization_error if an error occurs which prevents
this operation from succeeding.
!*/
const
std
::
vector
<
char
>
query_blob
(
// ----------------------------------------------------------------------------------------
database
&
db
,
const
std
::
string
&
query
std
::
string
query_text
(
);
database
&
db
,
/*!
const
std
::
string
&
query
ensures
);
- executes the given SQL query against db. If the query results in a
/*!
single row and column being returned then the data in the column is
ensures
returned as a binary BLOB.
- executes the given SQL query against db. If the query results in a
throws
single row and column being returned then the data in the column is
- sqlite_error if an error occurs which prevents this operation from
converted to text and returned.
succeeding.
throws
!*/
- sqlite_error if an error occurs which prevents this operation from
succeeding.
!*/
// ----------------------------------------------------------------------------------------
double
query_double
(
database
&
db
,
const
std
::
string
&
query
);
/*!
ensures
- executes the given SQL query against db. If the query results in a
single row and column being returned then the data in the column is
converted to a double and returned.
throws
- sqlite_error if an error occurs which prevents this operation from
succeeding.
!*/
// ----------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------
int
query_int
(
database
&
db
,
const
std
::
string
&
query
);
/*!
ensures
- executes the given SQL query against db. If the query results in a
single row and column being returned then the data in the column is
converted to an int and returned.
throws
- sqlite_error if an error occurs which prevents this operation from
succeeding.
!*/
// ----------------------------------------------------------------------------------------
int64
query_int64
(
database
&
db
,
const
std
::
string
&
query
);
/*!
ensures
- executes the given SQL query against db. If the query results in a
single row and column being returned then the data in the column is
converted to an int64 and returned.
throws
- sqlite_error if an error occurs which prevents this operation from
succeeding.
!*/
// ----------------------------------------------------------------------------------------
const
std
::
vector
<
char
>
query_blob
(
database
&
db
,
const
std
::
string
&
query
);
/*!
ensures
- executes the given SQL query against db. If the query results in a
single row and column being returned then the data in the column is
returned as a binary BLOB.
throws
- sqlite_error if an error occurs which prevents this operation from
succeeding.
!*/
// ----------------------------------------------------------------------------------------
}
}
}
#endif // DLIB_SQLiTE_TOOLS_H_
#endif // DLIB_SQLiTE_TOOLS_H_
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment