KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdbc > sql > DefaultJdbcNameGenerator


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdbc.sql;
13
14 import java.util.HashMap JavaDoc;
15 import java.util.HashSet JavaDoc;
16
17 import com.versant.core.common.BindingSupportImpl;
18
19 /**
20  * The standard name generator. This tries to make sure that generated names
21  * will be valid for all databases. This ensures that applications deployed
22  * against different databases maintain the same schema.
23  */

24 public class DefaultJdbcNameGenerator implements JdbcNameGenerator {
25
26     protected String JavaDoc databaseType;
27
28     protected int maxTableNameLength = 18;
29     protected int maxColumnNameLength = 18;
30     protected int maxConstraintNameLength = 18;
31     protected int maxIndexNameLength = 18;
32     protected String JavaDoc pkSuffix = "_id";
33     protected String JavaDoc pkConstraintPrefix = "pk_";
34     protected String JavaDoc refConstraintPrefix = "ref_";
35     protected String JavaDoc wordBreak = "_";
36     protected String JavaDoc sequenceColumnName = "seq";
37     protected String JavaDoc valueColumnName = "val";
38     protected String JavaDoc keyColumnName = "mapkey";
39
40     protected String JavaDoc classIdColumnName = "jdo_class";
41
42  
43     protected String JavaDoc polyRefClassIdSuffix = "_class";
44     protected String JavaDoc indexNamePrefix = "idx_";
45
46     private boolean nameCanStartWithUnderscore = true;
47
48     protected HashMap JavaDoc reservedWords = new HashMap JavaDoc();
49
50     /**
51      * This maps a table name to a HashSet of its column names.
52      */

53     protected HashMap JavaDoc tableMap = new HashMap JavaDoc();
54     /**
55      * All the constraint names we know about.
56      */

57     protected HashSet JavaDoc constraintNames = new HashSet JavaDoc();
58     /**
59      * All the index names we know about.
60      */

61     protected HashSet JavaDoc indexNames = new HashSet JavaDoc();
62
63     public DefaultJdbcNameGenerator() {
64         populateReservedWords(reservedWords);
65     }
66
67     /**
68      * This is called immediately after the name generator has been constructed
69      * to indicate which database is in use.
70      */

71     public void setDatabaseType(String JavaDoc db) {
72         databaseType = db;
73         if (db.equals("daffodil")) {
74             reservedWords.put("connection", "connectn");
75             reservedWords.put("recursive", "recursve");
76             reservedWords.put("timestamp", "timestmp");
77             reservedWords.put("interval", "intervl");
78             reservedWords.put("email", "eml");
79             reservedWords.put("action", "actn");
80         } else if (db.equals("mckoi")) {
81             reservedWords.put("sequence", "seqnce");
82         } else if (db.equals("mssql")) {
83             reservedWords.put("system_user", "sys_user");
84             reservedWords.put("identity", "ident");
85         } else if (db.equals("sapdb")) {
86             reservedWords.put("timestamp", "timestmp");
87         } else if (db.equals("firebird") || db.equals("interbase")) {
88             reservedWords.put("position", "positn");
89             reservedWords.put("admin", "admn");
90             reservedWords.put("timestamp", "timestmp");
91             reservedWords.put("action", "actn");
92         } else if (db.equals("informix") || db.equals("informixse")) {
93             reservedWords.put("interval", "intervl");
94         } else if (db.equals("mysql")) {
95             reservedWords.put("interval", "intervl");
96             reservedWords.put("change", "chnge");
97             reservedWords.put("keys", "kys");
98             reservedWords.put("xor", "xr");
99         } else if (db.equals("cache")) {
100             reservedWords.put("connection", "connectn");
101             reservedWords.put("timestamp", "timestmp");
102             reservedWords.put("interval", "intrval");
103             reservedWords.put("action", "actn");
104         } else if (db.equals("hypersonic")) {
105             reservedWords.put("position", "pos");
106         } else if (db.equals("oracle")) {
107             nameCanStartWithUnderscore = false;
108         }
109     }
110
111     /**
112      * Populate rw with all reserved words and their replacements.
113      * Any identifiers that match a reserved word will be replaced.
114      * All keys must be lower case.
115      */

116     protected void populateReservedWords(HashMap JavaDoc rw) {
117         rw.put("abort", "abrt");
118         rw.put("abs", "ab");
119         rw.put("absolute", "abslute");
120         rw.put("accept", "accpt");
121         rw.put("acces", "accs");
122         rw.put("access", "accss");
123         rw.put("activate", "actvate");
124         rw.put("add", "ad");
125         rw.put("addform", "addfrm");
126         rw.put("after", "aftr");
127         rw.put("all", "al");
128         rw.put("alter", "altr");
129         rw.put("and", "nd");
130         rw.put("andfilename", "andflename");
131         rw.put("any", "ny");
132         rw.put("anyfinish", "anyfnish");
133         rw.put("append", "appnd");
134         rw.put("archive", "archve");
135         rw.put("array", "arry");
136         rw.put("as", "asx");
137         rw.put("asc", "sc");
138         rw.put("ascending", "ascnding");
139         rw.put("ascii", "asci");
140         rw.put("assert", "assrt");
141         rw.put("assign", "assgn");
142         rw.put("at", "atx");
143         rw.put("attribute", "attrbute");
144         rw.put("attributes", "attrbutes");
145         rw.put("audit", "audt");
146         rw.put("authorization", "athorization");
147         rw.put("autonext", "atonext");
148         rw.put("average", "avrage");
149         rw.put("avg", "av");
150         rw.put("avgu", "avgup");
151         rw.put("backout", "bckout");
152         rw.put("before", "bfore");
153         rw.put("begin", "bgin");
154         rw.put("beginload", "bginload");
155         rw.put("beginmodify", "bginmodify");
156         rw.put("beginning", "bginning");
157         rw.put("begwork", "bgwork");
158         rw.put("between", "btween");
159         rw.put("betweenby", "btweenby");
160         rw.put("border", "brder");
161         rw.put("bottom", "bttom");
162         rw.put("break", "brak");
163         rw.put("breakdisplay", "brakdisplay");
164         rw.put("browse", "brwse");
165         rw.put("bufered", "bfered");
166         rw.put("buffer", "bffer");
167         rw.put("buffered", "bffered");
168         rw.put("bulk", "blk");
169         rw.put("by", "byx");
170         rw.put("byte", "byt");
171         rw.put("call", "cll");
172         rw.put("cancel", "cncel");
173         rw.put("cascade", "cscade");
174         rw.put("case", "cse");
175         rw.put("change", "chnge");
176         rw.put("char", "chr");
177         rw.put("char_convert", "chr_convert");
178         rw.put("character", "chracter");
179         rw.put("check", "chck");
180         rw.put("checkpoint", "chckpoint");
181         rw.put("chr2fl", "chr2f");
182         rw.put("chr2flo", "chr2flt");
183         rw.put("chr2floa", "chr2fla");
184         rw.put("chr2float", "chr2flat");
185         rw.put("chr2int", "chr2nt");
186         rw.put("clear", "clar");
187         rw.put("clearrow", "clarrow");
188         rw.put("clipped", "clpped");
189         rw.put("close", "clse");
190         rw.put("cluster", "clustr");
191         rw.put("clustered", "clstered");
192         rw.put("clustering", "clstering");
193         rw.put("cobol", "cbol");
194         rw.put("cold", "cld");
195         rw.put("column", "colmn");
196         rw.put("columns", "clumns");
197         rw.put("command", "cmmand");
198         rw.put("comment", "commnt");
199         rw.put("commit", "cmmit");
200         rw.put("committed", "cmmitted");
201         rw.put("compress", "comprss");
202         rw.put("compute", "cmpute");
203         rw.put("concat", "cncat");
204         rw.put("cond", "cnd");
205         rw.put("config", "cnfig");
206         rw.put("confirm", "cnfirm");
207         rw.put("connect", "connct");
208         rw.put("constraint", "cnstraint");
209         rw.put("construct", "cnstruct");
210         rw.put("contain", "cntain");
211         rw.put("contains", "cntains");
212         rw.put("continue", "cntinue");
213         rw.put("controlrow", "cntrolrow");
214         rw.put("convert", "cnvert");
215         rw.put("copy", "cpy");
216         rw.put("count", "cnt");
217         rw.put("countu", "cntu");
218         rw.put("countucreate", "cuntucreate");
219         rw.put("crash", "crsh");
220         rw.put("create", "creat");
221         rw.put("current", "currnt");
222         rw.put("cursor", "crsor");
223         rw.put("data", "dta");
224         rw.put("data_pgs", "dta_pgs");
225         rw.put("database", "dtabase");
226         rw.put("datapages", "dtapages");
227         rw.put("date", "dte");
228         rw.put("day", "dy");
229         rw.put("daynum", "dynum");
230         rw.put("dba", "dbax");
231         rw.put("dbcc", "dbc");
232         rw.put("dbe", "dbex");
233         rw.put("dbefile", "dbfile");
234         rw.put("dbefileo", "dbfileo");
235         rw.put("dbefileset", "dbfileset");
236         rw.put("dbspace", "dbspce");
237         rw.put("dbyte", "dbyt");
238         rw.put("dec", "dc");
239         rw.put("decending", "dcending");
240         rw.put("decimal", "deciml");
241         rw.put("declare", "dclare");
242         rw.put("default", "deflt");
243         rw.put("defaults", "dfaults");
244         rw.put("defer", "dfer");
245         rw.put("define", "dfine");
246         rw.put("definition", "dfinition");
247         rw.put("delete", "delte");
248         rw.put("deleterow", "dleterow");
249         rw.put("desc", "dsc");
250         rw.put("descending", "dscending");
251         rw.put("descendng", "dscendng");
252         rw.put("describe", "dscribe");
253         rw.put("descriptor", "dscriptor");
254         rw.put("destpos", "dstpos");
255         rw.put("destroy", "dstroy");
256         rw.put("device", "dvice");
257         rw.put("devspace", "dvspace");
258         rw.put("direct", "drect");
259         rw.put("dirty", "drty");
260         rw.put("disconnect", "dsconnect");
261         rw.put("disk", "dsk");
262         rw.put("displace", "dsplace");
263         rw.put("display", "dsplay");
264         rw.put("distinct", "distnct");
265         rw.put("distribution", "dstribution");
266         rw.put("div", "dv");
267         rw.put("do", "d");
268         rw.put("does", "des");
269         rw.put("domain", "dmain");
270         rw.put("double", "duble");
271         rw.put("down", "dwn");
272         rw.put("drop", "drp");
273         rw.put("dual", "dal");
274         rw.put("dummy", "dmmy");
275         rw.put("dump", "dmp");
276         rw.put("duplicates", "dplicates");
277         rw.put("each", "ech");
278         rw.put("ebcdic", "ebcdc");
279         rw.put("ed_string", "ed_strng");
280         rw.put("editadd", "edtadd");
281         rw.put("editupdate", "edtupdate");
282         rw.put("else", "els");
283         rw.put("elseif", "elsif");
284         rw.put("end", "en");
285         rw.put("end_error", "end_rror");
286         rw.put("end_fetch", "end_ftch");
287         rw.put("end_for", "end_fr");
288         rw.put("end_get", "end_gt");
289         rw.put("end_modify", "end_mdify");
290         rw.put("end_place", "end_plce");
291         rw.put("end_segment_s", "end_sgment_s");
292         rw.put("end_segment_string", "end_sgment_string");
293         rw.put("end_store", "end_stre");
294         rw.put("end_stream", "end_stram");
295         rw.put("enddata", "enddta");
296         rw.put("enddisplay", "enddsplay");
297         rw.put("endforms", "endfrms");
298         rw.put("endif", "endf");
299         rw.put("ending", "endng");
300         rw.put("endload", "endlad");
301         rw.put("endloop", "endlop");
302         rw.put("endmodify", "endmdify");
303         rw.put("endpos", "endps");
304         rw.put("endretrieve", "endrtrieve");
305         rw.put("endselect", "endslect");
306         rw.put("endwhile", "endwhle");
307         rw.put("eq", "e");
308         rw.put("erase", "erse");
309         rw.put("errlvl", "errlv");
310         rw.put("error", "errr");
311         rw.put("errorexit", "errrexit");
312         rw.put("evaluate", "evluate");
313         rw.put("evaluating", "evluating");
314         rw.put("every", "evry");
315         rw.put("except", "excpt");
316         rw.put("exclusive", "exclusve");
317         rw.put("exec", "exc");
318         rw.put("execute", "excute");
319         rw.put("exists", "exsts");
320         rw.put("exit", "ext");
321         rw.put("explicit", "explcit");
322         rw.put("extent", "extnt");
323         rw.put("external", "extrnal");
324         rw.put("false", "flse");
325         rw.put("fetch", "ftch");
326         rw.put("field", "feld");
327         rw.put("file", "fle");
328         rw.put("filename", "flename");
329         rw.put("fillfactor", "fllfactor");
330         rw.put("finalise", "fnalise");
331         rw.put("finalize", "fnalize");
332         rw.put("findstr", "fndstr");
333         rw.put("finish", "fnish");
334         rw.put("first", "frst");
335         rw.put("firstpos", "frstpos");
336         rw.put("fixed", "fxed");
337         rw.put("fl", "flx");
338         rw.put("float", "flt");
339         rw.put("flush", "flsh");
340         rw.put("for", "fr");
341         rw.put("foreach", "freach");
342         rw.put("format", "frmat");
343         rw.put("formdata", "frmdata");
344         rw.put("forminit", "frminit");
345         rw.put("forms", "frms");
346         rw.put("fortran", "frtran");
347         rw.put("found", "foundx");
348         rw.put("frant", "frnt");
349         rw.put("fraphic", "frphic");
350         rw.put("free", "fre");
351         rw.put("from", "frm");
352         rw.put("frs", "frss");
353         rw.put("function", "fnction");
354         rw.put("get", "gett");
355         rw.put("getform", "gtform");
356         rw.put("getoper", "gtoper");
357         rw.put("getrow", "gtrow");
358         rw.put("global", "glbal");
359         rw.put("globals", "glbals");
360         rw.put("go", "gox");
361         rw.put("goto", "gotox");
362         rw.put("grant", "grnt");
363         rw.put("graphic", "grphic");
364         rw.put("group", "grp");
365         rw.put("gt", "gtx");
366         rw.put("having", "havng");
367         rw.put("header", "hader");
368         rw.put("help", "hlp");
369         rw.put("help_frs", "hlp_frs");
370         rw.put("helpfile", "hlpfile");
371         rw.put("hold", "hld");
372         rw.put("holdlock", "hldlock");
373         rw.put("identified", "identifd");
374         rw.put("identifield", "idntifield");
375         rw.put("if", "ifx");
376         rw.put("ifdef", "ifdf");
377         rw.put("ignore", "ignre");
378         rw.put("image", "imge");
379         rw.put("immediate", "immediat");
380         rw.put("immidiate", "immidiat");
381         rw.put("implicit", "implcit");
382         rw.put("in", "inx");
383         rw.put("include", "inclde");
384         rw.put("increment", "incremnt");
385         rw.put("index", "indx");
386         rw.put("indexed", "indxed");
387         rw.put("indexname", "indxname");
388         rw.put("indexpages", "indxpages");
389         rw.put("indicator", "indcator");
390         rw.put("infield", "infeld");
391         rw.put("info", "inf");
392         rw.put("ingres", "ingrs");
393         rw.put("init", "initt");
394         rw.put("initial", "initl");
395         rw.put("initialise", "intialise");
396         rw.put("initialize", "intialize");
397         rw.put("inittable", "inttable");
398         rw.put("input", "inpt");
399         rw.put("inquir_frs", "inqir_frs");
400         rw.put("inquire_equel", "inqire_equel");
401         rw.put("inquire_frs", "inqire_frs");
402         rw.put("inquire_ingres", "inqire_ingres");
403         rw.put("insert", "insrt");
404         rw.put("insertrow", "insrtrow");
405         rw.put("instructions", "instrctions");
406         rw.put("int", "inte");
407         rw.put("int2chr", "int2ch");
408         rw.put("integer", "integr");
409         rw.put("integrity", "intgrity");
410         rw.put("interesect", "intresect");
411         rw.put("interrupt", "intrrupt");
412         rw.put("intersect", "intersct");
413         rw.put("into", "intox");
414         rw.put("intschr", "intsch");
415         rw.put("invoke", "invke");
416         rw.put("is", "isx");
417         rw.put("isam", "ism");
418         rw.put("isolation", "islation");
419         rw.put("journaling", "jurnaling");
420         rw.put("key", "ky");
421         rw.put("kill", "kll");
422         rw.put("label", "lbel");
423         rw.put("language", "lnguage");
424         rw.put("last", "lastx");
425         rw.put("lastpos", "lstpos");
426         rw.put("le", "lex");
427         rw.put("left", "lft");
428         rw.put("length", "lngth");
429         rw.put("lenstr", "lnstr");
430         rw.put("let", "lett");
431         rw.put("level", "lvl");
432         rw.put("like", "lke");
433         rw.put("likeproceduretp", "lkeproceduretp");
434         rw.put("line", "lne");
435         rw.put("lineno", "lneno");
436         rw.put("lines", "lnes");
437         rw.put("link", "lnk");
438         rw.put("list", "lst");
439         rw.put("load", "ld");
440         rw.put("loadtable", "ldtable");
441         rw.put("loadtableresume", "ldtableresume");
442         rw.put("local", "lcal");
443         rw.put("location", "lcation");
444         rw.put("lock", "lck");
445         rw.put("locking", "lcking");
446         rw.put("log", "lg");
447         rw.put("long", "lng");
448         rw.put("lower", "lwer");
449         rw.put("lpad", "lpd");
450         rw.put("lt", "ltx");
451         rw.put("main", "mainx");
452         rw.put("manual", "mnual");
453         rw.put("manuitem", "manitem");
454         rw.put("margin", "mrgin");
455         rw.put("matches", "mtches");
456         rw.put("matching", "mtching");
457         rw.put("max", "mx");
458         rw.put("maxextents", "maxextnts");
459         rw.put("maxpublicunion", "mxpublicunion");
460         rw.put("maxreclen", "mxreclen");
461         rw.put("mdy", "mdyy");
462         rw.put("menu", "mnu");
463         rw.put("menuitem", "mnuitem");
464         rw.put("menuitemscreen", "mnuitemscreen");
465         rw.put("message", "msg");
466         rw.put("messagerelocate", "msgrelocate");
467         rw.put("messagescroll", "msgscroll");
468         rw.put("mfetch", "mftch");
469         rw.put("min", "minx");
470         rw.put("minreclen", "mnreclen");
471         rw.put("minreturnuntil", "mnreturnuntil");
472         rw.put("minus", "mnus");
473         rw.put("mirrorexit", "mrrorexit");
474         rw.put("missing", "mssing");
475         rw.put("mixed", "mxed");
476         rw.put("mlslabel", "mlslabl");
477         rw.put("mod", "modd");
478         rw.put("mode", "mde");
479         rw.put("modify", "modfy");
480         rw.put("modifyrevokeupdat", "mdifyrevokeupdat");
481         rw.put("module", "mdule");
482         rw.put("money", "mney");
483         rw.put("monitor", "mnitor");
484         rw.put("month", "mnth");
485         rw.put("move", "mve");
486         rw.put("multi", "mlti");
487         rw.put("name", "nme");
488         rw.put("ne", "nex");
489         rw.put("need", "ned");
490         rw.put("new", "nw");
491         rw.put("newlog", "nwlog");
492         rw.put("next", "nxt");
493         rw.put("nextscrolldown", "nxtscrolldown");
494         rw.put("no", "nox");
495         rw.put("noaudit", "noaudt");
496         rw.put("nocompress", "nocomprss");
497         rw.put("nocr", "ncr");
498         rw.put("nojournaling", "njournaling");
499         rw.put("nolist", "nlist");
500         rw.put("nolog", "nlog");
501         rw.put("nonclustered", "nnclustered");
502         rw.put("normal", "nrmal");
503         rw.put("nosyssort", "nsyssort");
504         rw.put("not", "nt");
505         rw.put("notffound", "ntffound");
506         rw.put("notfound", "ntfound");
507         rw.put("notrans", "ntrans");
508         rw.put("notrim", "ntrim");
509         rw.put("notrimscrollup", "ntrimscrollup");
510         rw.put("notrollbackuser", "ntrollbackuser");
511         rw.put("nowait", "nowt");
512         rw.put("null", "nll");
513         rw.put("nullify", "nllify");
514         rw.put("nullsaveusing", "nllsaveusing");
515         rw.put("nullval", "nllval");
516         rw.put("num", "numbr");
517         rw.put("number", "numbr");
518         rw.put("numeric", "numerc");
519         rw.put("nxfield", "nxfeld");
520         rw.put("of", "ofx");
521         rw.put("off", "offx");
522         rw.put("offline", "offln");
523         rw.put("offsets", "offsts");
524         rw.put("ofsavepointvalues", "ofsvepointvalues");
525         rw.put("old", "oldd");
526         rw.put("on", "onx");
527         rw.put("once", "onc");
528         rw.put("online", "onln");
529         rw.put("onselectwhere", "onslectwhere");
530         rw.put("onto", "ont");
531         rw.put("open", "opn");
532         rw.put("opensetwhile", "opnsetwhile");
533         rw.put("opensleep", "opnsleep");
534         rw.put("optimize", "optmize");
535         rw.put("option", "optn");
536         rw.put("options", "optons");
537         rw.put("or", "orx");
538         rw.put("order", "ordr");
539         rw.put("ordersqlwork", "ordrsqlwork");
540         rw.put("orsomewith", "orsmewith");
541         rw.put("orsort", "orsrt");
542         rw.put("otherwise", "othrwise");
543         rw.put("out", "ot");
544         rw.put("outer", "outerx");
545         rw.put("output", "outputx");
546         rw.put("output page", "otput page");
547         rw.put("outstop", "otstop");
548         rw.put("over", "ovr");
549         rw.put("owner", "ownr");
550         rw.put("ownership", "ownrship");
551         rw.put("page", "pge");
552         rw.put("pageno", "pgeno");
553         rw.put("pages", "pges");
554         rw.put("param", "parm");
555         rw.put("partition", "prtition");
556         rw.put("pascal", "pscal");
557         rw.put("password", "passwd");
558         rw.put("pathname", "pthname");
559         rw.put("pattern", "pttern");
560         rw.put("pause", "puse");
561         rw.put("pctfree", "pctfr");
562         rw.put("percent", "prcent");
563         rw.put("perm", "prm");
564         rw.put("permanent", "prmanent");
565         rw.put("permit", "prmit");
566         rw.put("permitsum", "prmitsum");
567         rw.put("pipe", "ppe");
568         rw.put("place", "plce");
569         rw.put("plan", "pln");
570         rw.put("pli", "pl");
571         rw.put("pos", "ps");
572         rw.put("power", "pwer");
573         rw.put("precision", "prcision");
574         rw.put("prepare", "prpare");
575         rw.put("preparetable", "prparetable");
576         rw.put("preserve", "prserve");
577         rw.put("prev", "prv");
578         rw.put("previous", "prvious");
579         rw.put("prevision", "prvision");
580         rw.put("print", "prnt");
581         rw.put("printer", "prnter");
582         rw.put("printscreen", "prntscreen");
583         rw.put("printscreenscroll", "prntscreenscroll");
584         rw.put("printsubmenu", "prntsubmenu");
585         rw.put("printsumu", "prntsumu");
586         rw.put("prior", "prr");
587         rw.put("priv", "privx");
588         rw.put("private", "prvate");
589         rw.put("privilages", "prvilages");
590         rw.put("privilagesthen", "prvilagesthen");
591         rw.put("privileges", "privilgs");
592         rw.put("proc", "prc");
593         rw.put("procedure", "prcedure");
594         rw.put("processexit", "prcessexit");
595         rw.put("program", "prgram");
596         rw.put("progusage", "prgusage");
597         rw.put("prompt", "prmpt");
598         rw.put("promptscrolldown", "prmptscrolldown");
599         rw.put("prompttabledata", "prmpttabledata");
600         rw.put("protect", "prtect");
601         rw.put("psect", "psct");
602         rw.put("public", "publc");
603         rw.put("publicread", "pblicread");
604         rw.put("put", "pt");
605         rw.put("putform", "ptform");
606         rw.put("putformscrollup", "ptformscrollup");
607         rw.put("putformunloadtab", "ptformunloadtab");
608         rw.put("putoper", "ptoper");
609         rw.put("putopersleep", "ptopersleep");
610         rw.put("putrow", "ptrow");
611         rw.put("putrowsubmenu", "ptrowsubmenu");
612         rw.put("putrowup", "ptrowup");
613         rw.put("query", "qery");
614         rw.put("quick", "qick");
615         rw.put("quit", "qit");
616         rw.put("raiserror", "riserror");
617         rw.put("range", "rnge");
618         rw.put("rangeto", "rngeto");
619         rw.put("raw", "rawx");
620         rw.put("rdb$db_key", "rdb$db_ky");
621         rw.put("rdb$length", "rdb$lngth");
622         rw.put("rdb$missing", "rdb$mssing");
623         rw.put("rdb$value", "rdb$vlue");
624         rw.put("rdb4db_key", "rdb4db_ky");
625         rw.put("rdb4length", "rdb4lngth");
626         rw.put("rdb4missing", "rdb4mssing");
627         rw.put("rdb4value", "rdb4vlue");
628         rw.put("read", "rad");
629         rw.put("read_only", "rd_only");
630         rw.put("read_write", "rd_write");
631         rw.put("readonly", "rdonly");
632         rw.put("readpass", "rdpass");
633         rw.put("readtext", "rdtext");
634         rw.put("readwrite", "rdwrite");
635         rw.put("ready", "rdy");
636         rw.put("real", "rl");
637         rw.put("reconfigure", "rconfigure");
638         rw.put("reconnect", "rconnect");
639         rw.put("record", "rcord");
640         rw.put("recover", "rcover");
641         rw.put("redisplay", "rdisplay");
642         rw.put("redisplaytabledata", "rdisplaytabledata");
643         rw.put("redisplayvalidate", "rdisplayvalidate");
644         rw.put("reduced", "rduced");
645         rw.put("register", "rgister");
646         rw.put("registerunloaddata", "rgisterunloaddata");
647         rw.put("registervalidrow", "rgistervalidrow");
648         rw.put("reject", "rject");
649         rw.put("relative", "rlative");
650         rw.put("release", "rlease");
651         rw.put("reload", "rload");
652         rw.put("relocate", "rlocate");
653         rw.put("relocateunique", "rlocateunique");
654         rw.put("remove", "rmove");
655         rw.put("removeuprelocatev", "rmoveuprelocatev");
656         rw.put("removevalidate", "rmovevalidate");
657         rw.put("removewhenever", "rmovewhenever");
658         rw.put("rename", "renme");
659         rw.put("repeat", "rpeat");
660         rw.put("repeatable", "rpeatable");
661         rw.put("repeated", "rpeated");
662         rw.put("repeatvalidrow", "rpeatvalidrow");
663         rw.put("replace", "rplace");
664         rw.put("replaceuntil", "rplaceuntil");
665         rw.put("replstr", "rplstr");
666         rw.put("report", "rport");
667         rw.put("request_handle", "rquest_handle");
668         rw.put("reserved_pgs", "rserved_pgs");
669         rw.put("reserving", "rserving");
670         rw.put("reset", "rset");
671         rw.put("resource", "resrce");
672         rw.put("rest", "rst");
673         rw.put("restart", "rstart");
674         rw.put("restore", "rstore");
675         rw.put("restrict", "rstrict");
676         rw.put("resume", "rsume");
677         rw.put("retrieve", "rtrieve");
678         rw.put("retrieveupdate", "rtrieveupdate");
679         rw.put("return", "rturn");
680         rw.put("returning", "rturning");
681         rw.put("revoke", "revke");
682         rw.put("right", "rght");
683         rw.put("role", "rle");
684         rw.put("rollback", "rllback");
685         rw.put("rollforward", "rllforward");
686         rw.put("rololback", "rlolback");
687         rw.put("round", "rund");
688         rw.put("row", "rowx");
689         rw.put("rowcnt", "rwcnt");
690         rw.put("rowcount", "rwcount");
691         rw.put("rowid", "rwid");
692         rw.put("rownum", "rwnum");
693         rw.put("rows", "rws");
694         rw.put("rpad", "rpd");
695         rw.put("rule", "rle");
696         rw.put("run", "rn");
697         rw.put("runtime", "rntime");
698         rw.put("samplstdev", "smplstdev");
699         rw.put("save", "sve");
700         rw.put("savepoint", "svepoint");
701         rw.put("savepointwhere", "svepointwhere");
702         rw.put("saveview", "sveview");
703         rw.put("schema", "schma");
704         rw.put("scope", "scpe");
705         rw.put("screen", "scren");
706         rw.put("scroll", "scrll");
707         rw.put("scrolldown", "scrlldown");
708         rw.put("scrollup", "scrllup");
709         rw.put("search", "sarch");
710         rw.put("segment", "sgment");
711         rw.put("sel", "sl");
712         rw.put("sele", "sle");
713         rw.put("selec", "slec");
714         rw.put("select", "selct");
715         rw.put("selupd", "slupd");
716         rw.put("serial", "srial");
717         rw.put("session", "sessn");
718         rw.put("set", "st");
719         rw.put("set_equel", "st_equel");
720         rw.put("set_frs", "st_frs");
721         rw.put("set_ingres", "st_ingres");
722         rw.put("setuser", "stuser");
723         rw.put("setwith", "stwith");
724         rw.put("share", "shre");
725         rw.put("shared", "shred");
726         rw.put("short", "shrt");
727         rw.put("show", "shw");
728         rw.put("shutdown", "shtdown");
729         rw.put("size", "sze");
730         rw.put("skip", "skp");
731         rw.put("sleep", "slep");
732         rw.put("smallfloat", "smllfloat");
733         rw.put("smallint", "smallnt");
734         rw.put("some", "sme");
735         rw.put("sort", "srt");
736         rw.put("sorterd", "srterd");
737         rw.put("sounds", "sunds");
738         rw.put("sourcepos", "surcepos");
739         rw.put("space", "spce");
740         rw.put("spaces", "spces");
741         rw.put("sql", "sq");
742         rw.put("sqlcode", "sqlcde");
743         rw.put("sqlda", "sqld");
744         rw.put("sqlerror", "sqlrror");
745         rw.put("sqlexeption", "sqlxeption");
746         rw.put("sqlexplain", "sqlxplain");
747         rw.put("sqlnotfound", "sqlntfound");
748         rw.put("sqrt", "sqr");
749         rw.put("stability", "stbility");
750         rw.put("start", "strt");
751         rw.put("start_segment", "strt_segment");
752         rw.put("start_segmented_?", "strt_segmented");
753         rw.put("start_stream", "strt_stream");
754         rw.put("start_transaction", "strt_transaction");
755         rw.put("starting", "strting");
756         rw.put("startpos", "strtpos");
757         rw.put("state", "stte");
758         rw.put("statistics", "sttistics");
759         rw.put("stdev", "stdv");
760         rw.put("step", "stepx");
761         rw.put("stop", "stopx");
762         rw.put("store", "stre");
763         rw.put("string", "strng");
764         rw.put("submenu", "sbmenu");
765         rw.put("substr", "sbstr");
766         rw.put("succesfull", "sccesfull");
767         rw.put("successful", "successfl");
768         rw.put("successfull", "sccessfull");
769         rw.put("sum", "sm");
770         rw.put("sumu", "smu");
771         rw.put("superdba", "sperdba");
772         rw.put("syb_terminate", "syb_trminate");
773         rw.put("synonym", "synonm");
774         rw.put("sysdate", "sysdte");
775         rw.put("syssort", "syssrt");
776         rw.put("table", "tble");
777         rw.put("tabledata", "tbledata");
778         rw.put("temp", "tmp");
779         rw.put("temporary", "tmporary");
780         rw.put("terminate", "trminate");
781         rw.put("text", "txt");
782         rw.put("textsize", "txtsize");
783         rw.put("then", "thn");
784         rw.put("through", "thrugh");
785         rw.put("thru", "thr");
786         rw.put("tid", "td");
787         rw.put("time", "tme");
788         rw.put("to", "tox");
789         rw.put("today", "tday");
790         rw.put("tolower", "tlower");
791         rw.put("top", "topp");
792         rw.put("total", "ttal");
793         rw.put("toupper", "tupper");
794         rw.put("tp", "tpx");
795         rw.put("trailer", "triler");
796         rw.put("tran", "trn");
797         rw.put("trans", "trns");
798         rw.put("transaction", "trnsaction");
799         rw.put("transaction_handle", "trnsaction_handle");
800         rw.put("transfer", "trnsfer");
801         rw.put("trigger", "triggr");
802         rw.put("tring", "trng");
803         rw.put("true", "tre");
804         rw.put("trunc", "trnc");
805         rw.put("truncate", "trncate");
806         rw.put("tsequal", "tsqual");
807         rw.put("type", "typ");
808         rw.put("uid", "uidx");
809         rw.put("unbuffered", "unbffered");
810         rw.put("union", "unn");
811         rw.put("unique", "unque");
812         rw.put("unload", "unlad");
813         rw.put("unloaddata", "unladdata");
814         rw.put("unloadtable", "unladtable");
815         rw.put("unlock", "unlck");
816         rw.put("until", "untl");
817         rw.put("up", "u");
818         rw.put("update", "updte");
819         rw.put("upper", "uppr");
820         rw.put("usage", "usge");
821         rw.put("use", "us");
822         rw.put("used_pgs", "usd_pgs");
823         rw.put("user", "usr");
824         rw.put("using", "usng");
825         rw.put("validate", "validte");
826         rw.put("validrow", "vlidrow");
827         rw.put("value", "val");
828         rw.put("values", "vals");
829         rw.put("varc", "vrc");
830         rw.put("varch", "vrch");
831         rw.put("varcha", "vrcha");
832         rw.put("varchar", "varchr");
833         rw.put("varchar2", "varch2r");
834         rw.put("vargraphic", "vrgraphic");
835         rw.put("verb_time", "vrb_time");
836         rw.put("verify", "vrify");
837         rw.put("version", "vrsion");
838         rw.put("view", "vw");
839         rw.put("wait", "wit");
840         rw.put("waitfor", "witfor");
841         rw.put("waiting", "witing");
842         rw.put("warning", "wrning");
843         rw.put("weekday", "wekday");
844         rw.put("when", "whn");
845         rw.put("whenever", "whenevr");
846         rw.put("where", "whre");
847         rw.put("while", "whle");
848         rw.put("window", "wndow");
849         rw.put("with", "wth");
850         rw.put("without", "wthout");
851         rw.put("work", "wrk");
852         rw.put("wrap", "wrp");
853         rw.put("write", "wrte");
854         rw.put("writepass", "wrtepass");
855         rw.put("writetext", "wrtetext");
856         rw.put("year", "yr");
857     }
858
859     public int getMaxTableNameLength() {
860         return maxTableNameLength;
861     }
862
863     /**
864      * Set the max length in characters for a table name.
865      */

866     public void setMaxTableNameLength(int maxTableNameLength) {
867         this.maxTableNameLength = maxTableNameLength;
868     }
869
870     public int getMaxColumnNameLength() {
871         return maxColumnNameLength;
872     }
873
874     /**
875      * Set the max length in characters for a column name.
876      */

877     public void setMaxColumnNameLength(int maxColumnNameLength) {
878         this.maxColumnNameLength = maxColumnNameLength;
879     }
880
881     public String JavaDoc getPkSuffix() {
882         return pkSuffix;
883     }
884
885     /**
886      * Set the max length in characters for a constraint name.
887      */

888     public void setMaxConstraintNameLength(int maxConstraintNameLength) {
889         this.maxConstraintNameLength = maxConstraintNameLength;
890     }
891
892     public int getMaxConstraintNameLength() {
893         return maxConstraintNameLength;
894     }
895
896     /**
897      * Set the max length in characters for an index name.
898      */

899     public void setMaxIndexNameLength(int maxIndexNameLength) {
900         this.maxIndexNameLength = maxIndexNameLength;
901     }
902
903     public int getMaxIndexNameLength() {
904         return maxIndexNameLength;
905     }
906
907     /**
908      * Set the suffix added to table or field names to name primary key
909      * columns. The default is _id so the pk for the employee table will
910      * be employee_id.
911      */

912     public void setPkSuffix(String JavaDoc pkSuffix) {
913         this.pkSuffix = pkSuffix;
914     }
915
916     public String JavaDoc getPkConstraintPrefix() {
917         return pkConstraintPrefix;
918     }
919
920     /**
921      * Set the prefix added to a table name to generate its primary key
922      * constraint name.
923      */

924     public void setPkConstraintPrefix(String JavaDoc pkConstraintPrefix) {
925         this.pkConstraintPrefix = pkConstraintPrefix;
926     }
927
928     /**
929      * Set the string used to break 'words' when generating names from
930      * field and class names and so on.
931      */

932     public void setWordBreak(String JavaDoc wordBreak) {
933         this.wordBreak = wordBreak;
934     }
935
936     public String JavaDoc getWordBreak() {
937         return wordBreak;
938     }
939
940     /**
941      * Set the name used for sequence columns in link tables. These are used
942      * to preserve the order of elements in ordered collections (lists and
943      * arrays).
944      */

945     public void setSequenceColumnName(String JavaDoc sequenceColumnName) {
946         this.sequenceColumnName = sequenceColumnName;
947     }
948
949     public String JavaDoc getSequenceColumnName() {
950         return sequenceColumnName;
951     }
952
953     /**
954      * Set the name used for value columns in link tables where the values
955      * are not references to PC instances.
956      */

957     public void setValueColumnName(String JavaDoc valueColumnName) {
958         this.valueColumnName = valueColumnName;
959     }
960
961     public String JavaDoc getValueColumnName() {
962         return valueColumnName;
963     }
964
965     /**
966      * Set the name used for keys columns in link tables for maps where the
967      * keys are not references to PC instances.
968      */

969     public void setKeyColumnName(String JavaDoc keyColumnName) {
970         this.keyColumnName = keyColumnName;
971     }
972
973     public String JavaDoc getKeyColumnName() {
974         return keyColumnName;
975     }
976
977     /**
978      * Set the name used for classId columns. These are added to the table for
979      * the base class in an inheritance heirachy.
980      */

981     public void setClassIdColumnName(String JavaDoc classIdColumnName) {
982         this.classIdColumnName = classIdColumnName;
983     }
984
985     public String JavaDoc getClassIdColumnName() {
986         return classIdColumnName;
987     }
988
989     public String JavaDoc getIndexNamePrefix() {
990         return indexNamePrefix;
991     }
992
993     /**
994      * Set the prefix used to generate index names.
995      */

996     public void setIndexNamePrefix(String JavaDoc indexNamePrefix) {
997         this.indexNamePrefix = indexNamePrefix;
998     }
999
1000    /**
1001     * Generate a JDBC name from a java class or field name. This breaks
1002     * the name at each capital inserting underscores and converts to
1003     * lower case. If the name turns out to be a reserved word it is replaced.
1004     * Any leading underscores are removed.
1005     * '$' are also removed.
1006     */

1007    protected String JavaDoc getJdbcName(String JavaDoc name) {
1008        int n = name.length();
1009        StringBuffer JavaDoc ans = new StringBuffer JavaDoc(n + 4);
1010        int i = 0;
1011        for (; i < n && name.charAt(i) == '_'; i++) ;
1012        for (; i < n; i++) {
1013            char c = name.charAt(i);
1014            if (Character.isUpperCase(c)) {
1015                c = Character.toLowerCase(c);
1016                if (i > 0) ans.append(wordBreak);
1017            }
1018            if (c == '/') {
1019                ans.append(wordBreak);
1020            } else if (c != '$' && c != '.') {
1021                ans.append(c);
1022            }
1023        }
1024        name = ans.toString();
1025        String JavaDoc rep = (String JavaDoc)reservedWords.get(name);
1026        if (rep != null) name = rep;
1027        return name;
1028    }
1029
1030    /**
1031     * Shrink the supplied name to maxlen chars if it is longer than maxlen.
1032     * This implementation removes vowels first and then truncates if it has
1033     * to.
1034     */

1035    protected String JavaDoc shrinkName(String JavaDoc name, int maxlen) {
1036        if (!nameCanStartWithUnderscore){
1037            int n = name.length();
1038            int j = 0;
1039            for (; j < n && name.charAt(j) == '_'; j++);
1040            name = name.substring(j,n);
1041        }
1042        int len = name.length();
1043        if (len <= maxlen) return name;
1044        int todo = len - maxlen;
1045        StringBuffer JavaDoc s = new StringBuffer JavaDoc();
1046        s.append(name.charAt(0));
1047        int i;
1048        for (i = 1; todo > 0 && i < len;) {
1049            char c = name.charAt(i++);
1050            if (c == 'e' || c == 'a' || c == 'i' || c == 'o' || c == 'u') {
1051                --todo;
1052            } else {
1053                s.append(c);
1054            }
1055        }
1056        if (todo == 0) {
1057            s.append(name.substring(i));
1058        }
1059        if (s.length() > maxlen) s.setLength(maxlen);
1060        return s.toString();
1061    }
1062
1063    /**
1064     * Generate a new name comprised of name with i appended. Characters are
1065     * stripped from the end of name to make space for i if maxLen would
1066     * be exceeded. This is used to resolve generated name clashes.
1067     */

1068    protected String JavaDoc appendInt(String JavaDoc name, int i, int maxLen) {
1069        String JavaDoc is = Integer.toString(i);
1070        int cut = name.length() + is.length() - maxLen;
1071        if (cut > 0) {
1072            int len = maxLen - cut;
1073            if (len < 1) len = 1;
1074            return name.substring(0, len) + is;
1075        } else {
1076            return name + is;
1077        }
1078    }
1079
1080    /**
1081     * Add a table name specified in jdo meta data.
1082     *
1083     * @throws IllegalArgumentException if the name is invalid
1084     * (e.g. 'duplicate table name' or 'invalid character XXX in name'
1085     * etc.)
1086     */

1087    public void addTableName(String JavaDoc name) throws IllegalArgumentException JavaDoc {
1088        if (tableMap.containsKey(name)) {
1089            throw BindingSupportImpl.getInstance().illegalArgument("Duplicate table name '" +
1090                    name + "'");
1091        }
1092        tableMap.put(name, new HashSet JavaDoc());
1093    }
1094
1095    /**
1096     * Remove all information about table.
1097     */

1098    public void removeTableName(String JavaDoc name) {
1099        tableMap.remove(name);
1100    }
1101
1102    /**
1103     * Generate a table name for a persistent class. The name generator must
1104     * 'add' it.
1105     *
1106     * @see #addTableName
1107     */

1108    public String JavaDoc generateClassTableName(String JavaDoc className) {
1109        int dot = className.lastIndexOf('.');
1110        if (dot >= 0) className = className.substring(dot + 1);
1111
1112
1113
1114        String JavaDoc name = shrinkName(getJdbcName(className), maxTableNameLength);
1115        String JavaDoc n = name;
1116        for (int i = 2; tableMap.containsKey(n); i++) {
1117            n = appendInt(name, i, maxTableNameLength);
1118        }
1119        addTableName(n);
1120        return n;
1121    }
1122
1123    /**
1124     * Generate a table name for a link table (normally used to hold the values
1125     * of a collection or array). The name generator must 'add' it.
1126     *
1127     * @param tableName The table on the 1 side of the the link
1128     * @param fieldName The field the link table is for
1129     * @param elementTableName The table on the n side of the link or null if
1130     * none (e.g. a link table for a collection of String's)
1131     * @see #addTableName
1132     */

1133    public String JavaDoc generateLinkTableName(String JavaDoc tableName, String JavaDoc fieldName,
1134            String JavaDoc elementTableName) {
1135        String JavaDoc name;
1136        if (elementTableName == null) {
1137            name = tableName + wordBreak + getJdbcName(fieldName);
1138        } else {
1139            name = tableName + wordBreak + elementTableName;
1140        }
1141        name = shrinkName(name, maxTableNameLength);
1142        String JavaDoc n = name;
1143        for (int i = 2; tableMap.containsKey(n); i++) {
1144            n = appendInt(name, i, maxTableNameLength);
1145        }
1146        addTableName(n);
1147        return n;
1148    }
1149
1150    /**
1151     * Add the primary key constaint name specified in jdo meta data.
1152     *
1153     * @throws IllegalArgumentException if it is invalid
1154     */

1155    public void addPkConstraintName(String JavaDoc tableName, String JavaDoc pkConstraintName)
1156            throws IllegalArgumentException JavaDoc {
1157        if (constraintNames.contains(pkConstraintName)) {
1158            throw BindingSupportImpl.getInstance().illegalArgument("Duplicate constraint name: " +
1159                    pkConstraintName);
1160        }
1161        constraintNames.add(pkConstraintName);
1162    }
1163
1164    /**
1165     * Generate a name for the primary key constaint for tableName.
1166     */

1167    public String JavaDoc generatePkConstraintName(String JavaDoc tableName) {
1168        int maxlen = maxConstraintNameLength - pkConstraintPrefix.length();
1169        String JavaDoc name = pkConstraintPrefix + shrinkName(tableName, maxlen);
1170        String JavaDoc n = name;
1171        for (int i = 2; constraintNames.contains(n); i++) {
1172            n = appendInt(name, i, maxConstraintNameLength);
1173        }
1174        addPkConstraintName(tableName, n);
1175        return n;
1176    }
1177
1178    /**
1179     * Add the referential integrity constaint name specified in jdo meta data.
1180     *
1181     * @throws IllegalArgumentException if it is invalid
1182     */

1183    public void addRefConstraintName(String JavaDoc tableName,
1184            String JavaDoc refConstraintName)
1185            throws IllegalArgumentException JavaDoc {
1186        if (constraintNames.contains(refConstraintName)) {
1187            throw BindingSupportImpl.getInstance().illegalArgument("Duplicate constraint name: " +
1188                    refConstraintName);
1189        }
1190        constraintNames.add(refConstraintName);
1191    }
1192
1193    /**
1194     * Generate a name for a referential integrity constaint for tableName.
1195     * The name generator must add it.
1196     *
1197     * @param tableName The table with the constraint
1198     * @param refTableName The table being referenced
1199     * @param fkNames The names of the foreign keys in tableName
1200     * @param refPkNames The names of the primary key of refTableName
1201     * @see #addRefConstraintName
1202     */

1203    public String JavaDoc generateRefConstraintName(String JavaDoc tableName,
1204            String JavaDoc refTableName, String JavaDoc[] fkNames, String JavaDoc[] refPkNames) {
1205        int maxlen = maxConstraintNameLength - refConstraintPrefix.length();
1206        String JavaDoc name = refConstraintPrefix + shrinkName(
1207                tableName + wordBreak + refTableName, maxlen);
1208        String JavaDoc n = name;
1209        for (int i = 2; constraintNames.contains(n); i++) {
1210            n = appendInt(name, i, maxConstraintNameLength);
1211        }
1212        addRefConstraintName(tableName, n);
1213        return n;
1214    }
1215
1216    /**
1217     * Add a column name. The tableName will have already been added.
1218     *
1219     * @throws IllegalArgumentException if the name is invalid
1220     * (e.g. 'duplicate column name' or 'invalid character XXX in name'
1221     * etc.)
1222     */

1223    public void addColumnName(String JavaDoc tableName, String JavaDoc columnName)
1224            throws IllegalArgumentException JavaDoc {
1225        String JavaDoc err = addColumnNameImp(tableName, columnName);
1226        if (err != null) throw BindingSupportImpl.getInstance().illegalArgument(err);
1227    }
1228
1229    /**
1230     * Does the table contain the column?
1231     */

1232    public boolean isColumnInTable(String JavaDoc tableName, String JavaDoc columnName) {
1233        HashSet JavaDoc cols = (HashSet JavaDoc)tableMap.get(tableName);
1234        if (cols == null) {
1235            throw BindingSupportImpl.getInstance().internal(
1236                    "isColumnInTable called with unknown table: '" + tableName + "'");
1237        }
1238        return cols.contains(columnName);
1239    }
1240
1241    /**
1242     * Add a column name. The tableName will have already been added.
1243     *
1244     * @return null if ok or error message
1245     */

1246    protected String JavaDoc addColumnNameImp(String JavaDoc tableName, String JavaDoc columnName) {
1247        HashSet JavaDoc cols = (HashSet JavaDoc)tableMap.get(tableName);
1248        if (cols == null) {
1249            throw BindingSupportImpl.getInstance().internal(
1250                    "addColumnName called with unknown table: '" + tableName + "'");
1251        }
1252        if (cols.contains(columnName)) {
1253            return "Duplicate column name '" + columnName +
1254                    "' in table '" + tableName + "'";
1255        }
1256        cols.add(columnName);
1257        return null;
1258    }
1259
1260    /**
1261     * Generate and add a name for the primary key column for a PC class using
1262     * datastore identity.
1263     */

1264    public String JavaDoc generateDatastorePKName(String JavaDoc tableName) {
1265        String JavaDoc name = getPKNameForTableName(tableName, 0);
1266        for (int j = 2; addColumnNameImp(tableName, name) != null; j++) {
1267            name = getPKNameForTableName(tableName, j);
1268        }
1269        return name;
1270    }
1271
1272    /**
1273     * Generate a primary key column name from a table name and an index.
1274     * If the index is less then 2 it must be ignored. Otherwise it must
1275     * be used in the generated name. This is used to resolve name clashes.
1276     * The returned name must not exceed maxColumnNameLength.
1277     */

1278    protected String JavaDoc getPKNameForTableName(String JavaDoc tableName, int i) {
1279        int maxlen = maxColumnNameLength - pkSuffix.length();
1280        if (i < 2) {
1281            return shrinkName(tableName, maxlen) + pkSuffix;
1282        } else {
1283            return appendInt(tableName, i, maxlen) + pkSuffix;
1284        }
1285    }
1286
1287    /**
1288     * Generate and add the name for a classId column.
1289     *
1290     * @see #addColumnNameImp
1291     */

1292    public String JavaDoc generateClassIdColumnName(String JavaDoc tableName) {
1293        String JavaDoc name = shrinkName(classIdColumnName, maxColumnNameLength);
1294        for (int j = 2; addColumnNameImp(tableName, name) != null; j++) {
1295            name = appendInt(classIdColumnName, j, maxColumnNameLength);
1296        }
1297        return name;
1298    }
1299
1300    /**
1301     * Generate and add the name for a field column.
1302     */

1303    public String JavaDoc generateFieldColumnName(String JavaDoc tableName, String JavaDoc fieldName,
1304            boolean primaryKey) {
1305        fieldName = fieldName.substring(fieldName.lastIndexOf('.')+1);
1306        String JavaDoc name = getColumnNameForFieldName(fieldName, 0);
1307        for (int j = 2; addColumnNameImp(tableName, name) != null; j++) {
1308            name = getColumnNameForFieldName(fieldName, j);
1309        }
1310        return name;
1311    }
1312
1313    /**
1314     * Generate a column name from a field name and an index. If the index
1315     * is less then 2 it must be ignored. Otherwise it must be used in the
1316     * generated name. This is used to resolve name clashes. The returned
1317     * name must not exceed maxColumnNameLength.
1318     */

1319    protected String JavaDoc getColumnNameForFieldName(String JavaDoc fieldName, int i) {
1320        fieldName = getJdbcName(fieldName);
1321        if (i < 2) {
1322            return shrinkName(fieldName, maxColumnNameLength);
1323        } else {
1324            return appendInt(fieldName, i, maxColumnNameLength);
1325        }
1326    }
1327
1328    /**
1329     * Generate a column name from a field name, a column number and an index.
1330     * If the index is less then 2 it must be ignored. Otherwise it must be
1331     * used in the generated name. This is used to resolve name clashes. The
1332     * columnNumber must also be used in the name. The returned name must not
1333     * exceed maxColumnNameLength. This is used for fields comprising of
1334     * multiple columns.
1335     */

1336    protected String JavaDoc getColumnNameForFieldName(String JavaDoc fieldName, int col,
1337            int i) {
1338        fieldName = getJdbcName(fieldName);
1339        String JavaDoc suffix = Integer.toString(col);
1340        int maxlen = maxColumnNameLength - suffix.length();
1341        if (i < 2) {
1342            return appendInt(fieldName, col, maxlen) + suffix;
1343        } else {
1344            return shrinkName(fieldName, maxlen) + suffix;
1345        }
1346    }
1347
1348    /**
1349     * Generate and add names for one or more columns for a field that is
1350     * a reference to another PC class. Some of the columns may already have
1351     * names.
1352     *
1353     * @param columnNames Store the column names here (some may already have
1354     * names if specified in the .jdo meta data)
1355     * @param refTableName The table being referenced (null if not a JDBC class)
1356     * @param refPkNames The names of the primary key columns of refTableName
1357     * @param otherRefs Are there other field referencing the same class here?
1358     * @throws IllegalArgumentException if any existing names are invalid
1359     */

1360    public void generateRefFieldColumnNames(String JavaDoc tableName,
1361            String JavaDoc fieldName, String JavaDoc[] columnNames, String JavaDoc refTableName,
1362            String JavaDoc[] refPkNames, boolean otherRefs) {
1363        if (refTableName == null) {
1364            columnNames[0] = generateFieldColumnName(tableName, fieldName,
1365                    false);
1366            return;
1367        }
1368        String JavaDoc prefix;
1369        if (otherRefs || tableName.equals(refTableName)) {
1370            int maxlen = 0;
1371            for (int i = refPkNames.length - 1; i >= 0; i--) {
1372                int n = refPkNames[i].length();
1373                if (n > maxlen) maxlen = n;
1374            }
1375            int n2 = maxColumnNameLength / 2 - 1;
1376            maxlen = n2 - maxlen;
1377            if (maxlen < n2) maxlen = n2;
1378            prefix = shrinkName(getJdbcName(fieldName), maxlen) + "_";
1379        } else {
1380            prefix = "";
1381        }
1382        int n = columnNames.length;
1383        for (int i = 0; i < n; i++) {
1384            String JavaDoc name = columnNames[i];
1385            if (name != null) {
1386                if (!isColumnInTable(tableName, name)) {
1387                    addColumnNameImp(tableName, name);
1388                }
1389                continue;
1390            }
1391            String JavaDoc base = prefix + refPkNames[i];
1392            name = getColumnNameForRefColumn(base, 0);
1393            for (int j = 2; addColumnNameImp(tableName, name) != null; j++) {
1394                name = getColumnNameForRefColumn(base, j);
1395            }
1396            columnNames[i] = name;
1397        }
1398    }
1399
1400    /**
1401     * Generate a column name from a ref column name and an index. If the
1402     * index is less then 2 it must be ignored. Otherwise it must be used in
1403     * the generated name. This is used to resolve name clashes. The returned
1404     * name must not exceed maxColumnNameLength.
1405     */

1406    protected String JavaDoc getColumnNameForRefColumn(String JavaDoc base, int i) {
1407        if (i < 2) {
1408            return shrinkName(base, maxColumnNameLength);
1409        } else {
1410            return appendInt(base, i, maxColumnNameLength);
1411        }
1412    }
1413
1414    /**
1415     * Generate and add names for one or more columns for a field that is
1416     * a polymorphic reference to any other PC class. Some of the columns may
1417     * already have names.
1418     *
1419     * @param columnNames Store the column names here (some may already have
1420     * names if specified in the .jdo meta data). The class-id column
1421     * is at index 0.
1422     * @throws IllegalArgumentException if any existing names are invalid
1423     */

1424    public void generatePolyRefFieldColumnNames(String JavaDoc tableName,
1425            String JavaDoc fieldName, String JavaDoc[] columnNames)
1426            throws IllegalArgumentException JavaDoc {
1427        String JavaDoc fieldJdbc = getJdbcName(fieldName);
1428        if (columnNames[0] == null) {
1429            String JavaDoc name = fieldJdbc + polyRefClassIdSuffix;
1430            String JavaDoc n = name;
1431            for (int j = 2; addColumnNameImp(tableName, n) != null; j++) {
1432                n = appendInt(name, j, maxColumnNameLength);
1433            }
1434            columnNames[0] = n;
1435        }
1436        int nc = columnNames.length;
1437        boolean onePk = nc == 2;
1438        for (int i = 1; i < nc; i++) {
1439            String JavaDoc name;
1440            if (onePk) {
1441                name = fieldJdbc + pkSuffix;
1442            } else {
1443                name = fieldJdbc + pkSuffix + (char)('a' + i - 1);
1444            }
1445            String JavaDoc n = name;
1446            for (int j = 2; addColumnNameImp(tableName, n) != null; j++) {
1447                n = appendInt(name, j, maxColumnNameLength);
1448            }
1449            columnNames[i] = n;
1450        }
1451    }
1452
1453    /**
1454     * Generate and add names for the column(s) in a link table that reference
1455     * the primary key of the main table. Some of the columns may already
1456     * have names which must be kept (no need to add them).
1457     *
1458     * @param tableName The link table
1459     * @param mainTablePkNames The names of the main table primary key
1460     * @param linkMainRefNames The corresponding column names in the link table
1461     */

1462    public void generateLinkTableMainRefNames(String JavaDoc tableName,
1463            String JavaDoc[] mainTablePkNames, String JavaDoc[] linkMainRefNames) {
1464        int len = linkMainRefNames.length;
1465        for (int i = 0; i < len; i++) {
1466            String JavaDoc name = linkMainRefNames[i];
1467            if (name != null) continue;
1468            name = mainTablePkNames[i];
1469            String JavaDoc n = name;
1470            for (int j = 2; addColumnNameImp(tableName, n) != null; j++) {
1471                n = appendInt(name, j, maxColumnNameLength);
1472            }
1473            linkMainRefNames[i] = n;
1474        }
1475    }
1476
1477    /**
1478     * Generate and add the name for a the column in a link table that stores
1479     * the element sequence number.
1480     */

1481    public String JavaDoc generateLinkTableSequenceName(String JavaDoc tableName) {
1482        String JavaDoc n = sequenceColumnName;
1483        for (int j = 2; addColumnNameImp(tableName, n) != null; j++) {
1484            n = appendInt(sequenceColumnName, j, maxColumnNameLength);
1485        }
1486        return n;
1487    }
1488
1489    /**
1490     * Generate and add names for the column(s) in a link table that reference
1491     * the primary key of the value table. This is called for collections of
1492     * PC classes. Some of the columns may already have names which must be
1493     * kept (no need to add them).
1494     *
1495     * @param tableName The link table
1496     * @param valuePkNames The names of the value table primary key (may be
1497     * null if the value class is not stored in JDBC)
1498     * @param valueClassName The name of the value class
1499     * @param linkValueRefNames The corresponding column names in the link table
1500     */

1501    public void generateLinkTableValueRefNames(String JavaDoc tableName,
1502            String JavaDoc[] valuePkNames, String JavaDoc valueClassName,
1503            String JavaDoc[] linkValueRefNames, boolean key) {
1504        String JavaDoc valueBaseName = null;
1505        if (valuePkNames == null) {
1506            int dot = valueClassName.lastIndexOf('.');
1507            if (dot >= 0) valueClassName = valueClassName.substring(dot + 1);
1508            valueBaseName = shrinkName(getJdbcName(valueClassName),
1509                    maxColumnNameLength);
1510        }
1511        int len = linkValueRefNames.length;
1512        for (int i = 0; i < len; i++) {
1513            String JavaDoc name = linkValueRefNames[i];
1514            if (name != null) continue;
1515            if (valuePkNames == null) {
1516                name = valueBaseName;
1517            } else {
1518                name = valuePkNames[i];
1519            }
1520            String JavaDoc n = name;
1521            for (int j = 2; addColumnNameImp(tableName, n) != null; j++) {
1522                n = appendInt(name, j, maxColumnNameLength);
1523            }
1524            linkValueRefNames[i] = n;
1525        }
1526    }
1527
1528    /**
1529     * Generate and add the name for a the column in a link table that stores
1530     * the value where the value is not a PC class (int, String etc).
1531     *
1532     * @param tableName The link table
1533     * @param valueCls The value class
1534     * @param key Is this a key in a link table for a map?
1535     */

1536    public String JavaDoc generateLinkTableValueName(String JavaDoc tableName,
1537            Class JavaDoc valueCls, boolean key) {
1538        String JavaDoc name = key ? keyColumnName : valueColumnName;
1539        String JavaDoc n = name;
1540        for (int j = 2; addColumnNameImp(tableName, n) != null; j++) {
1541            n = appendInt(name, j, maxColumnNameLength);
1542        }
1543        return n;
1544    }
1545
1546    /**
1547     * Add an index name. The tableName will have already been added.
1548     */

1549    public void addIndexName(String JavaDoc tableName, String JavaDoc indexName) {
1550        // we do not throw a exception if there are duplicates, because some DB's
1551
// do it on DB level and some on Table level.
1552
// If openaccess generates a Index name then we always do it on DB level.
1553
indexNames.add(indexName);
1554    }
1555
1556    /**
1557     * Generate and add an index name.
1558     *
1559     * @see #addIndexName
1560     */

1561    public String JavaDoc generateIndexName(String JavaDoc tableName, String JavaDoc[] columnNames) {
1562        StringBuffer JavaDoc s = new StringBuffer JavaDoc();
1563        s.append(indexNamePrefix);
1564        s.append(tableName);
1565        s.append(wordBreak);
1566        int nc = columnNames.length;
1567        for (int i = 0; i < nc; i++) {
1568            if (i > 0) s.append(wordBreak);
1569            s.append(columnNames[i]);
1570        }
1571        String JavaDoc name = shrinkName(s.toString(), maxIndexNameLength);
1572        String JavaDoc n = name;
1573        for (int i = 2; indexNames.contains(n); i++) {
1574            n = appendInt(name, i, maxIndexNameLength);
1575        }
1576        addIndexName(tableName, n);
1577        return n;
1578    }
1579
1580    public String JavaDoc getPolyRefClassIdSuffix() {
1581        return polyRefClassIdSuffix;
1582    }
1583
1584    public void setPolyRefClassIdSuffix(String JavaDoc polyRefClassIdSuffix) {
1585        this.polyRefClassIdSuffix = polyRefClassIdSuffix;
1586    }
1587}
1588
1589
Popular Tags