Skip to content

Commit

Permalink
抽象DB接口兼容SQLite3,MySQL ORM支持DB接口
Browse files Browse the repository at this point in the history
Signed-off-by: sylar-yin <564628276@qq.com>
  • Loading branch information
sylar-yin committed Jul 16, 2019
1 parent 8810b00 commit a0e7ca4
Show file tree
Hide file tree
Showing 14 changed files with 1,601 additions and 222 deletions.
7 changes: 4 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ sylar_add_executable(test_crypto "tests/test_crypto.cc" sylar "${LIBS}")
sylar_add_executable(test_sqlite3 "tests/test_sqlite3.cc" sylar "${LIBS}")
sylar_add_executable(test_rock "tests/test_rock.cc" sylar "${LIBS}")
sylar_add_executable(test_email "tests/test_email.cc" sylar "${LIBS}")
sylar_add_executable(test_mysql "tests/test_mysql.cc" sylar "${LIBS}")

set(ORM_SRCS
sylar/orm/table.cc
Expand All @@ -187,9 +188,9 @@ add_library(test_module SHARED tests/test_module.cc)
sylar_add_executable(bin_sylar "sylar/main.cc" sylar "${LIBS}")
set_target_properties(bin_sylar PROPERTIES OUTPUT_NAME "sylar")

#add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/orm_out)
#set(OLIBS ${LIBS} orm_data)
#sylar_add_executable(test_orm "tests/test_orm.cc" orm_data "${OLIBS}")
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/orm_out)
set(OLIBS ${LIBS} orm_data)
sylar_add_executable(test_orm "tests/test_orm.cc" orm_data "${OLIBS}")

SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
SET(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
6 changes: 3 additions & 3 deletions bin/orm_conf/user.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<table name="user" namespace="test.orm">
<columns>
<column name="id" type="int64" desc="唯一主键" auto_increment="true"/>
<column name="name" type="string" desc="名称"/>
<column name="name" type="string" desc="名称" length="30"/>
<column name="email" type="string" default="xx@xx.com"/>
<column name="phone" type="string"/>
<column name="status" type="int32" default="10"/>
<column name="create_time" type="datetime" default="current_timestamp"/>
<column name="update_time" type="datetime" default="current_timestamp"/>
<column name="create_time" type="timestamp"/>
<column name="update_time" type="timestamp" default="current_timestamp" update="current_timestamp"/>
</columns>
<indexs>
<index name="pk" cols="id" type="pk" desc="主键"/>
Expand Down
59 changes: 55 additions & 4 deletions sylar/db/db.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,29 @@ class ISQLData {
typedef std::shared_ptr<ISQLData> ptr;
virtual ~ISQLData() {}

virtual int getErrno() const = 0;
virtual const std::string& getErrStr() const = 0;

virtual int getDataCount() = 0;
virtual int getColumnCount() = 0;
virtual int getColumnBytes(int idx) = 0;
virtual int getColumnType(int idx) = 0;
virtual std::string getColumnName(int idx) = 0;

virtual int getInt(int idx) = 0;
virtual double getDouble(int idx) = 0;
virtual bool isNull(int idx) = 0;
virtual int8_t getInt8(int idx) = 0;
virtual uint8_t getUint8(int idx) = 0;
virtual int16_t getInt16(int idx) = 0;
virtual uint16_t getUint16(int idx) = 0;
virtual int32_t getInt32(int idx) = 0;
virtual uint32_t getUint32(int idx) = 0;
virtual int64_t getInt64(int idx) = 0;
virtual const char* getText(int idx) = 0;
virtual std::string getTextString(int idx) = 0;
virtual uint64_t getUint64(int idx) = 0;
virtual float getFloat(int idx) = 0;
virtual double getDouble(int idx) = 0;
virtual std::string getString(int idx) = 0;
virtual std::string getBlob(int idx) = 0;
virtual time_t getTime(int idx) = 0;
virtual bool next() = 0;
};

Expand All @@ -41,6 +52,46 @@ class ISQLQuery {
virtual ISQLData::ptr query(const std::string& sql) = 0;
};

class IStmt {
public:
typedef std::shared_ptr<IStmt> ptr;
virtual ~IStmt(){}
virtual int bindInt8(int idx, int8_t& value) = 0;
virtual int bindUint8(int idx, uint8_t& value) = 0;
virtual int bindInt16(int idx, int16_t& value) = 0;
virtual int bindUint16(int idx, uint16_t& value) = 0;
virtual int bindInt32(int idx, int32_t& value) = 0;
virtual int bindUint32(int idx, uint32_t& value) = 0;
virtual int bindInt64(int idx, int64_t& value) = 0;
virtual int bindUint64(int idx, uint64_t& value) = 0;
virtual int bindFloat(int idx, float& value) = 0;
virtual int bindDouble(int idx, double& value) = 0;
virtual int bindString(int idx, char* value) = 0;
virtual int bindString(int idx, std::string& value) = 0;
virtual int bindBlob(int idx, void* value, int64_t size) = 0;
virtual int bindBlob(int idx, std::string& value) = 0;
virtual int bindTime(int idx, time_t value) = 0;
virtual int bindNull(int idx) = 0;

virtual int execute() = 0;
virtual int64_t getLastInsertId() = 0;
virtual ISQLData::ptr query() = 0;

virtual int getErrno() = 0;
virtual std::string getErrStr() = 0;
};

class IDB : public ISQLUpdate
,public ISQLQuery {
public:
typedef std::shared_ptr<IDB> ptr;
virtual ~IDB() {}

virtual IStmt::ptr prepare(const std::string& stmt) = 0;
virtual int getErrno() = 0;
virtual std::string getErrStr() = 0;
};

}

#endif
Loading

0 comments on commit a0e7ca4

Please sign in to comment.