-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
DatabaseTable.php
434 lines (377 loc) · 12.2 KB
/
DatabaseTable.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
<?php
/**
* Copyright Alvin Alexander, http://alvinalexander.com, 2011-2014.
* All Rights Reserved.
*
* This program is distributed free of charge under the terms and
* conditions of the GNU GPL v3. See our LICENSE file, or
* http://www.gnu.org/licenses/gpl.html for more information.
*
* The MDB2 and Smarty libraries are included under their own
* licensing terms.
*/
# REQUIREMENT: this file is required for the mapping of database field
# types to java field types:
# import 'CrudDatabaseTableFieldTypes.inc';
# TODO might need this syntax for the include statement below:
# require_once(dirname(__FILE__) . "/file.php");
class DatabaseTable
{
# the raw (unmodified) name of the database table in the database
private $raw_table_name;
# the possibly-modified name of the database table that you get after
# the prefix has been removed. ex: 'myapp_projects' becomes 'projects'.
private $clean_table_name;
# any prefix you have before your table names, like 'myapp_'
private $tablename_prefix;
# the name of the database table as converted to a java classname
# (camelcase rules for a class)
private $camelcase_table_name;
# the names of the database table fields as they appear in the database
private $raw_field_names = array();
# the name of the database table fields as they convert to camelcase names
private $camelcase_field_names = array();
# the database field types
private $db_field_types = array();
# the field types as they translate to java (and now scala)
private $java_field_types = array();
private $scala_field_types = array();
private $play_field_types = array();
private $play_json_field_types = array();
# an array that tells whether the desired fields are required (not null), or not
private $field_is_reqd = array();
function set_tablename_prefix($tablename_prefix)
{
$this->tablename_prefix = $tablename_prefix;
}
function get_raw_table_name()
{
return $this->raw_table_name;
}
function set_raw_table_name($name)
{
$this->raw_table_name = $name;
}
function get_clean_table_name()
{
$pos = strpos($this->raw_table_name, $this->tablename_prefix);
if ($pos !== false)
{
# the prefix ('myapp_') was found in the raw table name ('myapp_projects')
$len = strlen($this->tablename_prefix);
$this->clean_table_name = substr($this->raw_table_name, $len);
}
return $this->clean_table_name;
}
/**
* returns the clean (prefix-removed), singular ('ies' -> 'y') table name.
*/
function get_clean_table_name_singular()
{
#------------------------------------------------------------------------
# TODO - PROBABLY WANT TO ADD A SWITCH SOMEWHERE (APP.CFG) TO LET THE
# USER CONTROL THIS BEHAVIOR.
#------------------------------------------------------------------------
# TODO - MAY WANT TO TEST THE STRING BEFORE JUST DOING THESE REPLACEMENTS
#------------------------------------------------------------------------
# convert strings like 'entities' to 'entity'
$string = $this->clean_table_name;
$pattern = '/ies$/';
$replacement = 'y';
# convert strings like 'processes' to 'process'
$string = preg_replace($pattern, $replacement, $string);
$pattern = '/es$/';
$replacement = '';
$string = preg_replace($pattern, $replacement, $string);
# remove trailing 's' ... in most cases
$pattern = '/([bcdfghjklmnpqrstvwxyz])s$/';
$replacement = '$1';
$string = preg_replace($pattern, $replacement, $string);
return $string;
}
# set the raw database table field names array
function set_raw_field_names($field_names)
{
$this->raw_field_names = $field_names;
}
# set the raw database table field types
function set_db_field_types($field_types)
{
$this->db_field_types = $field_types;
}
function get_db_field_types()
{
return $this->db_field_types;
}
# setter/getter for '$field_is_reqd'
function set_field_is_reqd($field_is_reqd)
{
$this->field_is_reqd = $field_is_reqd;
}
function get_field_is_reqd()
{
return $this->field_is_reqd;
}
# returns an array of java field types that corresponds to the database
# table field types. a database field of 'integer' becomes 'int',
# a database field of 'text' becomes 'String', etc.
#
# this function requires the '$field_types_map' array to be set; it
# provides the mapping from database field types to java field types.
function get_java_field_types()
{
include 'CrudDatabaseTableFieldTypes.inc';
$count = 0;
foreach ($this->db_field_types as $field_type)
{
#echo "type = " . $field_type;
# this should be a simple map lookup (as long as all the key/value
# pairs are defined).
$java_field_type = $field_types_map[$field_type];
if (isset($java_field_type))
{
$this->java_field_types[$count] = $java_field_type;
}
else
{
# couldn't find a corresponding value in the map
$this->java_field_types[$count] = 'UNKNOWN';
}
$count++;
}
return $this->java_field_types;
}
# provides a mapping from database field types to scala field types
# TODO refactor the code; this is almost the same as the previous function.
# TODO maybe this function should be more like get_play_field_types, which does the work
# of determining whether a field should be an Option or not; i currently handle this
# in the template code.
function get_scala_field_types()
{
include 'CrudDatabaseTableFieldTypes.inc';
$count = 0;
foreach ($this->db_field_types as $field_type)
{
$scala_field_type = $scala_field_types_map[$field_type];
#echo "SCALA FIELD TYPE: $scala_field_type\n";
if (isset($scala_field_type)) {
$this->scala_field_type[$count] = $scala_field_type;
}
else {
# couldn't find a corresponding value in the map
$this->scala_field_type[$count] = 'UNKNOWN';
}
// kludge for the 'id' field
if ($this->raw_field_names[$count] == 'id') $this->scala_field_type[$count] = 'Long';
$count++;
}
return $this->scala_field_type;
}
function get_play_json_field_types()
{
include 'CrudDatabaseTableFieldTypes.inc';
$count = 0;
foreach ($this->db_field_types as $field_type)
{
$play_json_field_type = $play_json_field_types_map[$field_type];
if (isset($play_json_field_type)) {
$this->play_json_field_type[$count] = $play_json_field_type;
}
else {
# couldn't find a corresponding value in the map
$this->play_json_field_type[$count] = 'UNKNOWN';
}
// kludge for the 'id' field
if ($this->raw_field_names[$count] == 'id') $this->play_json_field_type[$count] = 'JsNumber';
$count++;
}
return $this->play_json_field_type;
}
#
# return an array of field types for the Scala Play Framework.
#
function get_play_field_types()
{
include 'CrudDatabaseTableFieldTypes.inc';
$count = 0;
foreach ($this->db_field_types as $field_type)
{
if ($this->field_is_reqd[$count] == true) {
$play_field_type = $play_reqd_field_types_map[$field_type];
} else {
$play_field_type = $play_optional_field_types_map[$field_type];
}
if (isset($play_field_type)) {
$this->play_field_type[$count] = $play_field_type;
} else {
$this->play_field_type[$count] = 'UNKNOWN';
}
// kludge for the 'id' field
if ($this->raw_field_names[$count] == 'id') $this->play_field_type[$count] = 'longNumber';
$count++;
}
return $this->play_field_type;
}
# convert a plural name to a singular name, as in turning
# 'users' into 'user'. useful for converting database table
# names into java class names.
# TODO - also need to handle 'es' at the end of a table name.
private function turn_plural_into_singular($string)
{
# get the last character from the string
$l = strlen($string);
$start = $l -1;
$last_char = substr($string, $start, 1);
# if the last char is not an 's', just return the original string
if ($last_char != 's') return $string;
# get the last two characters; if they are 'es', remove those and return
$es_check = substr($string, $start-1, 2);
if ($es_check == 'es')
{
return substr($string, 0, $l-2);
}
# otherwise, remove the last character ('s'), and return
return substr($string, 0, $l-1);
}
# convert a database table field name to a java class name
# (foo_bar_baz -> FooBarBaz)
function get_camelcase_table_name()
{
$table_name = $this->turn_plural_into_singular($this->raw_table_name);
$arr = explode('_', $table_name);
$l = count($arr);
for ($i=0; $i<$l; $i++)
{
$arr[$i] = ucwords($arr[$i]);
}
return implode($arr);
}
# TODO - this is currently returning "users", and needs to return "user"
# convert a database table field name to a java object name,
# i.e., a java instance variable name
# (foo_bar_baz -> fooBarBaz)
function get_java_object_name()
{
$table_name = $this->turn_plural_into_singular($this->raw_table_name);
$arr = explode('_', $table_name);
$l = count($arr);
for ($i=0; $i<$l; $i++)
{
if ($i != 0)
{
$arr[$i] = ucwords($arr[$i]);
}
}
return implode($arr);
}
# returns the array of camelcase field names, i.e., the database table
# field names converted to java field naming standards
function get_camelcase_field_names()
{
$count = 0;
foreach ($this->raw_field_names as $raw_field_name)
{
# create an array of words from the raw field name
$words = explode('_', $raw_field_name);
$length = count($words);
for ($i=0; $i<$length; $i++)
{
if ($i != 0) $words[$i] = ucwords($words[$i]);
}
$this->camelcase_field_names[$count] = implode($words);
$count++;
}
return $this->camelcase_field_names;
}
# returns the table fields as a csv list; this is very useful for Dao
# SQL INSERT statements.
# example string for four fields: "id,foo,bar,baz"
function get_fields_as_insert_stmt_csv_list()
{
$s = '';
$count = 0;
foreach ($this->raw_field_names as $raw_field_name)
{
$l = count($this->raw_field_names);
if ($count < $l-1)
{
$s = $s . $raw_field_name . ',';
}
else
{
$s = $s . $raw_field_name;
}
$count++;
}
return $s;
}
# returns a csv-string of '?' corresponding to the database table fields;
# very useful for Dao SQL INSERT statements.
# example string for four fields: "?,?,?,?"
function get_prep_stmt_insert_csv_string()
{
$s = '';
$count = 0;
foreach ($this->raw_field_names as $raw_field_name)
{
$l = count($this->raw_field_names);
if ($count < $l-1)
{
$s = $s . '?,';
}
else
{
$s = $s . '?';
}
$count++;
}
return $s;
}
# returns a string like "id=?,foo=?,bar=?,baz=?", which is very useful for
# SQL UPDATE statements, using the Java PreparedStatement syntax.
function get_fields_as_update_stmt_csv_list()
{
$s = '';
$count = 0;
foreach ($this->raw_field_names as $raw_field_name)
{
$l = count($this->raw_field_names);
if ($count < $l-1)
{
$s = $s . $raw_field_name . '=?,';
}
else
{
$s = $s . $raw_field_name . '=?';
}
$count++;
}
return $s;
}
}
# TODO - move all of this to some unit tests
#$dt = new DatabaseTable();
# --- TEST THE TABLE NAME ---
# setting the raw table name should create camelcase_table_name
#$dt->set_raw_table_name('foo_bar_baz');
#print $dt->get_raw_table_name() . "\n";
#print $dt->get_camelcase_table_name() . "\n";
# --- TEST THE TABLE FIELD NAMES ---
#$t_fields = array('a_foo', 'b_bar');
#$dt->set_raw_field_names($t_fields);
#$fields = $dt->get_camelcase_field_names();
#foreach ($fields as $f)
#{
# print "$f\n";
#}
# --- TEST THE TABLE FIELD TYPES ---
# setting the db field types should create the java field types
#$types = array('integer', 'text', 'poop');
#$dt->set_db_field_types($types);
#$j_types = $dt->get_java_field_types();
#foreach ($j_types as $jt)
#{
# print "$jt\n";
#}
#print_r($dt);
?>