GithubHelp home page GithubHelp logo

sql-generator's Introduction

SQL Generator

This library supports generating SQL from Excel sheets (.xls), CSV files and other formats.

Getting started

First, install sbt and giter8.

You can create a simple project with a giter8 template.
Download template at the directory where the source .xls are.

	> g8 geishatokyo/sql-gen

Then

	> sbt run

The library finds all the .xls files in the directory and generates insert, delete and update SQL files from them.

Data sheet formats

Excel (.xls) files

Each sheet corresponds to a database table.

The sheet name should be the desired table name.
Within the sheet, the first row must be composed of the column names.
Later rows are interpreted as records.

Define project

You write your conversion rules in your project class. Sample code is below.

import com.geishatokyo.sqlgen.project2._
object YourProject extends DefaultProject{

  def main(args : Array[String]) {
    inDir("hoge") >> YourProject >> asXls
  }

  addSheet("NewSheet");

  onSheet("Sheet1"){
    forColumn("column1") map(v => "Map to " + v) always;
    forColumn("column2") set("0") ifEmpty;
    forColumn("column3") renameTo("NewColumnName");
  }

  onSheet("Sheet2"){
    forColumn("ref other column") set( {
      column("name") + "_" + sheet("Sheet3").searchIdIs( column("foreignKey") )("name")
    })

  }

}

Grammer

Outside sheet

onSheet( _sheetName){
  ...Inside sheet rules
}
onSheet( _sheetNameRegex.r){ // example onSheet("""(S|s)heet\n+""".r){...}
  ...Inside sheet rules
}

addSheet( _sheetName)

ignore( sheet(_sheetName) )
ignore( coluimn(_column) )
guessColumnType( {
  case _patten => _ColumnType
})
guessId( _columnName => _isId)

Inside sheet

only use in onSheet scope.

forColumn( _columnName) map( _columnValue => _newColumnValue) always // (default)
                                                              ifEmpty
                                                              when( _columnValue => _bool)
                        set( _newColumnValue)                 ifEmpty // (default)
                                                              when( _columnValue => _bool)
                                                              always
                        ignore
                        isId
                        type_=( _ColumnType)
                        renameTo(_newColumnName)

renameTo(_newSheetName)

filterRow( _row => Boolean) // remain rows which return true.

Refer other sheet or column

These are use inside functions.

sheet( _sheetName) search( _row => _bool) : Row
                   searchIdIs( _idValue) : Row
                   find( _row => _bool) : Option[Row]
                   findIdIs( _idValue) : Option[Row]

column( _columnName) : String

Processing

   Input                 Project         Output
inDir( _dir)                            console
file(_file)         >>  _Project     >> asXls
workbook(_workbook)                     asSql

sql-generator's People

Contributors

gte-enrique avatar takezoux2 avatar

Watchers

nicai007 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.