Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
F
fmp_vip
Overview
Overview
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
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zhenfei.zhang
fmp_vip
Commits
53a320c2
Commit
53a320c2
authored
Jan 29, 2018
by
NitefullWind
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1. 修复重新构建出错问题。 2. 实现数据库连接管理类。3. 实现存储门店信息。
parent
e56e4444
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
116 additions
and
39 deletions
+116
-39
fmvip/db/database.h
+73
-3
fmvip/fmvip.pri
+7
-7
fmvip/task/tasksetstoreinfo.cpp
+24
-12
tests/autotest/tst_db_create.h
+7
-10
tests/autotest/tst_db_query.h
+5
-7
No files found.
fmvip/db/database.h
View file @
53a320c2
...
@@ -38,7 +38,9 @@
...
@@ -38,7 +38,9 @@
namespace
DB
{
namespace
DB
{
inline
std
::
auto_ptr
<
odb
::
database
>
using
namespace
odb
::
core
;
inline
std
::
shared_ptr
<
odb
::
database
>
OpenDatabase
(
const
char
*
dbName
=
"Test.db"
)
OpenDatabase
(
const
char
*
dbName
=
"Test.db"
)
{
{
using
namespace
std
;
using
namespace
std
;
...
@@ -47,8 +49,10 @@ OpenDatabase (const char *dbName = "Test.db")
...
@@ -47,8 +49,10 @@ OpenDatabase (const char *dbName = "Test.db")
#if defined(DATABASE_MYSQL)
#if defined(DATABASE_MYSQL)
auto_ptr
<
database
>
db
(
new
odb
::
mysql
::
database
(
dbName
));
auto_ptr
<
database
>
db
(
new
odb
::
mysql
::
database
(
dbName
));
#elif defined(DATABASE_SQLITE)
#elif defined(DATABASE_SQLITE)
auto_ptr
<
database
>
db
(
// auto_ptr<database> db (
new
odb
::
sqlite
::
database
(
dbName
,
SQLITE_OPEN_READWRITE
|
SQLITE_OPEN_CREATE
));
// new odb::sqlite::database (dbName, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE));
std
::
shared_ptr
<
database
>
db
(
new
odb
::
sqlite
::
database
(
dbName
,
SQLITE_OPEN_READWRITE
|
SQLITE_OPEN_CREATE
));
fstream
dbFile
;
fstream
dbFile
;
dbFile
.
open
(
dbName
,
ios
::
in
);
dbFile
.
open
(
dbName
,
ios
::
in
);
...
@@ -90,6 +94,72 @@ OpenDatabase (const char *dbName = "Test.db")
...
@@ -90,6 +94,72 @@ OpenDatabase (const char *dbName = "Test.db")
return
db
;
return
db
;
}
}
class
DBConnect
{
public
:
static
DBConnect
*
Instance
()
{
static
DBConnect
dbConnect
;
return
&
dbConnect
;
}
void
OpenDatabase
()
{
std
::
fstream
dbFile
;
dbFile
.
open
(
_dbName
.
c_str
(),
std
::
ios
::
in
);
if
(
!
dbFile
)
{
// Create the database schema. Due to bugs in SQLite foreign key
// support for DDL statements, we need to temporarily disable
// foreign keys.
//
{
connection_ptr
c
(
_db_pointer
->
connection
());
c
->
execute
(
"PRAGMA foreign_keys=OFF"
);
transaction
t
(
c
->
begin
());
schema_catalog
::
create_schema
(
*
_db_pointer
,
""
,
false
);
t
.
commit
();
c
->
execute
(
"PRAGMA foreign_keys=ON"
);
}
}
else
if
(
_db_pointer
->
schema_version
()
<
schema_catalog
::
current_version
(
*
_db_pointer
))
{
qDebug
()
<<
"Schema migrate. Schema version: "
<<
_db_pointer
->
schema_version
()
<<
" Current version: "
<<
schema_catalog
::
current_version
(
*
_db_pointer
)
<<
" Base version: "
<<
schema_catalog
::
base_version
(
*
_db_pointer
);
try
{
transaction
t
(
_db_pointer
->
begin
());
schema_catalog
::
migrate
(
*
_db_pointer
);
t
.
commit
();
}
catch
(
const
odb
::
exception
&
e
)
{
qDebug
()
<<
"Exception: schema migrate. "
<<
e
.
what
();
}
}
}
std
::
shared_ptr
<
odb
::
database
>
DBPointer
()
{
return
_db_pointer
;
}
private
:
DBConnect
()
:
_dbName
(
"Test.db"
)
{
_db_pointer
=
std
::
make_shared
<
odb
::
sqlite
::
database
>
(
_dbName
,
SQLITE_OPEN_READWRITE
|
SQLITE_OPEN_CREATE
);
OpenDatabase
();
}
std
::
shared_ptr
<
odb
::
database
>
_db_pointer
;
std
::
string
_dbName
;
};
inline
odb
::
database
*
DBP
()
{
return
DBConnect
::
Instance
()
->
DBPointer
().
get
();
}
inline
std
::
shared_ptr
<
odb
::
database
>
DBSP
()
{
return
DBConnect
::
Instance
()
->
DBPointer
();
}
}
}
#endif // DATABASE_H
#endif // DATABASE_H
fmvip/fmvip.pri
View file @
53a320c2
...
@@ -182,12 +182,12 @@ odb.variable_out = SOURCES
...
@@ -182,12 +182,12 @@ odb.variable_out = SOURCES
odb.clean = ${QMAKE_FILE_BASE}-odb.cxx ${QMAKE_FILE_BASE}-odb.hxx ${QMAKE_FILE_BASE}-odb.ixx ${QMAKE_FILE_BASE}.sql
odb.clean = ${QMAKE_FILE_BASE}-odb.cxx ${QMAKE_FILE_BASE}-odb.hxx ${QMAKE_FILE_BASE}-odb.ixx ${QMAKE_FILE_BASE}.sql
QMAKE_EXTRA_COMPILERS += odb
QMAKE_EXTRA_COMPILERS += odb
#
odbh.name = odb ${QMAKE_FILE_IN}
odbh.name = odb ${QMAKE_FILE_IN}
#
odbh.input = ODB_PWD_FILES
odbh.input = ODB_PWD_FILES
#
odbh.output = ${QMAKE_FILE_BASE}-odb.hxx
odbh.output = ${QMAKE_FILE_BASE}-odb.hxx
#odbh.commands = @true
odbh.commands = @
#
odbh.CONFIG = no_link
odbh.CONFIG = no_link
#
odbh.depends = ${QMAKE_FILE_BASE}-odb.cxx
odbh.depends = ${QMAKE_FILE_BASE}-odb.cxx
#
QMAKE_EXTRA_COMPILERS += odbh
QMAKE_EXTRA_COMPILERS += odbh
fmvip/task/tasksetstoreinfo.cpp
View file @
53a320c2
#include "tasksetstoreinfo.h"
#include "tasksetstoreinfo.h"
//#include "items/storeinfo.h"
#include "database.h"
//#include "dbop.h"
using
namespace
DB
;
using
namespace
odb
;
TaskSetStoreInfo
::
TaskSetStoreInfo
(
QJsonObject
&
jsonObj
,
QObject
*
parent
)
TaskSetStoreInfo
::
TaskSetStoreInfo
(
QJsonObject
&
jsonObj
,
QObject
*
parent
)
:
FMTaskSimple
(
jsonObj
,
FM_Set_Store_Info
,
0
,
parent
)
:
FMTaskSimple
(
jsonObj
,
FM_Set_Store_Info
,
0
,
parent
)
...
@@ -14,14 +16,24 @@ void TaskSetStoreInfo::packagePOSReq()
...
@@ -14,14 +16,24 @@ void TaskSetStoreInfo::packagePOSReq()
void
TaskSetStoreInfo
::
packagePOSRsp
()
void
TaskSetStoreInfo
::
packagePOSRsp
()
{
{
// FMItem::StoreInfo storeInfo;
QSharedPointer
<
StoreInfo
>
storeInfo
(
new
StoreInfo
());
// storeInfo.SetPropertiesByJson(posReqJsonObj);
storeInfo
->
setStoreId
(
getPosJsonValue
(
PosProps
.
StoreId
).
toString
());
// bool isOk = DBOP::Save(&storeInfo);
storeInfo
->
setPosId
(
getPosJsonValue
(
PosProps
.
PosId
).
toString
());
// int statusCode = isOk ? FM_API_SUCCESS : FM_API_ERROR;
storeInfo
->
setBusinessDate
(
getPosJsonValue
(
PosProps
.
BussinessDate
).
toString
());
// QString msg("");
storeInfo
->
setOperatorId
(
getPosJsonValue
(
PosProps
.
OperatorId
).
toString
());
// if(!isOk) {
// msg = QString::fromLocal8Bit("保存门店信息失败");
int
statusCode
=
FM_API_SUCCESS
;
// }
QString
msg
(
""
);
// posRspJsonObj[PosProps.StatusCode] = statusCode;
// posRspJsonObj[PosProps.Msg] = msg;
try
{
transaction
t
(
DBSP
()
->
begin
());
DBSP
()
->
persist
(
storeInfo
);
t
.
commit
();
}
catch
(
const
exception
&
e
)
{
statusCode
=
FM_API_ERROR
;
msg
=
QString
::
fromLocal8Bit
(
"保存门店信息失败"
);
FMP_ERROR
()
<<
msg
<<
e
.
what
();
}
posRspJsonObj
[
PosProps
.
StatusCode
]
=
statusCode
;
posRspJsonObj
[
PosProps
.
Msg
]
=
msg
;
}
}
tests/autotest/tst_db_create.h
View file @
53a320c2
...
@@ -25,8 +25,7 @@ protected:
...
@@ -25,8 +25,7 @@ protected:
}
}
try
{
try
{
_db
=
OpenDatabase
();
ASSERT_NE
(
DBSP
(),
nullptr
);
ASSERT_NE
(
_db
.
get
(),
nullptr
);
}
catch
(
const
odb
::
exception
&
e
)
{
}
catch
(
const
odb
::
exception
&
e
)
{
FAIL
()
<<
"Exception: "
<<
e
.
what
()
<<
std
::
endl
;
FAIL
()
<<
"Exception: "
<<
e
.
what
()
<<
std
::
endl
;
}
}
...
@@ -34,8 +33,6 @@ protected:
...
@@ -34,8 +33,6 @@ protected:
void
TearDown
()
void
TearDown
()
{
{
}
}
std
::
auto_ptr
<
database
>
_db
;
};
};
TEST_F
(
TestDBCreate
,
Insert
)
TEST_F
(
TestDBCreate
,
Insert
)
...
@@ -62,20 +59,20 @@ TEST_F(TestDBCreate, Insert)
...
@@ -62,20 +59,20 @@ TEST_F(TestDBCreate, Insert)
// 插入数据
// 插入数据
try
{
try
{
transaction
t
(
_db
->
begin
());
transaction
t
(
DBSP
()
->
begin
());
_db
->
persist
(
storeInfo
);
DBSP
()
->
persist
(
storeInfo
);
t
.
commit
();
t
.
commit
();
EXPECT_NE
(
storeInfo
->
id
(),
0
);
EXPECT_NE
(
storeInfo
->
id
(),
0
);
t
.
reset
(
_db
->
begin
());
t
.
reset
(
DBSP
()
->
begin
());
_db
->
persist
(
order
);
DBSP
()
->
persist
(
order
);
t
.
commit
();
t
.
commit
();
EXPECT_NE
(
order
->
id
(),
0
);
EXPECT_NE
(
order
->
id
(),
0
);
EXPECT_EQ
(
order
->
storeInfo
()
->
id
(),
storeInfo
->
id
());
EXPECT_EQ
(
order
->
storeInfo
()
->
id
(),
storeInfo
->
id
());
t
.
reset
(
_db
->
begin
());
t
.
reset
(
DBSP
()
->
begin
());
_db
->
persist
(
pay
);
DBSP
()
->
persist
(
pay
);
t
.
commit
();
t
.
commit
();
EXPECT_NE
(
pay
->
id
(),
0
);
EXPECT_NE
(
pay
->
id
(),
0
);
...
...
tests/autotest/tst_db_query.h
View file @
53a320c2
...
@@ -19,26 +19,24 @@ class TestDBQuery : public ::testing::Test
...
@@ -19,26 +19,24 @@ class TestDBQuery : public ::testing::Test
protected
:
protected
:
void
SetUp
()
void
SetUp
()
{
{
_db
=
OpenDatabase
();
ASSERT_NE
(
DBSP
(),
nullptr
);
ASSERT_NE
(
_db
.
get
(),
nullptr
);
}
}
void
TearDown
()
void
TearDown
()
{
{
}
}
std
::
auto_ptr
<
database
>
_db
;
};
};
TEST_F
(
TestDBQuery
,
Query
)
TEST_F
(
TestDBQuery
,
Query
)
{
{
try
{
try
{
transaction
t
(
_db
->
begin
());
transaction
t
(
DBSP
()
->
begin
());
QString
orderId
=
"2018001"
;
QString
orderId
=
"2018001"
;
query
<
Order
>
q
(
query
<
Order
>::
orderId
==
orderId
);
query
<
Order
>
q
(
query
<
Order
>::
orderId
==
orderId
);
result
<
Order
>
r1
(
_db
->
query
<
Order
>
(
q
));
result
<
Order
>
r1
(
DBSP
()
->
query
<
Order
>
(
q
));
EXPECT_FALSE
(
r1
.
empty
());
EXPECT_FALSE
(
r1
.
empty
());
...
@@ -52,13 +50,13 @@ TEST_F(TestDBQuery, Query)
...
@@ -52,13 +50,13 @@ TEST_F(TestDBQuery, Query)
TEST_F
(
TestDBQuery
,
QueryAnd
)
TEST_F
(
TestDBQuery
,
QueryAnd
)
{
{
try
{
try
{
transaction
t
(
_db
->
begin
());
transaction
t
(
DBSP
()
->
begin
());
QString
orderId
=
"2018001"
;
QString
orderId
=
"2018001"
;
query
<
Order
>
q
((
query
<
Order
>::
orderId
==
orderId
)
&&
(
query
<
Order
>::
fmOrderId
==
orderId
));
query
<
Order
>
q
((
query
<
Order
>::
orderId
==
orderId
)
&&
(
query
<
Order
>::
fmOrderId
==
orderId
));
result
<
Order
>
r
(
_db
->
query
<
Order
>
(
q
));
result
<
Order
>
r
(
DBSP
()
->
query
<
Order
>
(
q
));
EXPECT_TRUE
(
r
.
empty
());
EXPECT_TRUE
(
r
.
empty
());
...
...
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