Skip to content

Commit

Permalink
Added: Objectified wrapper for tables.
Browse files Browse the repository at this point in the history
  • Loading branch information
mynetx committed Mar 10, 2011
1 parent b8761cb commit 8abc31d
Show file tree
Hide file tree
Showing 4 changed files with 301 additions and 0 deletions.
6 changes: 6 additions & 0 deletions connector.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@
define('MONTY_ERROR_OBJECT', 3);
define('MONTY_ERROR_NUMERIC', 4);

define('MONTY_JOIN_NORMAL', 1);
define('MONTY_JOIN_LEFT', 2);
define('MONTY_JOIN_RIGHT', 3);

define('MONTY_NEXT_ARRAY', 1);
define('MONTY_NEXT_OBJECT', 2);

define('MONTY_QUERY_SELECT', 1);

/**
* Monty_Connector
*
Expand Down
11 changes: 11 additions & 0 deletions connector.mysql.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,15 @@ public function seek($intRow) {
}
return mysql_data_seek($intRow);
}

/**
* Monty_MySQL::table()
*
* @param string $strTable
* @param string $strShortcut
* @return object Monty_MySQL_Easy
*/
public function table($strTable, $strShortcut = null) {
return new Monty_MySQL_Easy($strTable, $strShortcut);
}
}
283 changes: 283 additions & 0 deletions connector.mysql.easy.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,283 @@
<?php

/**
* monty is a simple database wrapper.
* Copyright (C) 2011 mynetx.
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/**
* Monty_MySQL_Easy
*
* @package monty
* @author mynetx
* @copyright 2011
* @access public
*/
class Monty_MySQL_Easy extends Monty_MySQL
{
protected $_arrJoins;
protected $_arrTable;
protected $_arrWheres;
protected $_boolDirty;

/**
* Monty_MySQL_Easy::__construct()
*
* @param string $strTable
* @param string $strShortcut
* @return void
*/
public function __construct($strTable, $strShortcut = null)
{
parent::__construct();
if (!$strShortcut) {
$strShortcut = substr($strTable, 0, 1);
}
$this->_arrJoins = array();
$this->_arrTable = array($strTable, $strShortcut);
$this->_arrWheres = array();
$this->_boolDirty = true;
}

/**
* Monty_MySQL_Easy::eq()
*
* @param string $strField
* @param string $strValue
* @return void
*/
public function eq($strField, $strValue)
{
$this->where($strField, '=', $strValue);
}

/**
* Monty_MySQL_Easy::gt()
*
* @param string $strField
* @param string $strValue
* @return void
*/
public function gt($strField, $strValue)
{
$this->where($strField, '>', $strValue);
}

/**
* Monty_MySQL_Easy::gte()
*
* @param string $strField
* @param string $strValue
* @return void
*/
public function gte($strField, $strValue)
{
$this->where($strField, '>=', $strValue);
}

/**
* Monty_MySQL_Easy::join()
*
* @param string $strTable
* @param string $strShortcut
* @param int $intJoin
* @return void
*/
public function join($strTable, $strShortcut, $intJoin = MONTY_JOIN_NORMAL)
{
$this->_boolDirty = true;
$this->_arrJoins[] = array($strTable, $intJoin);
}

/**
* Monty_MySQL_Easy::like()
*
* @param string $strField
* @param string $strValue
* @return void
*/
public function like($strField, $strValue)
{
$this->where($strField, 'LIKE', $strValue);
}

/**
* Monty_MySQL_Easy::lt()
*
* @param string $strField
* @param string $strValue
* @return void
*/
public function lt($strField, $strValue)
{
$this->where($strField, '<', $strValue);
}

/**
* Monty_MySQL_Easy::lte()
*
* @param string $strField
* @param string $strValue
* @return void
*/
public function lte($strField, $strValue)
{
$this->where($strField, '<=', $strValue);
}

/**
* Monty_MySQL_Easy::ne()
*
* @param string $strField
* @param string $strValue
* @return void
*/
public function ne($strField, $strValue)
{
$this->where($strField, '!=', $strValue);
}

/**
* Monty_MySQL_Easy::next()
*
* @param int $intType
* @return mixed $mixRow
*/
public function next($intType = MONTY_NEXT_ARRAY)
{
$this->_buildQuery();
return parent::next($intType);
}

/**
* Monty_MySQL_Easy::nextfield()
*
* @param mixed $mixField
* @return mixed $mixField
*/
public function nextfield($mixField = 0)
{
$this->_buildQuery();
return parent::nextfield($mixField = 0);
}

/**
* Monty_MySQL_Easy::rows()
*
* @return int $intRows
*/
public function rows()
{
$this->_buildQuery();
return parent::rows();
}

/**
* Monty_MySQL_Easy::seek()
*
* @param int $intRow
* @return bool $boolHasSucceeded
*/
public function seek($intRow)
{
$this->_buildQuery();
return parent::seek($intRow);
}

/**
* Monty_MySQL_Easy::sql()
*
* @param int $intType
* @return string $strQuery
*/
public function sql($intType = MONTY_QUERY_SELECT) {
$this->_buildQuery($intType);
return $this->_strQuery;
}

/**
* Monty_MySQL_Easy::where()
*
* @param string $strField
* @param string $strComparison
* @param mixed $mixValue
* @return void
*/
public function where($strField, $strComparison, $mixValue)
{
$this->_boolDirty = true;
$this->_arrWheres[] = array($strField, $strComparison, $mixValue);
}

/**
* Monty_MySQL_Easy::_buildQuery()
*
* @param int $intType
* @return $boolHasSucceeded
* @access protected
*/
protected function _buildQuery($intType = MONTY_QUERY_SELECT)
{
if (!$this->_boolDirty) {
return false;
}
switch ($intType) {
case MONTY_QUERY_SELECT:
$strQuery = 'SELECT * FROM `' . $this->_arrTable[0] . '` ' . $this->
_arrTable[1];
foreach ($this->_arrJoins as $arrJoin) {
switch ($arrJoin[2]) {
case MONTY_JOIN_NORMAL:
$strQuery .= ' JOIN';
break;
case MONTY_JOIN_LEFT:
$strQuery .= ' LEFT JOIN';
break;
case MONTY_JOIN_RIGHT:
$strQuery .= ' RIGHT JOIN';
break;
}
$strQuery .= ' `' . $arrJoin[0] . '` ' . $arrJoin[1];
}
if (count($this->_arrWheres)) {
$strQuery .= ' WHERE';
for ($i = 0; $i < count($this->_arrWheres); $i++) {
$arrWhere = $this->_arrWheres[$i];
if (stristr($arrWhere[0], '.')) {
$arrTable = explode('.', $arrWhere[0], 2);
$strTable = $arrTable[0] . '.`' . $arrTable[1] . '`';
}
else {
$strTable = '`' . $arrWhere[0] . '`';
}
$strQuery .= ' ' . $strTable . ' ' . $arrWhere[1];
if (is_null($arrWhere[2])) {
$strQuery .= ' NULL';
}
else {
$strQuery .= ' "' . mysql_real_escape_string($arrWhere[2]) . '"';
}
if ($i + 1 < count($this->_arrWheres)) {
$strQuery .= ' AND';
}
}
}
break;
}
$this->_boolDirty = false;
return $this->query($strQuery);
}
}
1 change: 1 addition & 0 deletions loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@
require_once $strDirectory.'/monty.class.php';
require_once $strDirectory.'/connector.class.php';
require_once $strDirectory.'/connector.mysql.class.php';
require_once $strDirectory.'/connector.mysql.easy.class.php';

0 comments on commit 8abc31d

Please sign in to comment.