Просмотр исходного кода

Merge pull request #490 from visuddhinanda/master

使用medoo操作数据库
visuddhinanda 4 лет назад
Родитель
Сommit
455a4f6d69
3 измененных файлов с 89 добавлено и 19 удалено
  1. 3 0
      app/article/article.js
  2. 80 14
      app/db/table.php
  3. 6 5
      package.json

+ 3 - 0
app/article/article.js

@@ -125,6 +125,9 @@ function render_article_list(article_list,collectId="",articleId="") {
 
 function set_channal(channalid) {
 	let url = "../article/index.php?id=" + _articel_id;
+	if (_collection_id != "") {
+		url += "&collection=" + _collection_id;
+	}
 	if (channalid != "") {
 		url += "&channal=" + channalid;
 	}

+ 80 - 14
app/db/table.php

@@ -1,10 +1,11 @@
 <?php
-		/*
-        $table->beginTransaction($query)
-			  ->set($date)
-			  ->commit();
-		*/
 require_once "../redis/function.php";
+// Require Composer's autoloader.
+require '../../vendor/autoload.php';
+ 
+// Using Medoo namespace.
+use Medoo\Medoo;
+
 
 class Table
 {
@@ -13,16 +14,88 @@ class Table
     protected $redis;
     protected $errorMessage;
 	protected $field_setting;
+	protected $result;
+	protected $medoo;
+	protected $redisProfix;
     function __construct($db,$table,$user="",$password="",$redis=false) {
         $this->dbh = new PDO($db, $user, $password,array(PDO::ATTR_PERSISTENT=>true));
         $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
+		$database = new Medoo([
+			// Initialized and connected PDO object.
+			'pdo' => $this->dbh,
+		 
+			// [optional] Medoo will have different handle method according to different database type.
+			'type' => 'sqlite'
+		]);
+		$this->medoo = $database;
 		$this->redis = $redis;
 		$this->table = $table;
+		$this->result = ["ok"=>true,"message"=>"","data"=>array()];
+		$this->redisProfix = $table . "/:id";
     }
 	
-	public function setField($setting){
-		$this->field_setting = $setting;
+	public function index($columns,$where){
+		$output = $this->medoo->select(
+			$this->table,
+			$columns,
+			$where
+		);
+		$this->result["data"] = $output;
+		return $this->result;
+	}
+	public function create($data,$columns){
+		foreach ($columns as $value) {
+			# code...
+			$updateDate[$value] = $data[$value];
+		}
+		$this->medoo->insert(
+			$this->table,
+			$updateDate
+		);
+
+		$newId = $database->id;
+		$this->result["data"] = $newId;
+		return $this->result;
+	}
+	public function update($data,$columns,$where=null){
+		foreach ($columns as $value) {
+			# code...
+			$updateDate[$value] = $data[$value];
+		}
+		if($where==null){
+			$where = ["id"=>$data["id"]];
+		}
+		$this->medoo->update(
+			$this->table,
+			$updateDate,
+			$where
+		);
+
+		return $this->result;
+	}
+
+	public function get($columns,$where){
+		$output = $this->medoo->get(
+			$this->table,
+			$columns,
+			$where
+		);
+		$this->result["data"] = $output;
+		return $this->result;
+
 	}
+
+	public function delete($where){
+		$output = $this->medoo->delete(
+			$this->table,
+			$where
+		);
+		$this->result["data"] = $output->rowCount();
+		return $this->result;
+	}
+
+
+
 	protected function fetch($query,$params){
 		if (isset($params)) {
 			$stmt = $this->dbh->prepare($query);
@@ -58,13 +131,6 @@ class Table
 	}
 
 
-	public function syncList($time){
-
-	}
-
-	public function syncGet($time){
-
-	}
 }
 
 ?>

+ 6 - 5
package.json

@@ -4,13 +4,14 @@
 	"dependencies": {
 		"diff": "^5.0.0",
 		"editor.md": "^1.5.0",
-		"fullcalendar": "^5.8.0",
-		"highcharts": "^9.1.0",
+		"fullcalendar": "^5.9.0",
+		"highcharts": "^9.2.2",
 		"jquery": "^3.6.0",
 		"jquery-ui": "^1.12.1",
-		"marked": "^2.0.6",
-		"mermaid": "^8.10.1",
+		"marked": "^2.1.3",
+		"mermaid": "^8.12.0",
 		"tributejs": "^5.1.3",
-		"vue": "^3.0.11"
+		"vue": "^3.2.8",
+		"jquery.fancytree": "^2.38.0"
 	}
 }