Javascript、C#、php、asp、python 等語言的鏈式操作實現
一、什么是鏈式操作
把需要的下一步操作的對象通過上一步操作返回回來。使完成某些功能具有持續性。
二、鏈式操作優點
代碼更精簡優雅。鏈式操作能大大精簡代碼量,多項操作一行代碼一氣呵成,搞定;
鏈式操作應用場景除了想前端jquery方面的操作dom,后端web框架,在開發orm相關的框架也是常常用到
三、各種語言的鏈式操作實現
以下根據簡單的實現數據庫查詢的類似實現一些web框架的 orm。查詢類的各種DEMO
1、javascript 的鏈式操作大家最熟悉不過了,jquery 的整個框架就是鏈式操作實現,我這里寫的demo 當然跟jquery 實現的思路不是一致的,jquery具體實現方式可以看jquery源碼。
var Link = function (table){ this.sql = { "table" : "", "where" : " 1=1 ", "limit" : 0, "field" : "" } this.sql.table = table;
}
Link.prototype = {
where : function (where)
{ this.sql.where += " and "+ where; return this;
},
field : function (field)
{ this.sql.field = field; return this;
},
top : function (limit)
{ this.sql.limit = limit; return this;
},
query : function ()
{ var sqlstr = "SELECT "+this.sql.field+" FROM "+this.sql.table +" WHERE "+ this.sql.where+ " LIMIT 0,"+this.sql.limit; return sqlstr;
}
} var db = new Link("user"); var $val = db.where(" id = 1")
.top(10)
.field(" id, username , password")
.query();
console.log($val); 2、C# 實現鏈式,主要通過其.net 框架提供的擴展方法,代碼如下
using System; namespace ConsoleApplication1
{ public class sqlHelp
{ public sqlHelp (string table) { this.table = table;
} public string where = ""; public string field = ""; public int limit = 0; public string table = "";
} static class LinkExtended
{ public static sqlHelp Where(this sqlHelp h, string where)
{
h.where += " and"+ where; return h;
} public static sqlHelp Top(this sqlHelp h, int limit)
{
h.limit = limit; return h;
} public static sqlHelp Field(this sqlHelp h, string field)
{
h.field = field; return h;
} public static string Query(this sqlHelp h)
{ string sql = string.Format("SELECT {0} FROM {1} WHERE 1=1 {2} LIMIT 0, {3}",
h.field, h.table, h.where, h.limit); return sql;
}
} class Program
{ static void Main(string[] args)
{
sqlHelp db = new sqlHelp("user"); string sql = db.Top(10)
.Field("id, username, password")
.Where(" id =1")
.Query();
Console.WriteLine(sql);
Console.ReadLine();
}
}
} 3、php 實現鏈式操作可以使用php的魔術方法_call。但是我這里實現還是愿意一開始說的返回對象本身的思路沒有用到_call。
<?php class Link
{ static public $_garr= array( 'table'=>'',
'limit' =>0,
'where' => ' 1=1 ',
'field' => ' ' ); function __construct($table)
{
self::$_garr['table'] = $table ;
} public function where($where='')
{
self::$_garr['where'].= ' and '.$where ; return $this;
} public function field($field)
{
self::$_garr['field'] = $field ; return $this;
} public function top($top)
{
self::$_garr['limit'] = $top; return $this;
} public function query()
{ $sql_arr = self::$_garr; $sql = "SELECT {$sql_arr['field']} FROM {$sql_arr['table']} WHERE {$sql_arr['where']} LIMIT 0, {$sql_arr['limit']}"; return $sql;
}
}; $db = new Link("user"); $sql = $db->top(10) ->field("id, username, password ") ->where(" id = 1") ->query(); print($sql); ?> 4、asp(vbscript)這個我一直在網上搜索資料也沒見有人寫過, 我自認為我這里寫出來的是全網第一份(當然可能人家有我沒找到而已,呵呵所以自我陶醉下),因為vbscript 本身的類是非常弱的,但是
也提供了 defalut 和 me 這樣好用的特性,所以實現鏈式也不是什么難事( 如果熟練vbscript 的朋友可以能會想到 with .. end with 語言 其實這個語言本身有點鏈式的味道,但是思路上不叫鏈式操作)
Class Link '聲明一個全局字典 Dim sqlD public Default function construct(table) Set sqlD = CreateObject("Scripting.Dictionary") Call sqlD.add("table", table) Set construct = me End function Public Function where(whereStr) Call sqlD.add("where", whereStr) Set Where = me End Function Public Function top(limit) Call sqlD.add("limit", limit) Set top = me End Function Public Function field(fieldStr) Call sqlD.add("field", fieldStr) Set field = me End Function Public Function query() Dim sql
sql = "SELECT "& sqlD.item("field")&" FROM "&sqlD.item("table")&" WEHRE "&sqlD.item("where")
query = sql End function End Class Dim db, sql Set db = (New Link)("user")
sql = db.top(10).field("id, username, password").Where(" id = 1").query() msgbox sql 5、python 實現鏈式非常的方便簡單
class Link: def __init__(self, table):
self.table = table def where(self, where):
self.where = 'and ' +where return self; def field(self, field):
self.field = field return self def top(self, top):
self.limit = top return self; def query(self):
sql = 'SELECT '+self.field+' FROM '+self.table+' WHERE 1=1 '+ self.where+' limit 0,'+self.limit return sql
p = Link('user')
sql = p.top(str(10)).field('id, username, password').where('id=1').query() print(sql) 四、其他需要待續
echosong 在碼農生涯中,暫時只用到了以上些語言,所以目前也只實現了以上的語言鏈式操作,歡迎大家補充其他語言鏈式操作demo
原文 http://www.cnblogs.com/echosong/p/4591271.html 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!