Return to main page


/*
* Class that operate on table 'groups'. Database Mysql.
*
* @author: http://phpdao.com
* @date: 2009-10-17 02:53
*/
class GroupsMySqlDAO implements GroupsDAO{

/**
* Get Domain object by primry key
*
* @param String $id primary key
* @return GroupsMySql
*/
public function load($id){
$sql = 'SELECT * FROM groups WHERE id = ?';
$sqlQuery = new SqlQuery($sql);
$sqlQuery->setNumber($id);
return $this->getRow($sqlQuery);
}

/**
* Get all records from table
*/
public function queryAll(){
$sql = 'SELECT * FROM groups';
$sqlQuery = new SqlQuery($sql);
return $this->getList($sqlQuery);
}

/**
* Get all records from table ordered by field
*
* @param $orderColumn column name
*/
public function queryAllOrderBy($orderColumn){
$sql = 'SELECT * FROM groups ORDER BY '.$orderColumn;
$sqlQuery = new SqlQuery($sql);
return $this->getList($sqlQuery);
}

/**
* Delete record from table
* @param group primary key
*/
public function delete($id){
$sql = 'DELETE FROM groups WHERE id = ?';
$sqlQuery = new SqlQuery($sql);
$sqlQuery->setNumber($id);
return $this->executeUpdate($sqlQuery);
}

/**
* Insert record to table
*
* @param GroupsMySql group
*/
public function insert($group){
$sql = 'INSERT INTO groups (name) VALUES (?)';
$sqlQuery = new SqlQuery($sql);

$sqlQuery->set($group->name);

$id = $this->executeInsert($sqlQuery);
$group->id = $id;
return $id;
}

/**
* Update record in table
*
* @param GroupsMySql group
*/
public function update($group){
$sql = 'UPDATE groups SET name = ? WHERE id = ?';
$sqlQuery = new SqlQuery($sql);

$sqlQuery->set($group->name);

$sqlQuery->setNumber($group->id);
return $this->executeUpdate($sqlQuery);
}

/**
* Delete all rows
*/
public function clean(){
$sql = 'DELETE FROM groups';
$sqlQuery = new SqlQuery($sql);
return $this->executeUpdate($sqlQuery);
}

public function queryByName($value){
$sql = 'SELECT * FROM groups WHERE name = ?';
$sqlQuery = new SqlQuery($sql);
$sqlQuery->set($value);
return $this->getList($sqlQuery);
}


public function deleteByName($value){
$sql = 'DELETE FROM groups WHERE name = ?';
$sqlQuery = new SqlQuery($sql);
$sqlQuery->set($value);
return $this->executeUpdate($sqlQuery);
}



/**
* Read row
*
* @return GroupsMySql
*/
protected function readRow($row){
$group = new Group();

$group->id = $row['id'];
$group->name = $row['name'];

return $group;
}

protected function getList($sqlQuery){
$tab = QueryExecutor::execute($sqlQuery);
$ret = array();
for($i=0;$i $ret[$i] = $this->readRow($tab[$i]);
}
return $ret;
}

/**
* Get row
*
* @return GroupsMySql
*/
protected function getRow($sqlQuery){
$tab = QueryExecutor::execute($sqlQuery);
return $this->readRow($tab[0]);
}

/**
* Execute sql query
*/
protected function execute($sqlQuery){
return QueryExecutor::execute($sqlQuery);
}


/**
* Execute sql query
*/
protected function executeUpdate($sqlQuery){
return QueryExecutor::executeUpdate($sqlQuery);
}

/**
* Insert row to table
*/
protected function executeInsert($sqlQuery){
return QueryExecutor::executeInsert($sqlQuery);
}
}
?>


Return to main page

Links: tomcat monitoring