- Регистрация
- 1 Мар 2015
- Сообщения
- 1,467
- Баллы
- 155
Вы также можете использовать SQL-запрос (или таблицу) строк и столбцов (полей) данных базы данных и с помощью JSONWriter построить ответ вашей конечной точки на запросы Get и GetItem. Чтобы вернуть пользовательский JSON для запроса к базе данных, здесь есть реализации метода Get и GetItem.
Код:
procedure TFireDACResource1.Get(const AContext: TendpointContext;
const ARequest: TEndpointRequest; const AResponse: TEndpointResponse);
var
RowCount,FieldIndex : integer;
begin
EmployeeQuery.Close();
EmployeeQuery.SQL.Clear();
EmployeeQuery.SQL.Add('select * from employee');
EmployeeQuery.Open();
// using JSONWriter to craft custom JSON return for all rows
AResponse.building.JSONWriter.WriteStartArray;
for RowCount := 0 to EmployeeQuery.RecordCount-1 do begin
AResponse.building.JSONWriter.WriteStartObject;
for FieldIndex := 0 to EmployeeQuery.FieldCount-1 do begin
Aresponse.building.JSONWriter.WritePropertyName(
EmployeeQuery.Fields[FieldIndex].FieldName);
Aresponse.building.JSONWriter.WriteValue(
EmployeeQuery.FieldByName(
EmployeeQuery.Fields[FieldIndex].FieldName).AsString)
end;
AResponse.building.JSONWriter.WriteEndObject;
EmployeeQuery.Next
end;
AResponse.building.JSONWriter.WriteEndArray;
end;
procedure TFireDACResource1.GetItem(const AContext: TendpointContext;
const ARequest: TEndpointRequest; const AResponse: TEndpointResponse);
var
FieldIndex : integer;
LItem : string;
begin
LItem := ARequest.Params.Values['item'];
EmployeeQuery.Close();
EmployeeQuery.SQL.Clear();
EmployeeQuery.SQL.Add('select * from employee where EMP_NO = '+LItem);
EmployeeQuery.Open();
// using JSONWriter to craft custom JSON return for the selected employee row
AResponse.building.JSONWriter.WriteStartObject;
for FieldIndex := 0 to EmployeeQuery.FieldCount-1 do begin
Aresponse.building.JSONWriter.WritePropertyName(
EmployeeQuery.Fields[FieldIndex].FieldName);
Aresponse.building.JSONWriter.WriteValue(
EmployeeQuery.FieldByName(
EmployeeQuery.Fields[FieldIndex].FieldName).AsString)
end;
end;