Petapoco-sqlserver Model Add Comment

Source: Internet
Author: User

Petapoco is a lightweight ORM based on the T4 template, with high efficiency and a detailed

The rationale for getting annotations is to call the database:: Fn_listextendedproperty function, get extended properties Ms_description

TechNet Reference: sys.fn_listextendedproperty (Transact-SQL)

First, the PetaPoco.Core.ttinclude

1, 106 lines of original:

 Public string Sequencename;  Public BOOL Ignore;

New Description field, this is the comment of the table

 Public string Sequencename;  Public BOOL Ignore;  Public string Description;

2, 140 lines of original:

 Public string Sequencename;  Public BOOL Ignore;

New Description field, this is the comment for the column

 Public string Sequencename;  Public BOOL Ignore;  Public string Description;

3, 517 lines of original:

foreach (var in result) {    tbl. Columns=loadcolumns (TBL);                         // Mark The primary key    string primarykey=GETPK (tbl. Name);     var pkcolumn=tbl. Columns.singleordefault (X=>x.name.tolower (). Trim () = =Primarykey.tolower (). Trim ());     if (pkcolumn!=null)    {        PKCOLUMN.ISPK=true;    }}

After modification, call the function to get the table comment:

foreach(varTblinchresult) {    using(varCmddesc=_factory. CreateCommand ()) {cmddesc.connection=connection; Cmddesc.commandtext=Table_desc_sql; DbParameter P=NULL; P=Cmddesc.createparameter (); P.parametername="@schema"; P.value=tbl.        Schema;        CMDDESC.PARAMETERS.ADD (P); P=Cmddesc.createparameter (); P.parametername="@table"; P.value=tbl.        Name;        CMDDESC.PARAMETERS.ADD (P); using(varRdrdesc =Cmddesc.executereader ()) {            if(Rdrdesc.read ()) tbl. Description= rdrdesc["value"].        ToString (); }} tbl. Columns=Loadcolumns (TBL); //Mark the primary key    stringprimarykey=GETPK (tbl.    Name); varPkcolumn=tbl. Columns.singleordefault (X=>x.name.tolower (). Trim () = =Primarykey.tolower ().    Trim ()); if(pkcolumn!=NULL) {PKCOLUMN.ISPK=true; }}

4, 572 lines original, insert new code, get the comments for each column (return result above):

foreach(varColinchresult) {    using(varCmddesc =_factory. CreateCommand ()) {cmddesc.connection=_connection; Cmddesc.commandtext=Column_desc_sql; DbParameter Pdesc=NULL; Pdesc=Cmddesc.createparameter (); Pdesc.parametername="@schema"; Pdesc.value=tbl.        Schema;        CMDDESC.PARAMETERS.ADD (PDESC); Pdesc=Cmddesc.createparameter (); Pdesc.parametername="@table"; Pdesc.value=tbl.        Name;        CMDDESC.PARAMETERS.ADD (PDESC); Pdesc=Cmddesc.createparameter (); Pdesc.parametername="@column"; Pdesc.value=Col.        Name;        CMDDESC.PARAMETERS.ADD (PDESC); using(varRdrdesc =Cmddesc.executereader ()) {            if(Rdrdesc.read ()) Col. Description= rdrdesc["value"].        ToString (); }    }}

Lines 5, 672, 688 are original, insert new code, and store the SQL statement that called the function:

Const string @" SELECT value from:: Fn_listextendedproperty (' ms_description ', ' user ', @schema, ' table ', @table, NULL, NULL) " ; Const string @" SELECT value from:: Fn_listextendedproperty (' ms_description ', ' user ', @schema, ' table ', @table, ' column ', @column) /c9>";

Now that the comments have been obtained from the database, you need to insert the comments into the T4 template!

Ii. Modification of PetaPoco.Generator.ttinclude

1, 141 lines of original:

<#foreachin from inwhereselect  t) {# >   if (string. IsNullOrEmpty (TBL. Schema) {#>    [TableName ("<#=tbl. name#>")]else {#>

To add a Description comment to a table:

<#foreach(Table TBLinch  fromTinchTableswhere!t.ignoreSelectt) {#>/// <summary>    /// <#=tbl. Description?? "" #>    /// </summary><#if(string. IsNullOrEmpty (TBL. Schema) {#>[TableName ("<#=tbl. Name#>")]<#}Else{#>

2, 167 lines of original:

<#foreachin from inwhereselect  c) {         //  Column bindingsif (trackmodifiedcolumns) {#>

To add a Description comment for a column:

<#foreachin from inwhereselect  c) {        // Column bindings#> //<summary>///                  <#=col. Description?? "" #>        //</summary>if (trackmodifiedcolumns) {#>

This will be changed, open Database.tt Press Ctrl+s will be able to update the obtained comments!

In addition getinstance incredibly not a single case method, this simply can't endure, decisive change:

Petapoco.generator.ttinclude,38 Line modified to:

 Public Static<#=RepoName#>getinstance () {if(_instance!=NULL)        return_instance; if(factory!=NULL)        returnfactory.getinstance (); //Else//return new <#=RepoName#> ();    return_instance =New<#=RepoName#>();}

Petapoco-sqlserver Model Add Comment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.