Commit 4b3113a5 authored by Seblu's avatar Seblu
Browse files

Add error classes

Add test db
Add database classes
Database use sqlite3 to create database
parent abec6a12
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -2,6 +2,8 @@ bin_PROGRAMS= sls

sls_SOURCES=	src/sls.hh		\
		src/sls.cc		\
		src/error.hh		\
		src/error.cc		\
		src/option.hh		\
		src/option.cc		\
		src/database.hh		\
+124 −0
Original line number Diff line number Diff line
#include "sls.hh"
#include "database.hh"
#include "error.hh"

// *****************************************************************************
// Public Method
// *****************************************************************************
Database::Database() : db_(0) {}

void Database::open(const string &path) {

  if (path.empty())
    throw Error(ERR_DB, "DB filename is empty");

  if (db_)
    throw Error(ERR_DB, "DB already open");

  // Try to open db
  if (sqlite3_open(path.c_str(), &db_)) {
    string msg = (string) "Can't open database: " + sqlite3_errmsg(db_);
    sqlite3_close(db_);
    db_ = 0;
    throw Error(ERR_DB, msg);
  }

  // load structs
  load_hosts_();
  load_groups_();
  load_users_();
  load_scripts_();
  load_aliases_();

}

void Database::close() {
  if (!db_)
    throw Error(ERR_DB, "DB not open");

  // save structs
  save_hosts_();
  save_groups_();
  save_users_();
  save_scripts_();
  save_aliases_();

  // close db
  sqlite3_close(db_);
  db_ = 0;
}

// *****************************************************************************
// Protected Method
// *****************************************************************************

void Database::exec(const string &sql) {
  if (sql.empty())
    throw Error(ERR_DB, "SQL command is empty");

  if (!db_)
    throw Error(ERR_DB, "DB is not open");

  char *ErrMsg = 0;

  if (sqlite3_exec(db_, sql.c_str(), 0, 0, &ErrMsg) != SQLITE_OK) {
    string msg = (string) "SQL error: %s\n" +  ErrMsg;
    sqlite3_free(ErrMsg);
    throw Error(ERR_DB, msg);
  }
}

void Database::load_hosts_() {
  // create host table if not exists
  exec("BEGIN TRANSACTION;");
  exec("CREATE TABLE IF NOT EXISTS 'host' ( 'login' TEXT, 'pass' TEXT, 'ipv4' TEXT, 'level' NUMERIC );");
  exec("COMMIT;");
}

void Database::load_groups_() {
  // create group table if not exists
  exec("BEGIN TRANSACTION;");
  exec("CREATE TABLE IF NOT EXISTS 'group' ( 'name' TEXT, 'hosts' TEXT );");
  exec("COMMIT;");
}

void Database::load_users_() {
  // create user table if not exists
  exec("BEGIN TRANSACTION;");
  exec("CREATE TABLE IF NOT EXISTS 'user' ('login' TEXT, 'pass' TEXT, 'level' NUMERIC, 'priv' NUMERIC );");
  exec("COMMIT;");
}

void Database::load_scripts_() {
  // create script table if not exists
  exec("BEGIN TRANSACTION;");
  exec("CREATE TABLE IF NOT EXISTS 'script' ( 'name' TEXT, 'path' TEXT, 'level' NUMERIC );");
  exec("COMMIT;");
}

void Database::load_aliases_() {
  // create alias table if not exists
  exec("BEGIN TRANSACTION;");
  exec("CREATE TABLE IF NOT EXISTS 'alias' ( 'name' TEXT, 'value' TEXT );");
  exec("COMMIT;");
}

void Database::save_hosts_() {

}

void Database::save_groups_() {

}

void Database::save_users_() {

}

void Database::save_scripts_() {

}

void Database::save_aliases_() {

}
+30 −1
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@
# define DATABASE_HH_

# include <list>
# include <sqlite3.h>

// Host

@@ -55,7 +56,35 @@ typedef std::list<Alias> Aliases;
// Data container

class Database {

public:
  Database();

  void open(const string &path);
  void close();

protected:
  void load_hosts_();
  void load_groups_();
  void load_users_();
  void load_scripts_();
  void load_aliases_();

  void save_hosts_();
  void save_groups_();
  void save_users_();
  void save_scripts_();
  void save_aliases_();

  void exec(const string &sql);

private:
  sqlite3 *db_;

  Hosts hosts_;
  Groups groups_;
  Users users_;
  Scripts scripts_;
  Aliases aliases_;
};

#endif
+47 −17
Original line number Diff line number Diff line
//
// error.cc for sls
//
// Made by Seblu
// Login   <seblu@epita.fr>
//
// Started on  Tue Jun  5 22:46:04 2007 Seblu
// Last update Tue Jun  5 22:46:26 2007 Seblu
//
#include "error.hh"

#include "sls.hh"

Error::Error(int v)
{
  val_ = v;
}

Error::Error(const string s, int i)
/*!
** Constructor
**
** @param i error code
** @param s error message
*/
Error::Error(int i, const string s)
{
  val_ = i;
  msg_ << s;
}

/*!
** Constructor by copy
**
** @param e
*/
Error::Error(const Error &e)
{
  val_ = e.val_;
  msg_ << e.msg_.str();
}

/*!
** Print and error msg on std err
**
*/
void Error::print() const
{
  if (msg_.str() != "")
    std::cerr << msg_.str() << "." << std::endl;
}

/*!
** @return a copy of message description
*/
string Error::message() const
{
  return msg_.str();
}

/*!
** @return the error code
*/
int Error::code() const
{
  return val_;
}

/*!
** Set error code to @param v
*/
void Error::code(int v)
{
  val_ = v;
}

/*!
** Copy operator
*/
Error &Error::operator=(const Error &rhs)
{
  val_ = rhs.val_;
@@ -50,24 +68,36 @@ Error &Error::operator=(const Error &rhs)
  return *this;
}

/*!
** Print error message to @param o stream
*/
ostream &operator<<(ostream &o, const Error &e)
{
  o << e.msg_.str();
  return o;
}

/*!
** Concatenate @param val to errror message
*/
Error &operator<<(Error &e, int val)
{
  e.msg_ << val;
  return e;
}

/*!
** Concatenate @param s to errror message
*/
Error &operator<<(Error &e, const string &s)
{
  e.msg_ << s;
  return e;
}

/*!
** Concatenate @param s before error message
*/
Error &operator>>(Error &e, const string &s)
{
  string buf = s + e.msg_.str();

sls/trunk/src/error.hh

0 → 100644
+31 −0
Original line number Diff line number Diff line
#ifndef ERROR_HH
# define ERROR_HH

# include <ios>
# include <iostream>
# include <sstream>

typedef std::string string;
typedef std::stringstream sstream;
typedef std::ostream ostream;

class Error
{
public:
  Error(int i, string s = "");
  Error(const Error &e);
  void print() const;
  string message() const;
  int code() const;
  void code(int);
  Error &operator=(const Error &rhs);
  friend ostream &operator<<(ostream &o, const Error &e);
  friend Error &operator<<(Error &e, const string &s);
  friend Error &operator>>(Error &e, const string &s);
  friend Error &operator<<(Error &e, int val);
protected:
  sstream msg_;
  int val_;
};

#endif
Loading