DbTableAssistantService.java 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package com.sooka.common.db;
  2. import com.sooka.common.db.impl.M;
  3. import com.sooka.common.db.impl.MysqlDbTableAssistant;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.beans.factory.annotation.Qualifier;
  8. import org.springframework.stereotype.Service;
  9. import javax.sql.DataSource;
  10. import java.sql.Connection;
  11. import java.sql.SQLException;
  12. import java.sql.Statement;
  13. import static com.sooka.common.db.impl.M.BIG_INT_TYPE;
  14. /**
  15. * Description:很神奇的一个类 哈哈
  16. *
  17. *
  18. * @create 2017-05-08
  19. *
  20. **/
  21. @Service
  22. public class DbTableAssistantService{
  23. private final static Logger log = LoggerFactory.getLogger(DbTableAssistantService.class);
  24. private final String PRIMARY_KEY = "content_id";
  25. private final String TABLE_PREFIX = "t_cms_content_";
  26. @Autowired
  27. private MysqlDbTableAssistant mysql;
  28. @Autowired
  29. @Qualifier("masterDataSource")
  30. private DataSource dataSource;
  31. public void createDbtable(String tableName) throws SQLException {
  32. String sql = mysql.create().TableName(TABLE_PREFIX+tableName).InitColumn(PRIMARY_KEY, BIG_INT_TYPE,20,false,"0",true,true).BuilderSQL();
  33. Connection connection = dataSource.getConnection();
  34. Statement statement = connection.createStatement();
  35. statement.execute(sql);
  36. statement.close();
  37. connection.close();
  38. }
  39. public void deleteDbtable(String tableName) throws SQLException {
  40. log.info("create table [{}] begin",tableName);
  41. String sql = mysql.delete().TableName(TABLE_PREFIX+tableName).BuilderSQL();
  42. Connection connection = dataSource.getConnection();
  43. Statement statement = connection.createStatement();
  44. statement.execute(sql);
  45. statement.close();
  46. connection.close();
  47. }
  48. public void addDbTableColumn(String tableName, String columnName, M columnType, Integer length, boolean autoIncrement, String defaultValue, boolean isNotNull, boolean isPrimaryKey) throws SQLException {
  49. log.info("create table [{}] column [{}] begin",tableName,columnName);
  50. String sql = mysql.edit().TableName(TABLE_PREFIX+tableName).AddColumn(columnName,columnType,length,autoIncrement,defaultValue,isNotNull,isPrimaryKey).BuilderSQL();
  51. Connection connection = dataSource.getConnection();
  52. Statement statement = connection.createStatement();
  53. statement.execute(sql);
  54. statement.close();
  55. connection.close();
  56. }
  57. public void editDbTableColumn(String tableName, String columnName,String newColumnName, M columnType, Integer length, boolean autoIncrement, String defaultValue, boolean isNotNull) throws SQLException {
  58. log.info("create table [{}] column [{}] begin",tableName,columnName);
  59. String sql = mysql.edit().TableName(TABLE_PREFIX+tableName).ChangeColumn(columnName,newColumnName,columnType,length,autoIncrement,defaultValue,isNotNull).BuilderSQL();
  60. Connection connection = dataSource.getConnection();
  61. Statement statement = connection.createStatement();
  62. statement.execute(sql);
  63. statement.close();
  64. connection.close();
  65. }
  66. public void deleteDbTableColumn(String tableName, String columnName, boolean isPrimaryKey) throws SQLException {
  67. log.info("create table [{}] column [{}] begin",tableName,columnName);
  68. String sql = mysql.edit().TableName(TABLE_PREFIX+tableName).DropColumn(columnName,isPrimaryKey).BuilderSQL();
  69. Connection connection = dataSource.getConnection();
  70. Statement statement = connection.createStatement();
  71. statement.execute(sql);
  72. statement.close();
  73. connection.close();
  74. log.info("created table [{}] column [{}] success",tableName,columnName);
  75. }
  76. }