Sha256: e85509fbd1e809bbc65564a055ef6ab283dc640b40e8ea82049bb1e55100f431

Contents?: true

Size: 829 Bytes

Versions: 18

Compression:

Stored size: 829 Bytes

Contents

require_relative "user"

class Baza::Driver::Mysql::Users
  def initialize(args)
    @args = args
    @db = @args.fetch(:db)
  end

  def list
    result = []
    @db.query("SELECT * FROM mysql.user") do |user_data|
      user = Baza::Driver::Mysql::User.new(
        db: @db,
        data: user_data
      )

      if block_given?
        yield user
      else
        result << user
      end
    end

    result unless block_given?
  end

  def find_by_name(name)
    list do |user|
      return user if user.name == name.to_s
    end

    raise Baza::Errors::UserNotFound, "Could not find a user by that name: #{name}"
  end

  def create(data)
    @db.query("CREATE USER '#{@db.esc(data.fetch(:name))}'@'#{@db.esc(data.fetch(:host))}' IDENTIFIED BY '#{data.fetch(:password)}'")
    find_by_name(data.fetch(:name))
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
baza-0.0.38 lib/baza/driver/mysql/users.rb
baza-0.0.37 lib/baza/driver/mysql/users.rb
baza-0.0.36 lib/baza/driver/mysql/users.rb
baza-0.0.35 lib/baza/driver/mysql/users.rb
baza-0.0.34 lib/baza/driver/mysql/users.rb
baza-0.0.33 lib/baza/driver/mysql/users.rb
baza-0.0.32 lib/baza/driver/mysql/users.rb
baza-0.0.31 lib/baza/driver/mysql/users.rb
baza-0.0.30 lib/baza/driver/mysql/users.rb
baza-0.0.29 lib/baza/driver/mysql/users.rb
baza-0.0.28 lib/baza/driver/mysql/users.rb
baza-0.0.27 lib/baza/driver/mysql/users.rb
baza-0.0.26 lib/baza/driver/mysql/users.rb
baza-0.0.25 lib/baza/driver/mysql/users.rb
baza-0.0.24 lib/baza/driver/mysql/users.rb
baza-0.0.23 lib/baza/driver/mysql/users.rb
baza-0.0.22 lib/baza/driver/mysql/users.rb
baza-0.0.21 lib/baza/driver/mysql/users.rb