Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
U
uniset2
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
1
Issues
1
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
UniSet project repositories
uniset2
Commits
74f0786a
Commit
74f0786a
authored
Dec 25, 2012
by
Pavel Vainerman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(SQLite): работа над SQLiteInterface-ом
parent
1e332ce8
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
218 additions
and
14 deletions
+218
-14
Makefile.am
extensions/DBServer-SQLite/Makefile.am
+4
-0
SQLiteInterface.cc
extensions/DBServer-SQLite/SQLiteInterface.cc
+89
-14
SQLiteInterface.h
extensions/DBServer-SQLite/SQLiteInterface.h
+48
-0
create_db.sh
extensions/DBServer-SQLite/create_db.sh
+27
-0
test.cc
extensions/DBServer-SQLite/test.cc
+50
-0
No files found.
extensions/DBServer-SQLite/Makefile.am
View file @
74f0786a
...
@@ -14,6 +14,10 @@ bin_PROGRAMS = uniset-sqlite-dbserver
...
@@ -14,6 +14,10 @@ bin_PROGRAMS = uniset-sqlite-dbserver
uniset_sqlite_dbserver_LDADD
=
libUniSet-sqlite.la
$(top_builddir)
/lib/libUniSet.la
uniset_sqlite_dbserver_LDADD
=
libUniSet-sqlite.la
$(top_builddir)
/lib/libUniSet.la
uniset_sqlite_dbserver_SOURCES
=
main.cc
uniset_sqlite_dbserver_SOURCES
=
main.cc
noinst_PROGRAMS
=
sqlite-test
sqlite_test_LDADD
=
libUniSet-sqlite.la
$(top_builddir)
/lib/libUniSet.la
sqlite_test_SOURCES
=
test.cc
include
$(top_builddir)/conf/setting.mk
include
$(top_builddir)/conf/setting.mk
# install
# install
...
...
extensions/DBServer-SQLite/SQLiteInterface.cc
View file @
74f0786a
...
@@ -22,6 +22,7 @@
...
@@ -22,6 +22,7 @@
*/
*/
// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
#include <sstream>
#include <sstream>
#include <cstdio>
#include "UniSetTypes.h"
#include "UniSetTypes.h"
#include "SQLiteInterface.h"
#include "SQLiteInterface.h"
// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
...
@@ -31,6 +32,7 @@ using namespace UniSetTypes;
...
@@ -31,6 +32,7 @@ using namespace UniSetTypes;
SQLiteInterface
::
SQLiteInterface
()
:
SQLiteInterface
::
SQLiteInterface
()
:
db
(
0
),
db
(
0
),
curStmt
(
0
),
lastQ
(
""
),
lastQ
(
""
),
queryok
(
false
),
queryok
(
false
),
connected
(
false
),
connected
(
false
),
...
@@ -42,40 +44,35 @@ opCheckPause(50)
...
@@ -42,40 +44,35 @@ opCheckPause(50)
SQLiteInterface
::~
SQLiteInterface
()
SQLiteInterface
::~
SQLiteInterface
()
{
{
close
();
close
();
delete
db
;
if
(
db
)
delete
db
;
}
}
// -----------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------
bool
SQLiteInterface
::
connect
(
const
string
dbfile
)
bool
SQLiteInterface
::
connect
(
const
string
dbfile
)
{
{
int
rc
=
sqlite3_open
(
dbfile
.
c_str
(),
&
db
);
int
rc
=
sqlite3_open
(
dbfile
.
c_str
(),
&
db
);
if
(
!
rc
)
{
cerr
<<
sqlite3_errmsg
(
db
)
<<
endl
;
sqlite3_close
(
db
);
db
=
0
;
connected
=
false
;
return
false
;
}
if
(
rc
==
SQLITE_BUSY
||
rc
==
SQLITE_LOCKED
||
rc
==
SQLITE_INTERRUPT
||
rc
==
SQLITE_IOERR
)
if
(
rc
==
SQLITE_BUSY
||
rc
==
SQLITE_LOCKED
||
rc
==
SQLITE_INTERRUPT
||
rc
==
SQLITE_IOERR
)
{
{
cerr
<<
sqlite3_errmsg
(
db
)
<<
endl
;
cerr
<<
"SQLiteInterface::connect): rc="
<<
rc
<<
" error: "
<<
sqlite3_errmsg
(
db
)
<<
endl
;
sqlite3_close
(
db
);
sqlite3_close
(
db
);
db
=
0
;
db
=
0
;
connected
=
false
;
connected
=
false
;
return
false
;
return
false
;
}
}
connected
=
true
;
connected
=
true
;
return
true
;
return
true
;
}
}
// -----------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------
bool
SQLiteInterface
::
close
()
bool
SQLiteInterface
::
close
()
{
{
if
(
db
)
if
(
db
)
{
sqlite3_close
(
db
);
sqlite3_close
(
db
);
db
=
0
;
}
return
true
;
return
true
;
}
}
...
@@ -129,7 +126,7 @@ bool SQLiteInterface::query( const string q )
...
@@ -129,7 +126,7 @@ bool SQLiteInterface::query( const string q )
}
}
lastQ
=
q
;
lastQ
=
q
;
curStmt
=
pStmt
;
// int cnum = sqlite3_column_count(pStmt);
// int cnum = sqlite3_column_count(pStmt);
/*
/*
...
@@ -143,7 +140,7 @@ bool SQLiteInterface::query( const string q )
...
@@ -143,7 +140,7 @@ bool SQLiteInterface::query( const string q )
}
}
*/
*/
sqlite3_finalize
(
pStmt
);
//
sqlite3_finalize(pStmt);
queryok
=
true
;
queryok
=
true
;
return
true
;
return
true
;
}
}
...
@@ -177,6 +174,12 @@ const string SQLiteInterface::lastQuery()
...
@@ -177,6 +174,12 @@ const string SQLiteInterface::lastQuery()
return
lastQ
;
return
lastQ
;
}
}
// -----------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------
void
SQLiteInterface
::
freeResult
()
{
sqlite3_finalize
(
curStmt
);
curStmt
=
0
;
}
// -----------------------------------------------------------------------------------------
int
SQLiteInterface
::
insert_id
()
int
SQLiteInterface
::
insert_id
()
{
{
if
(
!
db
)
if
(
!
db
)
...
@@ -190,3 +193,75 @@ bool SQLiteInterface::isConnection()
...
@@ -190,3 +193,75 @@ bool SQLiteInterface::isConnection()
return
connected
;
return
connected
;
}
}
// -----------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------
SQLiteInterface
::
iterator
SQLiteInterface
::
begin
()
{
return
SQLiteIterator
(
curStmt
);
}
// -----------------------------------------------------------------------------------------
SQLiteInterface
::
iterator
SQLiteInterface
::
end
()
{
return
SQLiteIterator
();
}
// -----------------------------------------------------------------------------------------
SQLiteInterface
::
SQLiteIterator
SQLiteInterface
::
SQLiteIterator
::
operator
++
(
int
c
)
{
if
(
row
==
-
1
||
c
<
0
)
return
*
this
;
if
(
c
==
0
)
c
=
1
;
for
(
int
i
=
0
;
i
<
c
;
i
++
)
{
int
rc
=
sqlite3_step
(
stmt
);
// cerr << "**** ++: rc=" << rc << " err: " << sqlite3_errmsg( sqlite3_db_handle(stmt) ) << endl;
if
(
rc
!=
SQLITE_ROW
)
{
row
=
-
1
;
break
;
}
row
++
;
}
return
*
this
;
}
// -----------------------------------------------------------------------------------------
#if 0
SQLiteInterface::SQLiteIterator SQLiteInterface::SQLiteIterator::operator --()
{
}
#endif
// -----------------------------------------------------------------------------------------
std
::
string
SQLiteInterface
::
SQLiteIterator
::
get_text
(
int
col
)
{
return
string
(
(
char
*
)
sqlite3_column_text
(
stmt
,
col
)
);
}
// -----------------------------------------------------------------------------------------
int
SQLiteInterface
::
SQLiteIterator
::
get_int
(
int
col
)
{
return
sqlite3_column_int
(
stmt
,
col
);
}
// -----------------------------------------------------------------------------------------
double
SQLiteInterface
::
SQLiteIterator
::
get_double
(
int
col
)
{
return
sqlite3_column_double
(
stmt
,
col
);
}
// -----------------------------------------------------------------------------------------
int
SQLiteInterface
::
SQLiteIterator
::
get_num_cols
()
{
return
sqlite3_data_count
(
stmt
);
}
// -----------------------------------------------------------------------------------------
bool
SQLiteInterface
::
SQLiteIterator
::
is_end
()
{
return
(
row
==
-
1
);
}
// -----------------------------------------------------------------------------------------
void
SQLiteInterface
::
SQLiteIterator
::
free_result
()
{
sqlite3_finalize
(
stmt
);
stmt
=
0
;
}
// -----------------------------------------------------------------------------------------
extensions/DBServer-SQLite/SQLiteInterface.h
View file @
74f0786a
...
@@ -55,6 +55,53 @@ class SQLiteInterface
...
@@ -55,6 +55,53 @@ class SQLiteInterface
const
std
::
string
error
();
const
std
::
string
error
();
void
freeResult
();
class
SQLiteIterator
{
public
:
SQLiteIterator
(
sqlite3_stmt
*
s
)
:
stmt
(
s
),
row
(
0
){}
SQLiteIterator
()
:
stmt
(
0
),
row
(
-
1
){}
~
SQLiteIterator
(){};
SQLiteIterator
operator
++
(
int
);
// SQLiteIterator operator --();
inline
bool
operator
==
(
const
SQLiteIterator
&
other
)
const
{
if
(
row
==
-
1
&&
other
.
stmt
==
0
&&
other
.
row
==
-
1
)
return
true
;
return
(
stmt
==
other
.
stmt
&&
row
==
other
.
row
);
}
inline
bool
operator
!=
(
const
SQLiteIterator
&
other
)
const
{
return
!
operator
==
(
other
);
}
inline
int
row_num
(){
return
row
;
}
std
::
string
get_text
(
int
col
);
int
get_int
(
int
col
);
double
get_double
(
int
col
);
int
get_num_cols
();
void
free_result
();
bool
is_end
();
protected
:
sqlite3_stmt
*
stmt
;
int
row
;
};
typedef
SQLiteIterator
iterator
;
iterator
begin
();
iterator
end
();
protected
:
protected
:
bool
wait
(
sqlite3_stmt
*
stmt
,
int
result
);
bool
wait
(
sqlite3_stmt
*
stmt
,
int
result
);
...
@@ -62,6 +109,7 @@ class SQLiteInterface
...
@@ -62,6 +109,7 @@ class SQLiteInterface
private
:
private
:
sqlite3
*
db
;
sqlite3
*
db
;
sqlite3_stmt
*
curStmt
;
std
::
string
lastQ
;
std
::
string
lastQ
;
bool
queryok
;
// успешность текущего запроса
bool
queryok
;
// успешность текущего запроса
...
...
extensions/DBServer-SQLite/create_db.sh
0 → 100755
View file @
74f0786a
#!/bin/sh
dbname
=
test.db
[
-n
"
$1
"
]
&&
dbname
=
"
$1
"
sqlite3
$dbname
<<
"_EOF_"
DROP TABLE IF EXISTS main_history
;
CREATE TABLE main_history
(
id
INTEGER PRIMARY KEY AUTOINCREMENT,
date date
NOT NULL,
time time
NOT NULL,
time_usec INTEGER NOT NULL,
sensor_id INTEGER NOT NULL,
value DOUBLE NOT NULL,
node INTEGER NOT NULL,
confirm INTEGER DEFAULT NULL
)
;
INSERT INTO main_history VALUES
(
NULL,0,0,0,100,20.3,1,0
)
;
INSERT INTO main_history VALUES
(
NULL,0,0,0,101,20.65,1,0
)
;
INSERT INTO main_history VALUES
(
NULL,0,0,0,102,20.7,1,0
)
;
INSERT INTO main_history VALUES
(
NULL,0,0,0,103,20.1,1,0
)
;
_EOF_
# KEY main_history_sensor_id (sensor_id)
extensions/DBServer-SQLite/test.cc
0 → 100644
View file @
74f0786a
#include <iostream>
#include <sstream>
#include "Exceptions.h"
#include "SQLiteInterface.h"
// --------------------------------------------------------------------------
using
namespace
UniSetTypes
;
using
namespace
std
;
// --------------------------------------------------------------------------
int
main
(
int
argc
,
char
**
argv
)
{
try
{
SQLiteInterface
db
;
if
(
!
db
.
connect
(
"test.db"
)
)
{
cerr
<<
"db connect error: "
<<
db
.
error
()
<<
endl
;
return
1
;
}
stringstream
q
;
q
<<
"SELECT * from main_history"
;
if
(
!
db
.
query
(
q
.
str
())
)
{
cerr
<<
"db connect error: "
<<
db
.
error
()
<<
endl
;
return
1
;
}
SQLiteInterface
::
iterator
it
=
db
.
begin
();
for
(
;
it
!=
db
.
end
();
it
++
)
{
cout
<<
"get result: row="
<<
it
.
row_num
()
<<
" coln="
<<
it
.
get_num_cols
()
<<
endl
;
for
(
int
i
=
0
;
i
<
it
.
get_num_cols
();
i
++
)
cout
<<
it
.
get_text
(
i
)
<<
"("
<<
it
.
get_double
(
i
)
<<
") | "
;
cout
<<
endl
;
}
db
.
freeResult
();
db
.
close
();
}
catch
(
Exception
&
ex
)
{
cerr
<<
"(test): "
<<
ex
<<
endl
;
}
catch
(...)
{
cerr
<<
"(test): catch ..."
<<
endl
;
}
return
0
;
}
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