Util   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 10
c 3
b 2
f 0
lcom 1
cbo 2
dl 0
loc 112
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A create_table() 0 4 1
A delete_table() 0 4 1
A backup_structure() 0 5 1
B backup_data() 0 59 7
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\Drivers\Firebird;
17
18
/**
19
 * Firebird-specific backup, import and creation methods
20
 *
21
 * @package Query
22
 * @subpackage Drivers
23
 */
24
class Util extends \Query\AbstractUtil {
25
26
	/**
27
	 * Convenience public function to generate sql for creating a db table
28
	 *
29
	 * @deprecated Use the table builder class instead
30
	 * @param string $name
31
	 * @param array $fields
32
	 * @param array $constraints
33
	 * @param bool $if_not_exists
34
	 * @return string
35
	 */
36
	public function create_table($name, $fields, array $constraints=array(), $if_not_exists=FALSE)
37
	{
38
		return parent::create_table($name, $fields, $constraints, FALSE);
39
	}
40
41
	/**
42
	 * Drop the selected table
43
	 *
44
	 * @param string $name
45
	 * @return string
46
	 */
47
	public function delete_table($name)
48
	{
49
		return 'DROP TABLE '.$this->get_driver()->quote_table($name);
50
	}
51
52
	// --------------------------------------------------------------------------
53
54
	/**
55
	 * Create an SQL backup file for the current database's structure
56
	 *
57
	 * @param string $db_path
58
	 * @param string $new_file
59
	 * @return string
60
	 */
61
	public function backup_structure()
62
	{
63
		list($db_path, $new_file) = func_get_args();
64
		return ibase_backup($this->get_driver()->get_service(), $db_path, $new_file, \IBASE_BKP_METADATA_ONLY);
65
	}
66
67
	// --------------------------------------------------------------------------
68
69
	/**
70
	 * Create an SQL backup file for the current database's data
71
	 *
72
	 * @param array $exclude
73
	 * @param bool $system_tables
74
	 * @return string
75
	 */
76
	public function backup_data($exclude=array(), $system_tables=FALSE)
77
	{
78
		// Determine which tables to use
79
		$tables = $this->get_driver()->get_tables();
80
		if($system_tables == TRUE)
81
		{
82
			$tables = array_merge($tables, $this->get_driver()->get_system_tables());
83
		}
84
85
		// Filter out the tables you don't want
86
		if( ! empty($exclude))
87
		{
88
			$tables = array_diff($tables, $exclude);
89
		}
90
91
		$output_sql = '';
92
93
		// Get the data for each object
94
		foreach($tables as $t)
95
		{
96
			$sql = 'SELECT * FROM "'.trim($t).'"';
97
			$res = $this->get_driver()->query($sql);
98
			$obj_res = $res->fetchAll(\PDO::FETCH_ASSOC);
99
100
			// Don't add to the file if the table is empty
101
			if (count($obj_res) < 1)
102
			{
103
				continue;
104
			}
105
106
			// Nab the column names by getting the keys of the first row
107
			$columns = @array_keys($obj_res[0]);
108
109
			$insert_rows = array();
110
111
			// Create the insert statements
112
			foreach($obj_res as $row)
113
			{
114
				$row = array_values($row);
115
116
				// Quote values as needed by type
117
				if(stripos($t, 'RDB$') === FALSE)
118
				{
119
					$row = array_map(array($this->get_driver(), 'quote'), $row);
120
					$row = array_map('trim', $row);
121
				}
122
123
				$row_string = 'INSERT INTO "'.trim($t).'" ("'.implode('","', $columns).'") VALUES ('.implode(',', $row).');';
124
125
				$row = NULL;
126
127
				$insert_rows[] = $row_string;
128
			}
129
130
			$output_sql .= "\n\nSET TRANSACTION;\n".implode("\n", $insert_rows)."\nCOMMIT;";
131
		}
132
133
		return $output_sql;
134
	}
135
}
136
// End of firebird_util.php