我正在向EA编写插件,我想在存储库中创建新表但我不想为每种数据库类型编写查询,而且我也不知道如何编写一个查询,因为eq。 AUTO_INCREMENT
存在于MySQL中但在PostgreSQL中并不存在。所以你有什么建议?我应该使用nHibernate还是别的什么?
这是PostgreSQL中的查询我需要重写到其他数据库:
CREATE OR REPLACE FUNCTION create_history ()
RETURNS void AS
$_$
BEGIN
IF EXISTS (
SELECT *
FROM pg_catalog.pg_tables
WHERE schemaname = 'public'
AND tablename = 't_history'
) THEN
RAISE NOTICE ' ';
ELSE
CREATE TABLE t_history
(
id integer NOT NULL DEFAULT nextval(('"object_id_seq"'::text)::regclass),
object_id integer NOT NULL,
object_type character varying(255),
diagram_id integer DEFAULT 0,
name character varying(255),
alias character varying(255),
author character varying(255),
ch_author character varying(255),
version character varying(50) DEFAULT '1.0'::character varying,
note text,
package_id integer DEFAULT 0,
stereotype character varying(255),
ntype integer DEFAULT 0,
createddate timestamp without time zone DEFAULT now(),
status character varying(50),
abstract character(1),
tagged integer DEFAULT 0,
pdata1 character varying(255),
pdata2 text,
pdata3 text,
pdata4 text,
pdata5 character varying(255),
concurrency character varying(50),
visibility character varying(50),
persistence character varying(50),
cardinality character varying(50),
gentype character varying(50),
genfile character varying(255),
header1 text,
header2 text,
phase character varying(50),
scope character varying(25),
genoption text,
genlinks text,
classifier integer,
ea_guid character varying(40),
parentid integer,
runstate text,
classifier_guid character varying(40),
tpos integer,
isroot integer DEFAULT 0,
isleaf integer DEFAULT 0,
isspec integer DEFAULT 0,
isactive integer DEFAULT 0,
stateflags character varying(255),
packageflags character varying(255),
multiplicity character varying(50),
styleex text,
actionflags character varying(255),
eventflags character varying(255),
CONSTRAINT t_history_pkey PRIMARY KEY (id)
);
END IF;
END;
$_$ LANGUAGE plpgsql;
SELECT create_history();
修改
更改了查询
的MySQL
CREATE TABLE IF NOT EXISTS t_history
(
id integer NOT NULL AUTO_INCREMENT,
object_id integer NOT NULL,
name character varying(255),
author character varying(255),
ch_author character varying(255),
version character varying(50) DEFAULT '1.0',
note text,
package_id integer DEFAULT 0,
stereotype character varying(255),
createddate timestamp DEFAULT now(),
pdata1 character varying(255),
pdata2 text,
pdata3 text,
pdata4 text,
phase character varying(50),
CONSTRAINT t_history_pkey PRIMARY KEY (id)
);
MSSQL
if not exists (select * from sys.tables where name = 't_history' and type = 'U')
CREATE TABLE t_history
(
id integer NOT NULL IDENTITY PRIMARY KEY,
object_id integer NOT NULL,
name character varying(255),
author character varying(255),
ch_author character varying(255),
version character varying(50) DEFAULT '1.0',
note text,
package_id integer DEFAULT 0,
stereotype character varying(255),
createddate datetime DEFAULT getdate(),
pdata1 character varying(255),
pdata2 text,
pdata3 text,
pdata4 text,
phase character varying(50)
);
答案 0 :(得分:1)
鉴于您的脚本很小,它可能是手动"翻译"的最佳方法。它到Oracle / MySQL /您需要的任何数据库系统。它不应该花费超过几分钟,您可以充分利用查找/替换功能。
e.g。像这样:
Find: character varying
Replace with: VARCHAR2
Find: text
Replace with: CLOB
除非你已经有一个很大的C#应用程序。在这种情况下,您可以使用nHibernate(我假设它就像Hibernate for Java)并实现适当的类,函数和注释/配置。如果您计划将来使用更多SQL脚本,这种方法也有意义。
如果这是你将要使用的唯一脚本,Hibernate虽然相当矫枉过正。它会产生巨大的开销,并且需要花费更多的时间来进行设置而不仅仅是简单地翻译"将脚本转换成各种其他SQL方言。如果您要与数据库进行大量交易,这才有意义。