forked from fenisoft/fenixsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfssqlcodetemplate.pas
More file actions
170 lines (151 loc) · 4.42 KB
/
fssqlcodetemplate.pas
File metadata and controls
170 lines (151 loc) · 4.42 KB
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
(*
fenixsql
author Alessandro Batisti
http://code.google.com/p/fenixsql
http://fblib.altervista.org
file: fssqlcodetemplate.pas
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*)
unit fssqlcodetemplate;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils;
function TableCreate(const AName: string;const APk: string = ''): string;
function TableCreateWithAutoIncrement(AName: string; APk: string = 'ID'): string;
function TableInsert(const ATableName: string; AFields: TStringList): string;
function TableUpdate(const ATableName: string; AFields: TStringList): string;
implementation
uses fsdm;
function TableCreate(const AName: string; const APk: string = ''): string;
var
code: TStringList;
begin
code := TStringList.Create;
try
with code do
begin
Add('CREATE TABLE ' + AName);
Add('(');
if (APk <> '') then
Add(Format(' %s INTEGER NOT NULL,',[APk]));
Add(' column_name {< datatype> | COMPUTED BY (< expr>) | domain}');
Add(' [DEFAULT { literal | NULL | USER}] [NOT NULL]');
Add(' ');
Add(Format(' CONSTRAINT %s_PK',[AName]));
Add(Format(' PRIMARY KEY (%s)',[APk]));
Add(');');
end;
Result := code.Text;
finally
code.Free;
end;
end;
function TableCreateWithAutoIncrement(AName: string; APk: string = 'ID'): string;
var
code: TStringList;
begin
code := TStringList.Create;
try
with code do
begin
Add(Format('CREATE TABLE %s',[AName]));
Add('(');
Add(Format(' %s INTEGER NOT NULL,',[APk]));
Add(' column_name {< datatype> | COMPUTED BY (< expr>) | domain}');
Add(' [DEFAULT { literal | NULL | USER}] [NOT NULL]');
Add(' ');
Add(Format(' CONSTRAINT %s_PK',[AName]));
Add(Format(' PRIMARY KEY (%s)',[APk]));
Add(');');
Add(' ');
Add(Format('CREATE GENERATOR GEN_%s_ID;',[AName]));
Add('');
Add('SET TERM ^;');
Add(Format('CREATE TRIGGER TRIG_%s_BI FOR %s',[AName,AName]));
Add('ACTIVE BEFORE INSERT');
Add('POSITION 0');
Add('as');
Add('begin');
Add('/* Trigger Text */');
Add(Format(' if (new.%s is null )then',[APk]));
Add(Format(' new.%s = gen_id(GEN_%s_ID ,1);',[APk,AName]));
Add('end^');
Add('SET TERM ;^');
end;
Result := code.Text;
finally
code.Free;
end;
end;
function TableInsert(const ATableName: string; AFields: TStringList): string;
var
i: integer;
code: TStringList;
sep : string;
begin
code := TStringList.Create;
try
code.Add('insert into ' + ATableName);
code.Add('(');
for i:= 0 to AFields.Count - 1 do
begin
if (i < (AFields.Count - 1)) then
sep := ' ,'
else
sep := '';
code.Add(' ' + AFields.Strings[i] + sep);
end;
code.Add(')');
code.Add('values');
code.Add('(');
for i:= 0 to AFields.Count - 1 do
begin
if (i < (AFields.Count - 1 )) then
sep := ' ,'
else
sep := '';
code.Add(' ?' + sep);
end;
code.Add(')');
Result := code.Text;
finally
code.Free;
end;
end;
function TableUpdate(const ATableName: string; AFields: TStringList): string;
var
i: integer;
code: TStringList;
sep : string;
begin
code := TStringList.Create;
try
code.Add('update ' + ATableName);
for i:= 0 to AFields.Count - 1 do
begin
if (i < (AFields.Count - 1)) then
sep := ' ,'
else
sep := '';
if i = 0 then
code.Add('set ' + AFields.Strings[i] + ' = ?' + sep)
else
code.Add(' ' + AFields.Strings[i] + ' = ?' + sep)
end;
code.Add('where');
code.Add('<primary key> = ?');
Result := code.Text;
finally
code.Free;
end;
end;
end.