Crystal

Snippets

whoami

kobaken ( @kfly8 )

Mobile Factory

Application Engineer

Like Perl, JavaScript, Math




% crystal init app isucon
# edit shard.yml
 
dependencies:
  amethyst:
    github: Codcore/Amethyst
    branch: master

% shards install
require "amethyst"

class WorldController < Base::Controller
  actions :hello

  view "hello", "#{__DIR__}/../views"
  def hello
    @name = "World"
    respond_to do |format|
      format.html { render "hello" }
    end
  end
end

class HelloWorldApp < Base::App
  routes.draw do
    all "/",      "world#hello"
    get "/hello", "world#hello"
    register WorldController
  end
end

app = HelloWorldApp.new
app.serve




cp -r ruby/views

# and  s/.erb/.ecr/
# edit shard.yml

  mysql:
    github: waterlink/crystal-mysql
    branch: master
require "json"

# 0.9.0
class Config
  JSON.mapping({
    database: { type: DatabaseConfig }
  })
end
require "json"

# 0.8.0
class Config
  json_mapping({
    database: { type: DatabaseConfig }
  })
end
class DatabaseConfig
  JSON.mapping({
    dbname: String,
    host:   String,
    port:   Int32,
    username: String,
    password: String,
  })
end
require "mysql"

def setting
    path   = File.dirname(__FILE__) 
         + "/../config/local.json"
    
    ret    = File.read(path)
    return Config.from_json(ret)
 end
  def connection
    conf = setting

    MySQL.connect(
      conf.database.host,
      conf.database.username,
      conf.database.password,
      conf.database.dbname,
      conf.database.port,
      nil
    )
  end
def index
  mysql = connection
  ret = mysql.query("SELECT RAND()")
  @name = ret

  respond_to do |format|
    format.html { render "hello" }
  end
end
  def connection
    return $mysql if $mysql # cache
    conf = setting

    $mysql = MySQL.connect(
      conf.database.host,
      conf.database.username,
      conf.database.password,
      conf.database.dbname,
      conf.database.port,
      nil
    )
  end
def index
  mysql = connection.not_nil!
  ret = mysql.query("SELECT RAND()")
  @name = ret

  respond_to do |format|
    format.html { render "hello" }
  end
end
mysql = connection.not_nil!
ret = mysql.query("SELECT RAND()")
typeof(ret) 

# =>  
  Array(
    Array(
 Nil | String | Int32 | Int64 | Float64 | Bool | Time | MySQL::Types::Date
    )
  )?
alias MySQLRowType = 
   Nil | 
   String | 
   Int32 | 
   Int64 | 
   Float64 | 
   Bool | 
   Time | 
   MySQL::Types::Date
class MySQLResultInflator
  def initialize(@columns : Array(String))
  end

  def to_rows(result : Array(Array(MySQLRowType))?)
    result.not_nil!.map { |v| to_row(v) }
  end

  def to_row(row_data : Array(MySQLRowType))
    row = {} of String => MySQLRowType

    @columns.each_index do |index|
      col = @columns[index]
      val = row_data[index]
      row[col] = val
    end

    return row
  end
end

mysql = connection.not_nil!
ret   = mysql.query("SELECT RAND()")
mret  = MySQLResultInflator.new(["rand"])
rows  = mret.to_rows(ret)

# => [ { rand => 0.120446 } ]

Everything is going well...?

Thank you

Made with Slides.com