Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit c909ac701b92c5c6bcd5be17d5305f828aee1264 0 parents
@benallfree authored
Showing with 207 additions and 0 deletions.
  1. +197 −0 lib/db.php
  2. +5 −0 load.php
  3. +5 −0 manifest.php
197 lib/db.php
@@ -0,0 +1,197 @@
+<?
+
+
+
+function db_select($handle, $dbs=null, $dbh = null)
+{
+ $this_module = Click::$engine->modules_by_slug['com.benallfree.click.db'];
+ if($dbs)
+ {
+ if(is_array($dbs))
+ {
+ if(!isset($this_module->connections[$handle]) && !$dbs) click_error("Tried to select $handle, but no database settings were defined.");
+ if($dbs)
+ {
+ if(!$dbh) $dbh = db_connect($dbs);
+ $this_module->connections[$handle]['handle'] = $dbh;
+ $this_module->connections[$handle]['credentials'] = $dbs;
+ }
+ } else {
+ $this_module->connections[$handle]['handle'] = $dbs;
+ $this_module->connections[$handle]['credentials'] = array();
+ }
+ }
+ $this_module->current = $this_module->connections[$handle];
+ return $this_module->current;
+}
+
+function db_push($handle, $dbs=null)
+{
+ $this_module = Click::$engine->modules_by_slug['com.benallfree.click.db'];
+ $this_module->connection_stack[] = $this_module->current;
+ $this_module->current = db_select($handle, $dbs);
+ return $this_module->current;
+}
+
+function db_pop()
+{
+ $this_module = Click::$engine->modules_by_slug['com.benallfree.click.db'];
+ if(count($this_module->connection_stack)>0)
+ {
+ $this_module->current = array_pop($this_module->connection_stack);
+ }
+}
+
+function db_connect($database_settings)
+{
+ $this_module = Click::$engine->modules_by_slug['com.benallfree.click.db'];
+ $dbh=mysql_connect ($database_settings['host'], $database_settings['username'],$database_settings['password']);
+ if (!$dbh)
+ {
+ click_error('Cannot connect to the database because: ' . mysql_error());
+ }
+ if (!mysql_select_db($database_settings['catalog'], $dbh))
+ {
+ click_error(mysql_error($dbh));
+ }
+ return $dbh;
+}
+
+function query($sql)
+{
+ $this_module = Click::$engine->modules_by_slug['com.benallfree.click.db'];
+ $args = func_get_args();
+ array_shift($args);
+ $s = '';
+ $in_quote = false;
+ $in_escape = false;
+ for($i=0;$i<strlen($sql);$i++)
+ {
+ if(count($args)==0)
+ {
+ $s .= substr($sql, $i);
+ break;
+ }
+ $c = substr($sql, $i, 1);
+ if($in_escape)
+ {
+ $s.=$c;
+ $in_escape = false;
+ continue;
+ }
+ if($c == "'" && !$in_quote)
+ {
+ $in_quote = true;
+ continue;
+ }
+ if($c == "'" && $in_quote)
+ {
+ $next = substr($sql, $i+1, 1);
+ if($next == "'") continue;
+ }
+ if($c == '\\')
+ {
+ $in_escape = true;
+ continue;
+ }
+ $in_quote = false;
+ switch($c)
+ {
+ case "'":
+ $in_quote = true;
+ break;
+ case '?':
+ $s .= "'".mysql_real_escape_string(array_shift($args))."'";
+ break;
+ case '!':
+ $s.= array_shift($args);
+ break;
+ case '@':
+ $s .= mysql_real_escape_string(date( 'Y-m-d H:i:s e', array_shift($args)));
+ break;
+ default:
+ $s .= $c;
+ }
+ }
+ $sql = $s;
+
+ $sql = trim($sql);
+ $this_module->queries[]=$sql;
+ if ( preg_match('/^delete|^update/mi',$sql)>0 && preg_match('/\s+where\s+/mi', $sql)==0)
+ {
+ click_error("DELETE or UPDATE error. No WHERE specified", $sql);
+ }
+ $start = microtime(true);
+ $res = mysql_query($sql, $this_module->current['handle']);
+ $end = microtime(true);
+ $this_module->queries[] = (int)(($end-$start)*1000);
+ if ($res===FALSE) {
+ click_error(mysql_error($this_module->current['handle']), $sql);
+ }
+ if (gettype($res)=='resource') $this_module->queries[] = mysql_num_rows($res); else $this_module->queries[] = 0;
+ return $res;
+}
+
+function query_assoc($sql)
+{
+ $args = func_get_args();
+
+ $res = call_user_func_array('query', $args);
+ $assoc=array();
+ while($rec = mysql_fetch_assoc($res))
+ {
+ $assoc[]=$rec;
+ }
+ return $assoc;
+}
+
+function query_obj($sql)
+{
+ $args = func_get_args();
+ $recs = call_user_func_array('query_assoc', $args);
+ $res = array();
+ foreach($recs as $r)
+ {
+ $res[] = (object)$r;
+ }
+ return $res;
+}
+
+function query_file($fpath)
+{
+ $d = Click::$build['database'];
+ $cmd = "mysql -u {$d['username']} --password={$d['password']} -h {$d['host']} -D {$d['catalog']} < \"$fpath\"";
+ click_exec($cmd);
+}
+
+function db_table_exists($name)
+{
+ $res = query_assoc("show tables");
+
+ foreach(array_values($res) as $rec)
+ {
+ $rec = array_values($rec);
+ if ($rec[0]==$name) return true;
+ }
+ return false;
+}
+
+function db_dump($fname='db.gz', $include_data = true)
+{
+ if(!startswith($fname, '/')) $fname = BUILD_FPATH ."/{$fname}";
+ ensure_writable_folder(dirname($fname));
+ $extra = '';
+ if(!$include_data) $extra .= ' --no-data ';
+ $d = Click::$build['database'];
+ $cmd = "mysqldump {$extra} --compact -u {$d['username']} --password={$d['password']} -h {$d['host']} {$d['catalog']} | gzip > {$fname}";
+ click_exec($cmd);
+}
+
+function update_junction($table_name, $left_key_name, $left_key_id, $right_key_name, $right_key_ids)
+{
+ query("delete from {$table_name} where {$left_key_name} = ?", $left_key_id);
+ foreach($right_key_ids as $id)
+ {
+ query("insert into {$table_name} ({$left_key_name}, {$right_key_name}) values (?, ?)", $left_key_id, $id);
+ }
+}
5 load.php
@@ -0,0 +1,5 @@
+<?
+
+$this->queries = array();
+$this->connections = array();
+$this->connections_stack = array();
5 manifest.php
@@ -0,0 +1,5 @@
+<?
+
+$manifest = array(
+ 'slug'=>'com.benallfree.click.db',
+);
Please sign in to comment.
Something went wrong with that request. Please try again.