This repository has been archived by the owner on Sep 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Triton.neptune
229 lines (212 loc) · 8.26 KB
/
Triton.neptune
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
//#!/usr/bin/php
//#!/usr/bin/env php
require 'triton/TritonConsoleInterface.php';
require 'triton/triton.php';
require 'libs/connect/connect.php';
use Libs\Connect\Connect;
function readlineTriton($prompt = null)
{
if($prompt)
{
echo $prompt;
}
$fp = fopen('php://stdin','r');
$line = rtrim(fgets($fp, 1024));
return $line;
}
if($argv[1] == 'create:tableFile')
{
$database = ucfirst($argv[2]);
$table = ucfirst($argv[3]);
if(!file_exists('database/databases/' . $database . '/' . $table . 'Table.php'))
{
$tableFileContent = '<?php
namespace Database\\Databases\\' . $database . ';
class '.$table.'Table
{
public function table($table)
{
/*
* This is your Triton codes! After the Triton codes you wrote here,
* you will only have one line of console code, then the codes in the database!
* Detailed usage: http://neptunefw.org/doc/orm/triton-orm
*/
return $table;
}
}';
file_put_contents('database/databases/' . $database . '/' . $table . 'Table.php', $tableFileContent);
if(file_exists('database/databases/' . $database . '/' . $table . 'Table.php'))
{
if(file_get_contents('database/databases/' . $database . '/' . $table . 'Table.php', $tableFileContent) == $tableFileContent)
{
echo 'database/databases/' . $database . '/' . $table . 'Table.php Table file created.';
}
else
{
echo 'Table file created. However, a problem was encountered.';
}
}
else
{
echo 'Table file could not be created.';
}
}
else
{
echo 'database/databases/' . $database . '/' . $table . 'Table.php This table file already exists. Action canceled.';
}
}
else if($argv[1] == 'create:table')
{
$database = ucfirst($argv[2]);
$table = ucfirst($argv[3]);
$connectionSettings = file_get_contents('database/databases/' . $database . '/connection.ntconfig');
eval('$connectionSettings = ' . $connectionSettings);
if(file_exists('database/databases/' . $database . '/lock.nt'))
{
unlink('database/databases/' . $database . '/lock.nt');
}
$tConsole = new TritonConsoleInterface($database, $connectionSettings["host"], $connectionSettings["user"], $connectionSettings['pass']);
$value = __DIR__ . '/Database/Databases/' . $database . '/' . $table . 'Table.php';
$tConsole->createTable($value, $database);
}
else if($argv[1] == 'rename:table')
{
$database = $argv[2];
$tableOldName = $argv[3];
$tableNewName = $argv[4];
rename('database/databases/' . $database . '/' . ucfirst($tableOldName) . 'Table.php', 'database/databases/' . $database . '/' . ucfirst($tableNewName) . 'Table.php');
$tableFileNewContent = preg_replace('(class ' . ucfirst($tableOldName) . 'Table)', 'class ' . ucfirst($tableNewName) . 'Table', file_get_contents('database/databases/'. $database . '/' . ucfirst($tableNewName) . 'Table.php'));
file_put_contents('database/databases/'. $database . '/' . ucfirst($tableNewName) . 'Table.php', $tableFileNewContent);
$connectionSettings = file_get_contents('database/databases/'. $database . '/connection.ntconfig');
eval('$connectionSettings = ' . $connectionSettings);
try
{
$pdoConnection = new PDO('mysql:host=' . $connectionSettings['host'] . ';dbname=' . $database, $connectionSettings['user'], $connectionSettings['pass']);
}
catch (PDOException $e)
{
die('No connection to the database! We think the problem is yours. If it\'s us, please contact us! More information: ' . $e->getMessage());
}
$pdoConnection->exec("RENAME TABLE ".$tableOldName." TO ".$tableNewName." ");
}
else if($argv[1] == 'rename:tableColumn')
{
$database = ucfirst($argv[2]);
$table = ucfirst($argv[3]);
$columnOldName = $argv[4];
$columnNewName = $argv[5];
$connectionSettings = file_get_contents('database/databases/' . $database . '/connection.ntconfig');
eval('$connectionSettings = ' . $connectionSettings);
try
{
$pdoConnection = new PDO('mysql:host=' . $connectionSettings['host'] . ';dbname=' . $database, $connectionSettings['user'], $connectionSettings['pass']);
}
catch (PDOException $e)
{
die('No connection to the database! We think the problem is yours. If it\'s us, please contact us! More information: ' . $e->getMessage());
}
$showColumns = $pdoConnection->prepare('SHOW COLUMNS FROM ' . $table);
$showColumns->execute();
$columns = $showColumns->fetchAll();
foreach ($columns as $column)
{
if(array_search($columnOldName, $column))
{
$query = $pdoConnection->exec('ALTER TABLE ' . $table . ' CHANGE ' . $columnOldName . ' ' . $columnNewName . ' ' . $column['Type'] . ' ' . $column['Extra']);
$tConsole = new TritonConsoleInterface($database, $connectionSettings['host'], $connectionSettings['user'], $connectionSettings['pass']);
$tConsole->createTableFile($table, $database);
echo 'Created files.';
}
}
}
else if($argv[1] == 'delete:tableColumn')
{
$database = ucfirst($argv[2]);
$table = ucfirst($argv[3]);
$columnName = $argv[4];
$connectionSettings = file_get_contents('database/databases/' . $database . '/connection.ntconfig');
eval('$connectionSettings = ' .$connectionSettings);
try {
$pdoConnection = new PDO("mysql:host=" . $connectionSettings["host"] . ";dbname=" . $database, $connectionSettings['user'], $connectionSettings['pass']);
}
catch (PDOException $e)
{
die('No connection to the database! We think the problem is yours. If it\'s us, please contact us! More information: ' . $e->getMessage());
}
$query = $pdoConnection->exec('ALTER TABLE ' . $table . ' DROP COLUMN ' . $columnName . ' ');
$tConsole = new TritonConsoleInterface($database, $connectionSettings['host'], $connectionSettings['user'], $connectionSettings['pass']);
$tConsole->createTableFile($table, $database);
echo 'Created files.';
}
else if($argv[1] == 'create:tables')
{
$database = $argv[2];
$connectionSettings = file_get_contents('database/databases/' . $database . '/connection.ntconfig');
eval('$connectionSettings = ' .$connectionSettings);
try
{
$pdoConnection = new PDO("mysql:host=" . $connectionSettings["host"] . ";dbname=" . $database, $connectionSettings['user'], $connectionSettings['pass']);
}
catch (PDOException $e)
{
die('No connection to the database! We think the problem is yours. If it\'s us, please contact us! More information: ' . $e->getMessage());
}
$tConsole = new TritonConsoleInterface($database, $connectionSettings['host'], $connectionSettings['user'], $connectionSettings['pass']);
$tConsole->createTableFiles($database);
}
else if($argv[1] == 'delete:table')
{
$database = ucfirst($argv[2]);
$table = ucfirst($argv[3]);
$line = readlineTriton('Do yo want delete table? (Y/N)');
if($line == 'Y')
{
unlink('database/databases/' . $database . '/' . $table . 'Table.php');
$connectionSettings = file_get_contents('database/databases/' . $database . '/connection.ntconfig');
eval('$connectionSettings = ' . $connectionSettings);
try
{
$pdoConnection = new PDO("mysql:host=" . $connectionSettings["host"] . ";dbname=" . $database, $connectionSettings['user'], $connectionSettings['pass']);
}
catch (PDOException $e)
{
die('No connection to the database! We think the problem is yours. If it\'s us, please contact us! More information: ' . $e->getMessage());
}
$pdoConnection->exec("DROP TABLE ". $table );
}
else if($line == 'N')
{
echo 'Process canceled.';
}
}
else if($argv[1] == 'clean:table')
{
$database = ucfirst($argv[2]);
$table = ucfirst($argv[3]);
$line = readlineTriton('Are you sure you want to empty the chart? (Y/N)');
if($line == 'Y')
{
$connectionSettings = file_get_contents('database/databases/' . $database . '/connection.ntconfig');
eval('$connectionSettings = ' . $connectionSettings);
try
{
$pdoConnection = new PDO('mysql:host=' . $connectionSettings['host'] . ';dbname=' . $database, $connectionSettings['user'], $connectionSettings['pass']);
}
catch (PDOException $e)
{
die('No connection to the database! We think the problem is yours. If it\'s us, please contact us! More information: ' . $e->getMessage());
}
$pdoConnection->exec('TRUNCATE ' . $table );
echo 'The query was run.';
}
else if($line == 'N')
{
echo 'Process canceled.';
}
}
else
{
echo '"'. $argv[1] .'" isn\'t recognized.';
}