Skip to content

Commit b1a95c1

Browse files
committed
Updates to tableStore
1 parent 9fbce74 commit b1a95c1

2 files changed

Lines changed: 29 additions & 53 deletions

File tree

app/Http/Controllers/Controller.php

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -51,31 +51,12 @@ public function rowRemove_($tableName,$id)
5151
return TableController::tableGet($tableName);
5252
}
5353

54-
public function tableStore_(Request $request)
54+
public function tableStore_()
5555
{
56-
if (!$request->has('tablename'))
57-
return abort(400, "No tablename given.");
58-
59-
$tableName = $request->tablename;
60-
61-
if (!$request->has('columns'))
62-
return abort(400, "No columns given.");
63-
64-
$structure = array();
65-
$structure['id'] = "increments";
66-
67-
foreach ($request->columns as $column => $type) {
68-
69-
if ($column != "id") // Don´t use these
70-
if ($type=="text" || $type=="integer") // Enabled types
71-
$structure[$column] = $type;
72-
73-
}
74-
75-
return TableController::tableStore($tableName,$structure);
56+
return TableController::tableStore();
7657
}
7758

78-
public function rowStore_($tableName, Request $request)
59+
public function rowStore_($tableName)
7960
{
8061
if (!TableController::tableExists($tableName))
8162
return abort(404, "Table '$tableName' don´t exist.");
@@ -88,20 +69,19 @@ public function tableUpdate_($tableName, Request $request)
8869
if (!$request->has('columns'))
8970
return abort(400, "No columns given.");
9071

91-
$structure = array();
92-
$structure[] = array('originalName' => 'id', 'originalType' => "increments");
93-
94-
$columns = $request->input('columns');
72+
$tableinfoNew = [ 'tablename' => $request->input('tablename'), 'originalTablename' => $tableName ];
9573

96-
foreach ($columns as $columnNew){
74+
$tableinfoNew['columns'][] = array('originalName' => 'id', 'originalType' => "increments");
75+
76+
foreach ($request->input('columns') as $columnNew){
9777

9878
if ($columnNew['originalName'] != "id") // Don´t use these
9979
if (strtoupper($columnNew['originalType'])=="TEXT" || strtoupper($columnNew['originalType'])=="INTEGER") // Enabled types
100-
$structure[] = $columnNew;
80+
$tableinfoNew['columns'][] = $columnNew;
10181

10282
}
10383

104-
return TableController::tableUpdate($tableName,$structure);
84+
return TableController::tableUpdate($tableName,$tableinfoNew);
10585
}
10686

10787
public function rowUpdate_($tableName, $id, Request $request)

app/Http/Controllers/TableController.php

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static function tableExists($tableName)
3030
*/
3131
public static function tableInfo($tableName)
3232
{
33-
$result = [ 'tablename' => $tableName ];
33+
$result = [ 'tablename' => $tableName, 'originalTablename' => $tableName ];
3434

3535
$tableInfo = DB::select( "PRAGMA table_info('$tableName')" );
3636

@@ -95,30 +95,17 @@ public static function tableGet($tableName)
9595
/**
9696
* Create table.
9797
*
98-
* @param string $tableName
99-
* @param object $structure
10098
* @return array
10199
*/
102-
public static function tableStore($tableName,$structure)
100+
public static function tableStore()
103101
{
104-
if (TableController::tableExists($tableName))
105-
return TableController::tableUpdate($tableName,$structure);
106-
107-
Schema::create($tableName, function($t) use ($structure){
108-
109-
foreach ($structure as $column => $type){
110-
111-
if ($column=="id")
112-
$t->$type($column);
113-
114-
else
115-
$t->$type($column)->nullable();
116-
117-
}
102+
Schema::create('_temp_', function($table){
103+
104+
$table->increments('id');
118105

119106
});
120107

121-
return TableController::tableGet($tableName);
108+
return TableController::tableGet('_temp_');
122109
}
123110

124111
/**
@@ -128,15 +115,20 @@ public static function tableStore($tableName,$structure)
128115
* @param object $structure
129116
* @return array
130117
*/
131-
public static function tableUpdate($tableName,$structureNew)
118+
public static function tableUpdate($tableName,$tableInfoNew)
132119
{
133120
if (!TableController::tableExists($tableName))
134-
return TableController::tableStore($tableName,$structureNew);
121+
return TableController::tableStore();
135122

136-
$tableInfo = TableController::tableInfo($tableName);
123+
// Get Current table
124+
$tableInfoOld = TableController::tableInfo($tableName);
137125

138-
$structureOld = $tableInfo['columns'];
126+
$structureOld = $tableInfoOld['columns'];
127+
128+
// Get New table
129+
$structureNew = $tableInfoNew['columns'];
139130

131+
140132
// Check for new columns
141133
$addThese = array();
142134

@@ -208,8 +200,12 @@ public static function tableUpdate($tableName,$structureNew)
208200

209201
}
210202

203+
// Check for rename table
204+
if ($tableInfoNew['originalTablename'] != $tableInfoNew['tablename'])
205+
Schema::rename($tableInfoNew['originalTablename'], $tableInfoNew['tablename']);
206+
211207

212-
return TableController::tableGet($tableName);
208+
return TableController::tableGet($tableInfoNew['tablename']);
213209
}
214210

215211
}

0 commit comments

Comments
 (0)