AbstractUtil   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 111
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get_driver() 0 4 1
B create_table() 0 33 5
A delete_table() 0 4 1
backup_structure() 0 1 ?
backup_data() 0 1 ?
1
<?php
2
/**
3
 * Query
4
 *
5
 * Free Query Builder / Database Abstraction Layer
6
 *
7
 * @package		Query
8
 * @author		Timothy J. Warren
9
 * @copyright	Copyright (c) 2012 - 2015
10
 * @link 		https://github.com/aviat4ion/Query
11
 * @license		http://philsturgeon.co.uk/code/dbad-license
12
 */
13
14
// --------------------------------------------------------------------------
15
16
namespace Query;
17
18
// --------------------------------------------------------------------------
19
20
/**
21
 * Abstract class defining database / table creation methods
22
 *
23
 * @package Query
24
 * @subpackage Drivers
25
 * @method string quote_ident(string $sql)
26
 * @method string quote_table(string $sql)
27
 */
28
abstract class AbstractUtil {
29
30
	/**
31
	 * Reference to the current connection object
32
	 */
33
	private $conn;
34
35
	/**
36
	 * Save a reference to the connection object for later use
37
	 *
38
	 * @param DriverInterface $conn
39
	 */
40
	public function __construct(DriverInterface $conn)
41
	{
42
		$this->conn = $conn;
43
	}
44
45
	// --------------------------------------------------------------------------
46
47
	/**
48
	 * Get the driver object for the current connection
49
	 *
50
	 * @return Driver_Interface
51
	 */
52
	public function get_driver()
53
	{
54
		return $this->conn;
55
	}
56
57
	// --------------------------------------------------------------------------
58
59
	/**
60
	 * Convenience public function to generate sql for creating a db table
61
	 *
62
	 * @param string $name
63
	 * @param array $fields
64
	 * @param array $constraints
65
	 * @param bool $if_not_exists
66
	 * @return string
67
	 */
68
	public function create_table($name, $fields, array $constraints=array(), $if_not_exists=TRUE)
69
	{
70
		$exists_str = ($if_not_exists) ? ' IF NOT EXISTS ' : ' ';
71
72
		// Reorganize into an array indexed with column information
73
		// Eg $column_array[$colname] = array(
74
		// 		'type' => ...,
75
		// 		'constraint' => ...,
76
		// 		'index' => ...,
77
		// )
78
		$column_array = \array_zipper(array(
79
			'type' => $fields,
80
			'constraint' => $constraints
81
		));
82
83
		// Join column definitions together
84
		$columns = array();
85
		foreach($column_array as $n => $props)
86
		{
87
			$str = $this->get_driver()->quote_ident($n);
88
			$str .= (isset($props['type'])) ? " {$props['type']}" : "";
89
			$str .= (isset($props['constraint'])) ? " {$props['constraint']}" : "";
90
91
			$columns[] = $str;
92
		}
93
94
		// Generate the sql for the creation of the table
95
		$sql = 'CREATE TABLE'.$exists_str.$this->get_driver()->quote_table($name).' (';
96
		$sql .= implode(', ', $columns);
97
		$sql .= ')';
98
99
		return $sql;
100
	}
101
102
	// --------------------------------------------------------------------------
103
104
	/**
105
	 * Drop the selected table
106
	 *
107
	 * @param string $name
108
	 * @return string
109
	 */
110
	public function delete_table($name)
111
	{
112
		return 'DROP TABLE IF EXISTS '.$this->get_driver()->quote_table($name);
113
	}
114
115
116
	// --------------------------------------------------------------------------
117
	// ! Abstract Methods
118
	// --------------------------------------------------------------------------
119
120
	/**
121
	 * Return an SQL file with the database table structure
122
	 *
123
	 * @abstract
124
	 * @return string
125
	 */
126
	abstract public function backup_structure();
127
128
	// --------------------------------------------------------------------------
129
130
	/**
131
	 * Return an SQL file with the database data as insert statements
132
	 *
133
	 * @abstract
134
	 * @return string
135
	 */
136
	abstract public function backup_data();
137
138
}
139
// End of abstract_util.php