论坛首页 编程语言技术论坛

d语言写的一个简单的sql生成器

浏览 2991 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-01-16  
简陋的封装了一些单表操作

源代码

template eval( A... )
{
    const typeof(A[0]) eval = A[0];
}
char[] escape(char[] c){
    return "\""~c~"\"";
}
char[] column_def(char[] temp){
    return "typeof(this) " ~ temp ~ "(char[] v){ this.column[\"" ~ temp ~ "\"]=v;return this;}\n";
}

char[] table(char[] names)
{
    char[] table="char[][char[]] column;\n";
    char[] temp="";

    foreach (c;names){
        if(c==' '){
            table ~=column_def(temp);
            temp="";
        }else{
            temp~=c;
        }
    }
    if(temp!="")table~=column_def(temp);
    return table;
}

template Sql(char[] table_name,char[] column_list,char[] primary="id"){
   //TODO:assert primary in column_list

   mixin(table(column_list));

   typeof(this) opAssign(T)(T t){
        column=null;
        foreach(k,v;t)column[k]=v;
        return this;
   }
   char[] toString(){
       char[] result=table_name~"\n";
       foreach(k,v;column){
            result~="\t"~k~" = "~v~"\n";
       }
       return result;
   }
   char[] save(){
        if(primary in column)return update();
        else return insert();
   }
   
   char[] update(){
       assert(column[primary]!=null);
       char[] list="";
       foreach(k,v;column){
            if(k!=primary)
            list~=k~"="~escape(v)~",";
       }
       if(list!=""){
           list=list[0..$-1];
        }
       return "UPDATE "~table_name~" SET "~list~" WHERE "~primary~"="~escape(column[primary])~";";
      //UPDATE Person SET Address = 'Stien 12', City = 'Stavanger' WHERE LastName = 'Rasmussen'
   }
   char[] del(){
        assert(column[primary]!=null);
        return "DELETE FROM "~table_name~" WHERE "~primary~"="~escape(column[primary])~";";
   }
   char[] insert(){
       char[] key_list="";
       char[] value_list="";
       foreach(k,v;column){
            key_list~=k~",";
            value_list~=escape(v)~",";
       }
       if(key_list!=""){
           key_list=key_list[0..$-1];
           value_list=value_list[0..$-1];
        }
       return "INSERT INTO "~table_name~"("~key_list~") VALUES("~value_list~");";
       //INSERT INTO transcount_year(year, room_idx, dns_idx, redirip_id) VALUES(%d,%d,%d,%d);
   }

   static char[] where(char[] statement){
        return "SELECT * FROM "~table_name~" WHERE "~statement~";";
   }
   static char[] all(){
        return "SELECT * FROM "~table_name~";";
   }
}

用法演示
class ErrDnameMonth{
   mixin Sql!("conf_err_dname_month","id month dname dns_query_count web_hit_count");
}
void main(){

    Cout(ErrDnameMonth.where("id=1")).newline.newline;

    auto month=new ErrDnameMonth;

    month
        .month("12")
        .dname("www.donews.net")
        .dns_query_count("23242");

    month.web_hit_count="124";

    Cout(month.toString).newline.newline;
    Cout(month.insert).newline.newline;
    Cout(month.all).newline.newline;
    Cout(month.save).newline.newline;
    
    
    MysqlMgr mysql_mgr_;//假设这是一个数据库封装
    Row[] rows;//取回的数据为字典格式
    mysql_mgr_ = new MysqlMgr();
    rows = mysql_mgr_.queryFetchAll(month.all);
    
    foreach(r;rows){
        auto em=new ErrDnameMonth;
        em=r;
        em.dns_query_count="23";
        oo.format("{}",em.update).newline.newline;
        oo.format("{}",em.del).newline.newline;
    }

    Cin.get();
}
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics