我知道这是一个老话题,但我认为它永远不会过时。我现在正在开发类似的东西。以下是我的方法。我使用了一个服务器设置,其中包括MySQL、Apache、PHP和Zend Framework 2作为应用程序框架,但任何其他设置也应该能够正常工作。
下面是一个简单的实现指南,你可以从中进一步发展。
你需要实现自己的查询语言解释器,因为有效的SQL语句会太复杂。
例如:
select id, password from user where email_address = "xyz@xyz.com"
物理数据库布局:
表'specs':(应该缓存在您的数据访问层中)
- id:整数
- parent_id:整数
- name:varchar(255)
表'items':
- id:整数
- parent_id:整数
- spec_id:整数
- data:varchar(20000)
表'specs'的内容:
- 1,0,'user'
- 2,1,'email_address'
- 3,1,'password'
表'items'的内容:
- 1,0,1,''
- 2,1,2,'xyz@xyz.com'
- 3,1,3,'my password'
我们自己查询语言示例的翻译:
select id, password from user where email_address = "xyz@xyz.com"
转换为标准SQL的代码如下:
select
parent_id,
data
from
items
where
spec_id = 3
and
parent_id in
(
select
id
from
items
where
spec_id = 1
and
id in
(
select
parent_id
from
items
where
spec_id = 2
and
data = "xyz@xyz.com"
)
)
你需要将规格表缓存在关联数组或哈希表中,以便从规格名称中获取spec_id。否则,你需要插入更多的SQL开销来从名称中获取spec_id,例如在此代码片段中:
不好的例子,请勿使用,避免使用,应该缓存规格表!
select
parent_id,
data
from
items
where
spec_id = (select id from specs where name = "password")
and
parent_id in (
select
id
from
items
where
spec_id = (select id from specs where name = "user")
and
id in (
select
parent_id
from
items
where
spec_id = (select id from specs where name = "email_address")
and
data = "xyz@xyz.com"
)
)
我希望你能理解这个想法,并自行判断这种方法是否适合你。祝愉快!:-)