Skip to content

Commit

Permalink
Fix #3 Apply PEAR coding standards
Browse files Browse the repository at this point in the history
  • Loading branch information
mynetx committed Aug 2, 2013
2 parents 34ffab4 + 5d469c1 commit 3b94426
Show file tree
Hide file tree
Showing 9 changed files with 1,354 additions and 1,333 deletions.
7 changes: 5 additions & 2 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
monty - changelog
=================

2.3.1 (not yet released)
+ #3 Apply PEAR coding standards

2.3.0 (2013-08-01)
+ Return existing connector if Monty::getConnector() is called with second parameter
+ Add Monty::tableExists for checking if a table name exists in the database
+ #1 Return existing connector if Monty::getConnector() is called with second parameter
+ #2 Add Monty::tableExists for checking if a table name exists in the database

2.2.0 (2013-06-27)
+ Set default return type for database queries
Expand Down
36 changes: 18 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ First steps
require 'monty/loader.php';

// get the MySQL connector
$objConnector = Monty::getConnector();
$connector = Monty::getConnector();

// connect to a database
$objConnector->open('youruser', 'fancypass', 'holydatabase');
$connector->open('youruser', 'fancypass', 'holydatabase');

// not running the database on localhost? add a 4th parameter like this:
// $db->open('youruser', 'fancypass', 'holydatabase', 'pentagon.example.com');
Expand All @@ -46,47 +46,47 @@ $objConnector->open('youruser', 'fancypass', 'holydatabase');

// now there's two operation modes:
// the EASY one first
$objTable = $objConnector->table('themaintable');
$table = $connector->table('themaintable');

// want multiple tables?
// $objTable->add('anothertable');
// $table->add('anothertable');

// set a condition
$objTable->where('field', '=', 'value');
$table->where('field', '=', 'value');

// there are some shortcuts, like this one:
// $objTable->eq('field', 'value');
// $table->eq('field', 'value');

// you might also want to use ands/ors
// $objTable->or($objTable->eq('field1', 'value1'),
// $objTable->like('field2', 'value2'));
// $table->or($table->eq('field1', 'value1'),
// $table->like('field2', 'value2'));
// equals:
// ... WHERE field1 = "value1" OR field2 LIKE "value2"

// peek at the generated sql code
echo $objTable->sql() . '<br />';
echo $table->sql() . '<br />';

// loop through the results and display them
for($i = 0; $i < $objTable->rows(); $i++) {
$arrRow = $objTable->next();
echo $arrRow['field'] . ' = ' . $arrRow['value'] . '<br />';
for($i = 0; $i < $table->rows(); $i++) {
$row_array = $table->next();
echo $row_array['field'] . ' = ' . $row_array['value'] . '<br />';
}

// you could also have got an object instead, like this:
// $objRow = $objTable->next(MONTY_NEXT_OBJECT);
// echo $objRow->field;
// $row = $table->next(MONTY_NEXT_OBJECT);
// echo $row->field;

// for setting the object return type as default, put this statement
// at the top of your code:
// $objTable->setReturnType(MONTY_ALL_OBJECT);
// $table->setReturnType(MONTY_ALL_OBJECT);


// you can also run raw SQL like this (the nerd mode):
$objConnector->query('SELECT * FROM themaintable WHERE field = "value"');
echo $objConnector->rows();
$connector->query('SELECT * FROM themaintable WHERE field = "value"');
echo $connector->rows();

// check if a certain table exists at the moment:
if ($objConnector->tableExists('the_table')) {
if ($connector->tableExists('the_table')) {
// do something
}

Expand Down
119 changes: 68 additions & 51 deletions classes/connector.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,14 @@
/**
* monty is a simple database wrapper.
*
* @package monty
* @author J.M. <me@mynetx.net>
* @copyright 2011-2013 J.M. <me@mynetx.net>
*
* 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.
* PHP version 5
*
* 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 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/>.
* @category Database
* @package Monty
* @author J.M. <me@mynetx.net>
* @copyright 2011-2013 J.M. <me@mynetx.net>
* @license http://opensource.org/licenses/LGPL-3.0 GNU Lesser Public License 3.0
* @link https://github.com/mynetx/monty/
*/

define('MONTY_ALL_ARRAY', 1);
Expand Down Expand Up @@ -52,103 +44,128 @@
/**
* Monty_Connector
*
* @package monty
* @author mynetx
* @copyright 2011
* @access protected
* @category Database
* @package Monty
* @author J.M. <me@mynetx.net>
* @copyright 2011 J.M. <me@mynetx.net>
* @license http://opensource.org/licenses/LGPL-3.0 GNU Lesser Public License 3.0
* @link https://github.com/mynetx/monty/
*/
abstract class Monty_Connector
{

protected $_intReturnType;
protected $_intRows;
protected $_resQuery;
protected $_strQuery;
protected $return_type;
protected $number_rows;
protected $query_handle;
protected $query_string;

/**
* Monty_Connector::error()
*
* Get the last operation error.
* @param int $intType
*
* @param int $type Error message return type
*
* @return undefined
*/
public abstract function error($intType = MONTY_ERROR_STRING);
public abstract function error($type = MONTY_ERROR_STRING);

/**
* Monty_Connector::id()
*
* Get the last inserted auto-id.
*
* @return undefined
*/
public abstract function id();

/**
* Monty_Connector::next()
*
* Walk through the result set.
*
* @return undefined
*/
public abstract function next();

/**
* Monty_Connector::nextfield()
*
* Walk through the result set and get the next field.
*
* @return undefined
*/
public abstract function nextfield();

/**
* Monty_Connector::open()
*
* Open a database connection.
* @param string $strUser
* @param string $strPassword
* @param string $strDatabase
* @param string $strHost
*
* @param string $user The database user name
* @param string $password The database password
* @param string $database Name of the database to connect to
* @param string $host Host name of database server
*
* @return undefined
*/
public abstract function open($strUser, $strPassword, $strDatabase, $strHost = 'localhost');
public abstract function open(
$user,
$password,
$database,
$host = 'localhost'
);

/**
* Monty_Connector::query()
*
* Run a raw database query.
* @param string $strQuery
*
* @param string $query_string The SQL query to execute
*
* @return undefined
*/
public abstract function query($strQuery);
public abstract function query($query_string);

/**
* Monty_Connector::rows()
*
* Get the number of rows in the result set.
*
* @return undefined
*/
public abstract function rows();

/**
* Monty_Connector::seek()
*
* Seek a certain row in the result set.
* @param int $intRow
*
* @param int $row_number The row number to set the pointer to
*
* @return undefined
*/
public abstract function seek($intRow);
public abstract function seek($row_number);

/**
* Monty_Connector::setReturnType()
*
* Store default return type for database results
* @param int $intReturnType
*
* @param int $return_type The wanted return type
*
* @return undefined
*/
public abstract function setReturnType($intReturnType);
public abstract function setReturnType($return_type);

/**
* Monty_Connector::table()
*
* @param string $strTable
* @param string $strShortcut
* @param string $table_name The name of the table to get
* @param string $table_shortcut Optional shortcut character
*
* @return undefined
*/
public abstract function table($strTable, $strShortcut = null);
public abstract function table($table_name, $table_shortcut = null);

/**
* Monty_Connector::tableExists()
*
* Checks whether the given table exists
* @param string $strTable
*
* @param string $table_name The name of table to check for
*
* @return undefined
*/
public abstract function tableExists($strTable);
public abstract function tableExists($table_name);
}
Loading

0 comments on commit 3b94426

Please sign in to comment.