Генерация кода программы, создающей таблицы базы данных на основе уже созданной базы данных
set safety off
dir_windows_temp=addbs(sys(2023))
dbcfile=getfile('dbc','database') && source database
pathtbl=addbs(justpath(dbcfile))
dbcname=justfname(dbcfile)
out=putfile('txt') && target txt-file
strtofile('',out)
strtofile('if !file("'+dbcname+'")'+chr(13),out,.t.)
strtofile(chr(9)+'create data '+dbcname+chr(13),out,.t.)
strtofile('else'+chr(13),out,.t.)
strtofile(chr(9)+'open data '+dbcname+chr(13),out,.t.)
strtofile('endif'+chr(13),out,.t.)
if !empty(dbcfile) and !empty(out)
use "&dbcfile" in 0 alias dbctable again
select objectname,objectid from dbctable;
where UPPER(objecttype)='TABLE' into cursor dbccursor
use in dbctable
go top in 'dbccursor'
do while !eof('dbccursor')
tbl=alltr(dbccursor.objectname)
objid=dbccursor.objectid
if !file(pathtbl+alltr(tbl)+'.dbf')
messagebox('There are no table '+alltr(tbl)+', enter path to it.',16,'')
lf=locfile(tbl)
tbl=juststem(lf)
if !used(tbl)
use "&lf" in 0
endif
else
if !used(tbl)
use "&pathtbl.&tbl" in 0
endif
endif
sele &tbl
if used('stbl')
use in 'stbl'
endif
copy stru exte to &dir_windows_temp.stbl
use &dir_windows_temp.stbl
sele stbl
go top
strtofile('if !file("'+alltr(tbl)+'.dbf")'+chr(13),out,.t.)
cmd=chr(9)+'CREATE TABLE '+allt(Table_name)+'(;'+chr(13)+chr(9)+' '
strtofile(cmd,out,.t.)
cmd1=allt(Field_name) +' '+;
allt(Field_type)+;
iif(Field_type='M','','('+;
allt(str(Field_len))+;
iif (Field_type='N',','+allt(str(Field_dec))+')',')')+;
iif(!empty(field_null),' NULL','')+;
iif(!empty(field_defa),' default '+allt(field_defa),''))
strtofile(cmd1,out,.t.)
cmd2=''
do while !eof('stbl')
skip
if !empty(Field_name)
cmd2=cmd2+',;'+chr(13)+chr(9)+' '+;
allt(Field_name) +' '+;
allt(Field_type)+;
iif(Field_type='M','','('+;
allt(str(Field_len))+;
iif (Field_type='N',','+allt(str(Field_dec))+')',')')+;
iif(!empty(field_null),' NULL','')+;
iif(!empty(field_defa),' default '+allt(field_defa),''))
endif
enddo
strtofile(cmd2,out,.t.)
strtofile(')'+chr(13),out,.t.)
strtofile(chr(9)+'reindex'+tbl+'()'+chr(13),out,.t.)
strtofile('endif'+chr(13),out,.t.)
*** create indexes
strtofile('function reindex'+tbl+chr(13),out,.t.)
use "&dbcfile" in 0 alias dbctable again
* strtofile(chr(9)+'use '+alltr(tbl)+' in 0 excl'+chr(13),out,.t.)
select objectname from dbctable;
where UPPER(objecttype)='INDEX' and parentid=objid;
into cursor objindex
use in dbctable
do while !eof('objindex')
strtofile(chr(9)+'index on '+alltr(objindex.objectname)+' tag '+alltr(objindex.objectname)+chr(13),out,.t.)
skip in 'objindex'
enddo
strtofile('return'+chr(13),out,.t.)
****************
skip in dbccursor
enddo
endif
*use in dbctable
use in dbccursor
use in objindex