Max YO 5 gadi atpakaļ
vecāks
revīzija
5fd0d8a110

+ 19 - 2
helpers/helpers_array.php

@@ -41,7 +41,7 @@ if (! function_exists('arrayAddKeyFromColumn')) {
 }
 
 if (! function_exists('arrayGroupByColumn')) {
-	function arrayGroupByColumn($array, $column_key) {
+	function arrayGroupByColumn($array, $column_key, $preserve_keys=true) {
 		$results = array();
 		foreach ($array as $k => $v) {
 			if (is_callable($column_key)) {
@@ -53,7 +53,12 @@ if (! function_exists('arrayGroupByColumn')) {
 			if (! isset($results[$key_value])) {
 				$results[$key_value] = array();
 			}
-			$results[$key_value][$k] = $v;
+			if ($preserve_keys) {
+				$results[$key_value][$k] = $v;
+				
+			} else {
+				$results[$key_value][] = $v;
+			}
 		}
 		return $results;
 	}
@@ -130,3 +135,15 @@ if (! function_exists('exportToCsvFile')) {
 		exit;
 	}
 }
+
+
+if (! function_exists('array_map_with_keys')) {
+	function array_map_with_keys($func, $array) {
+		$ret = [];
+		foreach ($array as $k => $v) {
+			$ret[$k] = $func($v, $k);
+		}
+		return $ret;
+	}
+}
+

+ 11 - 3
src/Database/Sql/SqlQuery.php

@@ -118,10 +118,18 @@ class SqlQuery
 
 		$this->status = 'running';
 		$ts_start = microtime(true);
+		
+
+		if (! empty($_GET['debug_sql'])) {
+			echo $query . "<hr />";
+		}
+
+		$_query = $query;
+		if (! empty($_GET['dry_sql'])) {
+			$_query = "select 1 from ( select 1 ) tmp where 0";
+		}
 
-		//echo $query . "<hr />";
-		$rs = $this->db->getDriver()->execute($query);
-		//pre($query);
+		$rs = $this->db->getDriver()->execute($_query);
 		//pre($rs);
 
 		$ts_end = microtime(true);

+ 77 - 3
src/Database/Sql/SqlTable.php

@@ -79,10 +79,47 @@ class SqlTable
 		// TODO: gerer on_duplicate_key_updates comme le where, dans un tableau et non dans un string
 
 		$query = "insert " . $ignore_sql . " into " . $this->table_name . " (" . $fields_sql . ") values " . $inserts_sql . " " . $on_duplicate_key_updates_sql;
+
+		if (! empty($options['debug'])) {
+			echo "<pre>" .preg_replace('/\s+/', ' ', $query) . "</pre>";
+		}
+
+		if (! empty($options['dry'])) {
+			return true;
+		}
+
 		return $this->db->createQuery()->executeInsertAll($query);
 	}
 
 
+	public function insertSelect($insert_table, $insert_keys=null, $where=null, $options=[]) 
+	{
+		$options_select = array_slice($options, 0);
+		unset($options_select['debug']);
+
+		$ignore_sql = empty($options['ignore']) ? '' : 'ignore';
+		
+		$on_duplicate_key_updates_sql = empty($options['on_duplicate_key_updates']) ? "" : ("on duplicate key update " . $options['on_duplicate_key_updates']);
+
+		$sql_keys = $insert_keys ? ("(" . $insert_keys . ")") : "";
+
+		$insert = "insert " . $ignore_sql . " into " . $insert_table . " " . $sql_keys . PHP_EOL;
+
+		$query = $this->buildQuery($where, $options_select);
+		$query = $insert . " " . $query . " " . $on_duplicate_key_updates_sql;
+
+		if (! empty($options['debug'])) {
+			echo "<pre>" .preg_replace('/\s+/', ' ', $query) . "</pre>";
+		}
+
+		if (! empty($options['dry'])) {
+			return true;
+		}
+
+		return $this->db->createQuery()->execute($query);
+	}
+
+
 	public function update(array $updates=[], array $where=[], $options=[]) /* : int */
 	{
 		$limit_sql = (isset($options['limit']) && ! is_null($options['limit'])) ? ("limit " . $options['limit']) : "";
@@ -101,6 +138,15 @@ class SqlTable
 					set " . $this->db->buildSqlUpdateValues($updates) . "
 					where " . $this->db->buildSqlWhere($where) . "
 					" . $limit_sql;
+
+		if (! empty($options['debug'])) {
+			echo "<pre>" .preg_replace('/\s+/', ' ', $query) . "</pre>";
+		}
+
+		if (! empty($options['dry'])) {
+			return true;
+		}
+
 		return $this->db->createQuery()->executeUpdate($query);
 	}
 
@@ -112,6 +158,15 @@ class SqlTable
 		$query = "delete from " . $this->table_name . "
 					where " . $this->db->buildSqlWhere($where) . "
 					" . $limit_sql;
+
+		if (! empty($options['debug'])) {
+			echo "<pre>" .preg_replace('/\s+/', ' ', $query) . "</pre>";
+		}
+
+		if (! empty($options['dry'])) {
+			return true;
+		}
+
 		return $this->db->createQuery()->executeDelete($query);
 	}
 
@@ -137,15 +192,28 @@ class SqlTable
 	public function getAll($where=null, $options=[]) /* : array */
 	{
 		$query = $this->buildQuery($where, $options);
+
+		if (! empty($options['dry'])) {
+			return [];
+		}
+
 		return $this->db->createQuery()->executeSelectAll($query);
 	}
 
 	public function getAllWithFoundRows($where=null, $options=[]) /* : array */
 	{
 		$query = $this->buildQuery($where, $options);
-		$rs = $this->db->createQuery()->execute($query);
-		$data = $rs->fetchAll();
-		$found_rows = $rs->getfoundRowsCount();
+
+		if (! empty($options['dry'])) {
+			$data = [];
+			$found_rows = 0;
+
+		} else {
+			$rs = $this->db->createQuery()->execute($query);
+			$data = $rs->fetchAll();
+			$found_rows = $rs->getfoundRowsCount();
+
+		}
 		return ['found_rows' => $found_rows, 'data' => $data];
 	}
 
@@ -167,7 +235,13 @@ class SqlTable
 	{
 		$options['limit'] = 1;
 		//return $this->getAll($where, $options)->fetchOne();
+
 		$query = $this->buildQuery($where, $options);
+
+		if (! empty($options['dry'])) {
+			return [];
+		}
+
 		return $this->db->createQuery()->executeSelectOne($query);
 	}
 

+ 7 - 0
src/Database/Sql/SqlTableModel.php

@@ -103,6 +103,13 @@ class SqlTableModel
 		return $db->getTable(static::$table_name)->insertAll($rows, $options);
 	}
 
+	public static function insertSelect($insert_table, $insert_keys=null, $rows=[], $options=[])
+	{
+		$db = static::getDb();
+		static::checkTable();
+		return $db->getTable(static::$table_name)->insertSelect($insert_table, $insert_keys, $rows, $options);
+	}
+
 	public static function update($updates=[], $where=[], $options=[])
 	{
 		$db = static::getDb();

+ 2 - 0
src/Database/Sql/SqlTools.php

@@ -115,6 +115,7 @@ class SqlTools
 	public function buildSqlWhere($where)
 	{
         $where_sql = array("1" => "1");
+        
         if (! empty($where)) {
             foreach ($where as $key => $value) {
                 if (is_null($value)) {
@@ -147,6 +148,7 @@ class SqlTools
                     $where_sql[] = (string) $value;
                     
                 }else{
+                    pre($where, 1);
                     $where_sql[] = $key . ' = ' . $this->escape($value);
                     //$where_sql[] = $key . ' = ' . (string) $value;
                 }

+ 1 - 1
src/Templates/PhpTemplate.php

@@ -142,7 +142,7 @@ class PhpTemplate
 		}
 
 		if (is_null($tpl_path)) {
-			throw new \Exception("Template not found : " . $tpl, 1);
+			throw new \Exception("Template not found : " . $tpl . " (dirs: " . implode(" | ", $tpl_dirs) . ")", 1);
 		}
 		
 		//$tpl_vars = array_merge($this->variables, $extra_vars);