public function options_to_sql($options)
{
$table = array_key_exists('from', $options) ? $options['from'] : $this->get_fully_qualified_table_name();
$sql = new SQLBuilder($this->conn, $table);
if (array_key_exists('joins', $options)) {
$sql->joins($this->create_joins($options['joins']));
// by default, an inner join will not fetch the fields from the joined table
if (!array_key_exists('select', $options)) {
$options['select'] = $this->get_fully_qualified_table_name() . '.*';
}
}
if (array_key_exists('select', $options)) {
$sql->select($options['select']);
}
if (array_key_exists('conditions', $options)) {
if (!is_hash($options['conditions'])) {
if (is_string($options['conditions'])) {
$options['conditions'] = array($options['conditions']);
}
call_user_func_array(array($sql, 'where'), $options['conditions']);
} else {
if (!empty($options['mapped_names'])) {
$options['conditions'] = $this->map_names($options['conditions'], $options['mapped_names']);
}
$sql->where($options['conditions']);
}
}
if (array_key_exists('order', $options)) {
$sql->order($options['order']);
}
if (array_key_exists('limit', $options)) {
$sql->limit($options['limit']);
}
if (array_key_exists('offset', $options)) {
$sql->offset($options['offset']);
}
if (array_key_exists('group', $options)) {
$sql->group($options['group']);
}
if (array_key_exists('having', $options)) {
$sql->having($options['having']);
}
return $sql;
}