-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnodeCode.php
370 lines (292 loc) · 13.6 KB
/
nodeCode.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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<?php
//has the template for generating node files
//TODO note that sequalize is smart enough to switch between updatedAt and updated_at
//while laravel is not...
//which means that you need to use the underscored: true; variable on our sequalize
//objects for maximum parsimony and happiness
class nodeCode
{
public $index_text = "var ModelCache = {};
var columnToObjectMap = {};
//Javascript does not allow for default arguments into functions
//this handles the problem for functions
//with unpredicatable arguments
function has_default(arg, def) {
return (typeof arg == 'undefined' ? def : arg);
}
//select_name is now required!!
function buildCache(select_name){
var ORM;
var object_type;
var selected_col;
var data_cache = [];
//add code for auto-complete detection engine...
//we use the column name to figure out which of the ORMs
//we are going to use... this makes rebuilding trivial!!
ORM = columnToObjectMap[select_name];
object_type = ORM.name;
//if we passed in a select_name, use it, otherwise use the default
ORM.findAll().success(function(things) {
__.each(things,function(a_thing){
if(selected_col === undefined){
//the default is just the id...
selected_col = 'id';
selected = a_thing.attributes;
__.each(selected,function(key,value){
//choose which column to use as the select
//value for connecting objects...
//any field with 'name' at the end... or not... you never know..
//then choose the last 'name' in the list...
if(key.indexOf(\"name\") !== -1){
selected_col = key;
}
});
//now we should have the last colum with
//'name' in it... but is that what we really want?
//sometimes we want to be able to specify
//so we look around for 'select_name'...
__.each(selected,function(key,value){
//choose which column to use as the select
//value for connecting objects...
//any field with 'name' at the end... or not... you never know..
//then choose the last 'name' in the list...
if(key.indexOf(\"select_name\") !== -1){
selected_col = key;
}
});
}
my_value = a_thing[selected_col];
data_cache.push({
id: a_thing.id,
value: my_value
});
});
ModelCache[select_name] = data_cache;
//console.log(data_cache);
});
}
exports.buildCache = buildCache;
//Begin autogenerated Objects...
";
public $dir_name = 'node/sequelize/';
public $index_file_name = 'index.js';
public $orm_dir_name = 'node/orm/';
public $ORM_files = [];
public $ORM_file_content = [];
public $ORM_base_file_content = [];
public $ORM_process_file_content = [];
public $ORM_file_name = [];
public $ORM_base_file_name = [];
public $ORM_process_file_name = [];
public $overwrite = false;
public $debug = true;
public $c = ''; //comma character starts life as an empty string...
public $other_tables = []; //where we store our table names..
public $links_js = '';
public $map = [];
public $relations = [];
//var $namespace = 'namespace nod\orm;';
//var $namespace = 'namespace App/Models;';
public $namespace = '';
public function generate($table_data)
{
$object_name = $table_data['object_name'];
$table_name = $table_data['table_name'];
$database = $table_data['database'];
$object_label = $table_data['object_label'];
$has_many = $table_data['has_many'];
$belongs_to = $table_data['belongs_to'];
$rows_data = $table_data['table_row_results']; //contained the results of the INFORMATION_SCHEMA query...
$file_name = "$object_name.js";
$full_file = "$this->dir_name/$file_name";
$this->ORM_file_name[$object_name] = $full_file;
$base_file_name = "$object_name".'Base.js';
$full_base_file = "$this->dir_name/$base_file_name";
$this->ORM_base_file_name[$object_name] = $full_base_file;
$process_file_name = "$object_name.log";
$full_process_file = "$this->dir_name/$process_file_name";
$this->ORM_process_file_name[$object_name] = $full_process_file;
$this->each_table_start($table_data);
//this is just for logging the process...
$this->ORM_process_file_content[$object_name] = "Object $object_name has_many:\n";
$this->ORM_process_file_content[$object_name] .= var_export($has_many, true);
$this->ORM_process_file_content[$object_name] .= "Object $object_name belongs_to:\n";
$this->ORM_process_file_content[$object_name] .= var_export($belongs_to, true);
foreach ($table_data['table_cols'] as $col) {
//TODO here we should add row information that this generation process needs..
//for instance..
$this_rows_data = $rows_data[$col];
$this->each_row_start([
'object_name' => $object_name,
'table_name' => $table_name,
'database' => $database,
'col_label' => $object_label,
'col_name' => $col,
'row' => $this_rows_data,
]);
}
$this->write_file($object_name);
}
public function each_table_start($table_data)
{
$object_name = $table_data['object_name'];
$object_label = $table_data['object_label'];
$database = $table_data['database'];
$this_table = $table_data['table_name'];
//start headers...
$this->ORM_files[$object_name] = "//Generated by buildORM from the $database:$this_table
module.exports = function(sequelize, DataTypes) {
return sequelize.define('$object_name', {
";
$this_file_name = "$object_name.orm.js";
$project_file = "$this->orm_dir_name/$this_file_name";
$this->index_text .= "\n//importing $object_name from table $database:$this_table\n";
$this->index_text .= "var $object_name = sequelize.import(__dirname + '/$this_file_name');\n";
$this->index_text .= "exports.$object_name = $object_name;\n";
}
public function each_row_start($row_data)
{
$object_name = $row_data['object_name'];
$col_name = $row_data['col_name'];
$col_label = $row_data['col_label'];
$database = $row_data['database'];
$this_table = $row_data['table_name'];
$row = $row_data['row'];
$dust_html = '';
//I cannot remember what these mean??
$s = 'start';
$m = 'middle';
$e = 'end';
$col_explode = explode('_', $col_name);
$c = $this->c;
$foreign_key = false; //to start with we assume there are no relationships for this key...
if (count($col_explode) > 1) {
$end_col_name = array_pop($col_explode);
$other_table = array_pop($col_explode);
if (strcmp('id', strtolower($end_col_name)) == 0) {
//looks like an id..
$foreign_key = true;
$this->other_tables[$other_table] = $object_name; //make sure they are all there...
$this->index_text .= "// $col_name looks like an association\n";
$this->links_js .= "
//$other_table"."s.hasOne(
$other_table"."s.hasMany(
$object_name,
{
// as: '$col_name',
foreignKey: '$col_name'
}
);
columnToObjectMap['$col_name'] = $other_table"."s;
buildCache('$col_name');
";
}//dealt with id
if (strcmp('name', strtolower($end_col_name)) == 0) {
//then this is the magic name coolness..
//this is not the level to deal with this...
}
}//dealt with all special column meta types..
if ($this->debug) {
echo "INFO: working on $database:$this_table:$col_name\n";
}
$fixed = false;
if (strcmp('varchar', $row['DATA_TYPE']) == 0) {
$this->ORM_files[$object_name] .= "$c $col_name: { type: Sequelize.STRING }";
$length = $row['CHARACTER_MAXIMUM_LENGTH'];
if ($this->debug) {
if ($length != 255) {
echo "WARN: The length for varchar $database:$this_table:$col_name is not 255, but $length";
if ($length > 255) {
echo " Consider using a TEXT for this field\n";
} else {
echo " Consider increasing to 255\n";
}
}
}//end debug
$fixed = true;
}//end STRING logic...
if (strcmp('text', $row['DATA_TYPE']) == 0) {
$$this->ORM_files[$object_name] .= "$c $col_name: { type: Sequelize.TEXT }";
$fixed = true;
}
if (strcmp('int', $row['DATA_TYPE']) == 0 ||
strcmp('bigint', $row['DATA_TYPE']) == 0) {
$extra_stuff = '';
if (strcmp('PRI', $row['COLUMN_KEY']) == 0) {
$extra_stuff .= ' ,primaryKey: true ';
}
$this->ORM_files[$object_name] .= "$c $col_name: { type: Sequelize.INTEGER $extra_stuff}";
$fixed = true;
if (strcmp(strtolower($col_name), 'id') == 0) {
$foreign_key = true; //well, not technically
//but it keeps us from editing it directly...
$dust_html .= "
<input type='hidden' id='$col_name' name='$col_name' value='{".$col_name."}'>
";
}
if (!$foreign_key) {
//then this is an int that just wants to be a number
$dust_html .= "
$s $m<label for='$col_name'> $col_label</label>
$m<input type='number' step='1' id='$col_name' name='$col_name' value='{".$col_name."}'>
$e</li>
";
}
}
if (strcmp('datetime', $row['DATA_TYPE']) == 0) {
$this->ORM_files[$object_name] .= "$c $col_name: { type: Sequelize.DATE }";
$fixed = true;
$dust_html .= "
$s $m<label for='$col_name'> $col_label</label>
$m<input type='date' id='$col_name' name='$col_name' value='{".$col_name."}'>
$e</li>
";
}
if (strcmp('tinyint', $row['DATA_TYPE']) == 0) {
$this->ORM_files[$object_name] .= "$c $col_name: { type: Sequelize.BOOLEAN }";
$fixed = true;
if ($this->debug) {
if (strcmp('tinyint(1)', $row['COLUMN_TYPE']) != 0) {
echo "WARN: The length for $database:$this_table:$col_name is other than 1... might want to change that... tinyints are always boolean in this world\n";
}
}
$dust_html .= "
$s $m<label for='$col_name'> $col_label</label>
$m<input type='checkbox' step='1' id='$col_name' name='$col_name' value='{".$col_name."}'>
$e</li>
";
}
if (strcmp('float', $row['DATA_TYPE']) == 0) {
$this->ORM_files[$object_name] .= "$c $col_name: { type: Sequelize.FLOAT }";
$fixed = true;
$dust_html .= "
$s $m<label for='$col_name'> $col_label</label>
$m<input type='number' step='1' id='$col_name' name='$col_name' value='{".$col_name."}'>
$e
";
}
if (!$fixed) {
$data_type = $row['DATA_TYPE'];
echo "ERROR: for $database:$this_table:$col_name we cannot work with $data_type\n";
//$orm_index_js .= "//I did not understand what to do with $col_name\n";
}
$this->c = ",\n";
}
//end function each_row_start
public function write_file($object_name)
{
//always write the auto-generated stuff..
file_put_contents(
$this->ORM_base_file_name[$object_name],
$this->ORM_files[$object_name]
);
//always write the generate log for this file...
file_put_contents($this->ORM_process_file_name[$object_name], $this->ORM_process_file_content[$object_name]);
if (!file_exists($this->ORM_file_name[$object_name]) || $this->overwrite) {
//but only write the child class if it does not already exist...
//unless we are overwriting everything...
//so that user customizations are remembered.
file_put_contents($this->ORM_file_name[$object_name], $this->ORM_file_content[$object_name]);
}
}
}//end class