PluginProbe
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce / 1.2.4
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce v1.2.4
2.15.0 trunk 1.0.0 1.0.1 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.1.2 2.1.3 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 All 62 releases
hurrytimer / includes / utils / class-sql-builder.php

class-sql-builder.php in HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce 1.2.4, at includes/utils/class-sql-builder.php

95 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Hurrytimer;
3
4 class SQL_Builder
5 {
6 public $table;
7 public $charset_collate;
8 public $sql_fields = [];
9 const DELETE_CASCADE = 1;
10 public function __construct($_table)
11 {
12 global $wpdb;
13 $this->table = "{$wpdb->prefix}$_table";
14 $this->charset_collate = $wpdb->get_charset_collate();
15 }
16
17 public function create()
18 {
19 $sql = "CREATE TABLE {$this->table} (";
20 $this->sql_fields[] = $sql;
21 return $this;
22 }
23 private function _field($name, $type, $length, $unsigned = false, $is_nullable = false)
24 {
25 $sql = $name . ' ' . strtoupper($type) . '(' . $length . ') ';
26 $sql .= ($unsigned ? 'UNSIGNED ' : '');
27 $sql .= ($is_nullable ? 'NULL' : 'NOT NULL');
28 return $sql;
29 }
30 public function string($name, $length = 50, $is_nullable = false)
31 {
32 $sql = $this->_field($name, "VARCHAR", $length, false, $is_nullable);
33 $this->sql_fields[] = $sql;
34 return $this;
35 }
36 public function primary_key($name)
37 {
38 $sql = "PRIMARY KEY($name)";
39 $this->sql_fields[] = $sql;
40 return $this;
41 }
42 public function foreign_key($name, $reference, $delete)
43 {
44 global $wpdb;
45 $sql = "FOREIGN KEY($name) REFERENCES {$wpdb->prefix}$reference ON DELETE ";
46 switch ($delete) {
47 case self::DELETE_CASCADE:
48 $sql .= "CASCADE";
49 break;
50 }
51 $this->sql_fields[] = $sql;
52 return $this;
53 }
54
55 public function run()
56 {
57 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
58 dbDelta($this->build_query());
59 }
60
61 public function build_query()
62 {
63 $sql = array_shift($this->sql_fields);
64 $sql .= implode(',', $this->sql_fields);
65 $sql .= ')';
66 if ($this->charset_collate) {
67 $sql .= ' ' . $this->charset_collate . ';';
68 }
69 return $sql;
70 }
71
72 public function bigint($name, $length = 20, $unsigned = false, $is_nullable = false, $auto_increment = false)
73 {
74 $sql = $this->_field($name, 'BIGINT', $length, $unsigned, $is_nullable);
75 $sql .= ($auto_increment ? ' AUTO_INCREMENT' : '');
76 $this->sql_fields[] = $sql;
77 return $this;
78 }
79
80 public function boolean($name)
81 {
82 $sql = $this->_field($name, 'TINYINT', 1, true, true);
83 $this->sql_fields[] = $sql;
84 return $this;
85 }
86 public function timestamp($name, $is_nullable = false)
87 {
88 $sql = $name . ' ' . 'TIMESTAMP ';
89 $sql .= ($is_nullable ? 'NULL' : 'NOT NULL');
90 $this->sql_fields[] = $sql;
91 return $this;
92 }
93
94 }
95