dimanche 14 mai 2017

Create table with firedac without SQL Script

Firedac library centralizes database behavior and have a lot of methods which works fine without care about the Database Type. Actually, using native drivers for most common databases, Firedac hides subtle differences on syntax allowing very flexible changes of database platform. For example, generators and autoinc fields are easily detectable, CAST and parameters works fine allowing easy migration between databases.

How to use Firedac power to create New Table without instantiate FDQuery, which runs a SQL Script CREATE TABLE?

I hope to create any Object and, calling specific fieldbyname, record it on database, but first I need to certify:

  1. If Table is already created
  2. If Field is already created
  3. If record is already created

    TRecCustomer=record Id:integer; Name:String; Birthday:TDate; end; ICustomer = Interface procedure setCustomerId(Value:Integer); procedure setCustomerName(Value:String); procedure SetBirthday(Value:TDate); procedure Post; end; TCustomer = Class(TInterfacedObjet, ICustomer) CustomerObject=TRecCustomer; procedure setCustomerId(Value:Integer); procedure setCustomerName(Value:String); procedure SetBirthday(Value:TDate); procedure Post; end;

    TCustomer.Post; Begin If not TableExists('Customer') the CreateTable('Customer'); If not FieldExists('Name') the CreateField('name',ftString,[],40); If not FieldExists('Id') the CreateField('Id',ftInteger,[cAutoInc,cNotNull]); If not FieldExists('Birthday') the CreateField('birthday',ftDate); End;

I imagine the procedure CreateField(fieldName:String;fieldType:TDataType;cConstraints:TConstraints,length:integer=0); where TConstraints=set of ([cAutoInc,cNotNull,cUnique,cEtc]);

I can do It for specific database, for example Sqlite or Firebird, but I don't know hou to do for any database using Firedac resources.

Thanks a lot.

Aucun commentaire:

Enregistrer un commentaire