-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQLPlatform.php
161 lines (116 loc) · 4.47 KB
/
SQLPlatform.php
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/**
* ORM framework
* @author Cristian Lorenzetto <opensource.publicocean0@gmail.com>
*/
abstract class SQLPlatform
{
abstract public function convertFromSQLType($type,$value);
abstract public function convertToSQLType($type,$value);
abstract public function createSequenceSQL($name,$step=1,$start=1);
abstract public function dropSequenceSQL($name);
abstract public function getCurrentSequenceSQL($name);
abstract public function lastInsertId($pdo,$table,$column);
abstract public function getIncrementSequenceSQL($name);
abstract public function getExistsSequenceSQL($name);
public function getColumnDeclarationListSQL(array $fields)
{
$queryFields = array();
foreach ($fields as $fieldName => $field) {
$queryFields[] = $this->getColumnDeclarationSQL($fieldName, $field);
}
return implode(', ', $queryFields);
}
/**
* Obtain DBMS specific SQL code portion needed to set a default value
* declaration to be used in statements like CREATE TABLE.
*
* @param array $field field definition array
*
* @return string DBMS specific SQL code portion needed to set a default value
*/
public function getDefaultValueDeclarationSQL($field)
{
$default = empty($field['notnull']) ? ' DEFAULT NULL' : '';
if (isset($field['default'])) {
$default = " DEFAULT '".$field['default']."'";
}
return $default;
}
/**
* Obtain DBMS specific SQL code portion needed to declare a generic type
* field to be used in statements like CREATE TABLE.
*
* @param string $name name the field to be declared.
* @param array $field associative array with the name of the properties
* of the field being declared as array indexes. Currently, the types
* of supported field properties are as follows:
*
* length
* Integer value that determines the maximum length of the text
* field. If this argument is missing the field should be
* declared to have the longest length allowed by the DBMS.
*
* default
* Text value to be used as default for this field.
*
* notnull
* Boolean flag that indicates whether this field is constrained
* to not be set to null.
* charset
* Text value with the default CHARACTER SET for this field.
* collation
* Text value with the default COLLATION for this field.
* unique
* unique constraint
* check
* column check constraint
* columnDefinition
* a string that defines the complete column
*
* @return string DBMS specific SQL code portion that should be used to declare the column.
*/
public function getColumnDeclarationSQL($name, array $field)
{
$default = $this->getDefaultValueDeclarationSQL($field);
$notnull = (isset($field['notnull']) && $field['notnull']) ? ' NOT NULL' : '';
$mapping=$this->getToSQLTypeMappings();
$typeDecl = $mapping[$field['type']];
$columnDef = $typeDecl . $default . $notnull ;
return $name . ' ' . $columnDef;
}
abstract public function getCreateTableSQL($tableName, array $columns, array $options = array());
abstract public function getCreateDatabaseSQL($name);
abstract public function getDeleteDatabaseSQL($name);
abstract public function getDeleteTableSQL($name);
abstract public function getQuoteCharacter();
abstract public function getTableColumnsSQL($table,$database);
abstract public function getPrimaryTableColumnsSQL($table,$database);
/**
* getIndexFieldDeclarationList
* Obtain DBMS specific SQL code portion needed to set an index
* declaration to be used in statements like CREATE TABLE.
*
* @param array $fields
*
* @return string
*/
public function getIndexFieldDeclarationListSQL(array $fields)
{
$ret = array();
foreach ($fields as $field => $definition) {
if (is_array($definition)) {
$ret[] = $field;
} else {
$ret[] = $definition;
}
}
return implode(', ', $ret);
}
/**
* Name of this keyword list.
*
* @return string
*/
abstract public function getName();
}