Browse Source

More SQL building code

master
Peter J. Jones 8 years ago
parent
commit
e2431f8aba
3 changed files with 29 additions and 3 deletions
  1. 17
    1
      source/sql.cxx
  2. 3
    0
      source/sql.h
  3. 9
    2
      test/test_sql.cxx

+ 17
- 1
source/sql.cxx View File

@@ -83,11 +83,27 @@ int OrgFu::SQL::where (const char *condition, ...) {
conditions_.append(" ");
conditions_.append(cond);
} else if (!conditions_.empty()) {
conditions_.append(" AND ");
conditions_.append(" AND (");
conditions_.append(cond);
conditions_.append(")");
} else {
conditions_.append("(");
conditions_.append(cond);
conditions_.append(")");
}

return new_params;
}

//============================================================================
std::string OrgFu::SQL::make_end_sql (void) const {
std::string sql(" FROM ");
sql += table_name_;

if (!conditions_.empty()) {
sql += " WHERE ";
sql += conditions_;
}
return sql;
}

+ 3
- 0
source/sql.h View File

@@ -72,6 +72,9 @@ namespace OrgFu {
//========================================================================
int where (const char *condition, ...);
//========================================================================
std::string make_end_sql (void) const;
//========================================================================
// long count (void);

+ 9
- 2
test/test_sql.cxx View File

@@ -39,12 +39,19 @@ struct TestSQL {
assert(sql.where("OR something is null") == 0);
const char *final_sql =
"table=? AND foo=? "
" AND something is not null"
"(table=? AND foo=? )"
" AND (something is not null)"
" OR something is null";
// std::cerr << sql.conditions_ << std::endl;
assert(sql.conditions_ == final_sql);
const char *final_end_sql =
" FROM fake WHERE (table=? AND foo=? )"
" AND (something is not null)"
" OR something is null";
assert(sql.make_end_sql() == final_end_sql);
}
};


Loading…
Cancel
Save