Code Search for Developers
 
 
  

db.php from ECP (EliteCore Project) at Krugle


Show db.php syntax highlighted

<?php
class TA_Query {
	private $id;
	private $name;
	private $type;
	private $table;
	private $WHERE;
	private $LIMIT;
	private $ORDER;
	private $dynamic_fields = array (
		'time',
		'date'
	);
	private $params = array ();
	function __construct($id, $type, $table, $name) {
		$this->id = $id;
		$this->name = $name;
		$this->type = $type;
		$this->table = $table;
	}
	function addParam($name, $value, $type) {
		$this->params[$name . ";" . $type] = $value;
	}
	function increment($name, $size) {
		if ($this->type != TA :: UPDATE)
			return false;
		$this->params[$name . ";increment"] = $size;
		return true;
	}
	function WHERE($condition) {
		$this->WHERE = "WHERE " . $condition;
	}
	function LIMIT($string) {
		$this->LIMIT = "LIMIT " . $string;
	}
	function ORDER($name, $ordering) {
		$this->ORDER = "ORDER BY `" . $name . "` ";
		if ($ordering == TA :: ASCENDING) {
			$this->ORDER .= "ASC";
		} else
			if ($ordering == TA :: DESCENDING) {
				$this->ORDER .= "DESC";
			} else {
				$this->ORDER .= $ordering;
			}
	}
	function getName() {
		return $this->name;
	}
	function INSERT2SELECT() {
		if ($this->type == TA :: INSERT) {
			$query = "";
			$query .= "SELECT *";
			$query .= " FROM `" . $this->table . "` WHERE ";
			$i = 0;
			foreach ($this->params as $name => $value) {
				$data = explode(";", $name);
				$isDynamic = in_array((string) $data[0], $this->dynamic_fields);
				if (!$isDynamic) {
					$query .= "`" . $data[0] . "`=";
					if ($data[1] == TA :: STRING)
						$query .= "'" . $value . "' ";
					else {
						if (empty ($value))
							$query .= "'" . $value . "' ";
						else
							$query .= "'" . $value . "' ";
					}
				}
				if (!$isDynamic && $i != sizeof($this->params) - 1)
					$query .= "AND ";
				$i++;
			}
			$query .= "LIMIT 1 ";
			$query .= ";";

			return $query;
		} else
			return false;
	}
	function getQuery() {
		$query = "";
		if ($this->type == TA :: SELECT) {
			$query .= "SELECT ";
			if (!sizeof($this->params)) {
				$query .= "*";
			} else {
				$i = 0;
				foreach ($this->params as $name => $value) {
					$data = explode(";", $name);
					$query .= "`" . $data[0] . "` ";
					if ($i != sizeof($this->params) - 1)
						$query .= ", ";
					$i++;
				}
			}
			$query .= " FROM `" . $this->table . "` ";
		} else
			if ($this->type == TA :: UPDATE) {
				$query .= "UPDATE ";
				$query .= "`" . $this->table . "` ";
				if (!sizeof($this->params)) {
					exit ("NEED SOME PARAMETERS FOR UPDATE QUERY");
				} else {
					$i = 0;
					$query .= "SET ";
					foreach ($this->params as $name => $value) {
						$data = explode(";", $name);
						$query .= "`" . $data[0] . "`=";
						if ($data[1] == "increment") {
							$query .= "`" . $data[0] . "`+'" . $value . "' ";
						} else
							if ($data[1] == TA :: STRING)
								$query .= "'" . $value . "' ";
							else {
								if (empty ($value))
									$query .= "'" . $value . "' ";
								else
									//$query .= $value." ";
									$query .= "'" . $value . "' ";
							}
						if ($i != sizeof($this->params) - 1)
							$query .= ",";
						$i++;
					}
				}
			} else
				if ($this->type == TA :: INSERT) {
					$query .= "INSERT INTO ";
					$query .= "`" . $this->table . "` ";
					if (!sizeof($this->params)) {
						exit ("NEED NAMES AND VALUES FOR INSERT QUERY");
					} else {
						$i = 0;
						$query .= "(";
						foreach ($this->params as $name => $value) {
							$data = explode(";", $name);
							$query .= "`" . $data[0] . "`";
							if ($i != sizeof($this->params) - 1)
								$query .= ", ";
							$i++;
						}
						$query .= ") VALUES (";
						$i = 0;
						foreach ($this->params as $name => $value) {
							$data = explode(";", $name);
							if ($data[1] == TA :: STRING)
								$query .= "'" . $value . "'";
							else {
								if (empty ($value))
									$query .= "'" . $value . "' ";
								else
									$query .= "'" . $value . "' ";
								//$query .= $value." ";
							}
							if ($i != sizeof($this->params) - 1)
								$query .= ", ";
							$i++;
						}
						$query .= ")";
					}
				} else
					if ($this->type == TA :: DELETE) {
						$query .= "DELETE FROM ";
						$query .= "`" . $this->table . "` ";
					}
		if (!empty ($this->WHERE)) {
			$query .= $this->WHERE . " ";
		}
		if (!empty ($this->ORDER))
			$query .= $this->ORDER . " ";
		if (!empty ($this->LIMIT))
			$query .= $this->LIMIT . " ";
		$query .= ";";

		return $query;
	}
}

class TA_Result {
	private $result;
	function __construct($result) {
		$this->result = $result;
	}
	function getResult() {
		return $this->result;
	}
	function FetchObject() {
		$result = DB :: FetchObject($this->result);
		if ($result == NULL)
			$result = false;
		return $result;
	}
	function NumRows() {
		return DB :: NumRows($this->result);
	}
}

class TA {
	const SELECT = 1;
	const INSERT = 2;
	const UPDATE = 3;
	const DELETE = 4;

	const STRING = 10;
	const NUMBER = 11;
	const OTHER = 12;

	const ASCENDING = 20;
	const DESCENDING = 21;

	const NO_RESTRICT = 30;
	const ONCE = 31;

	const NO_LIMIT = "NO_LIMIT";

	private $currentInstance = false;

	private $queries = array ();
	private $results = array ();
	private $qid = 0;
	private $last_qid = 0;

	function __construct($instance = false) {
		$this->currentInstance = $instance;
	}

	function addQuery($type, $table, $ID = false) {
		if ($this->currentInstance !== false && $this->currentInstance !== NULL && $this->currentInstance->getName() != $this->currentInstance->getInstance()) {
			$table = str_replace($this->currentInstance->getName(), $this->currentInstance->getInstance(), $table);
		}
		if (DB :: $prefix != "xcs") {
			$table = str_replace("xcs", DB :: $prefix, $table);
		} else {
			// ECP over XCS support
			$table = str_replace("ecp", DB :: $prefix, $table);
		}
		$this->queries[$this->qid] = new TA_Query($this->qid, $type, $table, $ID);
		$this->last_qid = $this->qid;
		$this->qid++;
		DB :: $queries++;
	}

	function removeQuery($ID = false) {
		if (!sizeof($this->queries))
			return false;
		if ($ID === false) {
			unset ($this->queries[$this->last_qid]);
			if (!sizeof($this->results)) {
				DB :: CloseResult($this->results[$this->last_qid]->getResult());
				unset ($this->results[$this->last_qid]);
			}
			$this->qid--;
			$this->last_qid = $this->qid;
			return true;
		} else {
			foreach ($this->queries as $qid => $query) {
				if ($query->getName() == $ID) {
					unset ($this->queries[$qid]);
					if (!sizeof($this->results)) {
						DB :: CloseResult($this->results[$qid]->getResult());
						unset ($this->results[$qid]);
					}
					$this->qid--;
					$this->last_qid = $this->qid;
					return true;
				}
			}
		}
		return false;
	}
	function WHERE($condition) {
		$this->queries[$this->last_qid]->WHERE($condition);
	}
	function LIMIT($string) {
		if ((int) $string > 0 && $string != TA :: NO_LIMIT)
			$this->queries[$this->last_qid]->LIMIT($string);
	}
	function ORDER($name, $ordering = TA :: ASCENDING) {
		$this->queries[$this->last_qid]->ORDER($name, $ordering);
	}
	function addParam($name, $type = TA :: OTHER, $in_value = null) {
		if (!isset ($in_value))
			return false;

		if ($type == TA :: STRING) {
			$out_value = addslashes((string) $in_value);
			//$out_value = str_replace("\n","<br />",$out_value);
		} else
			if ($type == TA :: NUMBER) {
				if (empty ($in_value))
					$in_value = 0;
				else
					if ($in_value == NULL)
						$in_value = 0;
				$out_value = (int) $in_value;
			} else
				if ($type == TA :: OTHER) {
					$out_value = $in_value;
				}
		$this->queries[$this->last_qid]->addParam($name, $out_value, $type);
		return true;
	}
	function increment($name, $size = 1) {
		return $this->queries[$this->last_qid]->increment($name, $size);
	}
	function Result($ID = false) {
		if ($ID == false) {
			return $this->results[$this->last_qid];
		} else {
			foreach ($this->queries as $qid => $query) {
				if ($query->getName() == $ID) {
					return $this->results[$qid];
				}
			}
		}
	}
	function Execute($execute_only = false) {
		foreach ($this->queries as $qid => $TA_Query) {
			if ($execute_only === false || (is_array($execute_only) && in_array($this->queries[$qid]->getName(), $execute_only) || is_string($execute_only) && $execute_only == $this->queries[$qid]->getName())) {
				//$skip_duplicate= false;
				//if (($query= $this->queries[$qid]->INSERT2SELECT()) !== false) {
				//	$this->results[$qid]= new TA_Result(DB :: Query($query));
				//	if ($this->results[$qid]->NumRows()) {
				//		$skip_duplicate= true;
				//	}
				//}
				//if (!$skip_duplicate) {
				$query = $this->queries[$qid]->getQuery();
				//$query = utf8_encode($query);
				$this->results[$qid] = new TA_Result(DB :: Query($query));
				//}
				$this->last_qid = $qid;
			}
		}
		return true;
	}
	function End() {
		/*if (!sizeof($this->results)) {
			foreach ($this->results as $result) {
				DB :: CloseResult($result->getResult());
			}
		}*/
		unset ($this);
	}
}

interface DB_interface {
	const ASC = '~-asc-ending-~';
	const DESC= '~-desc-ending-~';
	
	public static function Connect($host, $username, $passwd, $dbname, $prefix = 'xcs', $port = '', $socket = '');
	public static function ServerVersion();
	public static function ServerInfo();
	public static function SelectDB($dbname);
	public static function Query($query);
	public static function SetPrefix($prefix);
	public static function RestorePrefix();
	public static function NumRows($result = null);
	public static function NumFields($result = null);
	public static function FetchRow($result = null);
	public static function FetchObject($result = null);
	public static function FetchArray($result = null);
	public static function Error();
	public static function Close();
	public static function EscapeString($string);
	public static function CloseResult($result = null);
}

abstract class DB_abstract implements DB_interface {
	public static $prefix = null;
	public static $old_prefix = null;
	public static $isConnected = false;
	public static $queries = 0;

	public static function Connect($host, $username, $passwd, $dbname, $prefix = 'xcs', $port = '', $socket = '') {
		self :: $isConnected = true;
	}
	public static function ServerVersion() {
	}
	public static function ServerInfo() {
	}
	public static function SelectDB($dbname) {
	}
	public static function Query($query) {
	}
	public static function MultiQuery($queries) {
		$inStr = false;
		$query = "";
		@ set_time_limit(5000);
		for ($pos = 0; $pos != strlen($queries); $pos++) {
			$char = substr($queries, $pos, 1);
			$query .= $char;

			if ($char == "'" && !$inStr)
				$inStr = true;
			else
				if ($char == "'" && $inStr)
					$inStr = false;

			if ($char == ";" && !$inStr) {
				DB :: Query($query);
				$query = "";
			}
		}
	}
	public static function SetPrefix($prefix) {
		if (self :: $prefix == $prefix)
			return false;
		self :: $old_prefix = self :: $prefix;
		//self :: $prefix= $prefix;
		return true;
	}
	public static function RestorePrefix() {
		if (!self :: $old_prefix)
			return false;
		self :: $prefix = self :: $old_prefix;
		return true;
	}
	public static function NumRows($result = null) {
	}
	public static function NumFields($result = null) {
	}
	public static function FetchRow($result = null) {
	}
	public static function FetchObject($result = null) {
	}
	public static function FetchArray($result = null) {
	}
	public static function Error() {
	}
	public static function Close() {
	}
	public static function EscapeString($string) {
		return String :: Escape($string);
	}
	public static function CloseResult($result = null) {
	}
	public static function Select($table, $object) {
		$currentModule = Engine :: getFlag('currentModule');

		$TA = new TA($currentModule);
		$TA->addQuery(TA :: SELECT, $table);
		$condition = '';
		$index = 0;
		foreach ($object as $name => $value) {
			$index++;
		}
		$sizeof = $index;
		$index = 0;
		foreach ($object as $name => $value) {
			$nextObject = next($object);
			prev($object);
			if ($value == DB :: ASC) {
				$TA->ORDER($name, TA :: ASCENDING);
			} else
				if ($value == DB :: DESC) {
					$TA->ORDER($name, TA :: DESCENDING);
				} else {
					if ($index != 0 && $index < ($sizeof -1) && ($nextObject != DB :: ASC || $nextObject != DB :: DESC)) {
						$condition .= ' AND ';
					}
					$condition .= "`" . $name . "`='" . self :: EscapeString($value) . "'";
				}
			$index++;
		}
		if (!empty ($condition))
			$TA->WHERE($condition);
		$TA->LIMIT(1);
		$TA->Execute();
		$Array = array ();
		while ($object = $TA->Result()->FetchObject()) {
			$Array[] = $object;
		}
		if (sizeof($Array)) {
			if (sizeof($Array) == 1) {
				$result = $Array[0];
			} else {
				$result = $Array;
			}
		} else {
			$result = false;
		}
		$TA->End();
		return $result;
	}
	public static function DeleteById($table, $id, $module = false) {
		$currentModule = Engine :: getFlag('currentModule');

		if (!$id)
			return false;

		if ($module === false && $currentModule !== false)
			$module = $currentModule;

		$TA = new TA($module);
		$TA->addQuery(TA :: DELETE, $table);
		$TA->WHERE("`id`='" . $id . "'");
		$TA->LIMIT(1);
		$TA->Execute();
		$TA->End();
		return true;
	}
	public static function IncrementById($table, $id, $column, $module = false) {
		$currentModule = Engine :: getFlag('currentModule');

		if (!$id)
			return false;

		if ($module === false && $currentModule !== false)
			$module = $currentModule;

		$TA = new TA($module);
		$TA->addQuery(TA :: UPDATE, $table);
		$TA->increment($column);
		$TA->WHERE("`id`='" . $id . "'");
		$TA->LIMIT(1);
		$TA->Execute();
		$TA->End();
		return true;
	}
	public static function UpdateById($table, $id, $params, $module = false) {
		$currentModule = Engine :: getFlag('currentModule');

		if (!$id)
			return false;

		if ($module === false && $currentModule !== false)
			$module = $currentModule;

		$TA = new TA($module);
		$TA->addQuery(TA :: UPDATE, $table);
		foreach ($params as $name => $value) {
			$TA->addParam($name, TA :: STRING, $value);
		}
		$TA->WHERE("`id`='" . $id . "'");
		$TA->Execute();
		$TA->End();
		return true;
	}
	public static function UpdateBy($table, $id, $params, $module = false) {
		$currentModule = Engine :: getFlag('currentModule');

		if (!$id)
			return false;

		if ($module === false && $currentModule !== false)
			$module = $currentModule;

		$TA = new TA($module);
		$TA->addQuery(TA :: UPDATE, $table);
		foreach ($params as $name => $value) {
			$TA->addParam($name, TA :: STRING, $value);
		}
		$key = key($id);
		$value = current($id);
		$TA->WHERE("`" . $key . "`='" . $value . "'");
		$TA->Execute();
		$TA->End();
		return true;
	}
	public static function Insert($table, $params, $module = false) {
		$currentModule = Engine :: getFlag('currentModule');

		if ($module === false && $currentModule !== false)
			$module = $currentModule;

		$TA = new TA($module);
		$TA->addQuery(TA :: INSERT, $table);
		foreach ($params as $name => $value) {
			$TA->addParam($name, TA :: STRING, $value);
		}
		$TA->Execute();
		$TA->End();
		return true;
	}
}
?>



See more files for this project here

ECP (EliteCore Project)

EliteCore Project is a PHP5.1/Javascript/AJAX/XHTML/CSS framework for creating WEB 2.0 applications and services.The basic open-source instalation can be also used as an interactive personal page or BLOG.This project uses the latest features available.

Project homepage: http://sourceforge.net/projects/elitecore
Programming language(s): JavaScript,PHP,XML
License: cpl

  debug/
    content.php
  exceptions/
    ajaxflush.php
    nomodule.php
    undefineddata.php
  interface/
    encryption.php
    form.php
    module_class.php
    session_interface.php
  renderers/
    default.php
  sql/
    mysql.php
    mysqli.php
  themes/
    ECP/
      accept.png
      add.png
      alt_star.gif
      anchor.png
      arrow_refresh.png
      asterisk_orange.png
      asterisk_yellow.png
      attach.png
      back.png
      cog_error.png
      cog_go.png
      comment.png
      comment_add.png
      comment_delete.png
      comment_edit.png
      comments.png
      comments_add.png
      comments_delete.png
      control_play_blue.png
      drive.png
      gnome-fs-directory.png
      gnome-mime-audio.png
      layers.png
      layout.png
      layout_add.png
      layout_content.png
      layout_delete.png
      layout_edit.png
      layout_error.png
      layout_header.png
      layout_link.png
      layout_sidebar.png
      lightbulb.png
      lightbulb_add.png
      lightbulb_delete.png
      lightbulb_off.png
      lightning.png
      lightning_add.png
      lightning_delete.png
      lightning_go.png
      link.png
      link_add.png
      link_break.png
      link_delete.png
      link_edit.png
      link_error.png
      link_go.png
      lock.png
      lock_add.png
      lock_break.png
      lock_delete.png
      lock_edit.png
      lock_go.png
      lock_open.png
      newspaper.png
      newspaper_add.png
      newspaper_delete.png
      newspaper_go.png
      newspaper_link.png
      note.gif
      note.png
      note_add.png
      note_delete.gif
      note_delete.png
      note_edit.png
      note_error.png
      note_go.png
      note_new.gif
      overlays.png
      package.png
      package_add.png
      package_delete.png
      package_go.png
      package_green.png
      package_link.png
      page.gif
      page.png
      page_add.png
      page_attach.png
      page_code.png
      page_copy.png
      page_delete.png
      page_edit.png
      page_error.png
      page_excel.png
      page_find.png
      page_gear.png
      page_go.png
      page_green.png
      page_key.png
      page_lightning.png
      page_link.png
      page_paintbrush.png
      page_paste.png
      page_red.png
      page_refresh.png
      page_save.png
      page_white.png
      pencil.png
      pencil_add.png
      pencil_delete.png
      pencil_go.png
      photo.png
      photo_add.png
      photo_delete.png
      photo_link.png
      photos.png
      picture.png
      picture_add.png
      picture_delete.png
      picture_edit.png
      picture_empty.png
      picture_error.png
      picture_go.png
      picture_key.png
      picture_link.png
      picture_save.png
      pictures.png
      plugin.png
      plugin_add.png
      plugin_delete.png
      plugin_disabled.png
      plugin_edit.png
      plugin_error.png
      plugin_go.png
      plugin_link.png
      report.png
      report_add.png
      report_delete.png
      report_disk.png
      report_edit.png
      report_go.png
      report_key.png
      report_link.png
      report_magnify.png
      report_picture.png
      report_user.png
      report_word.png
      script.png
      script_add.png
      script_code.png
      script_code_red.png
      script_delete.png
      script_edit.png
      script_error.png
      script_gear.png
      script_go.png
      script_key.png
      script_lightning.png
      script_link.png
      script_palette.png
      script_save.png
      star.png
      star_rating.gif
      stop.png
      style.png
      text_align_center.png
      text_align_justify.png
      text_align_left.png
      text_align_right.png
      text_allcaps.png
      text_bold.png
      text_columns.png
      text_dropcaps.png
      text_heading_1.png
      text_heading_2.png
      text_heading_3.png
      text_heading_4.png
      text_heading_5.png
      text_heading_6.png
      text_horizontalrule.png
      text_indent.png
      text_indent_remove.png
      text_italic.png
      text_kerning.png
      text_letter_omega.png
      text_letterspacing.png
      text_linespacing.png
      text_list_bullets.png
      text_list_numbers.png
      text_lowercase.png
      text_padding_bottom.png
      text_padding_left.png
      text_padding_right.png
      text_padding_top.png
      text_replace.png
      text_signature.png
      text_smallcaps.png
      text_strikethrough.png
      text_subscript.png
      text_superscript.png
      text_underline.png
      text_uppercase.png
      textfield.png
      textfield_add.png
      textfield_delete.png
      textfield_key.png
      textfield_rename.png
      tux.png
      vert_star.gif
    ECP.xml
  Icon.php
  Location.php
  Module.php
  ModulesManager.php
  MusicTags.php
  Page.php
  XHTMLParser.php
  XMLForms.php
  ajax.php
  author.html
  cache.php
  config.php
  date.php
  db.php
  debug.php
  ecp-full.php
  ecp-mini.php
  engine.php
  events.php
  filesystem.php
  footer.html
  i18n.php
  mailer.php
  main.css
  mcrypt.php
  mime.php
  mod_rewrite.php
  perspective.php
  rc4.php
  reflection.php
  session_passport.php
  storage.php
  string.php
  template.php
  texy.php
  user.php
  user_cache.php
  wysiwyg_texy.php
  xhtml_form.php
  xtea.php