Log in

View Full Version : HKV3



Dentafrice
08-07-2009, 02:58 AM
Just been bored lately, been playing around with PHP (haven't touched it since March :P).

Figured I'd throw something together..

Doesn't look that great right now, but I'm just really doing it to play around.. not sure if I'll ever release it.

Been playing around with method chaining, really pretty neat imo.

Example:



$data = $this->database->select()->where("username", $username)->on("users")->fetch_object();
And



$this->database->update()->where("id", $user_id)->set($field, $value)->on("users");
http://i29.tinypic.com/6iea2c.png

Invent
08-07-2009, 03:22 AM
Yeah, method chaning can be really useful when dealing with certain types of classes. It's especially useful with things such as DB drivers (like in your example).

Dentafrice
08-07-2009, 03:29 AM
Yeah, I store all the wheres, and sets in an array (as sort of a cache), until the on() method is called (which performs the query.. then clears the cache.. then sets the performed query into the class so the num_rows, fetch_array, and fetch_object classes can work with it).

Not that great, but just something to play around with:



<?php

class Database
{

private $database_details;
private $cache;
private $query;

public function init($database_details)
{
if (empty($database_details["server"]) || empty($database_details["username"]) ||
empty($database_details["name"]))
{
$this->core->error("Database details incomplete.", "database");
}

$this->database_details = $database_details;

$this->connect_database();
$this->select_database();
}

private function connect_database()
{
$connect = mysql_connect($this->database_details["server"], $this->
database_details["username"], $this->database_details["password"]);

if (!$connect)
{
$this->core->error("Could not connect to MySQL server.", "database");
}
}

private function select_database()
{
$select = mysql_select_db($this->database_details["name"]);

if (!$select)
{
$this->core->error("Could not select the database.", "database");
}
}

public function select($field = '')
{
$this->cache["type"] = "SELECT";

$field = ($field == '') ? '*' : "`{$field}`";

$this->cache["field"] = $field;

return $this;
}

public function update()
{
$this->cache["type"] = "UPDATE";

return $this;
}

public function where($field, $equals)
{
$this->cache["where"][] = "`{$field}`='{$equals}'";

return $this;
}

public function set($field, $equals)
{
$this->cache["set"][] = "`{$field}`='{$equals}'";

return $this;
}

public function limit($number)
{
$this->cache["limit"] = "0,{$number}";

return $this;
}

public function order($by, $dir)
{
$dir = strtoupper($dir);

$this->cache["order"] = "`{$by} {$dir}`";

return $this;
}

public function on($table)
{
switch ($this->cache["type"])
{
case "SELECT":
$query = "{$this->cache["type"]} {$this->cache["field"]} FROM `{$table}`";
break;
case "UPDATE":
$query = "{$this->cache["type"]} `{$table}`";
break;
}

if (!empty($this->cache["set"]))
{
$query .= ' SET';

$number_set = 0;
$count_set = count($this->cache["set"]);

foreach ($this->cache["set"] as $set)
{
$query .= "{$set} ";

if ($count_set != 1 && $number_set != ($count_set - 1))
{
$query .= ', ';
}

$number_set++;
}
}

if (!empty($this->cache["where"]))
{
$query .= ' WHERE';

$number = 0;
$count = count($this->cache["where"]);

foreach ($this->cache["where"] as $where)
{
$query .= "{$where} ";

if ($count != 1 && $number != ($count - 1))
{
$query .= "AND ";
}

$number++;
}

}

if ($this->cache["order"] != "")
{
$query .= " ORDER BY {$this->cache["order"]}";
}

if ($this->cache["limit"] != "")
{
$query .= " LIMIT {$this->cache["limit"]}";
}

$this->query($query);

$this->cache = "";

return $this;
}

public function query($query)
{
$query = @mysql_query($query);

$this->query = $query;

return $query;
}

public function fetch_array($query = '')
{
$query = ($query == '') ? $this->query : $query;

$array = @mysql_fetch_array($query);

return $array;
}

public function fetch_object($query = '')
{
$query = ($query == '') ? $this->query : $query;

$object = @mysql_fetch_object($query);

return $object;
}

public function num_rows($query = '')
{
$query = ($query == '') ? $this->query : $query;

$num_rows = @mysql_num_rows($query);

return $num_rows;
}

public function count_table($table)
{
return $this->select()->on($table)->num_rows();
}

}

Invent
08-07-2009, 03:40 AM
Nice! Looks similar to one of my old MySQLi driver classes from a while back in terms of how the sql clauses, etc are stored.



<?php

class MySQL_Drivers_MySQLi_Select {

protected static $_instance;
private $_options;

public function __construct() {

}

public static function factory() {

if( self::$_instance === NULL ) {

self::$_instance = new self();

}

return self::$_instance;

}

public function select( $columns ) {

$size = count( $columns );

if( $size <= 1 ) {

if( $columns == '' ) {

$_columns = '*';

} else if( $columns == '*' ) {

$_columns = '*';

} else {

$_columns = $columns;

}

} else {

$_columns = '';
$comma = '';

for( $i = 0; $i < $size; $i++ ) {

$k = $columns[ $i ];

$_columns .= $comma . $k;
$comma = ', ';

}

}

$this->_options[ 'columns' ] = $_columns;

return $this;

}

public function where( $statement, $value = '', $type = 'AND' ) {

if( $value instanceof MySQL_Drivers_MySQLi_Select ) {

$statement = str_replace( '?', $value->__toString(), $statement );

} else if( is_numeric( $value ) !== false ) {

$statement = str_replace( '?', $value, $statement );

} else if( is_string( $value ) !== false ) {

$statement = str_replace( '?', '\'' . $value . '\'', $statement );

} else {

$statement = str_replace( '?', $value, $statement );

}

$this->_options[ 'where' ][] = Array(

'statement' => '( ' . $statement . ' )',
'type' => $type

);

return $this;

}

public function _and( $statement, $value ) {

$this->where( $statement, $value, 'AND' );

return $this;

}

public function _or( $statement, $value ) {

$this->where( $statement, $value, 'OR' );

return $this;

}

public function limit( $from, $to = '' ) {

if( $to !== '' ) {

$this->_options[ 'limit' ] = $from . ',' . $to;

} else {

$this->_options[ 'limit' ] = $from;

}

return $this;

}

public function order( $column, $order ) {

$this->_options[ 'order' ][] = Array(

'column' => $column,
'order' => $order

);

return $this;

}

public function from( $table ) {

$this->_options[ 'table' ] = $table;

return $this;

}

public function join( $table, $on = '', $type = 'INNER' ) {

$type = strtoupper( $type );

if( isset( $this->_options[ 'join' ] ) !== false ) {

$this->_options[ 'join' ] .= $type . ' JOIN ' . $table . ( ( $on != '' ) ? ' ON ' . $on : '' );

} else {

$this->_options[ 'join' ] = $type . ' JOIN ' . $table . ( ( $on != '' ) ? ' ON ' . $on : '' );

}

return $this;

}

public function __toString() {

if( isset( $this->_options[ 'columns' ] ) !== true || $this->_options[ 'columns' ] == '' || isset( $this->_options[ 'table' ] ) !== true || $this->_options[ 'table' ] == '' ) {

throw new GNR_Exception( 'Invalid select statement.' );

} else {

/**
* Basic query
*/
$query = 'SELECT ' . $this->_options[ 'columns' ] . ' FROM ' . $this->_options[ 'table' ];

if( isset( $this->_options[ 'join' ] ) !== false ) {

$query .= ' ' . $this->_options[ 'join' ];

}

/**
* Where
*/
$where = 0;

if( isset( $this->_options[ 'where' ] ) !== false && count( $this->_options[ 'where' ] ) > 0 ) {

foreach( $this->_options[ 'where' ] as $index => $array ) {

$statement = $array[ 'statement' ];
$type = $array[ 'type' ];

if( $statement != '' ) {

if( $where === 0 ) {

$query .= ' WHERE ';

}

if( $where > 0 ) {

$query .= ' ' . $type . ' ';

}

$query .= $statement;

$where++;

}

}

}

/**
* Order by
*/

$order = 0;

if( isset( $this->_options[ 'order' ] ) !== false && count( $this->_options[ 'order' ] ) > 0 ) {

foreach( $this->_options[ 'order' ] as $index => $value ) {

$column = $value[ 'column' ];
$orderBy = $value[ 'order' ];

if( $column != '' ) {

if( $order === 0 ) {

$query .= ' ORDER BY ';

}

if( $order > 0 ) {

$order .= ', ';

}

if( $orderBy !== 'ASC' && $orderBy !== 'DESC' ) {

$oderBy = 'ASC';

}

$query .= '`' . $column . '` ' . $orderBy . '';

$order++;

}

}

}

/**
* Limit
*/
if( isset( $this->_options[ 'limit' ] ) !== false ) {

$query .= ' LIMIT ' . $this->_options[ 'limit' ];

}

return $query;

}

}

}

Dentafrice
08-07-2009, 04:00 AM
Great minds think alike! :P haha. kidding of course. yours is way more complex and nicer.

Chippiewill
08-07-2009, 05:40 AM
With what you two old geeks are up to I am going to say...

Awesome

Can I has?

Edit: Dentafrice, is that your car's 'Spec' in your sig?

Dentafrice
08-07-2009, 01:12 PM
haha it's just me working on it :P

Yes, that's my truck's specs lol.

Iszak
08-07-2009, 02:19 PM
I've looked into method chaining before, it's alright but personally I prefer ORM because it's much nicer and easier to manage such as ActiveRecord or DataMapper - should check them out.

Also Simon there's a typo on line 252 you've typed "$oderBy" instead of "$orderBy".

Agnostic Bear
08-07-2009, 02:22 PM
I've looked into method chaining before, it's alright but personally I prefer ORM because it's much nicer and easier to manage such as ActiveRecord or DataMapper - should check them out.

Also Simon there's a typo on line 252 you've typed "$oderBy" instead of "$orderBy".

Did you see the part that said old?

Dentafrice
08-07-2009, 02:25 PM
I've looked into method chaining before, it's alright but personally I prefer ORM because it's much nicer and easier to manage such as ActiveRecord or DataMapper - should check them out.

Also Simon there's a typo on line 252 you've typed "$oderBy" instead of "$orderBy".
I've just AR before. Really nice, but like I said.. I'm just playing around.

BoyBetterKnow
08-07-2009, 02:36 PM
I havn't looked at Housekeeping in years. What new features you planning?

Dentafrice
08-07-2009, 02:38 PM
I havn't looked at Housekeeping in years. What new features you planning?
Not sure yet. Just really something to stabilize my coding with, maybe learn a few more things.

Mostly it's going to be better code, better security, better coded features, and easier to modify then the other versions.

BoyBetterKnow
08-07-2009, 02:48 PM
Not sure yet. Just really something to stabilize my coding with, maybe learn a few more things.

Mostly it's going to be better code, better security, better coded features, and easier to modify then the other versions.

So open source and free? :eusa_danc

Iszak
08-07-2009, 02:51 PM
Did you see the part that said old?
If you're not going to contribute to the thread don't bother posting.


Mostly it's going to be better code, better security, better coded features, and easier to modify then the other versions.
I'm not sure there's much more security you can add? Although I didn't look at HSV2 from what I've heard it's secure. I think you should focus on customization (in templates etc.) as well as plugins? for extra features and even an updater like WordPress has.

Dentafrice
08-07-2009, 03:03 PM
So open source and free? :eusa_danc

Of course.


If you're not going to contribute to the thread don't bother posting.


I'm not sure there's much more security you can add? Although I didn't look at HSV2 from what I've heard it's secure. I think you should focus on customization (in templates etc.) as well as plugins? for extra features and even an updater like WordPress has.

Well it really wasn't that secure in some terms. This time I have added SHA1 w/MD5 for just a little more security.

Customization will probably be a feature, not really sure yet.. depends on how much time I get to be honest.

Want to hide these adverts? Register an account for free!