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
5545e179
Commit
5545e179
authored
Dec 26, 2012
by
Pavel Vainerman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(SQLite): немного подправил интерфейс..
parent
b6f3f03f
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
35 additions
and
26 deletions
+35
-26
DBServer_SQLite.cc
extensions/DBServer-SQLite/DBServer_SQLite.cc
+1
-11
SQLiteInterface.cc
extensions/DBServer-SQLite/SQLiteInterface.cc
+19
-10
SQLiteInterface.h
extensions/DBServer-SQLite/SQLiteInterface.h
+11
-5
create_db.sh
extensions/DBServer-SQLite/create_db.sh
+4
-0
No files found.
extensions/DBServer-SQLite/DBServer_SQLite.cc
View file @
5545e179
...
...
@@ -226,18 +226,8 @@ bool DBServer_SQLite::writeToBase( const string& query )
flushBuffer
();
// А теперь собственно запрос..
db
->
query
(
query
);
// Дело в том что на INSERT И UPDATE запросы
// db->query() может возвращать false и надо самому
// отдельно проверять действительно ли произошла ошибка
// см. SQLiteInterface::query.
string
err
(
db
->
error
());
if
(
err
.
empty
()
)
{
// db->freeResult();
if
(
db
->
insert
(
query
)
)
return
true
;
}
return
false
;
}
...
...
extensions/DBServer-SQLite/SQLiteInterface.cc
View file @
5545e179
...
...
@@ -33,6 +33,7 @@ using namespace UniSetTypes;
SQLiteInterface
::
SQLiteInterface
()
:
db
(
0
),
lastQ
(
""
),
lastE
(
""
),
queryok
(
false
),
connected
(
false
),
opTimeout
(
300
),
...
...
@@ -48,13 +49,20 @@ SQLiteInterface::~SQLiteInterface()
}
// -----------------------------------------------------------------------------------------
bool
SQLiteInterface
::
connect
(
const
string
dbfile
)
bool
SQLiteInterface
::
connect
(
const
string
dbfile
,
bool
create
)
{
int
rc
=
sqlite3_open
(
dbfile
.
c_str
(),
&
db
);
// т.к. sqlite3 по умолчанию, создаёт файл при открытии, то проверим "сами"
// if( !create && !UniSetTypes::file_exist(dbfile) )
// return false;
int
flags
=
create
?
0
:
SQLITE_OPEN_READWRITE
;
int
rc
=
sqlite3_open_v2
(
dbfile
.
c_str
(),
&
db
,
flags
,
NULL
);
if
(
rc
!=
SQLITE_OK
)
{
cerr
<<
"SQLiteInterface::connect): rc="
<<
rc
<<
" error: "
<<
sqlite3_errmsg
(
db
)
<<
endl
;
// cerr << "SQLiteInterface::connect): rc=" << rc << " error: " << sqlite3_errmsg(db) << endl;
lastE
=
"open '"
+
dbfile
+
"' error: "
+
string
(
sqlite3_errmsg
(
db
));
sqlite3_close
(
db
);
db
=
0
;
connected
=
false
;
...
...
@@ -87,7 +95,7 @@ bool SQLiteInterface::insert( const string q )
// Компилируем SQL запрос
if
(
sqlite3_prepare
(
db
,
q
.
c_str
(),
-
1
,
&
pStmt
,
NULL
)
!=
SQLITE_OK
)
{
queryok
=
false
;
queryok
=
false
;
return
false
;
}
...
...
@@ -96,7 +104,7 @@ bool SQLiteInterface::insert( const string q )
if
(
!
checkResult
(
rc
)
&&
!
wait
(
pStmt
,
SQLITE_DONE
)
)
{
sqlite3_finalize
(
pStmt
);
queryok
=
false
;
queryok
=
false
;
return
false
;
}
...
...
@@ -127,7 +135,7 @@ SQLiteResult SQLiteInterface::query( const string q )
if
(
!
checkResult
(
rc
)
&&
!
wait
(
pStmt
,
SQLITE_ROW
)
)
{
sqlite3_finalize
(
pStmt
);
queryok
=
false
;
queryok
=
false
;
return
SQLiteResult
();
}
...
...
@@ -152,12 +160,12 @@ bool SQLiteInterface::wait( sqlite3_stmt* stmt, int result )
return
false
;
}
// -----------------------------------------------------------------------------------------
const
string
SQLiteInterface
::
error
()
string
SQLiteInterface
::
error
()
{
if
(
!
db
)
return
""
;
if
(
db
)
lastE
=
sqlite3_errmsg
(
db
)
;
return
sqlite3_errmsg
(
db
)
;
return
lastE
;
}
// -----------------------------------------------------------------------------------------
const
string
SQLiteInterface
::
lastQuery
()
...
...
@@ -249,3 +257,4 @@ SQLiteResult::SQLiteResult( sqlite3_stmt* s, bool finalize )
if
(
finalize
)
sqlite3_finalize
(
s
);
}
// -----------------------------------------------------------------------------------------
extensions/DBServer-SQLite/SQLiteInterface.h
View file @
5545e179
...
...
@@ -40,8 +40,9 @@ class SQLiteInterface
SQLiteInterface
();
~
SQLiteInterface
();
bool
connect
(
const
std
::
string
dbfile
);
bool
connect
(
const
std
::
string
dbfile
,
bool
create
=
false
);
bool
close
();
bool
isConnection
();
inline
void
setOperationTimeout
(
timeout_t
msec
){
opTimeout
=
msec
;
}
inline
timeout_t
getOperationTimeout
(){
return
opTimeout
;
}
...
...
@@ -51,13 +52,11 @@ class SQLiteInterface
SQLiteResult
query
(
const
std
::
string
q
);
const
std
::
string
lastQuery
();
bool
insert
(
const
std
::
string
q
);
bool
isConnection
();
bool
insert
(
const
std
::
string
q
);
int
insert_id
();
const
std
::
string
error
();
std
::
string
error
();
protected
:
...
...
@@ -70,6 +69,7 @@ class SQLiteInterface
// sqlite3_stmt* curStmt;
std
::
string
lastQ
;
std
::
string
lastE
;
bool
queryok
;
// успешность текущего запроса
bool
connected
;
...
...
@@ -94,15 +94,21 @@ class SQLiteResult
inline
operator
bool
(){
return
!
res
.
empty
();
}
inline
int
size
(){
return
res
.
size
();
}
inline
bool
empty
(){
return
res
.
empty
();
}
protected
:
ROW
res
;
};
// ----------------------------------------------------------------------------
int
num_cols
(
SQLiteResult
::
iterator
&
);
// ROW
int
as_int
(
SQLiteResult
::
iterator
&
,
int
col
);
double
as_double
(
SQLiteResult
::
iterator
&
,
int
col
);
std
::
string
as_text
(
SQLiteResult
::
iterator
&
,
int
col
);
// ----------------------------------------------------------------------------
// COL
int
as_int
(
SQLiteResult
::
COL
::
iterator
&
);
double
as_double
(
SQLiteResult
::
COL
::
iterator
&
);
std
::
string
as_string
(
SQLiteResult
::
COL
::
iterator
&
);
...
...
extensions/DBServer-SQLite/create_db.sh
View file @
5545e179
...
...
@@ -21,6 +21,10 @@ 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
)
;
INSERT INTO main_history VALUES
(
NULL,0,0,0,105,20.3,1,0
)
;
INSERT INTO main_history VALUES
(
NULL,0,0,0,106,20.65,1,0
)
;
INSERT INTO main_history VALUES
(
NULL,0,0,0,107,20233.7,1,0
)
;
INSERT INTO main_history VALUES
(
NULL,0,0,0,108,245560.67671,1,0
)
;
_EOF_
...
...
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