Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change a lot #4

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions test/CMakeLists.txt → CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,27 @@ set(CMAKE_MACOSX_RPATH 0)

project(sql-builder)

set(DEBUG_FLAGS "-std=c++11 -g -O1 -Wall -Wextra -pedantic")
set(RELEASE_FLAGS "-std=c++11 -O3 -Wall -Wextra -pedantic")
# set(DEBUG_FLAGS "-std=c++11 -g -O1 -Wall -Wextra -pedantic")
# set(RELEASE_FLAGS "-std=c++11 -O3 -Wall -Wextra -pedantic")

set(CMAKE_CXX_FLAGS ${RELEASE_FLAGS})
set(CMAKE_CXX_FLAGS_DEBUG ${DEBUG_FLAGS})
set(CMAKE_CONFIGURATION_TYPES Debug Release)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR})

include_directories(sql-test "../")
include_directories(.)

set(SQL_TEST_SRC test/test.cpp
adapter.hpp
col.hpp
insert_model.hpp
delete_model.hpp
model.hpp
select_model.hpp
update_model.hpp)

set(SQL_TEST_SRC test.cpp)
add_executable(sql-test ${SQL_TEST_SRC})

add_test(all "sql-test")

enable_testing()
enable_testing()
105 changes: 64 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,68 @@
## Examples:

``` c++
using namespace sql;

InsertModel i;
i.insert("score", 100)
("name", std::string("six"))
("age", (unsigned char)20)
("address", "beijing")
("create_time", nullptr)
.into("user");
std::cout<<i.str()<<std::endl;
// insert into user(score, name, age, address, create_time) values(100, 'six', 20, 'beijing', '2016-03-25 10:15:59')

SelectModel s;
s.select("id", "age", "name", "address")
.from("user")
.where(column("score") > 60 and (column("age") >= 20 or column("address").is_not_null()))
.group_by("age")
.having(column("age") > 10)
.order_by("age desc")
.limit(10)
.offset(1);
std::cout<<s<<std::endl;
// select id, age, name, address from user where (score > 60) and ((age >= 20) or (address is not null)) group by age having age > 10 order by age desc limit 10 offset 1

std::vector<int> a = {1, 2, 3};
UpdateModel u;
u.update("user")
.set("name", "ddc")
("age", 18)
("score", nullptr)
("address", "beijing")
.where(column("id").in(a));
std::cout<<u<<std::endl;
// update user set name = 'ddc', age = 18, score = 18, address = 'beijing' where id in (1, 2, 3)

DeleteModel d;
d._delete()
.from("user")
.where(column("id") == 1);
std::cout<<d<<std::endl;
// delete from user where id = 1
#include <iostream>
#include <sstream>

#include "col.hpp"
#include "select_model.hpp"
#include "update_model.hpp"
#include "delete_model.hpp"
#include "insert_model.hpp"
#include "adapter.hpp"

/*
create table if not exists user (
`id` int(10) unsigned not null auto_increment,
`age` tinyint(8) unsigned,
`score` int(10) unsigned not null default 0,
`name` varchar(128) not null default '',
`address` varchar(256),
`create_time` datetime not null,
primary key(`id`)
)
*/

using namespace boosql;

int main()
{
shared_ptr<sqlite_adapter> a = make_shared<sqlite_adapter>();

select_model selector(a);
selector.from("users")
.select(col("*"))
.where(boosql::col("hello")["%hello"]) // like
.quote([](select_model & model) {
model.where(col("id") != 1).or_where(col("id") != 2);
});
select_model group(a);

group.from("group").select(col("a"), col("b"), col("c")).where(col("a") == 2);

selector.left_join(group).on("hello")("=", col("a")).or_on("id")("=", col("b")).end();

selector.group_by(col("hello")).order_by(col("hello")("DESC"));
cout << selector.str() << endl;

update_model updater(a);
updater.update("users")("hello", "hello")("world", "world").where(col("id") == 2);
cout << updater.str() << endl;

delete_model deleter(a);
deleter.from("users").where(col("id") == 1).or_where(col("name")["%hello"]);
cout << deleter.str() << endl;

insert_model insert(a);
insert.into("users")
("id", 1)
("name", "hello")
.next_row()
("id", 2)
("name", "world");

cout << insert.str() << endl;

return 0;
}
```
41 changes: 41 additions & 0 deletions adapter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once

#include <memory>
#include <string>

namespace boosql {

class adapter : public std::enable_shared_from_this<adapter>
{
public:
virtual std::string quote_value(const std::string & value) = 0;
virtual std::string quote_field(const std::string & field) = 0;
virtual std::string placeholder() = 0;
virtual adapter * clone() = 0;
};

class sqlite_adapter : public adapter
{
public:
std::string quote_value(const std::string & value) override
{
return "'" + value + "'";
}

std::string quote_field(const std::string & field) override
{
return "\"" + field + "\"";
}

std::string placeholder() override
{
return "?";
}

adapter * clone() override
{
return new sqlite_adapter();
}
};

}
Loading