山寨版 dbext
需要有python支持的vim,windows版的gvim已经有python支持
需要安装python2.4(其他版本好像不行)
pymssql,cx_oracle.
"==================================================
" File: SzDbext.vim
" Brief: 山寨版dbext
" Authors: shrek<shrek.wang AT gmail DOT com>
" Last Change: 2009-01-20 22:33:06
" Version: 0.00000000000000000000000000000002
" Licence: LGPL
"
"
" Usage: 1:修改SzDbext.vim中的数据库连接参数
" 2:放到 plugin 目录下
" 3:编辑任竟文件
" 4:用v命令选中文本块
" 4: ,, 查询选定的sql
" ,dt 查询选定文本的表结构
" ,lt 列出包含选定文本的表
"
" Todo: 1:将数据库的连接参数用vim变量独立出来
" 2:支持ddl
let g:selection=-1
python profile={}
function! GetDbOption()
python << EOF
from vim import *
aa=dict(host="192.168.0.280",user="**",password="**",database="data1")
bb=dict(host="192.168.0.281",user="**",password="**",database="sat2")
cc=dict(host="192.168.0.282",user="**",password="**",database="data3")
array=[aa,bb,cc]
selection=eval("g:selection")
if int(selection) < 0 :
for index,item in enumerate(array):
print str(index) + ": "+item["host"]+": "+item["database"]
command("let g:selection=input('please enter a selection')")
selection=eval("g:selection")
profile=array[int(selection)]
print profile
EOF
endfunction
function! GetVisualBlock() range
let save = @"
silent normal gvy
let vis_cmd = @"
let @" = save
return vis_cmd
endfunction
function! SwitchToSQLResult()
let s:cur_buf = bufnr("%")
let s:szdb_result_buf=bufnr("SzdbResult")
if bufwinnr(s:szdb_result_buf) > 0
exec bufwinnr(s:szdb_result_buf) . "wincmd w"
%d
else
exec 'silent! botright split SzdbResult'
exec "e SzdbResult"
exec "set nowrap"
endif
endfunction
function! QueryVisualSQL() range
python << EOF
import vim
vb=vim.eval("GetVisualBlock()")
lines=vb.split("\n")
sql=""
for line in lines:
sql=sql+line+" "
vim.command('call QuerySQL("'+sql+'")')
EOF
endfunction
function! QuerySQL(sql)
python << EOF
import vim
if not profile:
vim.command("call GetDbOption()")
conn=""
if profile.has_key("sid") :
import cx_Oracle
dns_tns=cx_Oracle.makedsn(profile["host"],1521,profile["sid"])
conn = cx_Oracle.connect(profile["user"], profile["password"], dns_tns)
else :
import pymssql
conn = pymssql.connect(host=profile["host"],user=profile["user"], \
password=profile["password"],database=profile["database"])
cur = conn.cursor()
sql=vim.eval("a:sql")
cur.execute(sql)
columns,maxlens=[],[]
vim.command("call SwitchToSQLResult()")
if cur.description:
for column in cur.description:
columns.append(column[0])
maxlens.append(0)
resultset=[columns]
for row in cur.fetchall():
for index,field in enumerate(row):
if (len(str(field))>maxlens[index]):
maxlens[index]=len(str(field))
resultset.append(row)
for index,field in enumerate(columns):
if (len(str(field))>maxlens[index]):
maxlens[index]=len(str(field))
headline=""
for item in maxlens:
headline=headline+"+"+("-"*item)+"--"
headline=headline+"+"
for rowindex,row in enumerate(resultset):
line=""
for index,field in enumerate(row):
line=line+"| "+str(field).replace("\n","").ljust(maxlens[index]+1)
if rowindex<2: vim.current.buffer.append(headline)
vim.current.buffer.append( line+"|")
vim.current.buffer.append( headline)
vim.current.buffer[0]="Host:"+profile["host"]+" Database:"+profile["database"]
vim.current.buffer.append("affected "+str(cur.rowcount)+" rows.")
conn.commit()
cur.close()
conn.close()
EOF
redraw!
exec bufwinnr(s:cur_buf) . "wincmd w"
endfunction
function! QueryTables()
python << EOF
import vim
vb=vim.eval("GetVisualBlock()")
if profile.has_key("sid"):
sql="select table_name from user_tables where table_name like '%"+vb.upper()+"%'"
else:
sql="SELECT name FROM sysobjects Where name like '%"+vb+"%' and type = 'U' order by name"
vim.command('call QuerySQL("'+sql+'")')
EOF
endfunction
function! DescTable()
python << EOF
import vim
vb=vim.eval("GetVisualBlock()")
if profile.has_key("sid"):
sql="select column_name,data_type from user_tab_columns where table_name = '"+vb.upper()+"'"
else:
sql="""Select col.[name] as '字段名', type.[name] as '类型' , col.[length]as '长度' \
From syscolumns as col \
Left Join systypes as type on col.xtype = type.xtype \
where col.id = (Select id From sysobjects Where name = '"""+vb+"')"
vim.command('call QuerySQL("'+sql+'")')
EOF
endfunction
map <silent>,, :call QueryVisualSQL()<cr>
map <silent>,dt :call DescTable()<cr>
map <silent>,lt :call QueryTables()<cr>
分享到:
相关推荐
笨方法学 Vim Script
python.vim_3.3
pythonwin for 2.5 vim编译器,是自己好不容易收集到的,希望大家喜欢
vimscript,由浅入深,勤加练习,写出让大家都喜欢用的vim插件,需要的知识都在这里。
Python-mode:将 Vim 变成 Python IDE 的一款多合一插件
*dbext* *dbext.vim* *db_ext* *db_ext.vim* *database-extension* *pgsql* *mysql* *asa* *ase* *ingres* *interbase* *sqlite* *sqlsrv* *ora* *db2* 1. Overview |dbext-overview| 2. Installation |...
Learn Vimscript the Hard Way PDF版,找了很久才找到
python.vim taglist pydiction 三个插件uzip 解压~
learn vimscript the hard way的mobi版。方便用kindle来离线阅读。
vim-jsx-pretty, [Vim script] 对vim的JSX语法非常突出 vim-jsx-pretty演示语法vim-jsx-pretty( 添加彩色配置) vim-jsx-pretty mxw/vim-jsx 自动缩进( 使用 vim-javascript )
包中包括: python.vim pythoncomplete.vim nerdtree.zip vim-autocomplpop.zip 四个插件及vim官网中对应的说明。 送给那些不能上官网的青年。
Vimapt是一个vim包管理器,"vimapt"是"Vim's Advantage Package Tools"的缩写
前言 vim是个伟大的编辑器,不仅在于她特立独行的编辑方式,还在于她强大的扩展能力。...在编译之前,configure的时候加上–enable-pythoninterp和–enable-python3interp选项,使之分别支持Python2和Pytho
python-vim这是我自己精简后的python vim开发包环境。实还有更加丰富的功能,但那只会让vim速度变得很慢。 所以我要说的是,并不是插件越多越好,而是越精简越好,把不用的插件都在.vimrc中注释掉。更多的介绍python...
针对python的VIM配置文件,包含如何配置YCM过程,具体不懂欢迎咨询。
本书将向您介绍Vimscript,这是用于自定义Vim并使您在Vim中的其余时间更有效的主要编程语言。
Vimscript的面向对象框架,外观和感觉都类似于Python,并力求最大程度地提高便利性并与内置函数兼容。 阅读: Vim和Python是朋友,让我们做更多吧! 例子 定义类并创建实例。 let MyClass = object#class ( ' ...