-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDataModel.php
More file actions
144 lines (137 loc) · 7.23 KB
/
Copy pathDataModel.php
File metadata and controls
144 lines (137 loc) · 7.23 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
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
<?php
/**
* APIShift Engine v1.0.0
*
* Copyright 2020-present Sapir Shemer, DevShift (devshift.biz)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author Sapir Shemer
*/
namespace APIShift\Core;
/**
* Interface that helps manage the structures and types of items and relations available in the system
*/
class DataModelManager {
/**
* Creates an item/relation using query rules
* @param array $query Query data to create the Item/Relation
* @param string $query['name'] Item/Relation name to create
* @param array $query['keys'] Collection of key names to types to add
* @param string|int $query['relation_type'] ID or name of the relation type to assign
* @param string|int|array $query['from'] Relation comming from item ID/name/collection
* @param string|int|array $query['to'] Relation comming from item ID/name/collection
*/
public static function create(array $query) {
// Validate query
if(!isset($query['name'])) Status::message(Status::ERROR, "No item/relation name specified");
if(!isset($query['keys'])) Status::message(Status::ERROR, "Cannot create an empty item/relation structure");
// Check if table/item exists
$check = DatabaseManager::getInstance("main")->query("SELECT id FROM " . $query['name']);
if(gettype($check) == 'array') Status::message(Status::ERROR, "Item/relation exist");
// Query construction string
$query_str = "";
// Create query string containing the key pairs and their types
foreach($query['keys'] as $key_name => $value) $query_str .= $key_name . " " . $value . ",";
// Check if item is relation and create relation
if(!isset($query['relation_type'])) {
// Create AI primary key based on system
switch(Configurations::DB_TYPE) {
case "MySQL":
$query_str .= "id int NOT NULL AUTO_INCREMENT, PRIMARY KEY (id)";
break;
case "MSSQL":
$query_str .= "id int NOT NULL PRIMARY KEY IDENTITY(1,1)";
break;
}
// Create the table
$result = DatabaseManager::query("main", "CREATE TABLE " . $query['name'] . " (" . $query_str . ");");
// Check if creation completed successfully
if($result == false) Status::message(Status::ERROR, "Couldn't create item in DB");
// Insert item data
$result = DatabaseManager::query("main", "INSERT INTO items (table_name) VALUES (:iname);", [ 'iname' => $query['name'] ]);
// Check if insertion completed successfully
if($result == false) {
Status::message(Status::ERROR, "Couldn't insert item to items");
// TOOD: Dispose of data if any was created
}
Status::message(Status::SUCCESS, "Created Item successfully");
}
switch($query['relation_type']) {
case 0: // 1 to 1
case 1: // 1 to n
// Table name to modify
$table_name = "";
// Get `to` table id
if(gettype($query['to']) != 'int') {
if($query['relation_type'] == 1) $table_name = $query['to'];
// Get 'to' table ID and store it in $query['to']
$result = DatabaseManager::query("main", "SELECT id FROM items WHERE name = :to_name", ['to_name' => $query['to']]);
if(gettype($result) != 'array' || count($result) == 0) Status::message(Status::ERROR, "Relation `to` item doesn't exist");
$query['to'] = $result[0]['id'];
}
else if($query['relation_type'] == 1){
// Get 'to' table name
$result = DatabaseManager::query("main", "SELECT name FROM items WHERE id = :to_id", ['to_id' => $query['to']]);
if(gettype($result) != 'array' || count($result) == 0) Status::message(Status::ERROR, "Relation `to` item doesn't exist");
$table_name = $result[0]['name'];
}
// Get `from` table id
if(gettype($query['from']) != 'int') {
if($query['relation_type'] == 0) $table_name = $query['from'];
// Get 'from' table ID and store it in $query['from']
$result = DatabaseManager::query("main", "SELECT id FROM items WHERE name = :from_name", ['from_name' => $query['from']]);
if(gettype($result) != 'array' || count($result) == 0) Status::message(Status::ERROR, "Relation `from` item doesn't exist");
$query['from'] = $result[0]['id'];
}
else if($query['relation_type'] == 0){
// Get 'from' table name
$result = DatabaseManager::query("main", "SELECT name FROM items WHERE id = :from_id", ['from_id' => $query['from']]);
if(gettype($result) != 'array' || count($result) == 0) Status::message(Status::ERROR, "Relation `from` item doesn't exist");
$table_name = $result[0]['name'];
}
// Create column in the `from` table to serve as the relation
switch(Configurations::DB_TYPE) {
case "MySQL":
$query_str .= $query['name'] . " int AUTO_INCREMENT";
break;
case "MSSQL":
$query_str .= $query['name'] . " int IDENTITY(1,1)";
break;
}
$result = DatabaseManager::query("main",
"ALTER TABLE " . $table_name . " ADD " . $query_str . ";" . // Create relation column
"INSERT INTO items (name) VALUES (:iname);" . // Add item
// Create the relation
"INSERT INTO relations (parent, `from`, `to`, `type`) VALUES ((SELECT id FROM items ORDER BY id DESC LIMIT 1), :fitem, :titem, :rtype);",
[
'iname' => $query['name'],
'fitem' => $query['from'],
'titem' => $query['to'],
'rtype' => $query['relation_type'],
]
);
if($result == false) {
Status::message(Status::ERROR, "Couldn't create Relation in DB");
// TOOD: Dispose of data if any was created
}
// Add Item & Relation
break;
case 3: // n to n
// TODO: Create relation as a new table
break;
default: Status::message(Status::ERROR, "Unrecognized relation type");
}
}
}
?>