dbname = $lm_db_name; $this->link = mysql_connect($lm_db_host, $lm_db_user, $lm_db_pass); if(!$this->link)return; if(!mysql_select_db($lm_db_name, $this->link))return; $this->connect=true; } /* general function to execute sql-code */ function &Execute($sqlstring) { global $query_count; $sqlstring = str_replace("#__", $this->prefix, $sqlstring); $query_count++; $this->rs = mysql_query($sqlstring, $this->link); if ($this->rs === false) { return false; } return $this; } function rs2array() { global $querycount; $querycount++; //simple debugging $trowset = false; while ($row = mysql_fetch_assoc ($this->rs)) { foreach($row as $var=>$val) { $row[$var]=($this->dbcoding)?dbdecode($val):$val; } $trowset[] = $row; } $this->rowset = $trowset; } /* return recordset array */ function GetArray($num = 0) { $this->rs2array(); if ($num > 0) { return array_slice ($this->rowset, 0, $num); } else { return $this->rowset; } } /*return a single row */ function GetRow($sql) { $this->SelectLimit($sql, 1); return $this->rowset[0]; } /* return recordset array */ function SelectLimit($sql, $numrows = false, $lm_offset = false) { if ($numrows || $lm_offset)$sql .= " LIMIT"; if ($lm_offset)$sql .= " $lm_offset,"; if ($numrows)$sql .= " $numrows"; $this->Execute($sql); $this->rs2array(); return $this->rowset; } /* return autoincremented id */ function Insert_ID() { return mysql_insert_id($this->link); } /*return last error message */ function ErrorMsg() { return mysql_error($this->link); } /* return affected rows from update or delete */ function Affected_Rows() { return mysql_affected_rows($this->link); } /* return number of rows in recordset */ function RecordCount() { if ($this->rs && is_resource($this->rs)) { return mysql_num_rows($this->rs); } else { return 0; } } function RowCount() { } /* return number of fileds (columns) in recordset */ function FieldCount() { if ($this->rs && is_resource($this->rs)) { return mysql_num_fields($this->rs); } else { return 0; } } /* return array of table names in database */ function MetaTables() { $result = mysql_list_tables($this->dbname, $this->link); $i = 0; $tb_names = false; while ($i < mysql_num_rows ($result)) { $tb_names[] = mysql_tablename ($result, $i); $i++; } return $tb_names; } /* return array of ADOFieldObjects, one object per table column */ function MetaColumns($table, $upper = true) { $fields = mysql_list_fields($this->dbname, $table, $this->link); $rs = $this->Execute("SHOW FIELDS FROM $table"); $sqlfields = $rs->GetArray(); $cols = false; $i = 0; while ($i < mysql_num_fields ($fields)) { $meta = mysql_fetch_field ($fields, $i); $fld = new ADOFieldObject(); $fld->name = $meta->name; $col_info = str_replace(array("(", ")"), array("|", ""), $sqlfields[$i]['Type']); $info_arr = explode("|", $col_info, 2); if (strstr($info_arr[0], "text")) { $fld->type = $info_arr[0]; $fld->max_length = ""; } else { $fld->type = $info_arr[0]; $fld->max_length = $info_arr[1]; } if ($meta->not_null) $fld->not_null = true; // $ado->has_default if ($meta->not_null) { $fld->has_default = true; $fld->default_value = $sqlfields[$i]['Default']; } $cols[$meta->name] = $fld; $i++; } return $cols; } /* return array of column names i table */ function MetaColumnNames($table) { $fields = mysql_list_fields($this->db_name, $table, $this->link); $cols = false; for ($i = 0; $i < mysql_num_fields($fields); $i++) { $cols[] = mysql_field_name($fields, $i) ; } return $cols; } } /* the main class */ class dbsqlite { var $link = false; var $rs = false; var $prefix = ''; var $rowset = false; var $lm_dbname = ''; var $dbcoding=true; var $connect = false; /* ADONewConnection class main */ function db() { } /* PConnect make a new connection to db dummy; set the database name here */ function PConnect($lm_db_host , $lm_db_user , $lm_db_pass , $lm_db_name = "sqlite") { global $lm_absolute_path; $this->dbname = $lm_absolute_path . 'data/' . $lm_db_name . '.db'; $this->link = sqlite_open($this->dbname); if(!$this->link)return; $this->connect=true; } /* general function to execute sql-code */ function &Execute($sqlstring) { global $query_count; $sqlstring = str_replace("#__", $this->prefix, $sqlstring); $sqlstring = preg_replace (array("/\s+/"), array(" "), stripslashes((trim($sqlstring)))); $tmp = explode (" ", $sqlstring, 5); $query_count++; switch ($tmp[0]) { case "CREATE": $sqlstring = str_replace("id int(10) auto_increment", "id INTEGER", $sqlstring); $this->rs = sqlite_query($sqlstring, $this->link); break; case "INSERT": default: $this->rs = sqlite_query($sqlstring, $this->link); } if ($this->rs === false) { return false; } return $this; } function rs2array() { $trowset = false; while ($row = sqlite_fetch_array($this->rs)) { $trow = false; foreach($row as $var => $val) { if (!is_numeric($var))$trow[$val] = $this->dbcoding?dbdecode($val):$val; // auto decode } $trowset[] = $trow; } $this->rowset = $trowset; } /* return recordset array */ function GetArray($num = 0) { $this->rs2array(); if ($num > 0) { return array_slice ($this->rowset, 0, $num); } else { return $this->rowset; } } /*return a single row */ function GetRow($sql) { $this->SelectLimit($sql, 1); return $this->rowset[0]; } /* return recordset array */ function SelectLimit($sql, $numrows = false, $lm_offset = false) { if ($numrows || $lm_offset)$sql .= " LIMIT"; if ($lm_offset)$sql .= " $lm_offset,"; if ($numrows)$sql .= " $numrows"; $this->Execute($sql); $this->rs2array(); return $this->rowset; } /* return autoincremented id */ function Insert_ID() { return sqlite_last_insert_rowid($this->link); } /*return last error message */ function ErrorMsg() { $error_id = sqlite_last_error($this->link); if ($error_id) { return sqlite_error_string($error_id); } } /* return affected rows from update or delete */ function Affected_Rows() { return sqlite_changes($this->link) ; } /* return number of rows in recordset */ function RecordCount() { if ($this->rs && is_resource($this->rs)) { return sqlite_num_rows ($this->rs); } else { return 0; } } function RowCount() { return $this->RecordCount(); } /* return number of fileds (columns) in recordset */ function FieldCount() { if ($this->rs && is_resource($this->rs)) { return sqlite_num_fields($this->rs); } else { return 0; } } /* return array of table names in database */ function MetaTables() { $rs = $this->Execute("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name"); $ttables = ''; foreach($rs->GetArray() as $row) { $ttables[] = $row['name']; } return $ttables; } /* return array of ADOFieldObjects, one object per table column */ function MetaColumns($table, $upper = true) { $query = "select sql from SQLITE_MASTER where type = 'table' and tbl_name = '$table'"; $sth = sqlite_query($this->link, $query); while ($row = sqlite_fetch_array($sth, SQLITE_NUM)) { $lines = explode("\n", $row[0]); $fields = explode(',', $row[0]); } for ($i = 0; $i < count($fields)-1; $i++) { if ($i == 0) { $pos = strpos($fields[0], '('); $len = strlen($fields[0]); $fields[0] = substr($fields[0], $pos + 1, $len - $pos); } if ($i == count($fields)-1) { $last = count($fields)-1; $pos = strpos($fields[$last], ')'); $len = strlen($fields[$last]); $fields[$last] = substr($fields[$last], 0, $pos); } $all_meta = trim($fields[$i]); $parts = explode(' ', $all_meta); $fld = new ADOFieldObject(); $fld->name = $parts[0]; $parts[0]=""; $coltype = implode(' ', $parts); if (strstr($coltype, "INTEGER")) { $fld->type = "INTEGER"; $fld->max_length = ""; $cols[$fld->name] = $fld; continue; } else if (strstr($coltype, "text")) { $fld->type = "text"; $fld->max_length = ""; } else { $tarr = explode(")", $coltype, 2); $col_info = str_replace("(", "|", $tarr[0]); $info_arr = explode("|", $col_info, 3); $fld->type = trim($info_arr[0]); $fld->max_length = $info_arr[1]; } if (strstr($coltype, "NOT NULL")) { $fld->not_null = true; } if ($fld->not_null) { $fld->has_default = true; $def_arr = explode("DEFAULT", $coltype, 3); $def = trim(str_replace("'", "", $def_arr[1])); $fld->default_value = $def; } $cols[$fld->name] = $fld; } return $cols; } /* return array of column names i table */ function MetaColumnNames($table) { $rs = $this->Execute("select * from $table limit 1"); if (!$rs) { return $false;} $cols = false; for ($i=0;$i