<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># DEPRECATED!
package DBIx::Custom::QueryBuilder;

use Object::Simple -base;

use Carp 'croak';
use DBIx::Custom::Query;
use DBIx::Custom::Util qw/_subname _deprecate/;

# Carp trust relationship
push @DBIx::Custom::CARP_NOT, __PACKAGE__;
push @DBIx::Custom::Where::CARP_NOT, __PACKAGE__;

# DEPRECATED!
sub build_query {
  my ($self, $sql) = @_;

  my $query = $self-&gt;_parse_tag($sql);
  my $tag_count = delete $query-&gt;{tag_count};
  _deprecate('0.24', qq/Tag system such as {? name} is DEPRECATED! / .
      qq/use parameter system such as :name instead/)
    if $tag_count;
  my $query2 = $self-&gt;_parse_parameter($query-&gt;sql);
  $query-&gt;sql($query2-&gt;sql);
  for (my $i =0; $i &lt; @{$query-&gt;columns}; $i++) {
    my $column = $query-&gt;columns-&gt;[$i];
    if ($column eq 'RESERVED_PARAMETER') {
      my $column2 = shift @{$query2-&gt;columns};
      croak ":name syntax is wrong"
        unless defined $column2;
      $query-&gt;columns-&gt;[$i] = $column2;
    }
  }
  return $query;
}

# DEPRECATED!
sub _parse_parameter {
  my ($self, $source) = @_;
  
  # Get and replace parameters
  my $sql = $source || '';
  my $columns = [];
  my $c = $self-&gt;dbi-&gt;safety_character;
  # Parameter regex
  $sql =~ s/([^:]):(\d+):([^:])/$1\\:$2\\:$3/g;
  my $re = qr/(^|.*?[^\\]):([$c\.]+)(?:\{(.*?)\})?(.*)/s;
  while ($sql =~ /$re/g) {
    push @$columns, $2;
    $sql = defined $3 ? "$1$2 $3 ?$4" : "$1?$4";
  }
  $sql =~ s/\\:/:/g;

  # Create query
  my $query = DBIx::Custom::Query-&gt;new(
    sql =&gt; $sql,
    columns =&gt; $columns
  );
  
  return $query;
}

# DEPRECATED
has 'dbi';

# DEPRECATED!
has tags =&gt; sub { {} };

# DEPRECATED!
sub register_tag {
  my $self = shift;
  
  _deprecate('0.24', "register_tag is DEPRECATED!");
  
  # Merge tag
  my $tags = ref $_[0] eq 'HASH' ? $_[0] : {@_};
  $self-&gt;tags({%{$self-&gt;tags}, %$tags});
  
  return $self;
}

# DEPRECATED!
sub _parse_tag {
  my ($self, $source) = @_;
  # Source
  $source ||= '';
  # Tree
  my @tree;
  # Value
  my $value = '';
  # State
  my $state = 'text';
  # Before charactor
  my $before = '';
  # Position
  my $pos = 0;
  # Parse
  my $original = $source;
  my $tag_count = 0;
  while (defined(my $c = substr($source, $pos, 1))) {
    # Last
    last unless length $c;
    # Parameter
    if ($c eq ':' &amp;&amp; (substr($source, $pos + 1, 1) || '') =~ /\w/) {
        push @tree, {type =&gt; 'param'};;
    }
    # State is text
    if ($state eq 'text') {
      # Tag start charactor
      if ($c eq '{') {
        # Escaped charactor
        if ($before eq "\\") {
          substr($value, -1, 1, '');
          $value .= $c;
        }
        # Tag start
        else {
          # Change state
          $state = 'tag';
          # Add text
          push @tree, {type =&gt; 'text', value =&gt; $value}
            if $value;
          # Clear
          $value = '';
        }
      }
      # Tag end charactor
      elsif ($c eq '}') {
        # Escaped charactor
        if ($before eq "\\") {
          substr($value, -1, 1, '');
          $value .= $c;
        }
        # Unexpected
        else {
          croak qq{Parsing error. unexpected "\}". }
            . qq{pos $pos of "$original" } . _subname
        }
      }
      # Normal charactor
      else { $value .= $c }
    }
    # State is tags
    else {
      # Tag start charactor
      if ($c eq '{') {
        # Escaped charactor
        if ($before eq "\\") {
          substr($value, -1, 1, '');
          $value .= $c;
        }
        # Unexpected
        else {
          croak qq{Parsing error. unexpected "\{". }
              . qq{pos $pos of "$original" } . _subname
        }
      }
      # Tag end charactor
      elsif ($c eq '}') {
        # Escaped charactor
        if ($before eq "\\") {
          substr($value, -1, 1, '');
          $value .= $c;
        }
        # Tag end
        else {
          # Change state
          $state = 'text';
          # Add tag
          my ($tag_name, @tag_args) = split /\s+/, $value;
          push @tree, {type =&gt; 'tag', tag_name =&gt; $tag_name, 
                       tag_args =&gt; \@tag_args};
          # Clear
          $value = '';
          # Countup
          $tag_count++;
        }
      }
      # Normal charactor
      else { $value .= $c }
    }
    # Save before charactor
    $before = $c;
    # increment position
    $pos++;
  }
  # Tag not finished
  croak qq{Tag not finished. "$original" } . _subname
    if $state eq 'tag';
  # Not contains tag
  return DBIx::Custom::Query-&gt;new(sql =&gt; $source, tag_count =&gt; $tag_count)
    if $tag_count == 0;
  # Add rest text
  push @tree, {type =&gt; 'text', value =&gt; $value}
    if $value;
  # SQL
  my $sql = '';
  # All Columns
  my $all_columns = [];
  # Tables
  my $tables = [];
  # Build SQL 
  for my $node (@tree) {
    # Text
    if ($node-&gt;{type} eq 'text') { $sql .= $node-&gt;{value} }
    # Parameter
    elsif ($node-&gt;{type} eq 'param') {
        push @$all_columns, 'RESERVED_PARAMETER';
    }
    # Tag
    else {
      # Tag name
      my $tag_name = $node-&gt;{tag_name};
      # Tag arguments
      my $tag_args = $node-&gt;{tag_args};
      # Table
      if ($tag_name eq 'table') {
        my $table = $tag_args-&gt;[0];
        push @$tables, $table;
        $sql .= $table;
        next;
      }
      # Get tag
      $self-&gt;dbi-&gt;{_tags} ||= {};
      my $tag = $self-&gt;tag_processors-&gt;{$tag_name}
                       || $self-&gt;dbi-&gt;{_tags}-&gt;{$tag_name};
      # Tag is not registered
      croak qq{Tag "$tag_name" is not registered } . _subname
        unless $tag;
      # Tag not sub reference
      croak qq{Tag "$tag_name" must be sub reference } . _subname
        unless ref $tag eq 'CODE';
      # Execute tag
      my $r = $tag-&gt;(@$tag_args);
      # Check tag return value
      croak qq{Tag "$tag_name" must return [STRING, ARRAY_REFERENCE] }
          . _subname
        unless ref $r eq 'ARRAY' &amp;&amp; defined $r-&gt;[0] &amp;&amp; ref $r-&gt;[1] eq 'ARRAY';
      # Part of SQL statement and colum names
      my ($part, $columns) = @$r;
      # Add columns
      push @$all_columns, @$columns;
      # Join part tag to SQL
      $sql .= $part;
    }
  }
  # Query
  my $query = DBIx::Custom::Query-&gt;new(
    sql =&gt; $sql,
    columns =&gt; $all_columns,
    tables =&gt; $tables,
    tag_count =&gt; $tag_count
  );
  return $query;
}

# DEPRECATED!
has tag_processors =&gt; sub { {} };

# DEPRECATED!
sub register_tag_processor {
  my $self = shift;
  _deprecate('0.24', "register_tag_processor is DEPRECATED!");
  # Merge tag
  my $tag_processors = ref $_[0] eq 'HASH' ? $_[0] : {@_};
  $self-&gt;tag_processors({%{$self-&gt;tag_processors}, %{$tag_processors}});
  return $self;
}

1;

=head1 NAME

DBIx::Custom::QueryBuilder - DEPRECATED!

=cut
</pre></body></html>