PostgreSQL
 sql >> डेटाबेस >  >> RDS >> PostgreSQL

केवल एक स्कीमा के लिए स्लीक कोड जनरेशन

उचित मान और स्कीमा नाम के साथ इस कोड का प्रयोग करें,

object CodeGenerator {

  def outputDir :String =""
  def pkg:String =""
  def schemaList:String = "schema1, schema2"
  def url:String  = "dburl"
  def fileName:String =""
  val user = "dbUsername"
  val password = "dbPassword"
  val slickDriver="scala.slick.driver.PostgresDriver"
  val JdbcDriver = "org.postgresql.Driver"
  val container = "Tables"


  def generate() = {    

    val driver: JdbcProfile = buildJdbcProfile

    val schemas = createSchemaList

    var model = createModel(driver,schemas)

    val codegen = new SourceCodeGenerator(model){
      // customize Scala table name (table class, table values, ...)
      override def tableName = dbTableName => dbTableName match {
        case _ => dbTableName+"Table"
      }
      override def code = {
        //imports is copied right out of
        //scala.slick.model.codegen.AbstractSourceCodeGenerator
        val imports = {
          "import scala.slick.model.ForeignKeyAction\n" +
            (if (tables.exists(_.hlistEnabled)) {
              "import scala.slick.collection.heterogenous._\n" +
                "import scala.slick.collection.heterogenous.syntax._\n"
            } else ""
              ) +
            (if (tables.exists(_.PlainSqlMapper.enabled)) {
              "import scala.slick.jdbc.{GetResult => GR}\n" +
                "// NOTE: GetResult mappers for plain SQL are only generated for tables where Slick knows how to map the types of all columns.\n"
            } else ""
              ) + "\n\n" //+ tables.map(t => s"implicit val ${t.model.name.table}Format = Json.format[${t.model.name.table}]").mkString("\n")+"\n\n"
        }


        val bySchema = tables.groupBy(t => {
          t.model.name.schema
        })
        val schemaFor = (schema: Option[String]) => {
          bySchema(schema).sortBy(_.model.name.table).map(
            _.code.mkString("\n")
          ).mkString("\n\n")
        }
      }

      val joins =   tables.flatMap( _.foreignKeys.map{  foreignKey  =>
        import foreignKey._
        val fkt =   referencingTable.TableClass.name
        val pkt =   referencedTable.TableClass.name
        val columns =   referencingColumns.map(_.name)  zip
          referencedColumns.map(_.name)
        s"implicit def autojoin${fkt + name.toString} = (left:${fkt} ,right:${pkt}) =>  "   +
          columns.map{
            case    (lcol,rcol) =>
              "left."+lcol  +   "   === "   +   "right."+rcol
          }.mkString("  &&  ")
      })

      override def entityName = dbTableName => dbTableName match {
        case _ => dbTableName
      }

      override def Table = new Table(_) {
        table =>
        // customize table value (TableQuery) name (uses tableName as a basis)
        override def TableValue = new TableValue {
          override def rawName = super.rawName.uncapitalize
        }
        // override generator responsible for columns
        override def Column = new Column(_){
          // customize Scala column names
          override def rawName = (table.model.name.table,this.model.name) match {

            case _ => super.rawName
          }
        }
      }

    }

    println(outputDir+"\\"+fileName)
    (new File(outputDir)).mkdirs()
    val fw = new FileWriter(outputDir+File.separator+fileName)
    fw.write(codegen.packageCode(slickDriver, pkg, container))
    fw.close()

  }

  def createModel(driver: JdbcProfile, schemas:Set[Option[String]]): Model = {
    driver.simple.Database
      .forURL(url, user = user, password = password, driver = JdbcDriver)
      .withSession { implicit session =>
      val filteredTables = driver.defaultTables.filter(
        (t: MTable) => schemas.contains(t.name.schema)
      )
      PostgresDriver.createModel(Some(filteredTables))
    }
  }

  def createSchemaList: Set[Option[String]] = {
    schemaList.split(",").map({
      case "" => None
      case (name: String) => Some(name)
    }).toSet
  }

  def buildJdbcProfile: JdbcProfile = {

    val module = currentMirror.staticModule(slickDriver)
    val reflectedModule = currentMirror.reflectModule(module)
    val driver = reflectedModule.instance.asInstanceOf[JdbcProfile]
    driver

  }
}


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. ट्रांजिटिव क्लोजर के लिए उपयोग की जाने वाली रिकर्सिव क्वेरी

  2. PostgreSQL त्रुटि:EXECUTE का क्वेरी स्ट्रिंग तर्क शून्य है

  3. विशिष्टता को लागू करने के लिए एक बहु-स्तंभ अनुक्रमणिका बनाएं

  4. 8589934592 बाइट्स की अनुमत स्मृति आकार समाप्त हो गया

  5. postgresql इस क्वेरी में अनुक्रमणिका का उपयोग क्यों नहीं करता है