Browse Source

Initial implementation SQLite

Maurits van der Schee 8 years ago
parent
commit
69b9b4b695
1 changed files with 15 additions and 16 deletions
  1. 15
    16
      api.php

+ 15
- 16
api.php View File

@@ -10,7 +10,7 @@ interface DatabaseInterface {
10 10
 	public function insertId($result);
11 11
 	public function affectedRows($result);
12 12
 	public function close($result);
13
-	public function fetchFields($result);
13
+	public function fetchFields($table);
14 14
 	public function addLimitToSql($sql,$limit,$offset);
15 15
 	public function likeEscape($string);
16 16
 	public function isBinaryType($field);
@@ -117,7 +117,7 @@ class MySQL implements DatabaseInterface {
117 117
 			if ($param===null) return 'NULL';
118 118
 			return "'".mysqli_real_escape_string($db,$param)."'";
119 119
 		}, $sql);
120
-		//if (!strpos($sql,'INFORMATION_SCHEMA')) echo "\n$sql\n";
120
+		//if (!strpos($sql,'INFORMATION_SCHEMA'))	echo "\n$sql\n";
121 121
 		return mysqli_query($db,$sql);
122 122
 	}
123 123
 
@@ -141,7 +141,8 @@ class MySQL implements DatabaseInterface {
141 141
 		return mysqli_free_result($result);
142 142
 	}
143 143
 
144
-	public function fetchFields($result) {
144
+	public function fetchFields($table) {
145
+		$result = $this->query('SELECT * FROM "!" WHERE 1=2;',array($table));
145 146
 		return mysqli_fetch_fields($result);
146 147
 	}
147 148
 
@@ -331,7 +332,8 @@ class PostgreSQL implements DatabaseInterface {
331 332
 		return pg_free_result($result);
332 333
 	}
333 334
 
334
-	public function fetchFields($result) {
335
+	public function fetchFields($table) {
336
+		$result = $this->query('SELECT * FROM "!" WHERE 1=2;',array($table));
335 337
 		$keys = array();
336 338
 		for($i=0;$i<pg_num_fields($result);$i++) {
337 339
 			$field = array();
@@ -537,7 +539,8 @@ class SQLServer implements DatabaseInterface {
537 539
 		return sqlsrv_free_stmt($result);
538 540
 	}
539 541
 
540
-	public function fetchFields($result) {
542
+	public function fetchFields($table) {
543
+		$result = $this->query('SELECT * FROM "!" WHERE 1=2;',array($table));
541 544
 		//var_dump(sqlsrv_field_metadata($result));
542 545
 		return array_map(function($a){
543 546
 			$p = array();
@@ -689,7 +692,7 @@ class SQLite implements DatabaseInterface {
689 692
 				return "'".$db->escapeString($v)."'";
690 693
 			},$param)).')';
691 694
 			if (is_object($param) && $param->type=='base64') {
692
-				return "'".$db->escapeString(base64_decode($param->data))."'";
695
+				return "x'".bin2hex(base64_decode($param->data))."'";
693 696
 			}
694 697
 			if ($param===null) return 'NULL';
695 698
 			return "'".$db->escapeString($param)."'";
@@ -723,14 +726,11 @@ class SQLite implements DatabaseInterface {
723 726
 		return $result->finalize();
724 727
 	}
725 728
 
726
-	public function fetchFields($result) {
727
-		$values = $result->fetchArray();
729
+	public function fetchFields($table) {
730
+		$result = $this->query('SELECT * FROM "sys/columns" WHERE "self"=?;',array($table));
728 731
 		$fields = array();
729
-		for($i = 0; $i < $result->numColumns() ; $i++){
730
-			$tab = array();
731
-			$tab['type'] = $result->columnType($i);
732
-			$tab['name'] = $result->columnName($i);
733
-			$fields[strtolower($tab['name'])] = (object)$tab;
732
+		while ($row = $this->fetchAssoc($result)){
733
+			$fields[strtolower($row['name'])] = (object)$row;
734 734
 		}
735 735
 		return $fields;
736 736
 	}
@@ -744,7 +744,7 @@ class SQLite implements DatabaseInterface {
744 744
 	}
745 745
 
746 746
 	public function isBinaryType($field) {
747
-		return ($field->type==4);
747
+		return (substr($field->type,0,4)=='blob');
748 748
 	}
749 749
 
750 750
 	public function base64Encode($string) {
@@ -1188,8 +1188,7 @@ class PHP_CRUD_API {
1188 1188
 
1189 1189
 	protected function findTableFields($table,$database) {
1190 1190
 		$fields = array();
1191
-		$result = $this->db->query('SELECT * FROM "!" WHERE 1=2;',array($table));
1192
-		foreach ($this->db->fetchFields($result) as $field) {
1191
+		foreach ($this->db->fetchFields($table) as $field) {
1193 1192
 			$fields[$field->name] = $field;
1194 1193
 		}
1195 1194
 		return $fields;

Loading…
Cancel
Save