Skip to content

UserGuide_D_LayerAutomaticGeneration

nishi_74322014 edited this page Nov 3, 2026 · 1 revision

Open Touryo UserGuide (D Layer Automatic Generation Edition)

This is a English page.
Click here for Japanese version of this file.

October 3, 2016

Introduction

Target audience

  • SEs and developers who develop applications with Open Touryo

Overview

This document summarizes how to use the D layer automatic generation function of the framework.

Trademarks

Company names and product names in this document are trademarks or registered trademarks of their respective companies.

License

This document is available under the Creative Commons CC BY 2.1 JP license.

※ The figures on this page are screenshots of the tool. Some figures (such as Figures 4.1-1 to 4.1-5) could not be extracted and only have captions.

Table of contents

1. Overview of the D layer automatic generation tool (Sumitsubo)

2. Automatic generation of definition information

3. Automatic generation of Dao and SQL files

4. Templates

5. Examples of customizing the processing

1. Overview of the D layer automatic generation tool (Sumitsubo)

The D layer automatic generation tool (Sumitsubo) of "Open Touryo" works as follows. It obtains table, column and primary key information (when obtainable) from the "DB schema information extraction function" and creates an intermediate file. It then passes this to the "D layer automatic generation function", which automatically generates the Dao and SQL files.

Figure 1-1 D layer automatic generation tool: generation method

The created Dao and SQL files can execute static processing (record-level CRUD processing) and dynamic processing (data access processing per data set) on tables and views.

Figure 1-2 D layer automatic generation tool: generated processing

1.1 Details of the automatically generated processing

With the D layer automatic generation tool, a data access library that can be used like common components can be generated automatically. In particular, update processing can be generated almost 100% automatically as Dao and SQL files by this tool.

Table 1.1 Details of the automatically generated processing

No. Query type Description and notes
1 INSERT Static Insert INSERTs with the "column list" and "insert parameters" of all columns set. For auto-increment columns such as Identity and TimeStamp, and timestamp columns with a DEFAULT value, manually delete the "column list" and "insert parameters" of that column [1]. For columns using the SEQUENCE NEXTVAL function, the SYSTIMESTAMP function, etc., modify the SQL directly.
INSERT Dynamic DynIns INSERTs by dynamically enabling only the "insert columns" corresponding to the "insert parameters" that were set. Do not specify an "insert parameter" for auto-increment columns.
2 SELECT Static Select SELECTs the target record with the "search parameters" of the primary key columns set. If there is a timestamp column, it is added to the dynamic "search parameters".
SELECT Dynamic DynSel SELECTs by dynamically enabling only the "search conditions" corresponding to the "search parameters" that were set.
3 UPDATE Static Update UPDATEs with the "search parameters" of the primary key columns set and only the "update columns" corresponding to the "update parameters" that were set dynamically enabled. Do not set an "update parameter" for auto-increment columns.
UPDATE Dynamic DynUpd UPDATEs with the "search conditions" and "update columns" dynamically enabled. ※ Update processing with dynamic "search conditions" is dangerous (a wrong condition may destroy a large amount of data), so be careful.
4 DELETE Static Delete DELETEs the target record with the "search parameters" of the primary key columns set.
DELETE Dynamic DynDel DELETEs by dynamically enabling only the "search conditions" corresponding to the "search parameters" that were set. ※ Dynamic update processing is dangerous, so be careful.

★★ Specifying the timestamp at update time from the UP is not recommended. Always adopt either the auto-increment method by the DBMS, or the method of setting the timestamp column name and update method in the tool GUI and statically updating with that value.

※ Unsupported processing: queries that use Join, queries that use special search conditions [2], and Insert / Update queries that use Select in update processing. ※ User parameter definitions are not subject to automatic generation, so define them directly in the output SQL file if necessary.

1.2 Structure of the automatically generated Dao and SQL files

The generated Dao and SQL files have the following structure.

Figure 1.2 Structure of the generated Dao and SQL files

Dao and SQL files with this structure (those in bold lines) are generated for each table and view (for each table or view, one Dao class file and eight SQL files are generated).

1.3 Handling unsupported processing

Processing that does not fall under static or dynamic processing on tables and views is not supported. Even for processing that does, queries using special search conditions and Insert / Update queries using Select are not supported. In these cases, create the SQL directly and perform data access using the Dao class.

Figure 1.3 How to handle data access processing not supported by D layer automatic generation

※ Even for complex SQL, if it is for reference, using a view lets it fit into the automatic generation development route of this tool.

2. Automatic generation of definition information

This chapter describes how to automatically generate D layer definition information.

  1. Start the [Generate D layer definition information file] tool: When you start the EXE of the D layer automatic generation tool (DaoGen_Tool.exe), the [Generate D layer definition information file] screen is displayed.

    Figure 2-1 Starting the [D layer, Dao and SQL file generation] screen

  2. Select the data provider, enter the connection string, select the combo box value and press the [Get schema information] button to view the schema information that can be obtained with the GetSchema method of the data provider (it cannot be viewed if the connection string is incorrect).

    Figure 2-2 Selecting the data provider, entering the connection string and testing schema information retrieval

  3. Press the [Connect to DB and list tables] button: After confirming in step 3 that the connection is correct, pressing this button obtains and lists the table and view information.

    Figure 2-4 Listing tables and views

  4. Delete unnecessary tables: With the tables and views to delete selected in the list box, press the [Delete selected tables] button.

    Figure 2-5 Deleting unnecessary tables and views

  5. Load column information: Press the [Load column information] button to load the column information of the tables and views in the list box.

    Figure 2-6 Loading column information

  6. Set primary key (or equivalent) information: With a table or view selected, press the [Set primary key of selected tables] button and check the primary key columns in the displayed dialog (for SQL Server and Oracle, the primary key information is set automatically).

    Figure 2-7-1 Setting primary key information

    If multiple tables are selected, the following dialog is displayed after closing the dialog ([OK] to continue, [Cancel] to abort).

    Figure 2-7-2 Setting primary key information

  7. Generate the D layer definition information: Select [Shift-JIS] or [UTF-8] from the [Select output encoding] group box and press the [Generate D layer definition information] button to output the D layer definition information file to the specified location.

    Figure 2-8 Generating the D layer definition information

  8. Check and edit the D layer definition information: The output D layer definition information is in CSV format, so it can easily be edited with Excel, etc. The [Table name] column contains the table or view name, the [Column information] column in that row contains the column information of the primary key (or equivalent), and the [Column information] columns in the rows below contain the column information other than the primary key.

    Figure 2-9 Checking and editing the D layer definition information

3. Automatic generation of Dao and SQL files

This chapter describes how to automatically generate Dao and SQL files.

  1. Start the [D layer, Dao and SQL file generation] tool: Start DaoGen_Tool.exe and press the [Generate Dao and SQL files] button on the [Generate D layer definition information file] screen to display the [D layer, Dao and SQL file generation] screen.

    Figure 3-1 Starting the [D layer, Dao and SQL file generation] screen

  2. Set the series of generation options: Specify the data provider, language, input information (template, encoding), output information (output destination, encoding, user name), etc.

    Figure 3-2 Setting the series of generation options

    • ※ If the D layer definition information contains a [Table name, Column information, ...] header, enable the [With header (ignore the first line)] check box.
    • ※ The timestamp column name must be standardized across all tables. If it is not an auto-increment column, set the "update method". To make the timestamp mandatory in the search conditions of Update and Delete, turn on [Timestamp required].
  3. Generate the Dao and SQL files: After completing the settings, press the [Generate Dao and SQL files] button to complete the D layer generation.

    Figure 3-3 Generating the Dao and SQL files

  4. Check the Dao and SQL files: Confirm that the Dao and SQL files have been generated at the specified output destination (the sample programs [4] are helpful for understanding how to use them).

4. Templates

This chapter describes the templates required to generate Dao and SQL files. Basically, the templates located at ~\root\files\DGenTemplates are used as they are, but customization may be required depending on the project.

4.1 Dao template

The customization points of the Dao template (DaoTemplate.cs) are as follows.

  • Comment header: Modify it according to the policy of the project. The values are embedded at generation time in the class name (_DaoClassName_), creation date and time (_TimeStamp_) and author (_UserName_).
//**********************************************************************************
//* クラス名        :_DaoClassName_
//* クラス日本語名  :自動生成Daoクラス
//*
//* 作成日時        :_TimeStamp_
//* 作成者          :棟梁 D層自動生成ツール(墨壺), _UserName_
//* 更新履歴        :
//*
//*  日時        更新者            内容
//*  ----------  ----------------  -------------------------------------------------
//*  20xx/xx/xx  XX XX         XXXX
//**********************************************************************************
  • Using (Import) clauses: Add or modify the Using clauses of the modules used in the Dao. In particular, change the Touryo part of the namespace to the namespace of the project.
#region using

// System~
using System;
using System.IO;
using System.Data;
using System.Collections;

// Framework
using Touryo.Infrastructure.Framework.Dao;
using Touryo.Infrastructure.Framework.Common;

// Components
using Touryo.Infrastructure.Public.Db;
using Touryo.Infrastructure.Public.Util;

// Business framework
using Touryo.Infrastructure.Business.Dao;

#endregion
  • Changing the name of the "data access parent class 2": Change the name of the inherited "data access parent class 2" for the project (the default is MyBaseDao).
#endregion

/// <summary>Automatically generated Dao class</summary>
public class _DaoClassName_ : MyBaseDao
{
    #region Instance variables
  • Prefix of property procedures for primary keys: The default is PK_. (Figure 4.1-4)
    • For primary keys: PK_columnName, for non-primary keys: columnName, for update processing: header_columnName_footer
#region Property procedures (setter, getter)

// ControlComment:RoopStart-PPPK

/// <summary>Sets the parameter of the parameterized query for the _ColumnName_ column (primary key column).</summary>
/// <remarks>Excludes parameters used in the SET clause of update processing</remarks>
public object PK__ColumnName_
{
    set
    {
        this.HtParameter["_ColumnName_"] = value;
    }
    get
    {
        return this.HtParameter["_ColumnName_"];
    }
}

// ControlComment:RoopEnd-PPPK
  • Methods for reference processing: The default is an interface using ExecSelectFill_DT(). If necessary, add interfaces using ExecSelectFill_DS() and ExecSelect_DR(). (Figure 4.1-5)
#region Select

/// <summary>Specifies the primary key and references one record.</summary>
/// <param name="dt">DataTable to store the result</param>
public void _SelectMethodName_(DataTable dt)
{
    // Set the SQL (Select) from the file.
    this.SetSqlByFile2("_SelectFileName_");

    // Set the parameters
    this.SetParametersFromHt();

    // Execute the SQL (Select) and return the return value.
    this.ExecSelectFill_DT(dt);
}

/// <summary>Specifies search conditions and references the result set.</summary>
/// <param name="dt">DataTable to store the result</param>
public void _DynSelMethodName_(DataTable dt)
{
    // Set the SQL (DynSel) from the file.
    this.SetSqlByFile2("_DynSelFileName_");

    // Set the parameters
    this.SetParametersFromHt();

    // Execute the SQL (DynSel) and return the return value.
    this.ExecSelectFill_DT(dt);
}

#endregion

4.2 SQL template

The customization point of the SQL templates is basically only the header part (the .sql and .xml files under ~\root\files\DGenTemplates\). The values are embedded at generation time in the file name (_FileName_), creation date and time (_TimeStamp_) and author (_UserName_).

Figure 4.2-1 Comment header of the SQL template

It is a good idea to create the comment header with the SQL trace output in mind.

Figure 4.2-2 Comment header information output to the SQL trace file

5. Examples of customizing the processing

This chapter shows customization points of the generated Dao and SQL files.

5.1 Insert processing

If there are columns with a default value, Identity (auto-increment) columns, or columns into which data is inserted using the SEQUENCE NEXTVAL function, etc., modify the SQL directly.

Figure 5.1 Example of an SQL file for Insert processing

5.1.1 When inserting with default values

When inserting into columns with a default value or Identity columns, no value is set at Insert time, so delete those columns from the SQL.

Figure 5.1.1 When inserting with default values

5.1.2 When inserting data using a function

If there are columns into which data is inserted using the SEQUENCE NEXTVAL function, etc., modify the SQL so that the value is set using the function at Insert time.

※ In Oracle, equivalents of Identity (auto-increment) columns and TimeStamp (auto-increment) columns can be built by yourself using BEFORE INSERT triggers and UPDATE triggers [5].

Figure 5.1.2 When inserting using a function

Footnotes

  1. In the case of auto-increment columns such as Identity and TimeStamp, and timestamp columns with a DEFAULT value.
  2. Between, Like, complex conditional expressions, etc.
  3. Join, special search conditions, Insert / Update queries using Select, etc.
  4. Refer to where the Dao and SQL files are used in the sample programs.
  5. Alternative implementation using Oracle BEFORE INSERT / UPDATE triggers.

-End-

Clone this wiki locally