00001 <?php
00014 require_once("Model.php");
00015 require_once("SQLDb.php");
00016
00017 function SQLDb_strtotime($string) {
00018 if (trim($string)) {
00019 return strtotime($string);
00020 }
00021 return -1;
00022 }
00023
00024 function SQLDb_now() {
00025 return date("Y-m-d")."T".date("H:i:sp");
00026 }
00027
00028 function SQLDb_password($string) {
00029 if (trim($string)) {
00030 return md5($string);
00031 }
00032 return -1;
00033 }
00034
00035 function SQLDb_init($dbhandle) {
00036 sqlite_create_function($dbhandle,"STRTOTIME","SQLDb_strtotime",1);
00037 sqlite_create_function($dbhandle,"NOW","SQLDb_now",0);
00038 sqlite_create_function($dbhandle,"PASSWORD","SQLDb_password",1);
00039 }
00040
00044 class SQLDb extends Model {
00048 public function SQLDb($conf = NULL) {
00049 Model::Model($conf);
00050 $this->dbh = NULL;
00051 $this->qry = NULL;
00052 $this->rowCount = false;
00053 $dbtype = $this->get("db_type");
00054 $dbname = $this->get("db_name");
00055 if ($dbname !== false && $dbtype !== false && $dbtype == "sqlite") {
00056 $this->open();
00057 }
00058 }
00059
00064 public function lastInsertRowId() {
00065 $db_type = $this->get("db_type");
00066 switch($db_type) {
00067 case 'mysql':
00068 return mysql_insert_id();
00069 default:
00070 return sqlite_last_insert_rowid($this->dbh);
00071 }
00072 }
00073
00078 public function numRows() {
00079 return $this->rowCount;
00080 }
00081
00087 public function escapeString($s) {
00088 $db_type = $this->get("db_type");
00089 switch($db_type) {
00090 case 'mysql':
00091 if ($this->dbh) {
00092 return mysql_real_escape_string(str_replace("--","—",$s),$this->dbh);
00093 } else {
00094 return mysql_escape_string(str_replace("--","—",$s));
00095 }
00096 default:
00097 return sqlite_escape_string(str_replace("--","—",$s));
00098 }
00099 }
00100
00106 public function open($name = "") {
00107 $db_type = $this->get("db_type");
00108 switch($db_type) {
00109 case 'mysql':
00110 $this->get("db_host") !== false ? $mysql_host = $this->get("db_host") : $mysql_host = "localhost";
00111 $this->get("db_user") !== false ? $mysql_user = $this->get("db_user") : $mysql_user = "root";
00112 $this->get("db_password") !== false ? $mysql_password = $this->get("db_password") : $mysql_password = "";
00113 $this->get("db_name") !== false ? $mysql_database = $this->get("db_name") : $mysql_database = "mysql";
00114
00115 $this->dbh = mysql_connect($mysql_host, $mysql_user, $mysql_password);
00116 if (! $this->dbh) {
00117 $this->error('Could not connect: ' . mysql_error());
00118 } else {
00119 $this->db_selected = mysql_select_db($mysql_database, $this->dbh);
00120 if (!$this->db_selected) {
00121 $this->error("Could not select Db: $mysql_database " . mysql_error());
00122 }
00123 }
00124 break;
00125 default:
00126 if ($this->dbh) {
00127 sqlite_close($this->dbh);
00128 }
00129 if ($name != "") {
00130 $this->set("db_name",$name);
00131 }
00132
00133 $this->get("db_perms") !== false ? $db_perms = $this->get("db_perms") : $db_perms = "0666";
00134 $db_name = $this->get("db_name");
00135 $this->dbh = sqlite_open($db_name,$db_perms,$error);
00136 if ($this->dbh) {
00137 SQLDb_init($this->dbh);
00138 } else {
00139 $this->error("error opening database ".$db_name.": ".$error);
00140 }
00141 break;
00142 }
00143
00144 return ($this->errorCount() == 0);
00145 }
00146
00152 public function execute($sql) {
00153 $lines = explode("\n",$sql);
00154 $sql_statement = "";
00155
00156
00157 foreach ($lines as $line) {
00158 $text = "";
00159 if (strpos($line,"--") === false) {
00160 $text = trim($line);
00161 } else if (strpos(trim($line),"--") > 1) {
00162 list($s,$junk) = explode('--',$line,2);
00163 trim($s) != "" ? $text = trim($s): "";
00164 }
00165 if ($text != "") {
00166 $sql_statement .= $text;
00167 }
00168 }
00169 $sql_statement = trim($sql_statement);
00170 if ($sql_statement != "") {
00171 $db_type = $this->get("db_type");
00172 switch($db_type) {
00173 case 'mysql':
00174 if (stripos($sql_statement,"SELECT") !== false) {
00175 $this->qry = mysql_query($sql_statement);
00176 $this->qry ? $this->rowCount = mysql_num_rows($this->qry) : $this->rowCount = 0;
00177 } else {
00178 $this->qry = mysql_query($sql_statement);
00179
00180 $this->rowCount = mysql_affected_rows();
00181 }
00182 if (! $this->qry) {
00183 $error = mysql_error();
00184 $this->error("\nDB error: ".$error."\nDatabase: ".$this->get("db_name")."\nSQL statement:\n".$sql_statement);
00185 }
00186 break;
00187 default:
00188 if (stripos($sql_statement,"SELECT") !== false) {
00189 $this->qry = sqlite_query($this->dbh, $sql_statement, SQLITE_ASSOC,$error);
00190 $this->rowCount = sqlite_num_rows($this->qry);
00191 } else {
00192 sqlite_exec($this->dbh, $sql_statement, $error);
00193 $this->rowCount = sqlite_changes($this->dbh);
00194 }
00195 $error || $this->qry === false ? $this->error("\nDB error: ".$error."\nDatabase: ".$this->get("db_name")."\nSQL statement:\n".$sql_statement) : "";
00196 break;
00197 }
00198 }
00199
00200 return ($this->errorCount() == 0);
00201 }
00202
00207 public function getRow() {
00208 $db_type = $this->get("db_type");
00209 switch($db_type) {
00210 case 'mysql':
00211 if ($this->qry) {
00212 $result = mysql_fetch_array($this->qry, MYSQL_ASSOC);
00213 } else {
00214 $this->error("no query available");
00215 }
00216 break;
00217 default:
00218 if ($this->qry) {
00219 $result = sqlite_fetch_array($this->qry, SQLITE_ASSOC);
00220 } else {
00221 $this->error("no query available");
00222 }
00223 break;
00224 }
00225 if ($result !== false) {
00226 foreach ($result as $key => $value) {
00227 $result[$key] = stripslashes($value);
00228 }
00229 }
00230 if ($this->errorCount() == 0) {
00231 return $result;
00232 }
00233 return false;
00234 }
00235
00236 public function close() {
00237 $db_type = $this->get("db_type");
00238 switch($db_type) {
00239 case 'mysql':
00240 if ($this->dbh) {
00241
00242
00243 } else {
00244 $this->error("database ".$this->get("db_name")." not open");
00245 }
00246 break;
00247 default:
00248 if ($this->dbh) {
00249 sqlite_close($this->dbh);
00250 } else {
00251 $this->error("database ".$this->get("db_name")." not open");
00252 }
00253 break;
00254 }
00255 return ($this->errorCount() == 0);
00256 }
00257 }
00258
00259 ?>