Completed
Push — master ( 20ce86...00401a )
by Chris
02:49
created

SqlServer::identifier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace Darya\Database\Query\Translator;
3
4
use Darya\Database\Query\AbstractSqlTranslator;
5
6
/**
7
 * Darya's SQL Server query translator.
8
 * 
9
 * TODO: Offset!
10
 * 
11
 * @author Chris Andrew <[email protected]>
12
 */
13
class SqlServer extends AbstractSqlTranslator {
14
	
15
	/**
16
	 * Resolve the given value as an identifier.
17
	 * 
18
	 * @param mixed $identifier
19
	 * @return mixed
20
	 */
21
	public function resolveIdentifier($identifier) {
22
		return $identifier;
23
	}
24
	
25
	/**
26
	 * Prepare a LIMIT clause using the given limit and offset.
27
	 * 
28
	 * @param int $limit  [optional]
29
	 * @param int $offset [optional]
30
	 * @return string
31
	 */
32
	protected function prepareLimit($limit = 0, $offset = 0) {
33
		if (!static::limitIsUseful($limit, $offset)) {
34
			return null;
35
		}
36
		
37
		$limit = (int) $limit;
38
		$offset = (int) $offset;
0 ignored issues
show
Unused Code introduced by
$offset is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
39
		
40
		return "TOP $limit";
41
	}
42
	
43
	/**
44
	 * Prepare a SELECT statement using the given columns, table, clauses and
45
	 * options.
46
	 * 
47
	 * @param string       $table
48
	 * @param array|string $columns
49
	 * @param string       $joins    [optional]
50
	 * @param string       $where    [optional]
51
	 * @param string       $order    [optional]
52
	 * @param string       $limit    [optional]
53
	 * @param bool         $distinct [optional]
54
	 * @return string
55
	 */
56 View Code Duplication
	protected function prepareSelect($table, $columns, $joins = null, $where = null, $order = null, $limit = null, $distinct = false) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
		$table = $this->identifier($table);
58
		
59
		$distinct = $distinct ? 'DISTINCT' : '';
60
		
61
		return static::concatenate(array('SELECT', $distinct, $limit, $columns, 'FROM', $table, $joins, $where, $order));
62
	}
63
	
64
	/**
65
	 * Prepare an UPDATE statement with the given table, data and clauses.
66
	 * 
67
	 * @param string $table
68
	 * @param array  $data
69
	 * @param string $where [optional]
70
	 * @param string $limit [optional]
71
	 * @return string
72
	 */
73 View Code Duplication
	protected function prepareUpdate($table, $data, $where = null, $limit = null) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
		$table = $this->identifier($table);
75
		
76
		foreach ($data as $key => $value) {
77
			$column = $this->identifier($key);
78
			$value = $this->value($value);
79
			$data[$key] = "$column = $value";
80
		}
81
		
82
		$values = implode(', ', $data);
83
		
84
		return static::concatenate(array('UPDATE', $limit, $table, 'SET', $values, $where));
85
	}
86
	
87
	/**
88
	 * Prepare a DELETE statement with the given table and clauses.
89
	 * 
90
	 * @param string $table
91
	 * @param string $where [optional]
92
	 * @param string $limit [optional]
93
	 * @return string
94
	 */
95 View Code Duplication
	protected function prepareDelete($table, $where = null, $limit = null) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
		$table = $this->identifier($table);
97
		
98
		if ($table == '*' || !$table || !$where) {
99
			return null;
100
		}
101
		
102
		return static::concatenate(array('DELETE', $limit, 'FROM', $table, $where));
103
	}
104
	
105
}
106