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
bf5e0138
Commit
bf5e0138
authored
Feb 02, 2014
by
Pavel Vainerman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(DBInterface): рефакторинг DBInterface:
- переименовал в MySQLInterface - Упростил, убрал лишние функции - добавил класс MySQLResult
parent
da30e9ae
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
229 additions
and
153 deletions
+229
-153
libuniset2.spec
conf/libuniset2.spec
+4
-1
DBServer_MySQL.cc
extensions/DBServer-MySQL/DBServer_MySQL.cc
+6
-16
DBServer_MySQL.h
extensions/DBServer-MySQL/DBServer_MySQL.h
+4
-4
Makefile.am
extensions/DBServer-MySQL/Makefile.am
+5
-1
MySQLInterface.cc
extensions/DBServer-MySQL/MySQLInterface.cc
+97
-107
MySQLInterface.h
extensions/DBServer-MySQL/MySQLInterface.h
+52
-24
test.cc
extensions/DBServer-MySQL/test.cc
+61
-0
No files found.
conf/libuniset2.spec
View file @
bf5e0138
...
@@ -8,7 +8,7 @@
...
@@ -8,7 +8,7 @@
Name: libuniset2
Name: libuniset2
Version: 2.0
Version: 2.0
Release: alt0.
6
Release: alt0.
7
Summary: UniSet - library for building distributed industrial control systems
Summary: UniSet - library for building distributed industrial control systems
...
@@ -333,6 +333,9 @@ mv -f %buildroot%python_sitelibdir_noarch/* %buildroot%python_sitelibdir/%oname
...
@@ -333,6 +333,9 @@ mv -f %buildroot%python_sitelibdir_noarch/* %buildroot%python_sitelibdir/%oname
%exclude %_pkgconfigdir/libUniSet2.pc
%exclude %_pkgconfigdir/libUniSet2.pc
%changelog
%changelog
* Sun Feb 02 2014 Pavel Vainerman <pv@altlinux.ru> 2.0-alt0.7
- refactoring DBInterface (rename to MySQLInterface, add MySQLResult class,..)
* Sun Feb 02 2014 Pavel Vainerman <pv@altlinux.ru> 2.0-alt0.6
* Sun Feb 02 2014 Pavel Vainerman <pv@altlinux.ru> 2.0-alt0.6
- add thresholds processing for ModbusMaster (TCP and RTU)
- add thresholds processing for ModbusMaster (TCP and RTU)
- minor fixes
- minor fixes
...
...
extensions/DBServer-MySQL/DBServer_MySQL.cc
View file @
bf5e0138
...
@@ -39,7 +39,7 @@ const Debug::type DBLEVEL = Debug::LEVEL1;
...
@@ -39,7 +39,7 @@ const Debug::type DBLEVEL = Debug::LEVEL1;
// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
DBServer_MySQL
::
DBServer_MySQL
(
ObjectId
id
)
:
DBServer_MySQL
::
DBServer_MySQL
(
ObjectId
id
)
:
DBServer
(
id
),
DBServer
(
id
),
db
(
new
DB
Interface
()),
db
(
new
MySQL
Interface
()),
PingTime
(
300000
),
PingTime
(
300000
),
ReconnectTime
(
180000
),
ReconnectTime
(
180000
),
connect_ok
(
false
),
connect_ok
(
false
),
...
@@ -59,7 +59,7 @@ DBServer_MySQL::DBServer_MySQL(ObjectId id):
...
@@ -59,7 +59,7 @@ DBServer_MySQL::DBServer_MySQL(ObjectId id):
DBServer_MySQL
::
DBServer_MySQL
()
:
DBServer_MySQL
::
DBServer_MySQL
()
:
DBServer
(
conf
->
getDBServer
()),
DBServer
(
conf
->
getDBServer
()),
db
(
new
DB
Interface
()),
db
(
new
MySQL
Interface
()),
PingTime
(
300000
),
PingTime
(
300000
),
ReconnectTime
(
180000
),
ReconnectTime
(
180000
),
connect_ok
(
false
),
connect_ok
(
false
),
...
@@ -82,7 +82,6 @@ DBServer_MySQL::~DBServer_MySQL()
...
@@ -82,7 +82,6 @@ DBServer_MySQL::~DBServer_MySQL()
{
{
if
(
db
!=
NULL
)
if
(
db
!=
NULL
)
{
{
db
->
freeResult
();
db
->
close
();
db
->
close
();
delete
db
;
delete
db
;
}
}
...
@@ -98,7 +97,6 @@ void DBServer_MySQL::sysCommand( const UniSetTypes::SystemMessage *sm )
...
@@ -98,7 +97,6 @@ void DBServer_MySQL::sysCommand( const UniSetTypes::SystemMessage *sm )
case
SystemMessage
:
:
Finish
:
case
SystemMessage
:
:
Finish
:
{
{
activate
=
false
;
activate
=
false
;
db
->
freeResult
();
db
->
close
();
db
->
close
();
}
}
break
;
break
;
...
@@ -106,7 +104,6 @@ void DBServer_MySQL::sysCommand( const UniSetTypes::SystemMessage *sm )
...
@@ -106,7 +104,6 @@ void DBServer_MySQL::sysCommand( const UniSetTypes::SystemMessage *sm )
case
SystemMessage
:
:
FoldUp
:
case
SystemMessage
:
:
FoldUp
:
{
{
activate
=
false
;
activate
=
false
;
db
->
freeResult
();
db
->
close
();
db
->
close
();
}
}
break
;
break
;
...
@@ -136,7 +133,6 @@ void DBServer_MySQL::confirmInfo( const UniSetTypes::ConfirmMessage* cem )
...
@@ -136,7 +133,6 @@ void DBServer_MySQL::confirmInfo( const UniSetTypes::ConfirmMessage* cem )
if
(
!
writeToBase
(
data
.
str
())
)
if
(
!
writeToBase
(
data
.
str
())
)
{
{
ucrit
<<
myname
<<
"(update_confirm): db error: "
<<
db
->
error
()
<<
endl
;
ucrit
<<
myname
<<
"(update_confirm): db error: "
<<
db
->
error
()
<<
endl
;
db
->
freeResult
();
}
}
}
}
catch
(
Exception
&
ex
)
catch
(
Exception
&
ex
)
...
@@ -183,13 +179,10 @@ bool DBServer_MySQL::writeToBase( const string& query )
...
@@ -183,13 +179,10 @@ bool DBServer_MySQL::writeToBase( const string& query )
// Дело в том что на INSERT И UPDATE запросы
// Дело в том что на INSERT И UPDATE запросы
// db->query() может возвращать false и надо самому
// db->query() может возвращать false и надо самому
// отдельно проверять действительно ли произошла ошибка
// отдельно проверять действительно ли произошла ошибка
// см.
DB
Interface::query.
// см.
MySQL
Interface::query.
string
err
(
db
->
error
());
string
err
(
db
->
error
());
if
(
err
.
empty
()
)
if
(
err
.
empty
()
)
{
db
->
freeResult
();
return
true
;
return
true
;
}
return
false
;
return
false
;
}
}
...
@@ -206,11 +199,9 @@ void DBServer_MySQL::flushBuffer()
...
@@ -206,11 +199,9 @@ void DBServer_MySQL::flushBuffer()
// Дело в том что на INSERT И UPDATE запросы
// Дело в том что на INSERT И UPDATE запросы
// db->query() может возвращать false и надо самому
// db->query() может возвращать false и надо самому
// отдельно проверять действительно ли произошла ошибка
// отдельно проверять действительно ли произошла ошибка
// см.
DB
Interface::query.
// см.
MySQL
Interface::query.
string
err
(
db
->
error
());
string
err
(
db
->
error
());
if
(
err
.
empty
()
)
if
(
!
err
.
empty
()
)
db
->
freeResult
();
else
ucrit
<<
myname
<<
"(writeToBase): error: "
<<
err
<<
ucrit
<<
myname
<<
"(writeToBase): error: "
<<
err
<<
" lost query: "
<<
qbuf
.
front
()
<<
endl
;
" lost query: "
<<
qbuf
.
front
()
<<
endl
;
...
@@ -247,7 +238,6 @@ void DBServer_MySQL::sensorInfo( const UniSetTypes::SensorMessage* si )
...
@@ -247,7 +238,6 @@ void DBServer_MySQL::sensorInfo( const UniSetTypes::SensorMessage* si )
if
(
!
writeToBase
(
data
.
str
())
)
if
(
!
writeToBase
(
data
.
str
())
)
{
{
ucrit
<<
myname
<<
"(insert) sensor msg error: "
<<
db
->
error
()
<<
endl
;
ucrit
<<
myname
<<
"(insert) sensor msg error: "
<<
db
->
error
()
<<
endl
;
db
->
freeResult
();
}
}
}
}
catch
(
Exception
&
ex
)
catch
(
Exception
&
ex
)
...
@@ -340,7 +330,7 @@ void DBServer_MySQL::init_dbserver()
...
@@ -340,7 +330,7 @@ void DBServer_MySQL::init_dbserver()
}
}
}
}
//--------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------
void
DBServer_MySQL
::
createTables
(
DB
Interface
*
db
)
void
DBServer_MySQL
::
createTables
(
MySQL
Interface
*
db
)
{
{
UniXML_iterator
it
(
conf
->
getNode
(
"Tables"
)
);
UniXML_iterator
it
(
conf
->
getNode
(
"Tables"
)
);
if
(
!
it
)
if
(
!
it
)
...
...
extensions/DBServer-MySQL/DBServer_MySQL.h
View file @
bf5e0138
...
@@ -27,7 +27,7 @@
...
@@ -27,7 +27,7 @@
#include <map>
#include <map>
#include <queue>
#include <queue>
#include "UniSetTypes.h"
#include "UniSetTypes.h"
#include "
DB
Interface.h"
#include "
MySQL
Interface.h"
#include "DBServer.h"
#include "DBServer.h"
//------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------
/*!
/*!
...
@@ -144,7 +144,7 @@ class DBServer_MySQL:
...
@@ -144,7 +144,7 @@ class DBServer_MySQL:
protected
:
protected
:
typedef
std
::
map
<
int
,
std
::
string
>
DBTableMap
;
typedef
std
::
map
<
int
,
std
::
string
>
DBTableMap
;
virtual
void
initDB
(
DB
Interface
*
db
){};
virtual
void
initDB
(
MySQL
Interface
*
db
){};
virtual
void
initDBTableMap
(
DBTableMap
&
tblMap
){};
virtual
void
initDBTableMap
(
DBTableMap
&
tblMap
){};
virtual
void
timerInfo
(
const
UniSetTypes
::
TimerMessage
*
tm
);
virtual
void
timerInfo
(
const
UniSetTypes
::
TimerMessage
*
tm
);
...
@@ -154,7 +154,7 @@ class DBServer_MySQL:
...
@@ -154,7 +154,7 @@ class DBServer_MySQL:
bool
writeToBase
(
const
string
&
query
);
bool
writeToBase
(
const
string
&
query
);
virtual
void
init_dbserver
();
virtual
void
init_dbserver
();
void
createTables
(
DB
Interface
*
db
);
void
createTables
(
MySQL
Interface
*
db
);
inline
const
char
*
tblName
(
int
key
)
inline
const
char
*
tblName
(
int
key
)
{
{
...
@@ -169,7 +169,7 @@ class DBServer_MySQL:
...
@@ -169,7 +169,7 @@ class DBServer_MySQL:
};
};
DB
Interface
*
db
;
MySQL
Interface
*
db
;
int
PingTime
;
int
PingTime
;
int
ReconnectTime
;
int
ReconnectTime
;
bool
connect_ok
;
/*! признак наличия соеднинения с сервером БД */
bool
connect_ok
;
/*! признак наличия соеднинения с сервером БД */
...
...
extensions/DBServer-MySQL/Makefile.am
View file @
bf5e0138
...
@@ -6,13 +6,17 @@ UMYSQL_VER=@LIBVER@
...
@@ -6,13 +6,17 @@ UMYSQL_VER=@LIBVER@
lib_LTLIBRARIES
=
libUniSet2-mysql.la
lib_LTLIBRARIES
=
libUniSet2-mysql.la
libUniSet2_mysql_la_LDFLAGS
=
-version-info
$(UMYSQL_VER)
libUniSet2_mysql_la_LDFLAGS
=
-version-info
$(UMYSQL_VER)
libUniSet2_mysql_la_SOURCES
=
DB
Interface.cc DBServer_MySQL.cc
libUniSet2_mysql_la_SOURCES
=
MySQL
Interface.cc DBServer_MySQL.cc
libUniSet2_mysql_la_LIBADD
=
$(top_builddir)
/lib/libUniSet2.la
-lmysqlclient
libUniSet2_mysql_la_LIBADD
=
$(top_builddir)
/lib/libUniSet2.la
-lmysqlclient
bin_PROGRAMS
=
@PACKAGE@-mysql-dbserver
bin_PROGRAMS
=
@PACKAGE@-mysql-dbserver
@PACKAGE@
_mysql_dbserver_LDADD
=
libUniSet2-mysql.la
$(top_builddir)
/lib/libUniSet2.la
@PACKAGE@
_mysql_dbserver_LDADD
=
libUniSet2-mysql.la
$(top_builddir)
/lib/libUniSet2.la
@PACKAGE@
_mysql_dbserver_SOURCES
=
main.cc
@PACKAGE@
_mysql_dbserver_SOURCES
=
main.cc
noinst_PROGRAMS
=
mysql-test
mysql_test_LDADD
=
libUniSet2-mysql.la
$(top_builddir)
/lib/libUniSet2.la
mysql_test_SOURCES
=
test.cc
# install
# install
devel_include_HEADERS
=
*
.h
devel_include_HEADERS
=
*
.h
devel_includedir
=
$(includedir)
/@PACKAGE@/mysql
devel_includedir
=
$(includedir)
/@PACKAGE@/mysql
...
...
extensions/DBServer-MySQL/
DB
Interface.cc
→
extensions/DBServer-MySQL/
MySQL
Interface.cc
View file @
bf5e0138
...
@@ -22,14 +22,14 @@
...
@@ -22,14 +22,14 @@
*/
*/
// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
#include <sstream>
#include <sstream>
#include "DBInterface.h"
#include "UniSetTypes.h"
#include "MySQLInterface.h"
using
namespace
std
;
using
namespace
std
;
using
namespace
UniSetTypes
;
// -----------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------
DBInterface
::
DBInterface
()
:
MySQLInterface
::
MySQLInterface
()
:
result
(
0
),
lastQ
(
""
),
lastQ
(
""
),
queryok
(
false
),
connected
(
false
)
connected
(
false
)
{
{
mysql
=
new
MYSQL
();
mysql
=
new
MYSQL
();
...
@@ -38,16 +38,16 @@ connected(false)
...
@@ -38,16 +38,16 @@ connected(false)
mysql_options
(
mysql
,
MYSQL_OPT_COMPRESS
,
0
);
mysql_options
(
mysql
,
MYSQL_OPT_COMPRESS
,
0
);
}
}
DBInterface
::~
DB
Interface
()
MySQLInterface
::~
MySQL
Interface
()
{
{
close
();
close
();
delete
mysql
;
delete
mysql
;
}
}
// -----------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------
bool
DB
Interface
::
connect
(
const
string
&
host
,
const
string
&
user
,
const
string
&
pswd
,
const
string
&
dbname
)
bool
MySQL
Interface
::
connect
(
const
string
&
host
,
const
string
&
user
,
const
string
&
pswd
,
const
string
&
dbname
)
{
{
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
(),
0
,
NULL
,
0
)
)
{
{
cout
<<
error
()
<<
endl
;
cout
<<
error
()
<<
endl
;
mysql_close
(
mysql
);
mysql_close
(
mysql
);
...
@@ -58,188 +58,178 @@ bool DBInterface::connect( const string& host, const string& user, const string&
...
@@ -58,188 +58,178 @@ bool DBInterface::connect( const string& host, const string& user, const string&
return
true
;
return
true
;
}
}
// -----------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------
bool
DB
Interface
::
close
()
bool
MySQL
Interface
::
close
()
{
{
mysql_close
(
mysql
);
mysql_close
(
mysql
);
return
true
;
return
true
;
}
}
// -----------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------
bool
DB
Interface
::
insert
(
const
string
&
q
)
bool
MySQL
Interface
::
insert
(
const
string
&
q
)
{
{
if
(
!
mysql
)
if
(
!
mysql
)
return
false
;
return
false
;
if
(
mysql_query
(
mysql
,
q
.
c_str
())
)
if
(
mysql_query
(
mysql
,
q
.
c_str
())
)
{
queryok
=
false
;
return
false
;
return
false
;
}
queryok
=
true
;
return
true
;
return
true
;
}
}
// -----------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------
bool
DBInterface
::
query
(
const
string
&
q
)
MySQLResult
MySQLInterface
::
query
(
const
std
::
string
&
q
)
{
{
if
(
!
mysql
)
if
(
!
mysql
)
return
false
;
return
MySQLResult
()
;
if
(
mysql_query
(
mysql
,
q
.
c_str
())
)
if
(
mysql_query
(
mysql
,
q
.
c_str
())
)
{
{
queryok
=
false
;
cerr
<<
error
()
<<
endl
;
return
false
;
return
MySQLResult
()
;
}
}
lastQ
=
q
;
lastQ
=
q
;
result
=
mysql_store_result
(
mysql
);
// _use_result - некорректно работает с _num_rows
MYSQL_RES
*
res
=
mysql_store_result
(
mysql
);
// _use_result - некорректно работает с _num_rows
if
(
numRows
()
==
0
)
if
(
mysql_num_rows
(
res
)
==
0
)
{
return
MySQLResult
();
queryok
=
false
;
return
false
;
}
queryok
=
true
;
return
MySQLResult
(
res
,
true
);
return
true
;
}
}
// -----------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------
bool
DBInterface
::
nextRecord
(
)
bool
MySQLInterface
::
query_ok
(
const
string
&
q
)
{
{
if
(
!
mysql
||
!
result
||
!
queryok
)
if
(
!
mysql
)
return
false
;
return
false
;
Row
=
mysql_fetch_row
(
result
);
if
(
mysql_query
(
mysql
,
q
.
c_str
())
)
if
(
Row
)
return
false
;
return
true
;
lastQ
=
q
;
MYSQL_RES
*
res
=
mysql_store_result
(
mysql
);
// _use_result - некорректно работает с _num_rows
if
(
mysql_num_rows
(
res
)
==
0
)
{
mysql_free_result
(
res
);
return
false
;
return
false
;
}
}
mysql_free_result
(
res
);
return
true
;
}
// -----------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------
const
string
DB
Interface
::
error
()
const
string
MySQL
Interface
::
error
()
{
{
return
mysql_error
(
mysql
);
return
mysql_error
(
mysql
);
}
}
// -----------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------
const
string
DB
Interface
::
lastQuery
()
const
string
MySQL
Interface
::
lastQuery
()
{
{
return
lastQ
;
return
lastQ
;
}
}
// -----------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------
void
DBInterface
::
freeResult
()
int
MySQLInterface
::
insert_id
()
{
{
if
(
!
mysql
||
!
result
||
!
queryok
)
if
(
!
mysql
)
return
;
return
0
;
queryok
=
false
;
return
mysql_insert_id
(
mysql
);
mysql_free_result
(
result
);
}
}
// -----------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------
int
DBInterface
::
insert_id
()
const
char
*
MySQLInterface
::
gethostinfo
()
{
{
if
(
!
mysql
)
return
mysql_get_host_info
(
mysql
);
return
0
;
}
// -----------------------------------------------------------------------------------------
bool
MySQLInterface
::
ping
()
{
if
(
!
mysql
||
!
connected
)
return
false
;
return
mysql_insert_id
(
mysql
);
// внимание mysql_ping возвращает 0
// если всё хорошо.... (поэтому мы инвертируем)
return
!
mysql_ping
(
mysql
);
}
}
// -----------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------
unsigned
int
DBInterface
::
numCols
()
bool
MySQLInterface
::
isConnection
()
{
{
if
(
result
)
return
ping
();
//!mysql;
return
mysql_num_fields
(
result
);
return
0
;
}
}
// -----------------------------------------------------------------------------------------
string
MySQLInterface
::
addslashes
(
const
string
&
str
)
{
ostringstream
tmp
;
for
(
unsigned
int
i
=
0
;
i
<
str
.
size
();
i
++
)
{
// if( !strcmp(str[i],'\'') )
if
(
str
[
i
]
==
'\''
)
tmp
<<
"
\\
"
;
tmp
<<
str
[
i
];
}
return
tmp
.
str
();
}
// -----------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------
unsigned
int
DBInterface
::
numRows
(
)
int
num_cols
(
MySQLResult
::
iterator
&
it
)
{
{
if
(
result
)
return
it
->
size
();
return
mysql_num_rows
(
result
);
return
0
;
}
}
// -----------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------
const
MYSQL_ROW
DBInterface
::
getRow
(
)
int
as_int
(
MySQLResult
::
iterator
&
it
,
int
col
)
{
{
return
Row
;
// if( col<0 || col >it->size() )
// return 0;
return
uni_atoi
(
(
*
it
)[
col
]
);
}
}
// -----------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------
const
char
*
DBInterface
::
gethostinfo
(
)
double
as_double
(
MySQLResult
::
iterator
&
it
,
int
col
)
{
{
return
mysql_get_host_info
(
mysql
);
return
atof
(
((
*
it
)[
col
]).
c_str
()
);
}
}
// -----------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------
/*
string
as_string
(
MySQLResult
::
iterator
&
it
,
int
col
)
bool DBInterface::createDB(const string dbname)
{
{
if(!mysql)
return
((
*
it
)[
col
]);
return false;
if( mysql_create_db(mysql, dbname.c_str()) )
return true;
return false;
}
}
// -----------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------
int
as_int
(
MySQLResult
::
COL
::
iterator
&
it
)
bool DBInterface::dropDB(const string dbname)
{
{
if( mysql_drop_db(mysql, dbname.c_str()))
return
uni_atoi
(
(
*
it
)
);
return true;
return false;
}
}
*/
// -----------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------
MYSQL_RES
*
DBInterface
::
listFields
(
const
string
&
table
,
const
string
&
wild
)
double
as_double
(
MySQLResult
::
COL
::
iterator
&
it
)
{
{
if
(
!
mysql
||
!
result
)
return
atof
(
(
*
it
).
c_str
()
);
return
0
;
MYSQL_RES
*
res
=
mysql_list_fields
(
mysql
,
table
.
c_str
(),
wild
.
c_str
());
unsigned
int
cols
=
mysql_num_fields
(
result
);
// numCols();
MYSQL_ROW
row
=
mysql_fetch_row
(
res
);
// MYSQL_FIELD *field = mysql_fetch_fields(res);
// cout << field << " | ";
for
(
unsigned
int
i
=
0
;
i
<
cols
;
i
++
)
{
cout
<<
row
[
i
]
<<
" | "
;
}
return
res
;
// mysql_list_fields(mysql, table,wild);
}
}
// -----------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------
bool
DBInterface
::
moveToRow
(
int
ind
)
std
::
string
as_string
(
MySQLResult
::
COL
::
iterator
&
it
)
{
{
if
(
!
mysql
||
!
result
)
return
(
*
it
);
return
false
;
mysql_data_seek
(
result
,
ind
);
return
true
;
}
}
// -----------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------
bool
DBInterface
::
ping
()
#if 0
MySQLResult::COL get_col( MySQLResult::ROW::iterator& it )
{
{
if
(
!
mysql
||
!
connected
)
return (*it);
return
false
;
// внимание mysql_ping возвращает 0
// если всё хорошо.... (поэтому мы инвертируем)
return
!
mysql_ping
(
mysql
);
}
}
#endif
// -----------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------
bool
DBInterface
::
isConnection
()
MySQLResult
::~
MySQLResult
()
{
{
return
ping
();
//!mysql;
}
}
// -----------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------
string
DBInterface
::
addslashes
(
const
string
&
str
)
MySQLResult
::
MySQLResult
(
MYSQL_RES
*
myres
,
bool
finalize
)
{
{
ostringstream
tmp
;
MYSQL_ROW
row
;
for
(
unsigned
int
i
=
0
;
i
<
str
.
size
();
i
++
)
unsigned
int
nfields
=
mysql_num_fields
(
myres
);
while
(
(
row
=
mysql_fetch_row
(
myres
))
)
{
{
// if( !strcmp(str[i],'\'') )
COL
c
;
if
(
str
[
i
]
==
'\''
)
for
(
unsigned
int
i
=
0
;
i
<
nfields
;
i
++
)
tmp
<<
"
\\
"
;
c
.
push_back
(
(
row
[
i
]
!=
0
?
string
(
row
[
i
])
:
""
)
);
tmp
<<
str
[
i
];
res
.
push_back
(
c
);
}
}
return
tmp
.
str
();
if
(
finalize
)
mysql_free_result
(
myres
);
}
}
// -----------------------------------------------------------------------------------------
extensions/DBServer-MySQL/
DB
Interface.h
→
extensions/DBServer-MySQL/
MySQL
Interface.h
View file @
bf5e0138
...
@@ -21,34 +21,39 @@
...
@@ -21,34 +21,39 @@
* \author Pavel Vainerman
* \author Pavel Vainerman
*/
*/
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#ifndef
DB
Interface_H_
#ifndef
MySQL
Interface_H_
#define
DB
Interface_H_
#define
MySQL
Interface_H_
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
#include <string>
#include <string>
#include <vector>
#include <deque>
#include <iostream>
#include <iostream>
//#warning Для использования mysql_create нужен define USE_OLD_FUNCTIONS
//#warning Для использования mysql_create нужен define USE_OLD_FUNCTIONS
//#define USE_OLD_FUNCTIONS
//#define USE_OLD_FUNCTIONS
#include <mysql/mysql.h>
#include <mysql/mysql.h>
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
class
DBInterface
class
MySQLResult
;
// ----------------------------------------------------------------------------
class
MySQLInterface
{
{
public
:
public
:
DBInterface
();
MySQLInterface
();
~
DBInterface
();
~
MySQLInterface
();
// bool createDB(const std::string dbname);
// bool dropDB(const std::string dbname);
MYSQL_RES
*
listFields
(
const
std
::
string
&
table
,
const
std
::
string
&
wild
);
// MySQLResult listFields( const std::string& table, const std::string& wild );
bool
connect
(
const
std
::
string
&
host
,
const
std
::
string
&
user
,
const
std
::
string
&
pswd
,
bool
connect
(
const
std
::
string
&
host
,
const
std
::
string
&
user
,
const
std
::
string
&
pswd
,
const
std
::
string
&
dbname
);
const
std
::
string
&
dbname
);
bool
close
();
bool
close
();
bool
query
(
const
std
::
string
&
q
);
bool
query_ok
(
const
std
::
string
&
q
);
// \param finalize - освободить буфер после запроса
MySQLResult
query
(
const
std
::
string
&
q
);
const
std
::
string
lastQuery
();
const
std
::
string
lastQuery
();
bool
insert
(
const
std
::
string
&
q
);
bool
insert
(
const
std
::
string
&
q
);
std
::
string
addslashes
(
const
std
::
string
&
str
);
std
::
string
addslashes
(
const
std
::
string
&
str
);
...
@@ -61,32 +66,55 @@ class DBInterface
...
@@ -61,32 +66,55 @@ class DBInterface
/*! связь с БД установлена (была) */
/*! связь с БД установлена (была) */
bool
isConnection
();
bool
isConnection
();
bool
nextRecord
();
void
freeResult
();
unsigned
int
numCols
();
unsigned
int
numRows
();
bool
moveToRow
(
int
ind
);
int
insert_id
();
int
insert_id
();
const
MYSQL_ROW
getRow
();
const
std
::
string
error
();
const
std
::
string
error
();
MYSQL_ROW
Row
;
// *******************
// *******************
const
char
*
gethostinfo
();
const
char
*
gethostinfo
();
protected
:
protected
:
private
:
private
:
MYSQL_RES
*
result
;
MYSQL
*
mysql
;
MYSQL
*
mysql
;
std
::
string
lastQ
;
std
::
string
lastQ
;
bool
queryok
;
// успешность текущего запроса
bool
connected
;
bool
connected
;
};
};
// ----------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------
class
MySQLResult
{
public
:
MySQLResult
(){}
MySQLResult
(
MYSQL_RES
*
r
,
bool
finalize
=
true
);
~
MySQLResult
();
typedef
std
::
vector
<
std
::
string
>
COL
;
typedef
std
::
deque
<
COL
>
ROW
;
typedef
ROW
::
iterator
iterator
;
inline
iterator
begin
(){
return
res
.
begin
();
}
inline
iterator
end
(){
return
res
.
end
();
}
inline
operator
bool
(){
return
!
res
.
empty
();
}
inline
size_t
size
(){
return
res
.
size
();
}
inline
bool
empty
(){
return
res
.
empty
();
}
protected
:
ROW
res
;
};
// ----------------------------------------------------------------------------------
int
num_cols
(
MySQLResult
::
iterator
&
);
// ROW
int
as_int
(
MySQLResult
::
iterator
&
,
int
col
);
double
as_double
(
MySQLResult
::
iterator
&
,
int
col
);
std
::
string
as_text
(
MySQLResult
::
iterator
&
,
int
col
);
// ----------------------------------------------------------------------------
// COL
int
as_int
(
MySQLResult
::
COL
::
iterator
&
);
double
as_double
(
MySQLResult
::
COL
::
iterator
&
);
std
::
string
as_string
(
MySQLResult
::
COL
::
iterator
&
);
// ----------------------------------------------------------------------------
#endif
#endif
extensions/DBServer-MySQL/test.cc
0 → 100644
View file @
bf5e0138
#include <iostream>
#include <sstream>
#include "Exceptions.h"
#include "MySQLInterface.h"
// --------------------------------------------------------------------------
using
namespace
UniSetTypes
;
using
namespace
std
;
// --------------------------------------------------------------------------
int
main
(
int
argc
,
char
**
argv
)
{
std
::
string
dbname
(
"test-db"
);
if
(
argc
>
1
)
dbname
=
string
(
argv
[
1
]);
try
{
MySQLInterface
db
;
if
(
!
db
.
connect
(
"localhost"
,
"dbadmin"
,
"dbadmin"
,
dbname
)
)
{
cerr
<<
"db connect error: "
<<
db
.
error
()
<<
endl
;
return
1
;
}
stringstream
q
;
q
<<
"SELECT * from main_history"
;
MySQLResult
r
=
db
.
query
(
q
.
str
());
if
(
!
r
)
{
cerr
<<
"db connect error: "
<<
db
.
error
()
<<
endl
;
return
1
;
}
for
(
MySQLResult
::
iterator
it
=
r
.
begin
();
it
!=
r
.
end
();
it
++
)
{
cout
<<
"ROW: "
;
MySQLResult
::
COL
col
(
*
it
);
for
(
MySQLResult
::
COL
::
iterator
cit
=
it
->
begin
();
cit
!=
it
->
end
();
cit
++
)
cout
<<
as_string
(
cit
)
<<
"("
<<
as_double
(
cit
)
<<
") | "
;
cout
<<
endl
;
}
db
.
close
();
}
catch
(
Exception
&
ex
)
{
cerr
<<
"(test): "
<<
ex
<<
endl
;
}
catch
(
std
::
exception
&
ex
)
{
cerr
<<
"(test): "
<<
ex
.
what
()
<<
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