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
a5a16445
Commit
a5a16445
authored
Jun 29, 2016
by
Pavel Vainerman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(DBServer_PGSQL): работа над оптимизацией интерфейса
parent
f628ffb0
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
49 additions
and
24 deletions
+49
-24
MySQLInterface.cc
extensions/DBServer-MySQL/MySQLInterface.cc
+2
-2
MySQLInterface.h
extensions/DBServer-MySQL/MySQLInterface.h
+1
-1
DBServer_PostgreSQL.cc
extensions/DBServer-PostgreSQL/DBServer_PostgreSQL.cc
+6
-5
DBServer_PostgreSQL.h
extensions/DBServer-PostgreSQL/DBServer_PostgreSQL.h
+5
-0
PostgreSQLInterface.cc
extensions/DBServer-PostgreSQL/PostgreSQLInterface.cc
+12
-8
PostgreSQLInterface.h
extensions/DBServer-PostgreSQL/PostgreSQLInterface.h
+3
-1
test.cc
extensions/DBServer-PostgreSQL/test.cc
+5
-2
DBInterface.h
include/DBInterface.h
+3
-2
DBInterface.cc
src/Services/DBInterface.cc
+10
-3
uniset2.files
uniset2.files
+2
-0
No files found.
extensions/DBServer-MySQL/MySQLInterface.cc
View file @
a5a16445
...
@@ -50,9 +50,9 @@ MySQLInterface::~MySQLInterface()
...
@@ -50,9 +50,9 @@ MySQLInterface::~MySQLInterface()
}
}
// -----------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------
bool
MySQLInterface
::
nconnect
(
const
string
&
host
,
const
string
&
user
,
const
string
&
pswd
,
const
string
&
dbname
)
bool
MySQLInterface
::
nconnect
(
const
string
&
host
,
const
string
&
user
,
const
string
&
pswd
,
const
string
&
dbname
,
unsigned
int
port
)
{
{
if
(
!
mysql_real_connect
(
mysql
,
host
.
c_str
(),
user
.
c_str
(),
pswd
.
c_str
(),
dbname
.
c_str
(),
0
,
NULL
,
0
)
)
if
(
!
mysql_real_connect
(
mysql
,
host
.
c_str
(),
user
.
c_str
(),
pswd
.
c_str
(),
dbname
.
c_str
(),
port
,
NULL
,
0
)
)
{
{
cout
<<
error
()
<<
endl
;
cout
<<
error
()
<<
endl
;
mysql_close
(
mysql
);
mysql_close
(
mysql
);
...
...
extensions/DBServer-MySQL/MySQLInterface.h
View file @
a5a16445
...
@@ -41,7 +41,7 @@ class MySQLInterface:
...
@@ -41,7 +41,7 @@ class MySQLInterface:
// DBResult listFields( const std::string& table, const std::string& wild );
// DBResult listFields( const std::string& table, const std::string& wild );
virtual
bool
nconnect
(
const
std
::
string
&
host
,
const
std
::
string
&
user
,
const
std
::
string
&
pswd
,
virtual
bool
nconnect
(
const
std
::
string
&
host
,
const
std
::
string
&
user
,
const
std
::
string
&
pswd
,
const
std
::
string
&
dbname
)
override
;
const
std
::
string
&
dbname
,
unsigned
int
port
=
0
)
override
;
virtual
bool
close
()
override
;
virtual
bool
close
()
override
;
bool
query_ok
(
const
std
::
string
&
q
);
bool
query_ok
(
const
std
::
string
&
q
);
...
...
extensions/DBServer-PostgreSQL/DBServer_PostgreSQL.cc
View file @
a5a16445
...
@@ -120,7 +120,7 @@ void DBServer_PostgreSQL::confirmInfo( const UniSetTypes::ConfirmMessage* cem )
...
@@ -120,7 +120,7 @@ void DBServer_PostgreSQL::confirmInfo( const UniSetTypes::ConfirmMessage* cem )
dbinfo
<<
myname
<<
"(update_confirm): "
<<
data
.
str
()
<<
endl
;
dbinfo
<<
myname
<<
"(update_confirm): "
<<
data
.
str
()
<<
endl
;
if
(
!
writeToBase
(
data
.
str
(
))
)
if
(
!
writeToBase
(
std
::
move
(
data
.
str
()
))
)
{
{
dbcrit
<<
myname
<<
"(update_confirm): db error: "
<<
db
->
error
()
<<
endl
;
dbcrit
<<
myname
<<
"(update_confirm): db error: "
<<
db
->
error
()
<<
endl
;
}
}
...
@@ -165,7 +165,7 @@ bool DBServer_PostgreSQL::writeToBase( const string& query )
...
@@ -165,7 +165,7 @@ bool DBServer_PostgreSQL::writeToBase( const string& query )
flushBuffer
();
flushBuffer
();
// А теперь собственно запрос..
// А теперь собственно запрос..
if
(
db
->
insert
AndSaveRowid
(
query
)
)
if
(
db
->
insert
(
query
)
)
return
true
;
return
true
;
return
false
;
return
false
;
...
@@ -178,7 +178,7 @@ void DBServer_PostgreSQL::flushBuffer()
...
@@ -178,7 +178,7 @@ void DBServer_PostgreSQL::flushBuffer()
// Сперва пробуем очистить всё что накопилось в очереди до этого...
// Сперва пробуем очистить всё что накопилось в очереди до этого...
while
(
!
qbuf
.
empty
()
)
while
(
!
qbuf
.
empty
()
)
{
{
if
(
!
db
->
insert
AndSaveRowid
(
qbuf
.
front
()
))
if
(
!
db
->
insert
(
qbuf
.
front
()
))
{
{
dbcrit
<<
myname
<<
"(writeToBase): error: "
<<
db
->
error
()
<<
" lost query: "
<<
qbuf
.
front
()
<<
endl
;
dbcrit
<<
myname
<<
"(writeToBase): error: "
<<
db
->
error
()
<<
" lost query: "
<<
qbuf
.
front
()
<<
endl
;
}
}
...
@@ -216,7 +216,7 @@ void DBServer_PostgreSQL::sensorInfo( const UniSetTypes::SensorMessage* si )
...
@@ -216,7 +216,7 @@ void DBServer_PostgreSQL::sensorInfo( const UniSetTypes::SensorMessage* si )
dbinfo
<<
myname
<<
"(insert_main_history): "
<<
data
.
str
()
<<
endl
;
dbinfo
<<
myname
<<
"(insert_main_history): "
<<
data
.
str
()
<<
endl
;
if
(
!
writeToBase
(
data
.
str
(
))
)
if
(
!
writeToBase
(
std
::
move
(
data
.
str
()
))
)
{
{
dbcrit
<<
myname
<<
"(insert) sensor msg error: "
<<
db
->
error
()
<<
endl
;
dbcrit
<<
myname
<<
"(insert) sensor msg error: "
<<
db
->
error
()
<<
endl
;
}
}
...
@@ -266,6 +266,7 @@ void DBServer_PostgreSQL::initDBServer()
...
@@ -266,6 +266,7 @@ void DBServer_PostgreSQL::initDBServer()
string
dbnode
(
conf
->
getProp
(
node
,
"dbnode"
));
string
dbnode
(
conf
->
getProp
(
node
,
"dbnode"
));
string
user
(
conf
->
getProp
(
node
,
"dbuser"
));
string
user
(
conf
->
getProp
(
node
,
"dbuser"
));
string
password
(
conf
->
getProp
(
node
,
"dbpass"
));
string
password
(
conf
->
getProp
(
node
,
"dbpass"
));
unsigned
int
dbport
=
conf
->
getPIntProp
(
node
,
"dbport"
,
5432
);
tblMap
[
UniSetTypes
::
Message
::
SensorInfo
]
=
"main_history"
;
tblMap
[
UniSetTypes
::
Message
::
SensorInfo
]
=
"main_history"
;
tblMap
[
UniSetTypes
::
Message
::
Confirm
]
=
"main_history"
;
tblMap
[
UniSetTypes
::
Message
::
Confirm
]
=
"main_history"
;
...
@@ -289,7 +290,7 @@ void DBServer_PostgreSQL::initDBServer()
...
@@ -289,7 +290,7 @@ void DBServer_PostgreSQL::initDBServer()
<<
" pingTime="
<<
PingTime
<<
" pingTime="
<<
PingTime
<<
" ReconnectTime="
<<
ReconnectTime
<<
endl
;
<<
" ReconnectTime="
<<
ReconnectTime
<<
endl
;
if
(
!
db
->
nconnect
(
dbnode
,
user
,
password
,
dbname
)
)
if
(
!
db
->
nconnect
(
dbnode
,
user
,
password
,
dbname
,
dbport
)
)
{
{
dbwarn
<<
myname
<<
"(init): DB connection error: "
<<
db
->
error
()
<<
endl
;
dbwarn
<<
myname
<<
"(init): DB connection error: "
<<
db
->
error
()
<<
endl
;
askTimer
(
DBServer_PostgreSQL
::
ReconnectTimer
,
ReconnectTime
);
askTimer
(
DBServer_PostgreSQL
::
ReconnectTimer
,
ReconnectTime
);
...
...
extensions/DBServer-PostgreSQL/DBServer_PostgreSQL.h
View file @
a5a16445
...
@@ -23,6 +23,11 @@
...
@@ -23,6 +23,11 @@
#include "PostgreSQLInterface.h"
#include "PostgreSQLInterface.h"
#include "DBServer.h"
#include "DBServer.h"
//------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------
/*!
* \brief The DBServer_PostgreSQL class
* Реализация работы с PostgreSQL.
*
*/
class
DBServer_PostgreSQL
:
class
DBServer_PostgreSQL
:
public
DBServer
public
DBServer
{
{
...
...
extensions/DBServer-PostgreSQL/PostgreSQLInterface.cc
View file @
a5a16445
...
@@ -50,18 +50,22 @@ bool PostgreSQLInterface::ping()
...
@@ -50,18 +50,22 @@ bool PostgreSQLInterface::ping()
return
db
&&
db
->
is_open
();
return
db
&&
db
->
is_open
();
}
}
// -----------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------
bool
PostgreSQLInterface
::
nconnect
(
const
string
&
host
,
const
string
&
user
,
const
string
&
pswd
,
const
string
&
dbname
)
bool
PostgreSQLInterface
::
nconnect
(
const
string
&
host
,
const
string
&
user
,
const
string
&
pswd
,
const
string
&
dbname
,
unsigned
int
port
)
{
{
if
(
db
)
if
(
db
)
return
true
;
return
true
;
std
::
string
conninfo
=
"dbname="
+
dbname
+
" host="
+
host
+
" user="
+
user
+
" password="
+
pswd
;
ostringstream
conninfo
;
conninfo
<<
"dbname="
<<
dbname
<<
" host="
<<
host
<<
" user="
<<
user
<<
" password="
<<
pswd
<<
" port="
<<
port
;
try
try
{
{
db
=
make_shared
<
pqxx
::
connection
>
(
conninfo
);
db
=
make_shared
<
pqxx
::
connection
>
(
std
::
move
(
conninfo
.
str
())
);
return
db
->
is_open
();
return
db
->
is_open
();
}
}
catch
(
const
std
::
exception
&
e
)
catch
(
const
std
::
exception
&
e
)
{
{
...
@@ -140,7 +144,7 @@ DBResult PostgreSQLInterface::query( const string& q )
...
@@ -140,7 +144,7 @@ DBResult PostgreSQLInterface::query( const string& q )
result
res
(
n
.
exec
(
q
)
);
result
res
(
n
.
exec
(
q
)
);
DBResult
dbres
;
DBResult
dbres
;
makeResult
(
dbres
,
res
);
makeResult
(
dbres
,
res
);
return
dbres
;
return
std
::
move
(
dbres
)
;
}
}
catch
(
const
std
::
exception
&
e
)
catch
(
const
std
::
exception
&
e
)
{
{
...
@@ -182,10 +186,10 @@ void PostgreSQLInterface::makeResult(DBResult& dbres, const pqxx::result& res )
...
@@ -182,10 +186,10 @@ void PostgreSQLInterface::makeResult(DBResult& dbres, const pqxx::result& res )
{
{
DBResult
::
COL
col
;
DBResult
::
COL
col
;
for
(
pqxx
::
result
::
tuple
::
size_type
i
=
0
;
i
<
c
.
size
();
i
++
)
for
(
pqxx
::
result
::
tuple
::
const_iterator
i
=
c
.
begin
();
i
!=
c
.
end
();
i
++
)
col
.
push_back
(
c
[
i
].
as
<
string
>
(
)
);
col
.
push_back
(
(
i
.
is_null
()
?
""
:
i
.
as
<
string
>
()
)
);
dbres
.
row
().
push_back
(
col
);
dbres
.
row
().
push_back
(
std
::
move
(
col
)
);
}
}
}
}
// -----------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------
...
...
extensions/DBServer-PostgreSQL/PostgreSQLInterface.h
View file @
a5a16445
...
@@ -33,7 +33,9 @@ class PostgreSQLInterface:
...
@@ -33,7 +33,9 @@ class PostgreSQLInterface:
PostgreSQLInterface
();
PostgreSQLInterface
();
~
PostgreSQLInterface
();
~
PostgreSQLInterface
();
virtual
bool
nconnect
(
const
std
::
string
&
host
,
const
std
::
string
&
user
,
const
std
::
string
&
pswd
,
const
std
::
string
&
dbname
)
override
;
virtual
bool
nconnect
(
const
std
::
string
&
host
,
const
std
::
string
&
user
,
const
std
::
string
&
pswd
,
const
std
::
string
&
dbname
,
unsigned
int
port
=
5432
)
override
;
virtual
bool
close
()
override
;
virtual
bool
close
()
override
;
virtual
bool
isConnection
()
override
;
virtual
bool
isConnection
()
override
;
virtual
bool
ping
()
override
;
// проверка доступности БД
virtual
bool
ping
()
override
;
// проверка доступности БД
...
...
extensions/DBServer-PostgreSQL/test.cc
View file @
a5a16445
...
@@ -16,13 +16,16 @@ int main(int argc, char** argv)
...
@@ -16,13 +16,16 @@ int main(int argc, char** argv)
try
try
{
{
PostgreSQLInterface
db
;
PostgreSQLInterface
db
;
cout
<<
"connect to '"
<<
dbname
<<
"'..."
<<
endl
;
if
(
!
db
.
nconnect
(
"localhost"
,
"dbadmin"
,
"dbadmin"
,
dbname
)
)
if
(
!
db
.
nconnect
(
"localhost"
,
"dbadmin"
,
"dbadmin"
,
dbname
)
)
{
{
cerr
<<
"
db
connect error: "
<<
db
.
error
()
<<
endl
;
cerr
<<
"
[FAILED]
connect error: "
<<
db
.
error
()
<<
endl
;
return
1
;
return
1
;
}
}
cout
<<
"connect to '"
<<
dbname
<<
"' [OK]"
<<
endl
;
stringstream
q
;
stringstream
q
;
q
<<
"SELECT * from main_history"
;
q
<<
"SELECT * from main_history"
;
...
@@ -31,7 +34,7 @@ int main(int argc, char** argv)
...
@@ -31,7 +34,7 @@ int main(int argc, char** argv)
if
(
!
r
)
if
(
!
r
)
{
{
cerr
<<
"
db connect
error: "
<<
db
.
error
()
<<
endl
;
cerr
<<
"
query
error: "
<<
db
.
error
()
<<
endl
;
return
1
;
return
1
;
}
}
...
...
include/DBInterface.h
View file @
a5a16445
...
@@ -37,9 +37,10 @@ class DBNetInterface : public DBInterface
...
@@ -37,9 +37,10 @@ class DBNetInterface : public DBInterface
DBNetInterface
()
{};
DBNetInterface
()
{};
virtual
~
DBNetInterface
()
{};
virtual
~
DBNetInterface
()
{};
// Для сетевых БД параметры должны быть в формате user@host:pswd:dbname
// Для сетевых БД параметры должны быть в формате user@host:pswd:dbname
:port
virtual
bool
connect
(
const
std
::
string
&
param
);
virtual
bool
connect
(
const
std
::
string
&
param
);
virtual
bool
nconnect
(
const
std
::
string
&
host
,
const
std
::
string
&
user
,
const
std
::
string
&
pswd
,
const
std
::
string
&
dbname
)
=
0
;
virtual
bool
nconnect
(
const
std
::
string
&
host
,
const
std
::
string
&
user
,
const
std
::
string
&
pswd
,
const
std
::
string
&
dbname
,
unsigned
int
port
)
=
0
;
};
};
// ----------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------
class
DBResult
class
DBResult
...
...
src/Services/DBInterface.cc
View file @
a5a16445
...
@@ -6,6 +6,7 @@ bool DBNetInterface::connect( const std::string& param )
...
@@ -6,6 +6,7 @@ bool DBNetInterface::connect( const std::string& param )
std
::
string
user
=
""
;
std
::
string
user
=
""
;
std
::
string
pswd
=
""
;
std
::
string
pswd
=
""
;
std
::
string
dbname
=
""
;
std
::
string
dbname
=
""
;
unsigned
int
port
=
0
;
for
(;;)
for
(;;)
{
{
...
@@ -32,10 +33,17 @@ bool DBNetInterface::connect( const std::string& param )
...
@@ -32,10 +33,17 @@ bool DBNetInterface::connect( const std::string& param )
prev
=
pos
+
1
;
prev
=
pos
+
1
;
pos
=
param
.
find_first_of
(
":"
,
prev
);
pos
=
param
.
find_first_of
(
":"
,
prev
);
dbname
=
param
.
substr
(
prev
,
pos
-
prev
);
dbname
=
param
.
substr
(
prev
,
pos
-
prev
);
if
(
pos
==
std
::
string
::
npos
)
break
;
prev
=
pos
+
1
;
pos
=
param
.
find_first_of
(
":"
,
prev
);
port
=
atoi
(
param
.
substr
(
prev
,
pos
-
prev
).
c_str
()
);
break
;
break
;
}
}
return
nconnect
(
host
,
user
,
pswd
,
dbname
);
return
nconnect
(
host
,
user
,
pswd
,
dbname
,
port
);
}
}
//--------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------
DBResult
::
ROW
&
DBResult
::
row
()
DBResult
::
ROW
&
DBResult
::
row
()
...
@@ -108,4 +116,4 @@ DBResult::COL DBResult::get_col( DBResult::iterator& it )
...
@@ -108,4 +116,4 @@ DBResult::COL DBResult::get_col( DBResult::iterator& it )
{
{
return (*it);
return (*it);
}
}
#endif
#endif
\ No newline at end of file
uniset2.files
View file @
a5a16445
...
@@ -268,6 +268,7 @@ include/ComPort485F.h
...
@@ -268,6 +268,7 @@ include/ComPort485F.h
include/Configuration.1.h
include/Configuration.1.h
include/Configuration.h
include/Configuration.h
include/DBServer.h
include/DBServer.h
include/DBInterface.h
include/Debug.h
include/Debug.h
include/DebugStream.h
include/DebugStream.h
include/DelayTimer.h
include/DelayTimer.h
...
@@ -402,6 +403,7 @@ src/Processes/NCRestorer_XML.cc
...
@@ -402,6 +403,7 @@ src/Processes/NCRestorer_XML.cc
src/Processes/CommonEventLoop.cc
src/Processes/CommonEventLoop.cc
src/Processes/EventLoopServer.cc
src/Processes/EventLoopServer.cc
src/Services/DBServer.cc
src/Services/DBServer.cc
src/Services/DBInterface.cc
src/Services/Makefile.am
src/Services/Makefile.am
src/Timers/Makefile.am
src/Timers/Makefile.am
src/Timers/PassiveCondTimer.cc
src/Timers/PassiveCondTimer.cc
...
...
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