Hello,
You can’t do it from a command line, but you can use “Tools->Wizzards->Publish Tables” command in schema modeler for creating datatables and delta commands.
I apologize, but the answer above is not correct. Data Abstract exposes all the API needed to load database metadata and to create a Service Schema based on this metadata. Below is an C# console app code that creates a Schema based on provided connection string. Please notify us if you need the same code in Delphi.
You need to create new C# Console Application. Set its TargetFramework to full 3.5 or 4.0 framework (ie not Client Profile). After this add references to assemblies
RemObjects.DataAbstract
and
RemObjects.DataAbstract.Server
And use this code (note that Schema file name and connection string are hardcoded in the Main method. Feel free to change this code to, say, console app that accepts all parameters via command line parameters etc
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using RemObjects.DataAbstract.Server;
using RemObjects.DataAbstract.Schema;
namespace ConsoleApplication2
{
class Program
{
static void Main(String[] args)
{
// Needed in any Data Abstract application
// Load configuration
if (!RemObjects.DataAbstract.Server.Configuration.Loaded)
RemObjects.DataAbstract.Server.Configuration.Load();
String connectionString = @"MSSQL.NET?Server=localhost;Database=Northwind;Integrated Security=SSPI";
String schemaFileName = @"U:\TestSchema.daSchema";
ServiceSchema lSchema = GenerateSchema(connectionString);
lSchema.Serialization.SaveToFile(schemaFileName);
}
static ServiceSchema GenerateSchema(String connectionString)
{
ServiceSchema lSchema = new ServiceSchema();
// 0. Determine connection type
String connectionType = connectionString.Substring(0, connectionString.IndexOf('?'));
// 1. Connect to the database
using (BaseConnection connection = new BaseConnection("<>", connectionString, true))
{
// 2. Get Database Table names
StringCollection tables = new StringCollection();
connection.GetTableNames(ref tables);
// 3. Get Database View names
StringCollection views = new StringCollection();
connection.GetViewNames(ref views);
// 4. Combine table and name collections
List entities = tables.Cast().ToList();
entities.AddRange(views.Cast());
// 5. Create Schema Data Tables
for (Int32 i = 0; i < entities.Count; i++)
{
String entityName = entities[i];
SchemaDataTable schemaTable = new SchemaDataTable { Name = entityName, IsPublic = true };
// Load table fields info
SchemaFieldCollection tableFields = new SchemaFieldCollection();
connection.GetTableFields(entityName, ref tableFields, true);
// Add all fields to the Schema Table definition
foreach (SchemaField field in tableFields)
schemaTable.Fields.Add(field);
// Add AutoSQL statement
SchemaSQLStatement statement = new SchemaSQLStatement { ConnectionType = connectionType, StatementType = StatementType.AutoSQL, TargetTable = entityName };
foreach (SchemaField field in tableFields)
statement.ColumnMappings.Add(new SchemaColumnMapping(field.Name, field.Name, field.Name));
schemaTable.Statements.Add(statement);
// Add created Schema Data Table to the Schema
lSchema.DataTables.Add(schemaTable);
}
// 6. Add table relationships
SchemaRelationshipCollection relationships = connection.GetForeignKeys();
foreach (SchemaRelationship relationship in relationships)
{
String relationshipName = String.Format("FK_{0}_{1}", relationship.DetailDataTableName, relationship.MasterDataTableName);
SchemaRelationship schemaRelationship;
if (lSchema.Relationships.FindItem(relationshipName, out schemaRelationship))
{
schemaRelationship.MasterFields = schemaRelationship.MasterFields + "," + relationship.MasterFields;
schemaRelationship.DetailFields = schemaRelationship.DetailFields + "," + relationship.DetailFields;
}
else
{
relationship.Name = relationshipName;
lSchema.Relationships.Add(relationship);
}
}
// 7. Add StoredProcedures (as Commands)
StringCollection storedProcedures = new StringCollection();
connection.GetStoredProcedureNames(ref storedProcedures);
foreach (String storedProcedure in storedProcedures)
{
SchemaCommand schemaCommand = new SchemaCommand { Name = storedProcedure, IsPublic = true };
SchemaParameterCollection commandParameters = new SchemaParameterCollection();
connection.GetCommandParams(storedProcedure, ref commandParameters, true);
foreach (SchemaParameter parameter in commandParameters)
schemaCommand.Parameters.Add(parameter);
SchemaSQLStatement statement = new SchemaSQLStatement { ConnectionType = connectionType, StatementType = StatementType.StoredProcedure, SQL = storedProcedure };
schemaCommand.Statements.Add(statement);
lSchema.Commands.Add(schemaCommand);
};
};
return lSchema;
}
}
}
procedure TDASampleService.DataAbstractServiceBeforeGetDatasetData(
aSender: TObject; const aDataset: IDADataset; const aIncludeSchema: Boolean;
const aMaxRecords: Integer);
begin
if aMaxRecords <> -1 then begin
(aDataset as IDAServerDataset).SQL := StringReplace((aDataset as IDAServerDataset).SQL,'SELECT', 'SELECT FIRST '+IntToStr(aMaxRecords));
end;
end;