Sha256: 1084ba68bca7cdb91c84fc15f94cd11e6149964c2d034e5d15e232d0a658f491

Contents?: true

Size: 1.27 KB

Versions: 2

Compression:

Stored size: 1.27 KB

Contents

class Baza::BaseSqlDriver
  attr_reader :baza, :conn, :sep_table, :sep_col, :sep_val
  attr_accessor :tables, :cols, :indexes

  def self.from_object(args)
  end

  def initialize(baza)
    @baza = baza

    @sep_table = "`"
    @sep_col = "`"
    @sep_val = "'"
  end

  def escape(string)
    return string.to_s.gsub(/([\0\n\r\032\'\"\\])/) do
      case $1
        when "\0" then "\\0"
        when "\n" then "\\n"
        when "\r" then "\\r"
        when "\032" then "\\Z"
        else "\\#{$1}"
      end
    end
  end

  alias esc escape
  alias escape_alternative escape

  #Escapes a string to be used as a column.
  def esc_col(string)
    string = string.to_s
    raise "Invalid column-string: #{string}" if string.index(@sep_col) != nil
    return string
  end

  alias esc_table esc_col

  def transaction
    query("BEGIN TRANSACTION")

    begin
      yield @baza
      query("COMMIT")
    rescue => e
      query("ROLLBACK")
    end
  end

  def insert_multi(tablename, arr_hashes, args = nil)
    sql = [] if args && args[:return_sql]

    @baza.transaction do
      arr_hashes.each do |hash|
        res = @baza.insert(tablename, hash, args)
        sql << res if args && args[:return_sql]
      end
    end

    return sql if args && args[:return_sql]
    return nil
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
baza-0.0.19 lib/baza/base_sql_driver.rb
baza-0.0.18 lib/baza/base_sql_driver.rb