forked from topspin/topspin-wordpress
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtopspin_db.php
More file actions
76 lines (73 loc) · 1.96 KB
/
topspin_db.php
File metadata and controls
76 lines (73 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
/*
* Last Modified: January 9, 2011
*
* ----------------------------------
* Change Log
* ----------------------------------
* 2012-01-09
- Updated topspin_table_column_add
* 2011-09-07
- Fixed topspin_table_column_add column (removed AFTER key)
* 2011-07-26
- File created
- New function topspin_table_column_exists()
- New function topspin_table_column_add()
*/
function topspin_table_column_exists($table,$column) {
/*
* Checks to see if a table column exists
*
* PARAMETERS:
* @table (string)
* @column (string)
*
* RETURNS:
* true if the column exists in the table
* false otherwise
*/
global $wpdb;
$sql = <<<EOD
SELECT COLUMN_NAME
FROM information_schema.COLUMNS
WHERE
TABLE_SCHEMA = '{$wpdb->dbname}'
AND TABLE_NAME = '{$wpdb->prefix}{$table}'
AND COLUMN_NAME = '{$column}'
EOD;
$res = $wpdb->get_var($sql);
return ($res) ? true : false;
}
function topspin_table_column_add($table,$column,$type='text',$options=null) {
/*
* Checks to see if a table column exists
*
* PARAMETERS:
* @table (string)
* @column (string)
* @type (string) enumeration: INT, BIGINT, VARCHAR(255), TEXT, LONGTEXT, TIMESTAMP (default: TEXT)
* @options (array) Additional key options
* @first (bool) Add to the beginning of table?
* @autoIncrement (bool) Add auto increment?
* @primaryKey (bool) Make it as a primary key?
*
* RETURNS:
* true if the column exists in the table
* false otherwise
*/
global $wpdb;
$defaults = array(
'first' => false,
'autoIncrement' => false,
'primaryKey' => false
);
$options = array_merge($defaults,$options);
$first = ($options['first']) ? 'FIRST' : '';
$autoIncrement = ($options['autoIncrement']) ? 'AUTO_INCREMENT' : '';
$primaryKey = ($options['primaryKey']) ? 'ADD PRIMARY KEY (`'.$column.'`)' : '';
$sql = <<<EOD
ALTER TABLE `{$wpdb->prefix}{$table}` ADD `{$column}` {$type} NOT NULL {$first} {$autoIncrement} {$primaryKey};
EOD;
$wpdb->query($sql);
}
?>