JFinal 调用yourMode.save(),遇到关键字怎么处理?

红薯官方 发布于 2015/05/04 17:39
阅读 348
收藏 2

@JFinal

你好!!

由于jqGrid需要配置,我就做了一个在数据库保存配置。

其中有一个关键字index,这个是jqGrid需要的参数,而且名字不能改动。

发现,在调用myGridColumnMode.save()系统保存,提示关键字不能使用,如果要用,要加[]符号,即[index]。

所以我就改了下方法:

this.set("[index]", index);

再测试:JFinal提示[index]不是一个模型映射的列名。

请教下波哥还有各位高手,这个问题有什么办法可以解决呢?谢谢!

加载中
0
JFinal
JFinal
this.put(key, value) 即可打完收工
JFinal
JFinal
回复 @Twisst : 改一下 SqlServerDialect即可,仿照一下 MysqlDialect
红薯官方
红薯官方
我用的是SQLServer呢!
红薯官方
红薯官方
回复 @JFinal : 就是我的数据库里面有个字段是index,myGridColumnModel.save()的时候就会出错。 原因是index是数据库的一个关键字。但我不能不用这个关键字呀。 查询都没问题,就是保存不了。put是不行的。如果用model.set("[index]", value)则JFinal会抛出类似于“无效的列”异常。 难道要改造dialect么?
JFinal
JFinal
回复 @Twisst : 如果是其它数据库,可以看一下 com.jfinal.plugin.activerecord.dialect.MysqlDialect.java ,学一下即可
JFinal
JFinal
回复 @Twisst : 难道你不是用的 MySql,mysql 已然对关键字做了自动化的处理,添加了字符"`"
下一页
0
红薯官方
红薯官方

@JFinal 如下代码改写,自动处理SQLServer关键字。

package com.rocbin.jfinal.dialect;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.Table;
import com.jfinal.plugin.activerecord.dialect.AnsiSqlDialect;

/**
 * AnsiSqlDialect 扩展,自动SQL关键字作为列名时的语法冲突
 * 
 * @author Rocbin 2015
 * @version 1.0
 *
 */
public class SQLServerDialect extends AnsiSqlDialect {

	private static final String SQL_WORDS_STRING = "ADD,EXTERNAL,PROCEDURE,ALL,FETCH,"
			+ "PUBLIC,ALTER,FILE,RAISERROR,AND,FILLFACTOR,READ,ANY,"
			+ "FOR,READTEXT,AS,FOREIGN,RECONFIGURE,ASC,FREETEXT,"
			+ "REFERENCES,AUTHORIZATION,FREETEXTTABLE,REPLICATION,"
			+ "BACKUP,RESTORE,BEGIN,FULL,RESTRICT,BETWEEN,FUNCTION,"
			+ "RETURN,BREAK,GOTO,REVERT,BROWSE,GRANT,REVOKE,BULK,GROUP"
			+ ",RIGHT,BY,HAVING,ROLLBACK,CASCADE,HOLDLOCK,ROWCOUNT,CASE,"
			+ "IDENTITY,ROWGUIDCOL,CHECK,IDENTITY_INSERT,RULE,CHECKPOINT,"
			+ "IDENTITYCOL,SAVE,CLOSE,IF,SCHEMA,CLUSTERED,IN,SECURITYAUDIT,"
			+ "COALESCE,INDEX,SELECT,COLLATE,INNER,SEMANTICKEYPHRASETABLE,"
			+ "COLUMN,INSERT,SEMANTICSIMILARITYDETAILSTABLE,COMMIT,INTERSECT,"
			+ "SEMANTICSIMILARITYTABLE,COMPUTE,INTO,SESSION_USER,CONSTRAINT,"
			+ "is,SET,CONTAINS,JOIN,SETUSER,CONTAINSTABLE,KEY,SHUTDOWN,CONTINUE,"
			+ "KILL,SOME,CONVERT,LEFT,STATISTICS,CREATE,LIKE,SYSTEM_USER,CROSS,"
			+ "LINENO,TABLE,CURRENT,LOAD,TABLESAMPLE,CURRENT_DATE,MERGE,TEXTSIZE,"
			+ "CURRENT_TIME,NATIONAL,THEN,CURRENT_TIMESTAMP,NOCHECK,CURRENT_USER,"
			+ "NONCLUSTERED,TOP,CURSOR,NOT,TRAN,DATABASE,NULL,TRANSACTION,DBCC,NULLIF,"
			+ "Trigger,DEALLOCATE,OF,TRUNCATE,DECLARE,OFF,TRY_CONVERT,DEFAULT,OFFSETS,"
			+ "TSEQUAL,DELETE,ON,UNION,DENY,OPEN,UNIQUE,DESC,OPENDATASOURCE,UNPIVOT,DISK,"
			+ "OPENQUERY,UPDATE,DISTINCT,OPENROWSET,UPDATETEXT,DISTRIBUTED,OPENXML,USE,DOUBLE,"
			+ "OPTION,USER,DROP,OR,VALUES,DUMP,ORDER,VARYING,ELSE,OUTER,View,END,OVER,WAITFOR,"
			+ "ERRLVL,PERCENT,WHEN,ESCAPE,PIVOT,WHERE,EXCEPT,PLAN,WHILE,EXEC,PRECISION,WITH,"
			+ "EXECUTE,PRIMARY,WITHIN GROUP,EXISTS,PRINT,WRITETEXT,EXIT,PROC";
	private static final String[] SQL_WORDS = SQL_WORDS_STRING.split(",");

	@Override
	public String forTableBuilderDoBuild(String tableName) {
		return "select * from " + tableName + " where 1 = 2";
	}

	@Override
	public void forModelSave(Table table, Map<String, Object> attrs,
			StringBuilder sql, List<Object> paras) {
		sql.append("insert into ").append(table.getName()).append("(");
		StringBuilder temp = new StringBuilder(") values(");
		for (Entry<String, Object> e : attrs.entrySet()) {
			String colName = e.getKey();
			if (table.hasColumnLabel(colName)) {
				if (paras.size() > 0) {
					sql.append(", ");
					temp.append(", ");
				}
				sql.append(handlerColName(colName));
				temp.append("?");
				paras.add(e.getValue());
			}
		}
		sql.append(temp.toString()).append(")");
	}

	@Override
	public String forModelDeleteById(Table table) {
		String pKey = table.getPrimaryKey();
		StringBuilder sql = new StringBuilder(45);
		sql.append("delete from ");
		sql.append(table.getName());
		sql.append(" where ").append(pKey).append(" = ?");
		return sql.toString();
	}

	@Override
	public void forModelUpdate(Table table, Map<String, Object> attrs,
			Set<String> modifyFlag, String pKey, Object id, StringBuilder sql,
			List<Object> paras) {
		sql.append("update ").append(table.getName()).append(" set ");
		for (Entry<String, Object> e : attrs.entrySet()) {
			String colName = e.getKey();
			if (!pKey.equalsIgnoreCase(colName) && modifyFlag.contains(colName)
					&& table.hasColumnLabel(colName)) {
				if (paras.size() > 0)
					sql.append(", ");
				sql.append(handlerColName(colName)).append(" = ? ");
				paras.add(e.getValue());
			}
		}
		sql.append(" where ").append(pKey).append(" = ?");
		paras.add(id);
	}

	@Override
	public String forModelFindById(Table table, String columns) {
		StringBuilder sql = new StringBuilder("select ");
		if (columns.trim().equals("*")) {
			sql.append(columns);
		} else {
			String[] columnsArray = columns.split(",");
			for (int i = 0; i < columnsArray.length; i++) {
				if (i > 0)
					sql.append(", ");
				sql.append(columnsArray[i].trim());
			}
		}
		sql.append(" from ");
		sql.append(table.getName());
		sql.append(" where ").append(table.getPrimaryKey()).append(" = ?");
		return sql.toString();
	}

	@Override
	public String forDbFindById(String tableName, String primaryKey,
			String columns) {
		StringBuilder sql = new StringBuilder("select ");
		if (columns.trim().equals("*")) {
			sql.append(columns);
		} else {
			String[] columnsArray = columns.split(",");
			for (int i = 0; i < columnsArray.length; i++) {
				if (i > 0)
					sql.append(", ");
				sql.append(columnsArray[i].trim());
			}
		}
		sql.append(" from ");
		sql.append(tableName.trim());
		sql.append(" where ").append(primaryKey).append(" = ?");
		return sql.toString();
	}

	@Override
	public String forDbDeleteById(String tableName, String primaryKey) {
		StringBuilder sql = new StringBuilder("delete from ");
		sql.append(tableName.trim());
		sql.append(" where ").append(primaryKey).append(" = ?");
		return sql.toString();
	}

	@Override
	public void forDbSave(StringBuilder sql, List<Object> paras,
			String tableName, Record record) {
		sql.append("insert into ");
		sql.append(tableName.trim()).append("(");
		StringBuilder temp = new StringBuilder();
		temp.append(") values(");

		for (Entry<String, Object> e : record.getColumns().entrySet()) {
			if (paras.size() > 0) {
				sql.append(", ");
				temp.append(", ");
			}
			sql.append(handlerColName(e.getKey()));
			temp.append("?");
			paras.add(e.getValue());
		}
		sql.append(temp.toString()).append(")");
	}

	@Override
	public void forDbUpdate(String tableName, String primaryKey, Object id,
			Record record, StringBuilder sql, List<Object> paras) {
		sql.append("update ").append(tableName.trim()).append(" set ");
		for (Entry<String, Object> e : record.getColumns().entrySet()) {
			String colName = e.getKey();
			if (!primaryKey.equalsIgnoreCase(colName)) {
				if (paras.size() > 0) {
					sql.append(", ");
				}
				sql.append(handlerColName(colName)).append(" = ? ");
				paras.add(e.getValue());
			}
		}
		sql.append(" where ").append(primaryKey).append(" = ?");
		paras.add(id);
	}

	/**
	 * 处理列名,如果是关键字则返回'[' + 列名 + ']'
	 * 
	 * @param name
	 * @return
	 */
	public String handlerColName(String name) {
		boolean isSqlWord = false;
		for (int i = 0; i < SQL_WORDS.length; i++) {
			if (name.equalsIgnoreCase(SQL_WORDS[i])) {
				isSqlWord = true;
				break;
			}
		}
		if (isSqlWord) {
			return "[".concat(name).concat("]");
		} else {
			return name;
		}
	}
}



0
夜辰
夜辰
可以尝试vo的model类
返回顶部
顶部